Skip to content

Commit dde066d

Browse files
committed
Finalize ory support
1 parent a1555ac commit dde066d

File tree

4 files changed

+21
-18
lines changed

4 files changed

+21
-18
lines changed

server/api-service/lowcoder-sdk/src/main/java/org/lowcoder/sdk/auth/Oauth2OryAuthConfig.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public Oauth2OryAuthConfig(
3838
}
3939

4040
@Override
41-
protected String replaceAuthUrlClientIdPlaceholder(String url) {
41+
public String replaceAuthUrlClientIdPlaceholder(String url) {
4242
return super.replaceAuthUrlClientIdPlaceholder(url).replace(INSTANCE_ID_PLACEHOLDER, instanceId);
4343
}
4444
}

server/api-service/lowcoder-sdk/src/main/java/org/lowcoder/sdk/auth/Oauth2SimpleAuthConfig.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public void merge(AbstractAuthConfig oldConfig) {
7474
}
7575
}
7676

77-
protected String replaceAuthUrlClientIdPlaceholder(String url) {
77+
public String replaceAuthUrlClientIdPlaceholder(String url) {
7878
return url.replace(CLIENT_ID_PLACEHOLDER, clientId);
7979
}
8080
}

server/api-service/lowcoder-server/src/main/java/org/lowcoder/api/authentication/request/oauth2/request/OryRequest.java

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,18 @@
88
import org.lowcoder.domain.user.model.AuthToken;
99
import org.lowcoder.domain.user.model.AuthUser;
1010
import org.lowcoder.sdk.auth.Oauth2OryAuthConfig;
11-
import org.lowcoder.sdk.auth.Oauth2SimpleAuthConfig;
1211
import org.lowcoder.sdk.util.JsonUtils;
1312
import org.lowcoder.sdk.webclient.WebClientBuildHelper;
1413
import org.springframework.core.ParameterizedTypeReference;
14+
import org.springframework.http.MediaType;
1515
import reactor.core.publisher.Mono;
1616

1717
import java.net.URI;
1818
import java.net.URISyntaxException;
1919
import java.util.Map;
2020

21+
import static org.springframework.web.reactive.function.BodyInserters.fromFormData;
22+
2123
public class OryRequest extends AbstractOauth2Request<Oauth2OryAuthConfig> {
2224

2325
public OryRequest(Oauth2OryAuthConfig config) {
@@ -28,13 +30,7 @@ public OryRequest(Oauth2OryAuthConfig config) {
2830
protected Mono<AuthToken> getAuthToken(OAuth2RequestContext context) {
2931
URI uri;
3032
try {
31-
uri = new URIBuilder(source.accessToken())
32-
.addParameter("code", context.getCode())
33-
.addParameter("client_id", config.getClientId())
34-
.addParameter("client_secret", config.getClientSecret())
35-
.addParameter("grant_type", "authorization_code")
36-
.addParameter("redirect_uri", context.getRedirectUrl())
37-
.build();
33+
uri = new URIBuilder(config.replaceAuthUrlClientIdPlaceholder(source.accessToken())).build();
3834
} catch (URISyntaxException e) {
3935
throw new RuntimeException(e);
4036
}
@@ -44,6 +40,12 @@ protected Mono<AuthToken> getAuthToken(OAuth2RequestContext context) {
4440
.build()
4541
.post()
4642
.uri(uri)
43+
.contentType(MediaType.APPLICATION_FORM_URLENCODED)
44+
.body(fromFormData("code", context.getCode())
45+
.with("client_id", config.getClientId())
46+
.with("client_secret", config.getClientSecret())
47+
.with("grant_type", "authorization_code")
48+
.with("redirect_uri", context.getRedirectUrl()))
4749
.exchangeToMono(response -> response.bodyToMono(new ParameterizedTypeReference<Map<String, Object>>() {
4850
}))
4951
.flatMap(map -> {
@@ -64,12 +66,7 @@ protected Mono<AuthToken> refreshAuthToken(String refreshToken) {
6466

6567
URI uri;
6668
try {
67-
uri = new URIBuilder(source.refresh())
68-
.addParameter("refresh_token", refreshToken)
69-
.addParameter("client_id", config.getClientId())
70-
.addParameter("client_secret", config.getClientSecret())
71-
.addParameter("grant_type", "refresh_token")
72-
.build();
69+
uri = new URIBuilder(config.replaceAuthUrlClientIdPlaceholder(source.refresh())).build();
7370
} catch (URISyntaxException e) {
7471
throw new RuntimeException(e);
7572
}
@@ -79,6 +76,11 @@ protected Mono<AuthToken> refreshAuthToken(String refreshToken) {
7976
.build()
8077
.post()
8178
.uri(uri)
79+
.contentType(MediaType.APPLICATION_FORM_URLENCODED)
80+
.body(fromFormData("refresh_token", refreshToken)
81+
.with("client_id", config.getClientId())
82+
.with("client_secret", config.getClientSecret())
83+
.with("grant_type", "refresh_token"))
8284
.exchangeToMono(response -> response.bodyToMono(new ParameterizedTypeReference<Map<String, Object>>() {
8385
}))
8486
.flatMap(map -> {
@@ -88,6 +90,7 @@ protected Mono<AuthToken> refreshAuthToken(String refreshToken) {
8890
AuthToken authToken = AuthToken.builder()
8991
.accessToken(MapUtils.getString(map, "access_token"))
9092
.expireIn(MapUtils.getIntValue(map, "expires_in"))
93+
.refreshToken(MapUtils.getString(map, "refresh_token"))
9194
.build();
9295
return Mono.just(authToken);
9396
});
@@ -100,7 +103,7 @@ protected Mono<AuthUser> getAuthUser(AuthToken authToken) {
100103
.systemProxy()
101104
.build()
102105
.post()
103-
.uri(source.userInfo())
106+
.uri(config.replaceAuthUrlClientIdPlaceholder(source.userInfo()))
104107
.header("Authorization", "Bearer " + authToken.getAccessToken())
105108
.exchangeToMono(response -> response.bodyToMono(new ParameterizedTypeReference<Map<String, Object>>() {
106109
}))

server/api-service/lowcoder-server/src/main/java/org/lowcoder/api/framework/filter/UserSessionPersistenceFilter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public Mono<Void> filter(@Nonnull ServerWebExchange exchange, WebFilterChain cha
5959
user.getConnections().forEach(connection -> {
6060
if(!connection.getAuthId().equals(DEFAULT_AUTH_CONFIG.getId())) {
6161
Instant next5Minutes = Instant.now().plusSeconds( 300 );
62-
boolean isAccessTokenExpiryNear = connection.getAuthConnectionAuthToken().getExpireAt() <= next5Minutes.toEpochMilli();
62+
boolean isAccessTokenExpiryNear = (connection.getAuthConnectionAuthToken().getExpireAt()*1000) <= next5Minutes.toEpochMilli();
6363
if(isAccessTokenExpiryNear) {
6464
connection.getOrgIds().forEach(orgId -> {
6565
FindAuthConfig findAuthConfig = authenticationService.findAuthConfigByAuthId(orgId, connection.getAuthId()).block();

0 commit comments

Comments
 (0)