@@ -31,7 +31,7 @@ type Service struct {
31
31
cipher db.Cipher
32
32
stateJWT * StateJWT
33
33
34
- verifierByIssuer map [string ]* goidc.IDTokenVerifier
34
+ // verifierByIssuer map[string]*goidc.IDTokenVerifier
35
35
sessionServiceAddress string
36
36
37
37
// TODO(at) remove by enhancing test setups
@@ -59,7 +59,6 @@ type AuthFlowResult struct {
59
59
60
60
func NewService (sessionServiceAddress string , dbConn * gorm.DB , cipher db.Cipher , stateJWT * StateJWT ) * Service {
61
61
return & Service {
62
- verifierByIssuer : map [string ]* goidc.IDTokenVerifier {},
63
62
sessionServiceAddress : sessionServiceAddress ,
64
63
65
64
dbConn : dbConn ,
@@ -126,13 +125,36 @@ func randString(size int) (string, error) {
126
125
}
127
126
128
127
func (s * Service ) GetClientConfigFromStartRequest (r * http.Request ) (* ClientConfig , error ) {
128
+ orgSlug := r .URL .Query ().Get ("orgSlug" )
129
+ if orgSlug != "" {
130
+ org , err := db .GetTeamBySlug (r .Context (), s .dbConn , orgSlug )
131
+ if err != nil {
132
+ return nil , fmt .Errorf ("Failed to find org: %w" , err )
133
+ }
134
+
135
+ dbEntries , err := db .ListOIDCClientConfigsForOrganization (r .Context (), s .dbConn , org .ID )
136
+ if err != nil {
137
+ return nil , fmt .Errorf ("Failed to find OIDC clients: %w" , err )
138
+ }
139
+ if len (dbEntries ) < 1 {
140
+ return nil , fmt .Errorf ("No OIDC clients." )
141
+ }
142
+
143
+ config , err := s .convertClientConfig (r .Context (), dbEntries [0 ])
144
+ if err != nil {
145
+ return nil , fmt .Errorf ("Failed to find OIDC clients: %w" , err )
146
+ }
147
+
148
+ return & config , nil
149
+ }
150
+
129
151
idParam := r .URL .Query ().Get ("id" )
130
152
if idParam == "" {
131
153
return nil , fmt .Errorf ("missing id parameter" )
132
154
}
133
155
134
156
if idParam != "" {
135
- config , err := s .getConfigById (idParam )
157
+ config , err := s .getConfigById (r . Context (), idParam )
136
158
if err != nil {
137
159
return nil , err
138
160
}
@@ -152,64 +174,53 @@ func (s *Service) GetClientConfigFromCallbackRequest(r *http.Request) (*ClientCo
152
174
if err != nil {
153
175
return nil , fmt .Errorf ("bad state param" )
154
176
}
155
- config , _ := s .getConfigById (state .ClientConfigID )
177
+ config , _ := s .getConfigById (r . Context (), state .ClientConfigID )
156
178
if config != nil {
157
179
return config , nil
158
180
}
159
181
160
182
return nil , fmt .Errorf ("failed to find OIDC config on callback" )
161
183
}
162
184
163
- func (s * Service ) getConfigById (id string ) (* ClientConfig , error ) {
185
+ func (s * Service ) getConfigById (ctx context. Context , id string ) (* ClientConfig , error ) {
164
186
uuid , err := uuid .Parse (id )
165
187
if err != nil {
166
188
return nil , err
167
189
}
168
- dbEntry , err := db .GetOIDCClientConfig (context . Background () , s .dbConn , uuid )
190
+ dbEntry , err := db .GetOIDCClientConfig (ctx , s .dbConn , uuid )
169
191
if err != nil {
170
192
return nil , err
171
193
}
172
- spec , err := dbEntry . Data . Decrypt ( s . cipher )
194
+ config , err := s . convertClientConfig ( ctx , dbEntry )
173
195
if err != nil {
174
196
log .Log .WithError (err ).Error ("Failed to decrypt oidc client config." )
175
197
return nil , status .Errorf (codes .Internal , "Failed to decrypt OIDC client config." )
176
198
}
177
199
178
- provider , err := oidc .NewProvider (context .Background (), dbEntry .Issuer )
179
- if err != nil {
180
- return nil , err
181
- }
200
+ return & config , nil
201
+ }
182
202
183
- if s .verifierByIssuer [dbEntry .Issuer ] == nil {
184
- if s .skipVerifyIdToken {
185
- s .verifierByIssuer [dbEntry .Issuer ] = provider .Verifier (& goidc.Config {
186
- ClientID : spec .ClientID ,
187
- SkipClientIDCheck : true ,
188
- SkipIssuerCheck : true ,
189
- SkipExpiryCheck : true ,
190
- InsecureSkipSignatureCheck : true ,
191
- })
192
- } else {
193
- s .verifierByIssuer [dbEntry .Issuer ] = provider .Verifier (& goidc.Config {
194
- ClientID : spec .ClientID ,
195
- })
196
- }
203
+ func (s * Service ) convertClientConfig (ctx context.Context , dbEntry db.OIDCClientConfig ) (ClientConfig , error ) {
204
+ spec , err := dbEntry .Data .Decrypt (s .cipher )
205
+ if err != nil {
206
+ log .Log .WithError (err ).Error ("Failed to decrypt oidc client config." )
207
+ return ClientConfig {}, status .Errorf (codes .Internal , "Failed to decrypt OIDC client config." )
197
208
}
198
209
199
- scopes := spec . Scopes
200
- if len ( scopes ) < 1 {
201
- scopes = [] string { "openid" }
210
+ provider , err := oidc . NewProvider ( ctx , dbEntry . Issuer )
211
+ if err != nil {
212
+ return ClientConfig {}, err
202
213
}
203
214
204
- return & ClientConfig {
215
+ return ClientConfig {
205
216
ID : dbEntry .ID .String (),
206
217
OrganizationID : dbEntry .OrganizationID .String (),
207
218
Issuer : dbEntry .Issuer ,
208
219
OAuth2Config : & oauth2.Config {
209
220
ClientID : spec .ClientID ,
210
221
ClientSecret : spec .ClientSecret ,
211
222
Endpoint : provider .Endpoint (),
212
- Scopes : scopes ,
223
+ Scopes : spec . Scopes ,
213
224
},
214
225
VerifierConfig : & goidc.Config {
215
226
ClientID : spec .ClientID ,
@@ -229,10 +240,13 @@ func (s *Service) Authenticate(ctx context.Context, params AuthenticateParams) (
229
240
return nil , fmt .Errorf ("id_token not found" )
230
241
}
231
242
232
- verifier := s . verifierByIssuer [ params .Issuer ]
233
- if verifier = = nil {
234
- return nil , fmt .Errorf ("verifier not found " )
243
+ provider , err := oidc . NewProvider ( ctx , params .Issuer )
244
+ if err ! = nil {
245
+ return nil , fmt .Errorf ("Failed to initialize provider. " )
235
246
}
247
+ verifier := provider .Verifier (& goidc.Config {
248
+ ClientID : params .OAuth2Result .ClientID ,
249
+ })
236
250
237
251
idToken , err := verifier .Verify (ctx , rawIDToken )
238
252
if err != nil {
0 commit comments