16
16
import org .lowcoder .domain .query .service .LibraryQueryRecordService ;
17
17
import org .lowcoder .domain .query .service .LibraryQueryService ;
18
18
import org .lowcoder .domain .query .service .QueryExecutionService ;
19
+ import org .lowcoder .domain .user .model .Connection ;
20
+ import org .lowcoder .domain .user .model .User ;
19
21
import org .lowcoder .infra .util .TupleUtils ;
20
22
import org .lowcoder .sdk .config .CommonConfig ;
21
23
import org .lowcoder .sdk .exception .BizError ;
22
24
import org .lowcoder .sdk .models .Property ;
23
25
import org .lowcoder .sdk .models .QueryExecutionResult ;
26
+ import org .lowcoder .sdk .plugin .restapi .RestApiDatasourceConfig ;
27
+ import org .lowcoder .sdk .plugin .restapi .auth .OAuthInheritAuthConfig ;
24
28
import org .lowcoder .sdk .query .QueryVisitorContext ;
25
29
import org .lowcoder .sdk .util .ExceptionUtils ;
26
30
import org .springframework .beans .factory .annotation .Autowired ;
33
37
import reactor .core .publisher .Timed ;
34
38
35
39
import javax .annotation .Nullable ;
40
+ import java .util .Collections ;
36
41
import java .util .List ;
42
+ import java .util .Optional ;
37
43
import java .util .stream .Collectors ;
38
44
39
45
import static org .lowcoder .domain .permission .model .ResourceAction .READ_APPLICATIONS ;
@@ -91,12 +97,13 @@ public Mono<QueryExecutionResult> executeApplicationQuery(ServerWebExchange exch
91
97
Mono <Datasource > datasourceMono = baseQueryMono .flatMap (query -> datasourceService .getById (query .getDatasourceId ())
92
98
.switchIfEmpty (deferredError (BizError .DATASOURCE_NOT_FOUND , "DATASOURCE_NOT_FOUND" , query .getDatasourceId ())))
93
99
.cache ();
94
- return sessionUserService .getVisitorId ()
95
- .delayUntil (userId -> checkExecutePermission (userId , queryExecutionRequest .getPath (), appId ,
100
+
101
+ return sessionUserService .getVisitor ()
102
+ .delayUntil (user -> checkExecutePermission (user .getId (), queryExecutionRequest .getPath (), appId ,
96
103
queryExecutionRequest .isViewMode ()))
97
104
.zipWhen (visitorId -> Mono .zip (appMono , appQueryMono , baseQueryMono , datasourceMono ), TupleUtils ::merge )
98
105
.flatMap (tuple -> {
99
- String userId = tuple .getT1 ();
106
+ String userId = tuple .getT1 (). getId () ;
100
107
Application app = tuple .getT2 ();
101
108
ApplicationQuery appQuery = tuple .getT3 ();
102
109
BaseQuery baseQuery = tuple .getT4 ();
@@ -107,8 +114,19 @@ public Mono<QueryExecutionResult> executeApplicationQuery(ServerWebExchange exch
107
114
}
108
115
109
116
MultiValueMap <String , HttpCookie > cookies = exchange .getRequest ().getCookies ();
110
- QueryVisitorContext queryVisitorContext = new QueryVisitorContext (userId , app .getOrganizationId (), port , cookies ,
111
- getAuthParamsAndHeadersInheritFromLogin (userId , app .getOrganizationId ()), commonConfig .getDisallowedHosts ());
117
+
118
+ Mono <List <Property >> paramsAndHeadersInheritFromLogin = Mono .empty ();
119
+
120
+ if (datasource .isRestApi ()) {
121
+ // then check if oauth inherited from login and save token
122
+ if (datasource .getDetailConfig () instanceof RestApiDatasourceConfig restApiDatasourceConfig
123
+ && restApiDatasourceConfig .isOauth2InheritFromLogin ()) {
124
+ paramsAndHeadersInheritFromLogin = getAuthParamsAndHeadersInheritFromLogin (tuple .getT1 (), ((OAuthInheritAuthConfig )restApiDatasourceConfig .getAuthConfig ()).getAuthId ());
125
+
126
+ }
127
+ }
128
+
129
+ QueryVisitorContext queryVisitorContext = new QueryVisitorContext (userId , app .getOrganizationId (), port , cookies , paramsAndHeadersInheritFromLogin , commonConfig .getDisallowedHosts ());
112
130
return queryExecutionService .executeQuery (datasource , baseQuery .getQueryConfig (), queryExecutionRequest .paramMap (),
113
131
appQuery .getTimeoutStr (), queryVisitorContext
114
132
)
@@ -174,8 +192,18 @@ private Mono<BaseQuery> getBaseQueryFromLibraryQuery(ApplicationQuery query) {
174
192
.map (LibraryQueryRecord ::getQuery );
175
193
}
176
194
177
- protected Mono <List <Property >> getAuthParamsAndHeadersInheritFromLogin (String userId , String orgId ) {
178
- return Mono .empty ();
195
+ protected Mono <List <Property >> getAuthParamsAndHeadersInheritFromLogin (User user , String authId ) {
196
+ if (authId == null ) {
197
+ return Mono .empty ();
198
+ }
199
+ Optional <Connection > activeConnectionOptional = user .getConnections ()
200
+ .stream ()
201
+ .filter (connection -> connection .getAuthId ().equals (authId ))
202
+ .findFirst ();
203
+ if (!activeConnectionOptional .isPresent () || activeConnectionOptional .get ().getAuthConnectionAuthToken () == null ) {
204
+ return Mono .empty ();
205
+ }
206
+ return Mono .just (Collections .singletonList (new Property ("Authorization" ,"Bearer " + activeConnectionOptional .get ().getAuthConnectionAuthToken ().getAccessToken (),"header" )));
179
207
}
180
208
181
209
protected void onNextOrError (QueryExecutionRequest queryExecutionRequest , QueryVisitorContext queryVisitorContext ,
0 commit comments