@@ -19,6 +19,7 @@ import (
19
19
20
20
"github.com/coder/coder/coderd/audit"
21
21
"github.com/coder/coder/coderd/database"
22
+ "github.com/coder/coder/coderd/gitauth"
22
23
"github.com/coder/coder/coderd/httpapi"
23
24
"github.com/coder/coder/coderd/httpmw"
24
25
"github.com/coder/coder/coderd/parameter"
@@ -243,6 +244,99 @@ func (api *API) templateVersionRichParameters(rw http.ResponseWriter, r *http.Re
243
244
httpapi .Write (ctx , rw , http .StatusOK , templateVersionParameters )
244
245
}
245
246
247
+ // @Summary Get git auth by template version
248
+ // @ID get-git-auth-by-template-version
249
+ // @Security CoderSessionToken
250
+ // @Produce json
251
+ // @Tags Templates
252
+ // @Param templateversion path string true "Template version ID" format(uuid)
253
+ // @Success 200 {array} codersdk.GitAuth
254
+ // @Router /templateversions/{templateversion}/gitauth [get]
255
+ func (api * API ) templateVersionGitAuth (rw http.ResponseWriter , r * http.Request ) {
256
+ ctx := r .Context ()
257
+ var (
258
+ apiKey = httpmw .APIKey (r )
259
+ templateVersion = httpmw .TemplateVersionParam (r )
260
+ template = httpmw .TemplateParam (r )
261
+ )
262
+
263
+ if ! api .Authorize (r , rbac .ActionRead , templateVersion .RBACObject (template )) {
264
+ httpapi .ResourceNotFound (rw )
265
+ return
266
+ }
267
+
268
+ rawProviders := templateVersion .GitAuthProviders
269
+ providers := make ([]codersdk.GitAuth , 0 )
270
+ for _ , rawProvider := range rawProviders {
271
+ var config * gitauth.Config
272
+ for _ , provider := range api .GitAuthConfigs {
273
+ if provider .ID == rawProvider {
274
+ config = provider
275
+ break
276
+ }
277
+ }
278
+ if config == nil {
279
+ httpapi .Write (ctx , rw , http .StatusNotFound , codersdk.Response {
280
+ Message : fmt .Sprintf ("The template version references a Git auth provider %q that no longer exists." , rawProvider ),
281
+ Detail : "You'll need to update the template version to use a different provider." ,
282
+ })
283
+ return
284
+ }
285
+
286
+ // This is the URL that will redirect the user with a state token.
287
+ redirectURL , err := api .AccessURL .Parse (fmt .Sprintf ("/gitauth/%s" , config .ID ))
288
+ if err != nil {
289
+ httpapi .Write (ctx , rw , http .StatusInternalServerError , codersdk.Response {
290
+ Message : "Failed to parse access URL." ,
291
+ Detail : err .Error (),
292
+ })
293
+ return
294
+ }
295
+
296
+ provider := codersdk.GitAuth {
297
+ ID : config .ID ,
298
+ Type : config .Type ,
299
+ AuthenticateURL : redirectURL .String (),
300
+ }
301
+
302
+ authLink , err := api .Database .GetGitAuthLink (ctx , database.GetGitAuthLinkParams {
303
+ ProviderID : config .ID ,
304
+ UserID : apiKey .UserID ,
305
+ })
306
+ // If there isn't an auth link, then the user just isn't authenticated.
307
+ if errors .Is (err , sql .ErrNoRows ) {
308
+ providers = append (providers , provider )
309
+ continue
310
+ }
311
+ if err != nil {
312
+ httpapi .Write (ctx , rw , http .StatusInternalServerError , codersdk.Response {
313
+ Message : "Internal error fetching Git auth link." ,
314
+ Detail : err .Error (),
315
+ })
316
+ return
317
+ }
318
+
319
+ _ , updated , err := refreshGitToken (ctx , api .Database , apiKey .UserID , config , authLink )
320
+ if err != nil {
321
+ httpapi .Write (ctx , rw , http .StatusInternalServerError , codersdk.Response {
322
+ Message : "Failed to refresh git auth token." ,
323
+ Detail : err .Error (),
324
+ })
325
+ return
326
+ }
327
+ // If the token couldn't be validated, then we assume the user isn't
328
+ // authenticated and return early.
329
+ if ! updated {
330
+ providers = append (providers , provider )
331
+ continue
332
+ }
333
+ provider .Authenticated = true
334
+ providers = append (providers , provider )
335
+ }
336
+
337
+ httpapi .Write (ctx , rw , http .StatusOK , providers )
338
+ }
339
+
246
340
// @Summary Get template variables by template version
247
341
// @ID get-template-variables-by-template-version
248
342
// @Security CoderSessionToken
@@ -1461,16 +1555,15 @@ func convertTemplateVersion(version database.TemplateVersion, job codersdk.Provi
1461
1555
}
1462
1556
1463
1557
return codersdk.TemplateVersion {
1464
- ID : version .ID ,
1465
- TemplateID : & version .TemplateID .UUID ,
1466
- OrganizationID : version .OrganizationID ,
1467
- CreatedAt : version .CreatedAt ,
1468
- UpdatedAt : version .UpdatedAt ,
1469
- GitAuthProviders : version .GitAuthProviders ,
1470
- Name : version .Name ,
1471
- Job : job ,
1472
- Readme : version .Readme ,
1473
- CreatedBy : createdBy ,
1558
+ ID : version .ID ,
1559
+ TemplateID : & version .TemplateID .UUID ,
1560
+ OrganizationID : version .OrganizationID ,
1561
+ CreatedAt : version .CreatedAt ,
1562
+ UpdatedAt : version .UpdatedAt ,
1563
+ Name : version .Name ,
1564
+ Job : job ,
1565
+ Readme : version .Readme ,
1566
+ CreatedBy : createdBy ,
1474
1567
}
1475
1568
}
1476
1569
0 commit comments