|
| 1 | +// Copyright 2016 The Gitea Authors. All rights reserved. |
| 2 | +// SPDX-License-Identifier: MIT |
| 3 | + |
| 4 | +// OIDC provider for Gitea Actions |
| 5 | +package v1 |
| 6 | + |
| 7 | +import ( |
| 8 | + "fmt" |
| 9 | + "net/http" |
| 10 | + |
| 11 | + actions_model "code.gitea.io/gitea/models/actions" |
| 12 | + "code.gitea.io/gitea/modules/context" |
| 13 | + "code.gitea.io/gitea/modules/git" |
| 14 | + "code.gitea.io/gitea/modules/setting" |
| 15 | + "code.gitea.io/gitea/modules/timeutil" |
| 16 | + auth_service "code.gitea.io/gitea/services/auth" |
| 17 | + "code.gitea.io/gitea/services/auth/source/oauth2" |
| 18 | + |
| 19 | + "github.com/golang-jwt/jwt/v5" |
| 20 | +) |
| 21 | + |
| 22 | +type IDTokenResponse struct { |
| 23 | + Value string `json:"value"` |
| 24 | + Count int `json:"count"` |
| 25 | +} |
| 26 | + |
| 27 | +type IDTokenErrorResponse struct { |
| 28 | + ErrorDescription string `json:"error_description"` |
| 29 | +} |
| 30 | + |
| 31 | +type IDToken struct { |
| 32 | + jwt.RegisteredClaims |
| 33 | + |
| 34 | + Ref string `json:"ref,omitempty"` |
| 35 | + SHA string `json:"sha,omitempty"` |
| 36 | + Repository string `json:"repository,omitempty"` |
| 37 | + RepositoryOwner string `json:"repository_owner,omitempty"` |
| 38 | + RepositoryOwnerID int `json:"repository_owner_id,omitempty"` |
| 39 | + RunID int `json:"run_id,omitempty"` |
| 40 | + RunNumber int `json:"run_number,omitempty"` |
| 41 | + RunAttempt int `json:"run_attempt,omitempty"` |
| 42 | + RepositoryVisibility string `json:"repository_visibility,omitempty"` |
| 43 | + RepositoryID int `json:"repository_id,omitempty"` |
| 44 | + ActorID int `json:"actor_id,omitempty"` |
| 45 | + Actor string `json:"actor,omitempty"` |
| 46 | + Workflow string `json:"workflow,omitempty"` |
| 47 | + EventName string `json:"event_name,omitempty"` |
| 48 | + RefType string `json:"ref_type,omitempty"` |
| 49 | + HeadRef string `json:"head_ref,omitempty"` |
| 50 | + BaseRef string `json:"base_ref,omitempty"` |
| 51 | + |
| 52 | + // Github's OIDC tokens have all of these, but I wasn't sure how |
| 53 | + // to populate them. Leaving them here to make future work easier. |
| 54 | + |
| 55 | + /* |
| 56 | + WorkflowRef string `json:"workflow_ref,omitempty"` |
| 57 | + WorkflowSHA string `json:"workflow_sha,omitempty"` |
| 58 | + JobWorkflowRef string `json:"job_workflow_ref,omitempty"` |
| 59 | + JobWorkflowSHA string `json:"job_workflow_sha,omitempty"` |
| 60 | + RunnerEnvironment string `json:"runner_environment,omitempty"` |
| 61 | + */ |
| 62 | +} |
| 63 | + |
| 64 | +func generateOIDCToken(ctx *context.APIContext) { |
| 65 | + if ctx.Doer == nil || ctx.Data["AuthedMethod"] != (&auth_service.OAuth2{}).Name() || ctx.Data["IsActionsToken"] != true { |
| 66 | + ctx.PlainText(http.StatusUnauthorized, "no valid authorization") |
| 67 | + return |
| 68 | + } |
| 69 | + |
| 70 | + task := ctx.Data["ActionsTask"].(*actions_model.ActionTask) |
| 71 | + if err := task.LoadJob(ctx); err != nil { |
| 72 | + ctx.PlainText(http.StatusUnauthorized, "no valid authorization") |
| 73 | + return |
| 74 | + } |
| 75 | + |
| 76 | + if mayCreateToken := task.Job.MayCreateIDToken(); !mayCreateToken { |
| 77 | + ctx.PlainText(http.StatusUnauthorized, "no valid authorization") |
| 78 | + return |
| 79 | + } |
| 80 | + |
| 81 | + if err := task.Job.LoadAttributes(ctx); err != nil { |
| 82 | + ctx.PlainText(http.StatusUnauthorized, "no valid authorization") |
| 83 | + return |
| 84 | + } |
| 85 | + |
| 86 | + if err := task.Job.Run.LoadAttributes(ctx); err != nil { |
| 87 | + ctx.PlainText(http.StatusUnauthorized, "no valid authorization") |
| 88 | + return |
| 89 | + } |
| 90 | + |
| 91 | + if err := task.Job.Run.Repo.LoadAttributes(ctx); err != nil { |
| 92 | + ctx.PlainText(http.StatusUnauthorized, "no valid authorization") |
| 93 | + return |
| 94 | + } |
| 95 | + |
| 96 | + eventName := task.Job.Run.EventName() |
| 97 | + ref, sha, baseRef, headRef := task.Job.Run.RefShaBaseRefAndHeadRef() |
| 98 | + |
| 99 | + jwtAudience := jwt.ClaimStrings{task.Job.Run.Repo.Owner.HTMLURL()} |
| 100 | + requestedAudience := ctx.Req.URL.Query().Get("audience") |
| 101 | + if requestedAudience != "" { |
| 102 | + jwtAudience = append(jwtAudience, requestedAudience) |
| 103 | + } |
| 104 | + |
| 105 | + // generate OIDC token |
| 106 | + issueTime := timeutil.TimeStampNow() |
| 107 | + expirationTime := timeutil.TimeStampNow().Add(15 * 60) |
| 108 | + notBeforeTime := timeutil.TimeStampNow().Add(-15 * 60) |
| 109 | + idToken := &IDToken{ |
| 110 | + RegisteredClaims: jwt.RegisteredClaims{ |
| 111 | + Issuer: setting.AppURL, |
| 112 | + Audience: jwtAudience, |
| 113 | + ExpiresAt: jwt.NewNumericDate(expirationTime.AsTime()), |
| 114 | + NotBefore: jwt.NewNumericDate(notBeforeTime.AsTime()), |
| 115 | + IssuedAt: jwt.NewNumericDate(issueTime.AsTime()), |
| 116 | + Subject: fmt.Sprintf("repo:%s:ref:%s", task.Job.Run.Repo.FullName(), ref), |
| 117 | + }, |
| 118 | + Ref: ref, |
| 119 | + SHA: sha, |
| 120 | + Repository: task.Job.Run.Repo.FullName(), |
| 121 | + RepositoryOwner: task.Job.Run.Repo.OwnerName, |
| 122 | + RepositoryOwnerID: int(task.Job.Run.Repo.OwnerID), |
| 123 | + RunID: int(task.Job.RunID), |
| 124 | + RunNumber: int(task.Job.Run.Index), |
| 125 | + RunAttempt: int(task.Job.Attempt), |
| 126 | + RepositoryID: int(task.Job.Run.RepoID), |
| 127 | + ActorID: int(task.Job.Run.TriggerUserID), |
| 128 | + Actor: task.Job.Run.TriggerUser.Name, |
| 129 | + Workflow: task.Job.Run.WorkflowID, |
| 130 | + EventName: eventName, |
| 131 | + RefType: git.RefName(task.Job.Run.Ref).RefType(), |
| 132 | + BaseRef: baseRef, |
| 133 | + HeadRef: headRef, |
| 134 | + } |
| 135 | + |
| 136 | + if task.Job.Run.Repo.IsPrivate { |
| 137 | + idToken.RepositoryVisibility = "private" |
| 138 | + } else { |
| 139 | + idToken.RepositoryVisibility = "public" |
| 140 | + } |
| 141 | + |
| 142 | + signedIDToken, err := oauth2.SignToken(idToken, oauth2.DefaultSigningKey) |
| 143 | + if err != nil { |
| 144 | + ctx.JSON(http.StatusInternalServerError, &IDTokenErrorResponse{ |
| 145 | + ErrorDescription: "unable to sign token", |
| 146 | + }) |
| 147 | + return |
| 148 | + } |
| 149 | + |
| 150 | + ctx.JSON(http.StatusOK, IDTokenResponse{ |
| 151 | + Value: signedIDToken, |
| 152 | + Count: len(signedIDToken), |
| 153 | + }) |
| 154 | +} |
0 commit comments