-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Add artifacts v4 jwt to job message and accept it #28885
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
Merged
lunny
merged 4 commits into
go-gitea:main
from
ChristopherHX:jwt-based-actions-runtime-token
Feb 2, 2024
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
33eaca4
Add artifacts v4 jwt to job message and accept it
ChristopherHX e4cd2b6
add test
ChristopherHX e887171
Merge branch 'main' into jwt-based-actions-runtime-token
GiteaBot aab27c9
Merge branch 'main' into jwt-based-actions-runtime-token
GiteaBot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
// Copyright 2024 The Gitea Authors. All rights reserved. | ||
// SPDX-License-Identifier: MIT | ||
|
||
package actions | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"strings" | ||
"time" | ||
|
||
"code.gitea.io/gitea/modules/log" | ||
"code.gitea.io/gitea/modules/setting" | ||
|
||
"github.com/golang-jwt/jwt/v5" | ||
) | ||
|
||
type actionsClaims struct { | ||
jwt.RegisteredClaims | ||
Scp string `json:"scp"` | ||
TaskID int64 | ||
RunID int64 | ||
JobID int64 | ||
} | ||
|
||
func CreateAuthorizationToken(taskID, runID, jobID int64) (string, error) { | ||
now := time.Now() | ||
|
||
claims := actionsClaims{ | ||
RegisteredClaims: jwt.RegisteredClaims{ | ||
ExpiresAt: jwt.NewNumericDate(now.Add(24 * time.Hour)), | ||
NotBefore: jwt.NewNumericDate(now), | ||
}, | ||
Scp: fmt.Sprintf("Actions.Results:%d:%d", runID, jobID), | ||
TaskID: taskID, | ||
RunID: runID, | ||
JobID: jobID, | ||
} | ||
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims) | ||
|
||
tokenString, err := token.SignedString([]byte(setting.SecretKey)) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
return tokenString, nil | ||
} | ||
|
||
func ParseAuthorizationToken(req *http.Request) (int64, error) { | ||
h := req.Header.Get("Authorization") | ||
if h == "" { | ||
return 0, nil | ||
} | ||
|
||
parts := strings.SplitN(h, " ", 2) | ||
if len(parts) != 2 { | ||
log.Error("split token failed: %s", h) | ||
return 0, fmt.Errorf("split token failed") | ||
} | ||
|
||
token, err := jwt.ParseWithClaims(parts[1], &actionsClaims{}, func(t *jwt.Token) (any, error) { | ||
if _, ok := t.Method.(*jwt.SigningMethodHMAC); !ok { | ||
return nil, fmt.Errorf("unexpected signing method: %v", t.Header["alg"]) | ||
} | ||
return []byte(setting.SecretKey), nil | ||
}) | ||
if err != nil { | ||
return 0, err | ||
} | ||
|
||
c, ok := token.Claims.(*actionsClaims) | ||
if !token.Valid || !ok { | ||
return 0, fmt.Errorf("invalid token claim") | ||
} | ||
|
||
return c.TaskID, nil | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// Copyright 2024 The Gitea Authors. All rights reserved. | ||
// SPDX-License-Identifier: MIT | ||
|
||
package actions | ||
|
||
import ( | ||
"net/http" | ||
"testing" | ||
|
||
"code.gitea.io/gitea/modules/setting" | ||
|
||
"github.com/golang-jwt/jwt/v5" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestCreateAuthorizationToken(t *testing.T) { | ||
var taskID int64 = 23 | ||
token, err := CreateAuthorizationToken(taskID, 1, 2) | ||
assert.Nil(t, err) | ||
assert.NotEqual(t, "", token) | ||
claims := jwt.MapClaims{} | ||
_, err = jwt.ParseWithClaims(token, claims, func(t *jwt.Token) (interface{}, error) { | ||
return []byte(setting.SecretKey), nil | ||
}) | ||
assert.Nil(t, err) | ||
scp, ok := claims["scp"] | ||
assert.True(t, ok, "Has scp claim in jwt token") | ||
assert.Contains(t, scp, "Actions.Results:1:2") | ||
taskIDClaim, ok := claims["TaskID"] | ||
assert.True(t, ok, "Has TaskID claim in jwt token") | ||
assert.Equal(t, float64(taskID), taskIDClaim, "Supplied taskid must match stored one") | ||
} | ||
|
||
func TestParseAuthorizationToken(t *testing.T) { | ||
var taskID int64 = 23 | ||
token, err := CreateAuthorizationToken(taskID, 1, 2) | ||
assert.Nil(t, err) | ||
assert.NotEqual(t, "", token) | ||
headers := http.Header{} | ||
headers.Set("Authorization", "Bearer "+token) | ||
rTaskID, err := ParseAuthorizationToken(&http.Request{ | ||
Header: headers, | ||
}) | ||
assert.Nil(t, err) | ||
assert.Equal(t, taskID, rTaskID) | ||
} | ||
|
||
func TestParseAuthorizationTokenNoAuthHeader(t *testing.T) { | ||
headers := http.Header{} | ||
rTaskID, err := ParseAuthorizationToken(&http.Request{ | ||
Header: headers, | ||
}) | ||
assert.Nil(t, err) | ||
assert.Equal(t, int64(0), rTaskID) | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.