Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow the actions user to login via the jwt token #32527

Merged
merged 6 commits into from
Nov 20, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 47 additions & 1 deletion services/auth/oauth2.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package auth

import (
"context"
"errors"
"net/http"
"strings"
"time"
Expand All @@ -16,7 +17,9 @@ import (
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/timeutil"
"code.gitea.io/gitea/modules/util"
"code.gitea.io/gitea/modules/web/middleware"
"code.gitea.io/gitea/services/actions"
"code.gitea.io/gitea/services/oauth2_provider"
)

Expand Down Expand Up @@ -131,6 +134,40 @@ func (o *OAuth2) userIDFromToken(ctx context.Context, tokenSHA string, store Dat
return t.UID
}

// parseActionJWT identifies actions runner JWTs that look like an
// OAuth token, but needs to be parsed by its code
func parseActionsJWT(req *http.Request, store DataStore) (*user_model.User, error) {
taskID, err := actions.ParseAuthorizationToken(req)
if err != nil || taskID == 0 {
return nil, nil
}

// Verify the task exists
task, err := actions_model.GetTaskByID(req.Context(), taskID)
if err != nil {
if errors.Is(err, util.ErrNotExist) {
return nil, nil
}

return nil, err
}

// Verify that it's running
if task.Status != actions_model.StatusRunning {
return nil, nil
}

store.GetData()["IsActionsToken"] = true
store.GetData()["ActionsTaskID"] = taskID

user, err := user_model.GetPossibleUserByID(req.Context(), user_model.ActionsUserID)
if err != nil {
return nil, err
}

return user, nil
}

// Verify extracts the user ID from the OAuth token in the query parameters
// or the "Authorization" header and returns the corresponding user object for that ID.
// If verification is successful returns an existing user object.
Expand All @@ -142,6 +179,15 @@ func (o *OAuth2) Verify(req *http.Request, w http.ResponseWriter, store DataStor
return nil, nil
}

user, err := parseActionsJWT(req, store)
wxiaoguang marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return nil, err
}

if user != nil {
return user, nil
}

token, ok := parseToken(req)
if !ok {
return nil, nil
Expand All @@ -154,7 +200,7 @@ func (o *OAuth2) Verify(req *http.Request, w http.ResponseWriter, store DataStor
}
log.Trace("OAuth2 Authorization: Found token for user[%d]", id)

user, err := user_model.GetPossibleUserByID(req.Context(), id)
user, err = user_model.GetPossibleUserByID(req.Context(), id)
if err != nil {
if !user_model.IsErrUserNotExist(err) {
log.Error("GetUserByName: %v", err)
Expand Down
Loading