6
6
import io .swagger .v3 .oas .annotations .tags .Tag ;
7
7
import lombok .AllArgsConstructor ;
8
8
import lombok .SneakyThrows ;
9
+ import org .apache .commons .codec .binary .Base64 ;
9
10
import org .hswebframework .web .authorization .Authentication ;
10
11
import org .hswebframework .web .authorization .annotation .Authorize ;
11
12
import org .hswebframework .web .authorization .exception .UnAuthorizedException ;
19
20
import org .hswebframework .web .oauth2 .server .code .AuthorizationCodeTokenRequest ;
20
21
import org .hswebframework .web .oauth2 .server .credential .ClientCredentialRequest ;
21
22
import org .hswebframework .web .oauth2 .server .refresh .RefreshTokenRequest ;
23
+ import org .springframework .http .HttpHeaders ;
22
24
import org .springframework .http .MediaType ;
23
25
import org .springframework .http .ResponseEntity ;
24
26
import org .springframework .util .MultiValueMap ;
25
27
import org .springframework .web .bind .annotation .*;
26
28
import org .springframework .web .server .ServerWebExchange ;
27
29
import reactor .core .publisher .Mono ;
30
+ import reactor .util .function .Tuple2 ;
31
+ import reactor .util .function .Tuples ;
28
32
29
33
import java .net .URLEncoder ;
34
+ import java .util .Arrays ;
30
35
import java .util .HashMap ;
31
36
import java .util .Map ;
32
37
import java .util .Optional ;
@@ -84,10 +89,10 @@ public Mono<ResponseEntity<AccessToken>> requestTokenByCode(
84
89
@ RequestParam ("grant_type" ) GrantType grantType ,
85
90
ServerWebExchange exchange ) {
86
91
Map <String , String > params = exchange .getRequest ().getQueryParams ().toSingleValueMap ();
87
-
92
+ Tuple2 < String , String > clientIdAndSecret = getClientIdAndClientSecret ( params , exchange );
88
93
return this
89
- .getOAuth2Client (params . get ( "client_id" ))
90
- .doOnNext (client -> client .validateSecret (params . get ( "client_secret" )))
94
+ .getOAuth2Client (clientIdAndSecret . getT1 ( ))
95
+ .doOnNext (client -> client .validateSecret (clientIdAndSecret . getT2 ( )))
91
96
.flatMap (client -> grantType .requestToken (oAuth2GrantService , client , new HashMap <>(params )))
92
97
.map (ResponseEntity ::ok );
93
98
}
@@ -106,15 +111,28 @@ public Mono<ResponseEntity<AccessToken>> requestTokenByCode(ServerWebExchange ex
106
111
.getFormData ()
107
112
.map (MultiValueMap ::toSingleValueMap )
108
113
.flatMap (params -> {
114
+ Tuple2 <String ,String > clientIdAndSecret = getClientIdAndClientSecret (params ,exchange );
109
115
GrantType grantType = GrantType .of (params .get ("grant_type" ));
110
116
return this
111
- .getOAuth2Client (params . get ( "client_id" ))
112
- .doOnNext (client -> client .validateSecret (params . get ( "client_secret" )))
117
+ .getOAuth2Client (clientIdAndSecret . getT1 ( ))
118
+ .doOnNext (client -> client .validateSecret (clientIdAndSecret . getT2 ( )))
113
119
.flatMap (client -> grantType .requestToken (oAuth2GrantService , client , new HashMap <>(params )))
114
120
.map (ResponseEntity ::ok );
115
121
});
116
122
}
117
123
124
+ private Tuple2 <String , String > getClientIdAndClientSecret (Map <String , String > params , ServerWebExchange exchange ) {
125
+ String authorization = exchange .getRequest ().getHeaders ().getFirst (HttpHeaders .AUTHORIZATION );
126
+ if (authorization != null && authorization .startsWith ("Basic " )) {
127
+ String [] arr = new String (Base64 .decodeBase64 (authorization .substring (5 ))).split (":" );
128
+ if (arr .length >= 2 ) {
129
+ return Tuples .of (arr [0 ], arr [1 ]);
130
+ }
131
+ return Tuples .of (arr [0 ], arr [0 ]);
132
+ }
133
+ return Tuples .of (params .getOrDefault ("client_id" ,"" ),params .getOrDefault ("client_secret" ,"" ));
134
+ }
135
+
118
136
public enum GrantType {
119
137
authorization_code {
120
138
@ Override
@@ -132,7 +150,7 @@ Mono<AccessToken> requestToken(OAuth2GrantService service, OAuth2Client client,
132
150
.requestToken (new ClientCredentialRequest (client , param ));
133
151
}
134
152
},
135
- refresh_token {
153
+ refresh_token {
136
154
@ Override
137
155
Mono <AccessToken > requestToken (OAuth2GrantService service , OAuth2Client client , Map <String , String > param ) {
138
156
return service
@@ -143,10 +161,10 @@ Mono<AccessToken> requestToken(OAuth2GrantService service, OAuth2Client client,
143
161
144
162
abstract Mono <AccessToken > requestToken (OAuth2GrantService service , OAuth2Client client , Map <String , String > param );
145
163
146
- static GrantType of (String name ){
164
+ static GrantType of (String name ) {
147
165
try {
148
166
return GrantType .valueOf (name );
149
- }catch (Throwable e ){
167
+ } catch (Throwable e ) {
150
168
throw new OAuth2Exception (ErrorType .UNSUPPORTED_GRANT_TYPE );
151
169
}
152
170
}
0 commit comments