|
@ -0,0 +1,104 @@ |
|
|
|
|
|
package de.fd.fh.server.access.web; |
|
|
|
|
|
|
|
|
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper; |
|
|
|
|
|
import de.fd.fh.server.access.AccessService; |
|
|
|
|
|
import de.fd.fh.server.access.AccessToken; |
|
|
|
|
|
import de.fd.fh.server.user.UserId; |
|
|
|
|
|
import de.fd.fh.shared.Utils; |
|
|
|
|
|
import de.fd.fh.shared.network.messages.LoginRequest; |
|
|
|
|
|
import de.fd.fh.shared.network.messages.RegistrateRequest; |
|
|
|
|
|
|
|
|
|
|
|
import static spark.Spark.*; |
|
|
|
|
|
|
|
|
|
|
|
public class AccessController |
|
|
|
|
|
{ |
|
|
|
|
|
private final ObjectMapper objectMapper = new ObjectMapper(); |
|
|
|
|
|
|
|
|
|
|
|
public AccessController(final AccessService service) |
|
|
|
|
|
{ |
|
|
|
|
|
before("/*", |
|
|
|
|
|
(req, res) -> |
|
|
|
|
|
{ |
|
|
|
|
|
final String path = req.pathInfo(); |
|
|
|
|
|
final String token = req.headers(Utils.AUTHENTICATION_HEADER); |
|
|
|
|
|
|
|
|
|
|
|
final AccessToken accessToken = service.before(path, token); |
|
|
|
|
|
|
|
|
|
|
|
req.session().attribute("userId", |
|
|
|
|
|
accessToken); |
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
post("/accounts/registrate", |
|
|
|
|
|
(request, response) -> |
|
|
|
|
|
{ |
|
|
|
|
|
final RegistrateRequest message = |
|
|
|
|
|
objectMapper.readValue(request.body(), RegistrateRequest.class); |
|
|
|
|
|
|
|
|
|
|
|
if (service.createPlayer(message)) |
|
|
|
|
|
{ |
|
|
|
|
|
response.status(201); |
|
|
|
|
|
} |
|
|
|
|
|
else |
|
|
|
|
|
{ |
|
|
|
|
|
response.status(400); |
|
|
|
|
|
} |
|
|
|
|
|
return response; |
|
|
|
|
|
} |
|
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
post("/accounts/login", |
|
|
|
|
|
(request, response) -> |
|
|
|
|
|
{ |
|
|
|
|
|
final String header = request.headers(Utils.AUTHENTICATION_HEADER); |
|
|
|
|
|
|
|
|
|
|
|
final LoginRequest login = service.authorization(header); |
|
|
|
|
|
|
|
|
|
|
|
if (login == null) |
|
|
|
|
|
{ |
|
|
|
|
|
response.status(401); |
|
|
|
|
|
} |
|
|
|
|
|
else |
|
|
|
|
|
{ |
|
|
|
|
|
response.status(200); |
|
|
|
|
|
response.type("application/json"); |
|
|
|
|
|
response.body(objectMapper.writeValueAsString(login)); |
|
|
|
|
|
} |
|
|
|
|
|
return response; |
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
post("/accounts/logout", |
|
|
|
|
|
(request, response) -> |
|
|
|
|
|
{ |
|
|
|
|
|
final String token = request.headers(Utils.AUTHENTICATION_HEADER); |
|
|
|
|
|
|
|
|
|
|
|
if (service.logout(token)) |
|
|
|
|
|
{ |
|
|
|
|
|
response.status(200); |
|
|
|
|
|
} |
|
|
|
|
|
else |
|
|
|
|
|
{ |
|
|
|
|
|
response.status(400); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return response; |
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
delete("/accounts/:player_id", |
|
|
|
|
|
(request, response) -> |
|
|
|
|
|
{ |
|
|
|
|
|
final UserId userId = UserId.of(request.params(":player_id")); |
|
|
|
|
|
final AccessToken token = request.session().attribute("userId"); |
|
|
|
|
|
|
|
|
|
|
|
if (service.deleteAccount(userId, token)) |
|
|
|
|
|
{ |
|
|
|
|
|
response.status(200); |
|
|
|
|
|
} |
|
|
|
|
|
else |
|
|
|
|
|
{ |
|
|
|
|
|
response.status(400); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return response; |
|
|
|
|
|
}); |
|
|
|
|
|
} |
|
|
|
|
|
} |