1
1
package com .aliyunidaas .sample .common ;
2
2
3
3
import com .aliyunidaas .sample .common .cache .CacheManager ;
4
- import com .aliyunidaas .sample .common .config .CustomOidcConfiguration ;
4
+ import com .aliyunidaas .sample .common .config .InitConfiguration ;
5
5
import com .aliyunidaas .sample .common .factory .CodeChallengeMethodFactory ;
6
- import com .aliyunidaas .sample .common .factory .ParameterNameFactory ;
6
+ import com .aliyunidaas .sample .common .factory .ConstantParams ;
7
7
import com .aliyunidaas .sample .common .util .CommonUtil ;
8
8
import org .apache .commons .lang .StringUtils ;
9
9
import org .springframework .beans .factory .annotation .Autowired ;
@@ -41,25 +41,25 @@ public class SimpleAuthnInterceptor implements HandlerInterceptor {
41
41
private final static String UTF_8 = "UTF-8" ;
42
42
43
43
@ Autowired
44
- private CustomOidcConfiguration customOidcConfiguration ;
44
+ private InitConfiguration initConfiguration ;
45
45
46
46
@ Autowired
47
47
private CacheManager cacheManager ;
48
48
49
49
@ Override
50
50
public boolean preHandle (HttpServletRequest request , HttpServletResponse response , Object handler )
51
51
throws IOException , NoSuchAlgorithmException , IllegalAccessException {
52
- Cookie cookie = WebUtils .getCookie (request , ParameterNameFactory .COOKIE_NAME );
52
+ Cookie cookie = WebUtils .getCookie (request , ConstantParams .COOKIE_NAME );
53
53
if (cookie == null ) {
54
54
String state = UUID .randomUUID ().toString ();
55
- String redirectUri = customOidcConfiguration .getRedirectUri ();
56
- String iDaaSLoginUri = getIDaaSLoginUri (request , state , redirectUri );
57
- if (customOidcConfiguration .isPkceRequired ()) {
58
- iDaaSLoginUri = getIDaaSLoginUri (state , iDaaSLoginUri );
55
+ String redirectUri = initConfiguration . getOidcConfig () .getRedirectUri ();
56
+ String eiamLoginUri = getEiamLoginUri (request , state , redirectUri );
57
+ if (initConfiguration . getOidcConfig () .isPkceRequired ()) {
58
+ eiamLoginUri = getEiamLoginUri (state , eiamLoginUri );
59
59
}
60
- response .sendRedirect (iDaaSLoginUri );
60
+ response .sendRedirect (eiamLoginUri );
61
61
} else {
62
- String cookieValue = cacheManager .getCache (CommonUtil .generateCacheKey (cookie .getValue (), ParameterNameFactory .COOKIE_NAME ));
62
+ String cookieValue = cacheManager .getCache (CommonUtil .generateCacheKey (cookie .getValue (), ConstantParams .COOKIE_NAME ));
63
63
if (StringUtils .isBlank (cookieValue ) || !cookie .getValue ().equals (cookieValue )) {
64
64
throw new IllegalAccessException (ILLEGAL_ACCESS_EXCEPTION_MESSAGE );
65
65
}
@@ -74,47 +74,46 @@ public boolean preHandle(HttpServletRequest request, HttpServletResponse respons
74
74
* @param state 用于作为缓存的key以及作为随机值用于跨站保护
75
75
* @return 登录的重定向地址
76
76
*/
77
- private String getIDaaSLoginUri (HttpServletRequest request , String state , String redirectUri )
77
+ private String getEiamLoginUri (HttpServletRequest request , String state , String redirectUri )
78
78
throws MalformedURLException , UnsupportedEncodingException {
79
- EndpointContext endpointContext = cacheManager .getCache (customOidcConfiguration .getIssuer ());
79
+ EndpointContext endpointContext = cacheManager .getCache (initConfiguration . getOidcConfig () .getIssuer ());
80
80
String authorizationEndpoint = endpointContext .getAuthorizationEndpoint ();
81
- final String clientId = customOidcConfiguration .getClientId ();
82
- final String scopes = customOidcConfiguration .getScopes ().replace (" " , "%20" );
81
+ final String clientId = initConfiguration .getOidcConfig ().getClientId ();
83
82
84
83
final String queryString = request .getQueryString ();
85
84
final String requestUrl = request .getRequestURL ().toString ();
86
85
final URL url = new URL (requestUrl );
87
86
String callbackUrl = url .getPath () + (queryString == null ? "" : "?" + queryString );
88
- cacheManager .setCache (CommonUtil .generateCacheKey (state , ParameterNameFactory .URI ), callbackUrl );
87
+ cacheManager .setCache (CommonUtil .generateCacheKey (state , ConstantParams .URI ), callbackUrl );
89
88
// responseType = code , It means that this is an authorization code request
90
89
return authorizationEndpoint +
91
90
"?response_type=code"
92
91
+ "&client_id=" + URLEncoder .encode (clientId , UTF_8 )
93
92
+ "&redirect_uri=" + URLEncoder .encode (redirectUri , UTF_8 )
94
- + "&scope=" + URLEncoder .encode (scopes , UTF_8 )
93
+ + "&scope=" + URLEncoder .encode (initConfiguration . getOidcConfig (). getScopes () , UTF_8 ). replace ( "+" , "%20" )
95
94
+ "&state=" + URLEncoder .encode (state , UTF_8 );
96
95
}
97
96
98
97
/**
99
98
* Obtain the redirection URL of Authorization Code With PKCE Flow
100
99
*
101
100
* @param state 缓存code verifier 的key
102
- * @param iDaaSLoginUri 授权码情况下的重定向地址
101
+ * @param eiamLoginUri 授权码情况下的重定向地址
103
102
* @return pkce情况下登录的的重定向地址
104
103
* @throws NoSuchAlgorithmException
105
104
*/
106
- private String getIDaaSLoginUri (String state , String iDaaSLoginUri ) throws NoSuchAlgorithmException , UnsupportedEncodingException {
105
+ private String getEiamLoginUri (String state , String eiamLoginUri ) throws NoSuchAlgorithmException , UnsupportedEncodingException {
107
106
String codeVerifier = createCodeVerifier ();
108
107
String codeChallenge = codeVerifier ;
109
- String codeChallengeMethod = customOidcConfiguration .getCodeChallengeMethod ();
108
+ String codeChallengeMethod = initConfiguration . getOidcConfig () .getCodeChallengeMethod ();
110
109
if (codeChallengeMethod .equals (CodeChallengeMethodFactory .SHA_256 )) {
111
110
codeChallenge = createHash (codeVerifier );
112
111
}
113
- cacheManager .setCache (CommonUtil .generateCacheKey (state , ParameterNameFactory .CODE_VERIFIER ), codeVerifier );
114
- iDaaSLoginUri = iDaaSLoginUri
115
- + "&code_challenge_method=" + URLEncoder .encode (customOidcConfiguration .getCodeChallengeMethod (), UTF_8 )
112
+ cacheManager .setCache (CommonUtil .generateCacheKey (state , ConstantParams .CODE_VERIFIER ), codeVerifier );
113
+ eiamLoginUri = eiamLoginUri
114
+ + "&code_challenge_method=" + URLEncoder .encode (initConfiguration . getOidcConfig () .getCodeChallengeMethod (), UTF_8 )
116
115
+ "&code_challenge=" + URLEncoder .encode (codeChallenge , UTF_8 );
117
- return iDaaSLoginUri ;
116
+ return eiamLoginUri ;
118
117
}
119
118
120
119
/**
0 commit comments