|
1 | 1 | package com.patternknife.securityhelper.oauth2.client.config.securityimpl.introspector;
|
2 | 2 |
|
| 3 | +import io.github.patternknife.securityhelper.oauth2.api.config.security.message.DefaultSecurityUserExceptionMessage; |
3 | 4 | import io.github.patternknife.securityhelper.oauth2.api.config.security.message.ISecurityUserExceptionMessageService;
|
4 | 5 | import io.github.patternknife.securityhelper.oauth2.api.config.security.response.error.exception.KnifeOauth2AuthenticationException;
|
5 | 6 | import io.github.patternknife.securityhelper.oauth2.api.config.security.serivce.persistence.authorization.OAuth2AuthorizationServiceImpl;
|
6 | 7 | import io.github.patternknife.securityhelper.oauth2.api.config.security.serivce.userdetail.ConditionalDetailsService;
|
7 | 8 | import org.springframework.beans.factory.annotation.Value;
|
| 9 | +import org.springframework.security.oauth2.server.authorization.OAuth2Authorization; |
| 10 | +import org.springframework.security.oauth2.server.authorization.OAuth2TokenType; |
8 | 11 | import org.springframework.security.oauth2.server.resource.introspection.SpringOpaqueTokenIntrospector;
|
9 | 12 | import org.springframework.stereotype.Component;
|
10 | 13 | import org.springframework.security.oauth2.core.OAuth2AuthenticatedPrincipal;
|
11 |
| -import org.springframework.security.oauth2.server.resource.introspection.NimbusOpaqueTokenIntrospector; |
12 | 14 | import org.springframework.security.oauth2.server.resource.introspection.OpaqueTokenIntrospector;
|
13 | 15 |
|
| 16 | +/* |
| 17 | +* Set this to your resource servers |
| 18 | +* */ |
14 | 19 | @Component
|
15 | 20 | public class CustomDefaultResourceServerTokenIntrospector implements OpaqueTokenIntrospector {
|
16 | 21 |
|
17 | 22 | private final OpaqueTokenIntrospector delegate;
|
18 | 23 |
|
| 24 | + /* |
| 25 | + * api : resource servers call the authorization server |
| 26 | + * database : the database is shared with the authorization server and resource servers |
| 27 | + * */ |
| 28 | + @Value("${patternknife.securityhelper.oauth2.introspection.type}") String introspectionType; |
| 29 | + @Value("${patternknife.securityhelper.oauth2.introspection.uri}") String introspectionUri; |
| 30 | + @Value("${patternknife.securityhelper.oauth2.introspection.client-id}") String clientId; |
| 31 | + @Value("${patternknife.securityhelper.oauth2.introspection.client-secret}") String clientSecret; |
| 32 | + |
| 33 | + |
| 34 | + private final OAuth2AuthorizationServiceImpl authorizationService; |
| 35 | + private final ConditionalDetailsService conditionalDetailsService; |
| 36 | + private final ISecurityUserExceptionMessageService iSecurityUserExceptionMessageService; |
| 37 | + |
| 38 | + |
19 | 39 | public CustomDefaultResourceServerTokenIntrospector(
|
20 |
| - @Value("${security.oauth2.introspection.uri}") String introspectionUri, |
21 |
| - @Value("${security.oauth2.introspection.client-id}") String clientId, |
22 |
| - @Value("${security.oauth2.introspection.client-secret}") String clientSecret) { |
| 40 | + OAuth2AuthorizationServiceImpl authorizationService, |
| 41 | + ConditionalDetailsService conditionalDetailsService, |
| 42 | + ISecurityUserExceptionMessageService iSecurityUserExceptionMessageService, |
| 43 | + @Value("${patternknife.securityhelper.oauth2.introspection.type}") String introspectionType, |
| 44 | + @Value("${patternknife.securityhelper.oauth2.introspection.uri}") String introspectionUri, |
| 45 | + @Value("${patternknife.securityhelper.oauth2.introspection.client-id}") String clientId, |
| 46 | + @Value("${patternknife.securityhelper.oauth2.introspection.client-secret}") String clientSecret) { |
23 | 47 | this.delegate = new SpringOpaqueTokenIntrospector(introspectionUri, clientId, clientSecret);
|
| 48 | + this.authorizationService = authorizationService; |
| 49 | + this.conditionalDetailsService = conditionalDetailsService; |
| 50 | + this.iSecurityUserExceptionMessageService = iSecurityUserExceptionMessageService; |
24 | 51 | }
|
25 | 52 |
|
26 | 53 | @Override
|
27 | 54 | public OAuth2AuthenticatedPrincipal introspect(String token) {
|
28 |
| - try { |
29 |
| - return delegate.introspect(token); |
30 |
| - } catch (Exception e) { |
31 |
| - throw new KnifeOauth2AuthenticationException(e.getMessage()); |
| 55 | + if(introspectionType.equals("api")) { |
| 56 | + try { |
| 57 | + return delegate.introspect(token); |
| 58 | + } catch (Exception e) { |
| 59 | + throw new KnifeOauth2AuthenticationException(e.getMessage()); |
| 60 | + } |
| 61 | + } else if (introspectionType.equals("database")) { |
| 62 | + OAuth2Authorization oAuth2Authorization = authorizationService.findByToken(token, OAuth2TokenType.ACCESS_TOKEN); |
| 63 | + |
| 64 | + if(oAuth2Authorization == null || oAuth2Authorization.getAccessToken() == null || oAuth2Authorization.getAccessToken().isExpired() |
| 65 | + || oAuth2Authorization.getRefreshToken() == null || oAuth2Authorization.getRefreshToken().isExpired()){ |
| 66 | + throw new KnifeOauth2AuthenticationException(iSecurityUserExceptionMessageService.getUserMessage(DefaultSecurityUserExceptionMessage.AUTHENTICATION_TOKEN_FAILURE)); |
| 67 | + } |
| 68 | + return (OAuth2AuthenticatedPrincipal) conditionalDetailsService.loadUserByUsername(oAuth2Authorization.getPrincipalName(), (String) oAuth2Authorization.getAttributes().get("client_id")); |
| 69 | + }else{ |
| 70 | + throw new KnifeOauth2AuthenticationException("Wrong introspection type : " + introspectionType); |
32 | 71 | }
|
33 | 72 | }
|
34 | 73 | }
|
|
0 commit comments