1
1
package org .lowcoder .api .authentication .request .oauth2 ;
2
2
3
+ import java .util .HashMap ;
3
4
import java .util .Set ;
4
5
5
6
import org .lowcoder .api .authentication .request .AuthRequest ;
9
10
import org .lowcoder .sdk .auth .Oauth2KeycloakAuthConfig ;
10
11
import org .lowcoder .sdk .auth .Oauth2OryAuthConfig ;
11
12
import org .lowcoder .sdk .auth .Oauth2SimpleAuthConfig ;
13
+ import org .lowcoder .sdk .auth .constants .AuthTypeConstants ;
12
14
import org .springframework .stereotype .Component ;
13
15
14
16
import reactor .core .publisher .Mono ;
@@ -25,10 +27,98 @@ public Mono<AuthRequest> build(OAuth2RequestContext context) {
25
27
26
28
private AbstractOauth2Request <? extends Oauth2SimpleAuthConfig > buildRequest (OAuth2RequestContext context ) {
27
29
return switch (context .getAuthConfig ().getAuthType ()) {
28
- case GITHUB -> new GithubRequest ((Oauth2SimpleAuthConfig ) context .getAuthConfig ());
29
- case GOOGLE -> new GoogleRequest ((Oauth2SimpleAuthConfig ) context .getAuthConfig ());
30
- case ORY -> new OryRequest ((Oauth2OryAuthConfig ) context .getAuthConfig ());
31
- case KEYCLOAK -> new KeycloakRequest ((Oauth2KeycloakAuthConfig )context .getAuthConfig ());
30
+ case GITHUB -> {
31
+ HashMap <String , String > sourceMappings = new HashMap <>();
32
+ sourceMappings .put ("uid" , "id" );
33
+ sourceMappings .put ("email" , "email" );
34
+ sourceMappings .put ("username" , "login" );
35
+ sourceMappings .put ("avatar" , "avatar_url" );
36
+ Oauth2SimpleAuthConfig config = (Oauth2SimpleAuthConfig ) context .getAuthConfig ();
37
+ yield new GenericAuthRequest (Oauth2GenericAuthConfig .builder ()
38
+ .tokenEndpoint (Oauth2DefaultSource .GITHUB .accessToken ())
39
+ .userInfoEndpoint (Oauth2DefaultSource .GITHUB .userInfo ())
40
+ .userInfoIntrospection (true )
41
+ .source (config .getSource ())
42
+ .sourceName (config .getSourceName ())
43
+ .enableRegister (config .isEnableRegister ())
44
+ .enable (config .isEnable ())
45
+ .scope ("read:email read:user" )
46
+ .userCanSelectAccounts (true )
47
+ .sourceMappings (sourceMappings )
48
+ .clientSecret (config .getClientSecret ())
49
+ .clientId (config .getClientId ())
50
+ .authType (GENERIC )
51
+ .build ());
52
+ }
53
+ case GOOGLE -> {
54
+ HashMap <String , String > sourceMappings = new HashMap <>();
55
+ sourceMappings .put ("uid" , "sub" );
56
+ sourceMappings .put ("email" , "email" );
57
+ sourceMappings .put ("username" , "email" );
58
+ sourceMappings .put ("avatar" , "picture" );
59
+ Oauth2SimpleAuthConfig config = (Oauth2SimpleAuthConfig ) context .getAuthConfig ();
60
+ yield new GenericAuthRequest (Oauth2GenericAuthConfig .builder ()
61
+ .tokenEndpoint (Oauth2DefaultSource .GOOGLE .accessToken ())
62
+ .userInfoEndpoint (Oauth2DefaultSource .GOOGLE .userInfo ())
63
+ .userInfoIntrospection (true )
64
+ .source (config .getSource ())
65
+ .sourceName (config .getSourceName ())
66
+ .enableRegister (config .isEnableRegister ())
67
+ .enable (config .isEnable ())
68
+ .scope ("openid email profile" )
69
+ .userCanSelectAccounts (true )
70
+ .sourceMappings (sourceMappings )
71
+ .clientSecret (config .getClientSecret ())
72
+ .clientId (config .getClientId ())
73
+ .authType (GENERIC )
74
+ .build ());
75
+ }
76
+ case ORY -> {
77
+ HashMap <String , String > sourceMappings = new HashMap <>();
78
+ sourceMappings .put ("uid" , "sub" );
79
+ sourceMappings .put ("email" , "email" );
80
+ sourceMappings .put ("username" , "email" );
81
+ sourceMappings .put ("avatar" , "picture" );
82
+ Oauth2OryAuthConfig config = (Oauth2OryAuthConfig ) context .getAuthConfig ();
83
+ yield new GenericAuthRequest (Oauth2GenericAuthConfig .builder ()
84
+ .tokenEndpoint (config .replaceAuthUrlClientIdPlaceholder (Oauth2DefaultSource .ORY .accessToken ()))
85
+ .userInfoEndpoint (config .replaceAuthUrlClientIdPlaceholder (Oauth2DefaultSource .ORY .userInfo ()))
86
+ .userInfoIntrospection (true )
87
+ .source (config .getSource ())
88
+ .sourceName (config .getSourceName ())
89
+ .enableRegister (config .isEnableRegister ())
90
+ .enable (config .isEnable ())
91
+ .scope (config .getScope ())
92
+ .userCanSelectAccounts (false )
93
+ .sourceMappings (sourceMappings )
94
+ .clientSecret (config .getClientSecret ())
95
+ .clientId (config .getClientId ())
96
+ .authType (GENERIC )
97
+ .build ());
98
+ }
99
+ case KEYCLOAK -> {
100
+ HashMap <String , String > sourceMappings = new HashMap <>();
101
+ sourceMappings .put ("uid" , "sub" );
102
+ sourceMappings .put ("email" , "email" );
103
+ sourceMappings .put ("username" , "email" );
104
+ sourceMappings .put ("avatar" , "false" );
105
+ Oauth2KeycloakAuthConfig config = (Oauth2KeycloakAuthConfig ) context .getAuthConfig ();
106
+ yield new GenericAuthRequest (Oauth2GenericAuthConfig .builder ()
107
+ .tokenEndpoint (config .replaceAuthUrlClientIdPlaceholder (Oauth2DefaultSource .KEYCLOAK .accessToken ()))
108
+ .userInfoEndpoint (config .replaceAuthUrlClientIdPlaceholder (Oauth2DefaultSource .KEYCLOAK .userInfo ()))
109
+ .userInfoIntrospection (true )
110
+ .source (config .getSource ())
111
+ .sourceName (config .getSourceName ())
112
+ .enableRegister (config .isEnableRegister ())
113
+ .enable (config .isEnable ())
114
+ .scope (config .getScope ())
115
+ .userCanSelectAccounts (false )
116
+ .sourceMappings (sourceMappings )
117
+ .clientSecret (config .getClientSecret ())
118
+ .clientId (config .getClientId ())
119
+ .authType (GENERIC )
120
+ .build ());
121
+ }
32
122
case GENERIC -> new GenericAuthRequest ((Oauth2GenericAuthConfig ) context .getAuthConfig ());
33
123
default -> throw new UnsupportedOperationException (context .getAuthConfig ().getAuthType ());
34
124
};
0 commit comments