The goal is to enable access to Git LFS resources with Authorized Integrations JWTs. Blocker that needed to be resolved is that adding the `AuthorizedIntegration` auth method would conflict with the LFS tokens, which are handed out during git ssh clones to allow access to LFS resources -- `AuthorizedIntegration` would mark these as `AuthenticationAttemptedIncorrectCredential`, and therefore the requests would 401 before they got to the LFS-specific token validation routines. The fix is to move LFS token authentication into an authentication group so that it could be resolved at the same time as the authorized integration, rather than doing it inside the LFS server routines. Refactors for LFS tokens are covered by refreshed test automation. Authorized integrations LFS Access has been manually tested, and will be further covered in an end-to-end integration test (https://code.forgejo.org/forgejo/end-to-end/pulls/1954). ## Checklist The [contributor guide](https://forgejo.org/docs/next/contributor/) contains information that will be helpful to first time contributors. All work and communication must conform to Forgejo's [AI Agreement](https://codeberg.org/forgejo/governance/src/branch/main/AIAgreement.md). There also are a few [conditions for merging Pull Requests in Forgejo repositories](https://codeberg.org/forgejo/governance/src/branch/main/PullRequestsAgreement.md). You are also welcome to join the [Forgejo development chatroom](https://matrix.to/#/#forgejo-development:matrix.org). ### Tests for Go changes - I added test coverage for Go changes... - [x] in their respective `*_test.go` for unit tests. - [ ] in the `tests/integration` directory if it involves interactions with a live Forgejo server. - I ran... - [ ] `make pr-go` before pushing ### Documentation - [ ] I created a pull request [to the documentation](https://codeberg.org/forgejo/docs) to explain to Forgejo users how to use this change. - [x] I did not document these changes and I do not expect someone else to do it. ### Release notes - [ ] This change will be noticed by a Forgejo user or admin (feature, bug fix, performance, etc.). I suggest to include a release note for this change. - [x] This change is not visible to a Forgejo user or admin (refactor, dependency upgrade, etc.). I think there is no need to add a release note for this change. Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/12725 Reviewed-by: Andreas Ahlenstorf <aahlenst@noreply.codeberg.org> Reviewed-by: Gusted <gusted@noreply.codeberg.org>
161 lines
5.4 KiB
Go
161 lines
5.4 KiB
Go
// Copyright 2025 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package method
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http/httptest"
|
|
"testing"
|
|
"time"
|
|
|
|
auth_model "forgejo.org/models/auth"
|
|
"forgejo.org/models/unittest"
|
|
"forgejo.org/modules/lfs"
|
|
"forgejo.org/modules/optional"
|
|
"forgejo.org/modules/setting"
|
|
"forgejo.org/services/auth"
|
|
"forgejo.org/services/authz"
|
|
|
|
"github.com/golang-jwt/jwt/v5"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
type authTokenOptions struct {
|
|
Op string
|
|
UserID int64
|
|
RepoID int64
|
|
}
|
|
|
|
func getLFSAuthTokenWithBearer(opts authTokenOptions) (string, error) {
|
|
now := time.Now()
|
|
claims := lfs.Claims{
|
|
RegisteredClaims: jwt.RegisteredClaims{
|
|
ExpiresAt: jwt.NewNumericDate(now.Add(setting.LFS.HTTPAuthExpiry)),
|
|
NotBefore: jwt.NewNumericDate(now),
|
|
},
|
|
RepoID: opts.RepoID,
|
|
Op: opts.Op,
|
|
UserID: opts.UserID,
|
|
}
|
|
|
|
// Sign and get the complete encoded token as a string using the secret
|
|
tokenString, err := setting.LFS.SigningKey.JWT(claims)
|
|
if err != nil {
|
|
return "", fmt.Errorf("failed to sign LFS JWT token: %w", err)
|
|
}
|
|
return "Bearer " + tokenString, nil
|
|
}
|
|
|
|
func testAuthenticate(t *testing.T, cfg string) {
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
|
var err error
|
|
setting.CfgProvider, err = setting.NewConfigProviderFromData(cfg)
|
|
require.NoError(t, err, "Config")
|
|
setting.LoadCommonSettings()
|
|
|
|
t.Run("no bearer token", func(t *testing.T) {
|
|
a := &LFSToken{}
|
|
req := httptest.NewRequest("GET", "https://example.org", nil)
|
|
output := a.Verify(req, nil, nil)
|
|
requireOutput[*auth.AuthenticationNotAttempted](t, output)
|
|
})
|
|
|
|
t.Run("bearer not a JWT", func(t *testing.T) {
|
|
a := &LFSToken{}
|
|
req := httptest.NewRequest("GET", "https://example.org", nil)
|
|
req.Header.Set("Authorization", "Bearer abc")
|
|
output := a.Verify(req, nil, nil)
|
|
err := requireOutput[*auth.AuthenticationAttemptedIncorrectCredential](t, output).Error
|
|
require.ErrorContains(t, err, "token is malformed")
|
|
})
|
|
|
|
t.Run("token valid op=download", func(t *testing.T) {
|
|
bearerAuth, err := getLFSAuthTokenWithBearer(authTokenOptions{Op: "download", UserID: 2, RepoID: 1})
|
|
require.NoError(t, err)
|
|
a := &LFSToken{}
|
|
req := httptest.NewRequest("GET", "https://example.org", nil)
|
|
req.Header.Set("Authorization", bearerAuth)
|
|
output := a.Verify(req, nil, nil)
|
|
result := requireOutput[*auth.AuthenticationSuccess](t, output).Result
|
|
assert.EqualValues(t, 2, result.User().ID)
|
|
assert.Equal(t, optional.Some(auth_model.AccessTokenScopeReadRepository), result.Scope())
|
|
require.NotNil(t, result.Reducer())
|
|
|
|
// No direct way to query an authz.Reducer for its specific repos permitted, so this is a bit of a workaround to
|
|
// see if it's targeting the repo from the JWT:
|
|
repoGetter, isRepoGetter := result.(authz.RepoGetter)
|
|
require.True(t, isRepoGetter)
|
|
assert.EqualValues(t, 1, repoGetter.GetTargetRepoID())
|
|
})
|
|
|
|
t.Run("token valid op=upload", func(t *testing.T) {
|
|
bearerAuth, err := getLFSAuthTokenWithBearer(authTokenOptions{Op: "upload", UserID: 2, RepoID: 24})
|
|
require.NoError(t, err)
|
|
a := &LFSToken{}
|
|
req := httptest.NewRequest("GET", "https://example.org", nil)
|
|
req.Header.Set("Authorization", bearerAuth)
|
|
output := a.Verify(req, nil, nil)
|
|
result := requireOutput[*auth.AuthenticationSuccess](t, output).Result
|
|
assert.EqualValues(t, 2, result.User().ID)
|
|
assert.Equal(t, optional.Some(auth_model.AccessTokenScopeWriteRepository), result.Scope())
|
|
require.NotNil(t, result.Reducer())
|
|
|
|
// No direct way to query an authz.Reducer for its specific repos permitted, so this is a bit of a workaround to
|
|
// see if it's targeting the repo from the JWT:
|
|
repoGetter, isRepoGetter := result.(authz.RepoGetter)
|
|
require.True(t, isRepoGetter)
|
|
assert.EqualValues(t, 24, repoGetter.GetTargetRepoID())
|
|
})
|
|
|
|
t.Run("token signature malformed", func(t *testing.T) {
|
|
bearerAuth, err := getLFSAuthTokenWithBearer(authTokenOptions{Op: "download", UserID: 2, RepoID: 1})
|
|
bearerAuth += "malformed"
|
|
|
|
require.NoError(t, err)
|
|
a := &LFSToken{}
|
|
req := httptest.NewRequest("GET", "https://example.org", nil)
|
|
req.Header.Set("Authorization", bearerAuth)
|
|
output := a.Verify(req, nil, nil)
|
|
err = requireOutput[*auth.AuthenticationAttemptedIncorrectCredential](t, output).Error
|
|
require.ErrorContains(t, err, "token signature is invalid")
|
|
})
|
|
|
|
t.Run("invalid user", func(t *testing.T) {
|
|
bearerAuth, err := getLFSAuthTokenWithBearer(authTokenOptions{Op: "download", UserID: 999, RepoID: 1})
|
|
require.NoError(t, err)
|
|
a := &LFSToken{}
|
|
req := httptest.NewRequest("GET", "https://example.org", nil)
|
|
req.Header.Set("Authorization", bearerAuth)
|
|
output := a.Verify(req, nil, nil)
|
|
err = requireOutput[*auth.AuthenticationError](t, output).Error
|
|
require.ErrorContains(t, err, "user does not exist")
|
|
})
|
|
}
|
|
|
|
type namedCfg struct {
|
|
name, cfg string
|
|
}
|
|
|
|
var iniCommon = `[security]
|
|
INSTALL_LOCK = true
|
|
INTERNAL_TOKEN = ForgejoForgejoForgejoForgejoForgejoForgejo_ # don't use in prod
|
|
[oauth2]
|
|
JWT_SECRET = ForgejoForgejoForgejoForgejoForgejoForgejo_ # don't use in prod
|
|
[server]
|
|
LFS_START_SERVER = true
|
|
`
|
|
|
|
var cfgVariants = []namedCfg{
|
|
{name: "HS256_default", cfg: `LFS_JWT_SECRET = ForgejoForgejoForgejoForgejoForgejoForgejo_`},
|
|
{name: "RS256", cfg: `LFS_JWT_SIGNING_ALGORITHM = RS256`},
|
|
}
|
|
|
|
func TestAuthenticate(t *testing.T) {
|
|
for _, v := range cfgVariants {
|
|
cfg := iniCommon + v.cfg
|
|
t.Run(v.name, func(t *testing.T) { testAuthenticate(t, cfg) })
|
|
}
|
|
}
|