Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package io.cobl.authentication.controller;
- import io.cobl.account.service.AccountService;
- import io.cobl.authentication.model.AuthenticationParams;
- import io.cobl.authentication.model.AuthenticationResponse;
- import io.cobl.authentication.service.TokenService;
- import io.cobl.common.CoblServiceResponse;
- import io.cobl.common.CoblServiceStatus;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.beans.factory.annotation.Qualifier;
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RequestMethod;
- import org.springframework.web.bind.annotation.ResponseBody;
- @Controller("cobl.authenticationController")
- public class AuthenticationController
- {
- private static final String AUTHENTICATION_ERROR_MESSAGE = "Invalid authentication attempt.";
- @Autowired
- @Qualifier("cobl.accountService")
- private AccountService accountService;
- @Autowired
- @Qualifier("cobl.tokenService")
- private TokenService tokenService;
- @RequestMapping(value = "/authenticate", method = RequestMethod.GET, produces = "application/json")
- public
- @ResponseBody
- CoblServiceResponse<AuthenticationResponse> authenticate(final AuthenticationParams authenticationParams) throws Exception
- {
- final CoblServiceResponse<AuthenticationResponse> csr = new CoblServiceResponse<>();
- final String username = authenticationParams.getUsername();
- final String password = authenticationParams.getPassword();
- if (accountService.checkPassword(username, password))
- {
- final String token = tokenService.encodeToken(username);
- final AuthenticationResponse ar = new AuthenticationResponse();
- ar.setToken(token);
- csr.setPayload(ar);
- }
- else
- {
- csr.setStatus(CoblServiceStatus.AUTHENTICATION_FAILURE);
- csr.addMessage(AUTHENTICATION_ERROR_MESSAGE);
- }
- return csr;
- }
- }
Add Comment
Please, Sign In to add comment