|
| 1 | +package org.lowcoder.api.authentication.request.oauth2.request; |
| 2 | + |
| 3 | +import org.lowcoder.api.authentication.request.AuthException; |
| 4 | +import org.lowcoder.api.authentication.request.oauth2.GenericOAuthProviderSource; |
| 5 | +import org.lowcoder.api.authentication.request.oauth2.OAuth2RequestContext; |
| 6 | +import org.lowcoder.domain.user.model.AuthToken; |
| 7 | +import org.lowcoder.domain.user.model.AuthUser; |
| 8 | +import org.lowcoder.sdk.auth.Oauth2GenericAuthConfig; |
| 9 | +import org.lowcoder.sdk.auth.Oauth2KeycloakAuthConfig; |
| 10 | +import org.lowcoder.sdk.util.JsonUtils; |
| 11 | +import org.lowcoder.sdk.webclient.WebClientBuildHelper; |
| 12 | +import org.springframework.http.MediaType; |
| 13 | +import org.springframework.web.reactive.function.BodyInserters; |
| 14 | +import reactor.core.publisher.Mono; |
| 15 | + |
| 16 | +import java.util.Map; |
| 17 | + |
| 18 | +import static org.lowcoder.api.authentication.util.AuthenticationUtils.mapToAuthToken; |
| 19 | +import static org.lowcoder.api.authentication.util.AuthenticationUtils.mapToAuthUser; |
| 20 | + |
| 21 | +/** |
| 22 | + * This class is for Generic Auth Request |
| 23 | + */ |
| 24 | +public class GenericAuthRequest extends AbstractOauth2Request<Oauth2GenericAuthConfig>{ |
| 25 | + |
| 26 | + public GenericAuthRequest(Oauth2GenericAuthConfig context) { |
| 27 | + super(context, new GenericOAuthProviderSource(context)); |
| 28 | + } |
| 29 | + |
| 30 | + @Override |
| 31 | + protected Mono<AuthToken> getAuthToken(OAuth2RequestContext context) { |
| 32 | + return WebClientBuildHelper.builder() |
| 33 | + .systemProxy() |
| 34 | + .build() |
| 35 | + .post() |
| 36 | + .uri(config.getTokenEndpoint()) |
| 37 | + .contentType(MediaType.APPLICATION_FORM_URLENCODED) |
| 38 | + .body(BodyInserters.fromFormData("code", context.getCode()) |
| 39 | + .with("client_id", config.getClientId()) |
| 40 | + .with("client_secret", config.getClientSecret()) |
| 41 | + .with("grant_type", "authorization_code") |
| 42 | + .with("redirect_uri", context.getRedirectUrl())) |
| 43 | + .retrieve() |
| 44 | + .bodyToMono(Map.class) |
| 45 | + .flatMap(map -> { |
| 46 | + if (map.containsKey("error") || map.containsKey("error_description")) { |
| 47 | + return Mono.error(new AuthException(JsonUtils.toJson(map))); |
| 48 | + } |
| 49 | + return Mono.just(mapToAuthToken(map)); |
| 50 | + }); |
| 51 | + } |
| 52 | + |
| 53 | + @Override |
| 54 | + protected Mono<AuthToken> refreshAuthToken(String refreshToken) { |
| 55 | + return WebClientBuildHelper.builder() |
| 56 | + .systemProxy() |
| 57 | + .build() |
| 58 | + .post() |
| 59 | + .uri(config.getTokenEndpoint()) |
| 60 | + .body(BodyInserters.fromFormData("grant_type", "refresh_token") |
| 61 | + .with("refresh_token", refreshToken) |
| 62 | + .with("client_id", config.getClientId()) |
| 63 | + .with("client_secret", config.getClientSecret())) |
| 64 | + .retrieve() |
| 65 | + .bodyToMono(Map.class) |
| 66 | + .flatMap(map -> { |
| 67 | + if (map.containsKey("error") || map.containsKey("error_description")) { |
| 68 | + return Mono.error(new AuthException(JsonUtils.toJson(map))); |
| 69 | + } |
| 70 | + return Mono.just(mapToAuthToken(map)); |
| 71 | + }); |
| 72 | + } |
| 73 | + |
| 74 | + @Override |
| 75 | + protected Mono<AuthUser> getAuthUser(AuthToken authToken) { |
| 76 | + return WebClientBuildHelper.builder() |
| 77 | + .systemProxy() |
| 78 | + .build() |
| 79 | + .get() |
| 80 | + .uri(config.getUserInfoEndpoint()) |
| 81 | + .headers(headers -> headers.setBearerAuth(authToken.getAccessToken())) |
| 82 | + .retrieve() |
| 83 | + .bodyToMono(Map.class) |
| 84 | + .flatMap(map -> { |
| 85 | + if (map.containsKey("error") || map.containsKey("error_description")) { |
| 86 | + return Mono.error(new AuthException(JsonUtils.toJson(map))); |
| 87 | + } |
| 88 | + return Mono.just(mapToAuthUser(map)); |
| 89 | + }); |
| 90 | + } |
| 91 | +} |
0 commit comments