Skip to content

Commit 2c8f12d

Browse files
committed
优化clientId和secure获取逻辑
1 parent 87699e5 commit 2c8f12d

File tree

1 file changed

+26
-8
lines changed

1 file changed

+26
-8
lines changed

hsweb-authorization/hsweb-authorization-oauth2/src/main/java/org/hswebframework/web/oauth2/server/web/OAuth2AuthorizeController.java

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import io.swagger.v3.oas.annotations.tags.Tag;
77
import lombok.AllArgsConstructor;
88
import lombok.SneakyThrows;
9+
import org.apache.commons.codec.binary.Base64;
910
import org.hswebframework.web.authorization.Authentication;
1011
import org.hswebframework.web.authorization.annotation.Authorize;
1112
import org.hswebframework.web.authorization.exception.UnAuthorizedException;
@@ -19,14 +20,18 @@
1920
import org.hswebframework.web.oauth2.server.code.AuthorizationCodeTokenRequest;
2021
import org.hswebframework.web.oauth2.server.credential.ClientCredentialRequest;
2122
import org.hswebframework.web.oauth2.server.refresh.RefreshTokenRequest;
23+
import org.springframework.http.HttpHeaders;
2224
import org.springframework.http.MediaType;
2325
import org.springframework.http.ResponseEntity;
2426
import org.springframework.util.MultiValueMap;
2527
import org.springframework.web.bind.annotation.*;
2628
import org.springframework.web.server.ServerWebExchange;
2729
import reactor.core.publisher.Mono;
30+
import reactor.util.function.Tuple2;
31+
import reactor.util.function.Tuples;
2832

2933
import java.net.URLEncoder;
34+
import java.util.Arrays;
3035
import java.util.HashMap;
3136
import java.util.Map;
3237
import java.util.Optional;
@@ -84,10 +89,10 @@ public Mono<ResponseEntity<AccessToken>> requestTokenByCode(
8489
@RequestParam("grant_type") GrantType grantType,
8590
ServerWebExchange exchange) {
8691
Map<String, String> params = exchange.getRequest().getQueryParams().toSingleValueMap();
87-
92+
Tuple2<String,String> clientIdAndSecret = getClientIdAndClientSecret(params,exchange);
8893
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()))
9196
.flatMap(client -> grantType.requestToken(oAuth2GrantService, client, new HashMap<>(params)))
9297
.map(ResponseEntity::ok);
9398
}
@@ -106,15 +111,28 @@ public Mono<ResponseEntity<AccessToken>> requestTokenByCode(ServerWebExchange ex
106111
.getFormData()
107112
.map(MultiValueMap::toSingleValueMap)
108113
.flatMap(params -> {
114+
Tuple2<String,String> clientIdAndSecret = getClientIdAndClientSecret(params,exchange);
109115
GrantType grantType = GrantType.of(params.get("grant_type"));
110116
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()))
113119
.flatMap(client -> grantType.requestToken(oAuth2GrantService, client, new HashMap<>(params)))
114120
.map(ResponseEntity::ok);
115121
});
116122
}
117123

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+
118136
public enum GrantType {
119137
authorization_code {
120138
@Override
@@ -132,7 +150,7 @@ Mono<AccessToken> requestToken(OAuth2GrantService service, OAuth2Client client,
132150
.requestToken(new ClientCredentialRequest(client, param));
133151
}
134152
},
135-
refresh_token{
153+
refresh_token {
136154
@Override
137155
Mono<AccessToken> requestToken(OAuth2GrantService service, OAuth2Client client, Map<String, String> param) {
138156
return service
@@ -143,10 +161,10 @@ Mono<AccessToken> requestToken(OAuth2GrantService service, OAuth2Client client,
143161

144162
abstract Mono<AccessToken> requestToken(OAuth2GrantService service, OAuth2Client client, Map<String, String> param);
145163

146-
static GrantType of(String name){
164+
static GrantType of(String name) {
147165
try {
148166
return GrantType.valueOf(name);
149-
}catch (Throwable e){
167+
} catch (Throwable e) {
150168
throw new OAuth2Exception(ErrorType.UNSUPPORTED_GRANT_TYPE);
151169
}
152170
}

0 commit comments

Comments
 (0)