@Test public void createAccessToken_checkClient() { OAuth2AccessTokenEntity token = service.createAccessToken(authentication); assertThat(token.getClient().getClientId(), equalTo(clientId)); }
@Override public OAuth2AccessTokenEntity createAccessToken(OAuth2Authentication authentication) throws AuthenticationException, InvalidClientException { if (authentication != null && authentication.getOAuth2Request() != null) { OAuth2Request clientAuth = authentication.getOAuth2Request(); ClientDetailsEntity client = clientDetailsService.loadClientByClientId(clientAuth.getClientId()); if (client == null) { throw new InvalidClientException("Client not found: " + clientAuth.getClientId()); } OAuth2AccessTokenEntity token = new OAuth2AccessTokenEntity(); token.setClient(client); Set<String> scopes = Sets.newHashSet(clientAuth.getScope()); scopes = scopeService.removeRestrictedScopes(scopes); token.setScope(scopes); if (client.getAccessTokenValiditySeconds() != null && client.getAccessTokenValiditySeconds() > 0) { Date expiration = new Date(System.currentTimeMillis() + (client.getAccessTokenValiditySeconds() * 1000L)); token.setExpiration(expiration); } AuthenticationHolderEntity authHolder = new AuthenticationHolderEntity(); authHolder.setAuthentication(authentication); authHolder = authenticationHolderRepository.save(authHolder); token.setAuthenticationHolder(authHolder); if (client.isAllowRefresh() && scopes.contains(SystemScopeService.OFFLINE_ACCESS)) { OAuth2RefreshTokenEntity refreshToken = new OAuth2RefreshTokenEntity(); JWTClaimsSet refreshClaims = new JWTClaimsSet(); if (client.getRefreshTokenValiditySeconds() != null) { Date expiration = new Date(System.currentTimeMillis() + (client.getRefreshTokenValiditySeconds() * 1000L)); refreshToken.setExpiration(expiration); refreshClaims.setExpirationTime(expiration); } refreshClaims.setJWTID(UUID.randomUUID().toString()); PlainJWT refreshJwt = new PlainJWT(refreshClaims); refreshToken.setJwt(refreshJwt); refreshToken.setAuthenticationHolder(authHolder); refreshToken.setClient(client); OAuth2RefreshTokenEntity savedRefreshToken = tokenRepository.saveRefreshToken(refreshToken); token.setRefreshToken(savedRefreshToken); } OAuth2AccessTokenEntity enhancedToken = (OAuth2AccessTokenEntity) tokenEnhancer.enhance(token, authentication); OAuth2AccessTokenEntity savedToken = tokenRepository.saveAccessToken(enhancedToken); OAuth2Request originalAuthRequest = authHolder.getAuthentication().getOAuth2Request(); if (originalAuthRequest.getExtensions() != null && originalAuthRequest.getExtensions().containsKey("approved_site")) { Long apId = (Long) originalAuthRequest.getExtensions().get("approved_site"); ApprovedSite ap = approvedSiteService.getById(apId); Set<OAuth2AccessTokenEntity> apTokens = ap.getApprovedAccessTokens(); apTokens.add(savedToken); ap.setApprovedAccessTokens(apTokens); approvedSiteService.save(ap); } if (savedToken.getRefreshToken() != null) { tokenRepository.saveRefreshToken(savedToken.getRefreshToken()); } return savedToken; } throw new AuthenticationCredentialsNotFoundException("No authentication credentials found"); }
DefaultOAuth2ProviderTokenService implements OAuth2TokenEntityService { @Override public OAuth2AccessTokenEntity createAccessToken(OAuth2Authentication authentication) throws AuthenticationException, InvalidClientException { if (authentication != null && authentication.getOAuth2Request() != null) { OAuth2Request clientAuth = authentication.getOAuth2Request(); ClientDetailsEntity client = clientDetailsService.loadClientByClientId(clientAuth.getClientId()); if (client == null) { throw new InvalidClientException("Client not found: " + clientAuth.getClientId()); } OAuth2AccessTokenEntity token = new OAuth2AccessTokenEntity(); token.setClient(client); Set<String> scopes = Sets.newHashSet(clientAuth.getScope()); scopes = scopeService.removeRestrictedScopes(scopes); token.setScope(scopes); if (client.getAccessTokenValiditySeconds() != null && client.getAccessTokenValiditySeconds() > 0) { Date expiration = new Date(System.currentTimeMillis() + (client.getAccessTokenValiditySeconds() * 1000L)); token.setExpiration(expiration); } AuthenticationHolderEntity authHolder = new AuthenticationHolderEntity(); authHolder.setAuthentication(authentication); authHolder = authenticationHolderRepository.save(authHolder); token.setAuthenticationHolder(authHolder); if (client.isAllowRefresh() && scopes.contains(SystemScopeService.OFFLINE_ACCESS)) { OAuth2RefreshTokenEntity refreshToken = new OAuth2RefreshTokenEntity(); JWTClaimsSet refreshClaims = new JWTClaimsSet(); if (client.getRefreshTokenValiditySeconds() != null) { Date expiration = new Date(System.currentTimeMillis() + (client.getRefreshTokenValiditySeconds() * 1000L)); refreshToken.setExpiration(expiration); refreshClaims.setExpirationTime(expiration); } refreshClaims.setJWTID(UUID.randomUUID().toString()); PlainJWT refreshJwt = new PlainJWT(refreshClaims); refreshToken.setJwt(refreshJwt); refreshToken.setAuthenticationHolder(authHolder); refreshToken.setClient(client); OAuth2RefreshTokenEntity savedRefreshToken = tokenRepository.saveRefreshToken(refreshToken); token.setRefreshToken(savedRefreshToken); } OAuth2AccessTokenEntity enhancedToken = (OAuth2AccessTokenEntity) tokenEnhancer.enhance(token, authentication); OAuth2AccessTokenEntity savedToken = tokenRepository.saveAccessToken(enhancedToken); OAuth2Request originalAuthRequest = authHolder.getAuthentication().getOAuth2Request(); if (originalAuthRequest.getExtensions() != null && originalAuthRequest.getExtensions().containsKey("approved_site")) { Long apId = (Long) originalAuthRequest.getExtensions().get("approved_site"); ApprovedSite ap = approvedSiteService.getById(apId); Set<OAuth2AccessTokenEntity> apTokens = ap.getApprovedAccessTokens(); apTokens.add(savedToken); ap.setApprovedAccessTokens(apTokens); approvedSiteService.save(ap); } if (savedToken.getRefreshToken() != null) { tokenRepository.saveRefreshToken(savedToken.getRefreshToken()); } return savedToken; } throw new AuthenticationCredentialsNotFoundException("No authentication credentials found"); } }
DefaultOAuth2ProviderTokenService implements OAuth2TokenEntityService { @Override public OAuth2AccessTokenEntity createAccessToken(OAuth2Authentication authentication) throws AuthenticationException, InvalidClientException { if (authentication != null && authentication.getOAuth2Request() != null) { OAuth2Request clientAuth = authentication.getOAuth2Request(); ClientDetailsEntity client = clientDetailsService.loadClientByClientId(clientAuth.getClientId()); if (client == null) { throw new InvalidClientException("Client not found: " + clientAuth.getClientId()); } OAuth2AccessTokenEntity token = new OAuth2AccessTokenEntity(); token.setClient(client); Set<String> scopes = Sets.newHashSet(clientAuth.getScope()); scopes = scopeService.removeRestrictedScopes(scopes); token.setScope(scopes); if (client.getAccessTokenValiditySeconds() != null && client.getAccessTokenValiditySeconds() > 0) { Date expiration = new Date(System.currentTimeMillis() + (client.getAccessTokenValiditySeconds() * 1000L)); token.setExpiration(expiration); } AuthenticationHolderEntity authHolder = new AuthenticationHolderEntity(); authHolder.setAuthentication(authentication); authHolder = authenticationHolderRepository.save(authHolder); token.setAuthenticationHolder(authHolder); if (client.isAllowRefresh() && scopes.contains(SystemScopeService.OFFLINE_ACCESS)) { OAuth2RefreshTokenEntity refreshToken = new OAuth2RefreshTokenEntity(); JWTClaimsSet refreshClaims = new JWTClaimsSet(); if (client.getRefreshTokenValiditySeconds() != null) { Date expiration = new Date(System.currentTimeMillis() + (client.getRefreshTokenValiditySeconds() * 1000L)); refreshToken.setExpiration(expiration); refreshClaims.setExpirationTime(expiration); } refreshClaims.setJWTID(UUID.randomUUID().toString()); PlainJWT refreshJwt = new PlainJWT(refreshClaims); refreshToken.setJwt(refreshJwt); refreshToken.setAuthenticationHolder(authHolder); refreshToken.setClient(client); OAuth2RefreshTokenEntity savedRefreshToken = tokenRepository.saveRefreshToken(refreshToken); token.setRefreshToken(savedRefreshToken); } OAuth2AccessTokenEntity enhancedToken = (OAuth2AccessTokenEntity) tokenEnhancer.enhance(token, authentication); OAuth2AccessTokenEntity savedToken = tokenRepository.saveAccessToken(enhancedToken); OAuth2Request originalAuthRequest = authHolder.getAuthentication().getOAuth2Request(); if (originalAuthRequest.getExtensions() != null && originalAuthRequest.getExtensions().containsKey("approved_site")) { Long apId = (Long) originalAuthRequest.getExtensions().get("approved_site"); ApprovedSite ap = approvedSiteService.getById(apId); Set<OAuth2AccessTokenEntity> apTokens = ap.getApprovedAccessTokens(); apTokens.add(savedToken); ap.setApprovedAccessTokens(apTokens); approvedSiteService.save(ap); } if (savedToken.getRefreshToken() != null) { tokenRepository.saveRefreshToken(savedToken.getRefreshToken()); } return savedToken; } throw new AuthenticationCredentialsNotFoundException("No authentication credentials found"); }  }
DefaultOAuth2ProviderTokenService implements OAuth2TokenEntityService { @Override public OAuth2AccessTokenEntity createAccessToken(OAuth2Authentication authentication) throws AuthenticationException, InvalidClientException { if (authentication != null && authentication.getOAuth2Request() != null) { OAuth2Request clientAuth = authentication.getOAuth2Request(); ClientDetailsEntity client = clientDetailsService.loadClientByClientId(clientAuth.getClientId()); if (client == null) { throw new InvalidClientException("Client not found: " + clientAuth.getClientId()); } OAuth2AccessTokenEntity token = new OAuth2AccessTokenEntity(); token.setClient(client); Set<String> scopes = Sets.newHashSet(clientAuth.getScope()); scopes = scopeService.removeRestrictedScopes(scopes); token.setScope(scopes); if (client.getAccessTokenValiditySeconds() != null && client.getAccessTokenValiditySeconds() > 0) { Date expiration = new Date(System.currentTimeMillis() + (client.getAccessTokenValiditySeconds() * 1000L)); token.setExpiration(expiration); } AuthenticationHolderEntity authHolder = new AuthenticationHolderEntity(); authHolder.setAuthentication(authentication); authHolder = authenticationHolderRepository.save(authHolder); token.setAuthenticationHolder(authHolder); if (client.isAllowRefresh() && scopes.contains(SystemScopeService.OFFLINE_ACCESS)) { OAuth2RefreshTokenEntity refreshToken = new OAuth2RefreshTokenEntity(); JWTClaimsSet refreshClaims = new JWTClaimsSet(); if (client.getRefreshTokenValiditySeconds() != null) { Date expiration = new Date(System.currentTimeMillis() + (client.getRefreshTokenValiditySeconds() * 1000L)); refreshToken.setExpiration(expiration); refreshClaims.setExpirationTime(expiration); } refreshClaims.setJWTID(UUID.randomUUID().toString()); PlainJWT refreshJwt = new PlainJWT(refreshClaims); refreshToken.setJwt(refreshJwt); refreshToken.setAuthenticationHolder(authHolder); refreshToken.setClient(client); OAuth2RefreshTokenEntity savedRefreshToken = tokenRepository.saveRefreshToken(refreshToken); token.setRefreshToken(savedRefreshToken); } OAuth2AccessTokenEntity enhancedToken = (OAuth2AccessTokenEntity) tokenEnhancer.enhance(token, authentication); OAuth2AccessTokenEntity savedToken = tokenRepository.saveAccessToken(enhancedToken); OAuth2Request originalAuthRequest = authHolder.getAuthentication().getOAuth2Request(); if (originalAuthRequest.getExtensions() != null && originalAuthRequest.getExtensions().containsKey("approved_site")) { Long apId = (Long) originalAuthRequest.getExtensions().get("approved_site"); ApprovedSite ap = approvedSiteService.getById(apId); Set<OAuth2AccessTokenEntity> apTokens = ap.getApprovedAccessTokens(); apTokens.add(savedToken); ap.setApprovedAccessTokens(apTokens); approvedSiteService.save(ap); } if (savedToken.getRefreshToken() != null) { tokenRepository.saveRefreshToken(savedToken.getRefreshToken()); } return savedToken; } throw new AuthenticationCredentialsNotFoundException("No authentication credentials found"); }  @Override Set<OAuth2AccessTokenEntity> getAllAccessTokensForUser(String id); @Override Set<OAuth2RefreshTokenEntity> getAllRefreshTokensForUser(String id); @Override OAuth2AccessTokenEntity getAccessTokenById(Long id); @Override OAuth2RefreshTokenEntity getRefreshTokenById(Long id); @Override OAuth2AccessTokenEntity createAccessToken(OAuth2Authentication authentication); @Override OAuth2AccessTokenEntity refreshAccessToken(String refreshTokenValue, TokenRequest authRequest); @Override OAuth2Authentication loadAuthentication(String accessTokenValue); @Override OAuth2AccessTokenEntity readAccessToken(String accessTokenValue); @Override OAuth2AccessTokenEntity getAccessToken(OAuth2Authentication authentication); @Override OAuth2RefreshTokenEntity getRefreshToken(String refreshTokenValue); @Override void revokeRefreshToken(OAuth2RefreshTokenEntity refreshToken); @Override void revokeAccessToken(OAuth2AccessTokenEntity accessToken); @Override List<OAuth2AccessTokenEntity> getAccessTokensForClient(ClientDetailsEntity client); @Override List<OAuth2RefreshTokenEntity> getRefreshTokensForClient(ClientDetailsEntity client); @Override void clearExpiredTokens(); @Override OAuth2AccessTokenEntity saveAccessToken(OAuth2AccessTokenEntity accessToken); @Override OAuth2RefreshTokenEntity saveRefreshToken(OAuth2RefreshTokenEntity refreshToken); TokenEnhancer getTokenEnhancer(); void setTokenEnhancer(TokenEnhancer tokenEnhancer); @Override OAuth2AccessTokenEntity getAccessTokenForIdToken(OAuth2AccessTokenEntity idToken); @Override OAuth2AccessTokenEntity getRegistrationAccessTokenForClient(ClientDetailsEntity client); }
DefaultOAuth2ProviderTokenService implements OAuth2TokenEntityService { @Override public OAuth2AccessTokenEntity createAccessToken(OAuth2Authentication authentication) throws AuthenticationException, InvalidClientException { if (authentication != null && authentication.getOAuth2Request() != null) { OAuth2Request clientAuth = authentication.getOAuth2Request(); ClientDetailsEntity client = clientDetailsService.loadClientByClientId(clientAuth.getClientId()); if (client == null) { throw new InvalidClientException("Client not found: " + clientAuth.getClientId()); } OAuth2AccessTokenEntity token = new OAuth2AccessTokenEntity(); token.setClient(client); Set<String> scopes = Sets.newHashSet(clientAuth.getScope()); scopes = scopeService.removeRestrictedScopes(scopes); token.setScope(scopes); if (client.getAccessTokenValiditySeconds() != null && client.getAccessTokenValiditySeconds() > 0) { Date expiration = new Date(System.currentTimeMillis() + (client.getAccessTokenValiditySeconds() * 1000L)); token.setExpiration(expiration); } AuthenticationHolderEntity authHolder = new AuthenticationHolderEntity(); authHolder.setAuthentication(authentication); authHolder = authenticationHolderRepository.save(authHolder); token.setAuthenticationHolder(authHolder); if (client.isAllowRefresh() && scopes.contains(SystemScopeService.OFFLINE_ACCESS)) { OAuth2RefreshTokenEntity refreshToken = new OAuth2RefreshTokenEntity(); JWTClaimsSet refreshClaims = new JWTClaimsSet(); if (client.getRefreshTokenValiditySeconds() != null) { Date expiration = new Date(System.currentTimeMillis() + (client.getRefreshTokenValiditySeconds() * 1000L)); refreshToken.setExpiration(expiration); refreshClaims.setExpirationTime(expiration); } refreshClaims.setJWTID(UUID.randomUUID().toString()); PlainJWT refreshJwt = new PlainJWT(refreshClaims); refreshToken.setJwt(refreshJwt); refreshToken.setAuthenticationHolder(authHolder); refreshToken.setClient(client); OAuth2RefreshTokenEntity savedRefreshToken = tokenRepository.saveRefreshToken(refreshToken); token.setRefreshToken(savedRefreshToken); } OAuth2AccessTokenEntity enhancedToken = (OAuth2AccessTokenEntity) tokenEnhancer.enhance(token, authentication); OAuth2AccessTokenEntity savedToken = tokenRepository.saveAccessToken(enhancedToken); OAuth2Request originalAuthRequest = authHolder.getAuthentication().getOAuth2Request(); if (originalAuthRequest.getExtensions() != null && originalAuthRequest.getExtensions().containsKey("approved_site")) { Long apId = (Long) originalAuthRequest.getExtensions().get("approved_site"); ApprovedSite ap = approvedSiteService.getById(apId); Set<OAuth2AccessTokenEntity> apTokens = ap.getApprovedAccessTokens(); apTokens.add(savedToken); ap.setApprovedAccessTokens(apTokens); approvedSiteService.save(ap); } if (savedToken.getRefreshToken() != null) { tokenRepository.saveRefreshToken(savedToken.getRefreshToken()); } return savedToken; } throw new AuthenticationCredentialsNotFoundException("No authentication credentials found"); }  @Override Set<OAuth2AccessTokenEntity> getAllAccessTokensForUser(String id); @Override Set<OAuth2RefreshTokenEntity> getAllRefreshTokensForUser(String id); @Override OAuth2AccessTokenEntity getAccessTokenById(Long id); @Override OAuth2RefreshTokenEntity getRefreshTokenById(Long id); @Override OAuth2AccessTokenEntity createAccessToken(OAuth2Authentication authentication); @Override OAuth2AccessTokenEntity refreshAccessToken(String refreshTokenValue, TokenRequest authRequest); @Override OAuth2Authentication loadAuthentication(String accessTokenValue); @Override OAuth2AccessTokenEntity readAccessToken(String accessTokenValue); @Override OAuth2AccessTokenEntity getAccessToken(OAuth2Authentication authentication); @Override OAuth2RefreshTokenEntity getRefreshToken(String refreshTokenValue); @Override void revokeRefreshToken(OAuth2RefreshTokenEntity refreshToken); @Override void revokeAccessToken(OAuth2AccessTokenEntity accessToken); @Override List<OAuth2AccessTokenEntity> getAccessTokensForClient(ClientDetailsEntity client); @Override List<OAuth2RefreshTokenEntity> getRefreshTokensForClient(ClientDetailsEntity client); @Override void clearExpiredTokens(); @Override OAuth2AccessTokenEntity saveAccessToken(OAuth2AccessTokenEntity accessToken); @Override OAuth2RefreshTokenEntity saveRefreshToken(OAuth2RefreshTokenEntity refreshToken); TokenEnhancer getTokenEnhancer(); void setTokenEnhancer(TokenEnhancer tokenEnhancer); @Override OAuth2AccessTokenEntity getAccessTokenForIdToken(OAuth2AccessTokenEntity idToken); @Override OAuth2AccessTokenEntity getRegistrationAccessTokenForClient(ClientDetailsEntity client);  }