Guest User

AuthenticationApiResource

a guest
Sep 2nd, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.09 KB | None | 0 0
  1. @Qualifier("ldapAuthProvider") final LdapAuthenticationProvider ldapAuthenticationProvider,
  2.  
  3. @Post
  4. {
  5. ...
  6. final JsonObject requestJson = new JsonParser().parse(apiRequestBodyAsJson).getAsJsonObject();
  7. final String username = decrypt if not using plaintext;
  8. final String password = decrypt if not using plaintext;
  9. final Authentication authentication = new UsernamePasswordAuthenticationToken(username, password);
  10.  
  11. final Collection<String> permissions = new ArrayList<>();
  12. AuthenticatedUserData authenticatedUserData = new AuthenticatedUserData(username, permissions);
  13. CryptographyData encryptData =
  14. this.cryptographyReadPlatformService.getPublicKey(CryptographyApiConstants.loginAuth);
  15. String text = username + password + nowDate + encryptData.getKeyValue();
  16. text = org.apache.commons.codec.digest.DigestUtils.sha256Hex(text);
  17. final Authentication ldapauthenticationCheck = this.ldapAuthenticationProvider.authenticate(authentication);
  18. if (ldapauthenticationCheck.isAuthenticated()) {
  19. final Collection<GrantedAuthority> authorities = new ArrayList<>(ldapauthenticationCheck.getAuthorities());
  20. for (final GrantedAuthority grantedAuthority : authorities) {
  21. permissions.add(grantedAuthority.getAuthority());
  22. }
  23.  
  24. byte[] base64EncodedAuthenticationKey = Base64.encode(text);
  25. final AppUser principal = (AppUser) ldapauthenticationCheck.getPrincipal();
  26. sessionService.createSession(principal, new String(base64EncodedAuthenticationKey));
  27. final Collection<RoleData> roles = new ArrayList<>();
  28. final Set<Role> userRoles = principal.getRoles();
  29. for (final Role role : userRoles) {
  30. roles.add(role.toData());
  31. }
  32.  
  33. final Long officeId = principal.getOffice().getId();
  34. final String officeName = principal.getOffice().getName();
  35.  
  36. final Long staffId = principal.getStaffId();
  37. final String staffDisplayName = principal.getStaffDisplayName();
  38.  
  39. final EnumOptionData organisationalRole = principal.organisationalRoleData();
  40. final String firstname = principal.getFirstname();
  41. final String lastname = principal.getLastname();
  42.  
  43. if (this.springSecurityPlatformSecurityContext.doesPasswordHasToBeRenewed(principal)) {
  44. authenticatedUserData = new AuthenticatedUserData(username, principal.getId(), new String(
  45. base64EncodedAuthenticationKey), firstname, lastname, principal.isFirstTimeLoginRemaining());
  46. } else {
  47. authenticatedUserData = new AuthenticatedUserData(username, officeId, officeName, staffId, staffDisplayName,
  48. organisationalRole, roles, permissions, principal.getId(), new String(base64EncodedAuthenticationKey),
  49. firstname, lastname);
  50. }
  51.  
  52.  
  53. }
Add Comment
Please, Sign In to add comment