Advertisement
Guest User

Untitled

a guest
Aug 17th, 2017
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.38 KB | None | 0 0
  1. import static javax.security.enterprise.identitystore.CredentialValidationResult.INVALID_RESULT;
  2. import static javax.security.enterprise.identitystore.CredentialValidationResult.NOT_VALIDATED_RESULT;
  3.  
  4. import id.swhp.AccountStore;
  5. import id.swhp.entity.Account;
  6. import id.swhp.exception.AccountNotVerifiedException;
  7. import id.swhp.exception.InvalidCredentialException;
  8. import javax.enterprise.context.ApplicationScoped;
  9. import javax.inject.Inject;
  10. import javax.security.enterprise.credential.CallerOnlyCredential;
  11. import javax.security.enterprise.credential.Credential;
  12. import javax.security.enterprise.credential.UsernamePasswordCredential;
  13. import javax.security.enterprise.identitystore.CredentialValidationResult;
  14. import javax.security.enterprise.identitystore.IdentityStore;
  15.  
  16. /**
  17. *
  18. * @author Sukma Wardana
  19. * @since 1.0.0
  20. */
  21. @ApplicationScoped
  22. public class SecurityIdentityStore implements IdentityStore {
  23.  
  24. // call our EJB service to validate the account
  25. @Inject
  26. AccountStore accountStore;
  27.  
  28. @Override
  29. public CredentialValidationResult validate(Credential credential) {
  30. try {
  31.  
  32. // check if the credential was UsernamePasswordCredential
  33. if (credential instanceof UsernamePasswordCredential) {
  34. String username = ((UsernamePasswordCredential) credential).getCaller();
  35. String password = ((UsernamePasswordCredential) credential).getPasswordAsString();
  36.  
  37. return validate(this.accountStore.getByUsernameAndPassword(username, password));
  38. }
  39.  
  40. // check if the credential was UsernamePasswordCredential
  41. if (credential instanceof CallerOnlyCredential) {
  42. String username = ((CallerOnlyCredential) credential).getCaller();
  43.  
  44. return validate(
  45. this.accountStore.getByUsername(username)
  46. .orElseThrow(InvalidCredentialException::new)
  47. );
  48. }
  49.  
  50. } catch (InvalidCredentialException e) {
  51. return INVALID_RESULT;
  52. }
  53. return NOT_VALIDATED_RESULT;
  54. }
  55.  
  56. // before return the CredentialValidationResult, check if the account is active or not
  57. private CredentialValidationResult validate(Account account) {
  58. if (!account.isActive()) {
  59. throw new AccountNotVerifiedException();
  60. }
  61.  
  62. return new CredentialValidationResult(account.getUsername());
  63. }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement