Guest User

Untitled

a guest
Feb 16th, 2017
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.91 KB | None | 0 0
  1. package io.cobl.authentication.controller;
  2.  
  3. import io.cobl.account.service.AccountService;
  4. import io.cobl.authentication.model.AuthenticationParams;
  5. import io.cobl.authentication.model.AuthenticationResponse;
  6. import io.cobl.authentication.service.TokenService;
  7. import io.cobl.common.CoblServiceResponse;
  8. import io.cobl.common.CoblServiceStatus;
  9. import org.springframework.beans.factory.annotation.Autowired;
  10. import org.springframework.beans.factory.annotation.Qualifier;
  11. import org.springframework.stereotype.Controller;
  12. import org.springframework.web.bind.annotation.RequestMapping;
  13. import org.springframework.web.bind.annotation.RequestMethod;
  14. import org.springframework.web.bind.annotation.ResponseBody;
  15.  
  16. @Controller("cobl.authenticationController")
  17. public class AuthenticationController
  18. {
  19.     private static final String AUTHENTICATION_ERROR_MESSAGE = "Invalid authentication attempt.";
  20.  
  21.     @Autowired
  22.     @Qualifier("cobl.accountService")
  23.     private AccountService accountService;
  24.  
  25.     @Autowired
  26.     @Qualifier("cobl.tokenService")
  27.     private TokenService tokenService;
  28.  
  29.     @RequestMapping(value = "/authenticate", method = RequestMethod.GET, produces = "application/json")
  30.     public
  31.     @ResponseBody
  32.     CoblServiceResponse<AuthenticationResponse> authenticate(final AuthenticationParams authenticationParams) throws Exception
  33.     {
  34.         final CoblServiceResponse<AuthenticationResponse> csr = new CoblServiceResponse<>();
  35.  
  36.         final String username = authenticationParams.getUsername();
  37.         final String password = authenticationParams.getPassword();
  38.  
  39.         if (accountService.checkPassword(username, password))
  40.         {
  41.             final String token = tokenService.encodeToken(username);
  42.             final AuthenticationResponse ar = new AuthenticationResponse();
  43.             ar.setToken(token);
  44.             csr.setPayload(ar);
  45.         }
  46.         else
  47.         {
  48.             csr.setStatus(CoblServiceStatus.AUTHENTICATION_FAILURE);
  49.             csr.addMessage(AUTHENTICATION_ERROR_MESSAGE);
  50.         }
  51.  
  52.         return csr;
  53.     }
  54. }
Add Comment
Please, Sign In to add comment