Created
March 19, 2020 20:17
-
-
Save thinkmicroservices/8fe3cb0f7f88df5349d7716380e101ab to your computer and use it in GitHub Desktop.
AuthenticationService: authenticate().java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * | |
| * @param username | |
| * @param password | |
| * @return | |
| * @throws AuthenticationException | |
| */ | |
| public AuthenticationToken authenticate(String username, String password) throws | |
| AuthenticationException { | |
| log.debug("authenticate:" + username); | |
| if ((checkRequiredActiveServices) && (activeServicesRequiredForAuthentication.size() > 0)) { | |
| log.info("requiredServices:{}", this.activeServicesRequiredForAuthentication.toString()); | |
| List<String> activeServices = discoveryClient.getServices().stream().map(String::toUpperCase).collect(Collectors.toList());; | |
| log.info("Discovery services:{}", activeServices); | |
| if (!activeServices.containsAll(this.activeServicesRequiredForAuthentication)) { | |
| log.info("all required services are not available,"); | |
| List<String> required = new ArrayList<>(this.activeServicesRequiredForAuthentication); | |
| required.removeAll(activeServices); | |
| log.info("missing services:{}", required); | |
| throw new AuthenticationException("error.authentication.required.services.unavailable", required.toString()); | |
| } | |
| } | |
| User user = this.loadUserByUsername(username); | |
| // throw exception if no user found | |
| if (user == null) { | |
| this.accountEventSource | |
| .accountEvents() | |
| .send(MessageBuilder | |
| .withPayload(new CredentialsAuthenticationRequestedEvent(null, username, false)) | |
| .setHeader("type", "CREDENTIALS_AUTHENTICATION_REQUEST_EVENT").build()); | |
| throw new AuthenticationException("error.authentication.credentials.invalid"); | |
| } | |
| // throw exception if the user account is disabled | |
| boolean isEnabled = this.isAccountEnabled(username); | |
| log.debug("{} activeStatus is {}", username, isEnabled); | |
| if (isEnabled == false) { | |
| this.accountEventSource | |
| .accountEvents() | |
| .send(MessageBuilder | |
| .withPayload(new CredentialsAuthenticationRequestedEvent(null, username, false)) | |
| .setHeader("type", "CREDENTIALS_AUTHENTICATION_REQUEST_EVENT").build()); | |
| throw new AuthenticationException("error.authentication.account.disabled"); | |
| } | |
| // check if password matches | |
| User testUser = this.userRepository.findByUsername(username); | |
| if (this.bcryptEncoder.matches(password, testUser.getPassword())) { | |
| Set<Role> roles = user.getRoles(); | |
| ArrayList<GrantedAuthority> authorities = this.getGrantedAuthorities(roles); | |
| java.sql.Timestamp lastLogonTimestamp = new java.sql.Timestamp((new java.util.Date().getTime())); | |
| String refreshToken = UUID.randomUUID().toString(); | |
| LocalDateTime refreshTokenExpirationTimestamp = LocalDateTime.now(); | |
| refreshTokenExpirationTimestamp = refreshTokenExpirationTimestamp.plusMinutes(this.refreshTokenExpirationIntervalMinutes); | |
| LocalDateTime tokenExpiresAtTimestamp = LocalDateTime.now(); | |
| tokenExpiresAtTimestamp = tokenExpiresAtTimestamp.plusMinutes(this.tokenExpirationIntervalMinutes); | |
| user.setRefreshTokenExpirationAt(Timestamp.valueOf(refreshTokenExpirationTimestamp)); | |
| user.setRefreshToken(refreshToken); | |
| user.setLastLogon(lastLogonTimestamp); | |
| user.setTokenIssuedAt(lastLogonTimestamp); | |
| user.setTokenExpirationAt(Timestamp.valueOf(tokenExpiresAtTimestamp)); | |
| this.userRepository.save(user); | |
| final String tokenString = jwtProvider.generateToken(user, | |
| authorities, | |
| lastLogonTimestamp.getTime(), // issued at | |
| Timestamp.valueOf(tokenExpiresAtTimestamp).getTime(), | |
| refreshToken, | |
| Timestamp.valueOf(refreshTokenExpirationTimestamp).getTime()); | |
| this.accountEventSource.accountEvents() | |
| .send(MessageBuilder.withPayload(new CredentialsAuthenticationRequestedEvent(user.getAccountId(), username, true)) | |
| .setHeader("type", "CREDENTIALS_AUTHENTICATION_REQUEST_EVENT").build()); | |
| return new AuthenticationToken(tokenString); | |
| } else { | |
| log.debug("alternate authentication Failed"); | |
| throw new AuthenticationException("error.authentication.credentials.invalid"); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment