mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2024-12-13 11:42:23 -05:00
0734596eaa
Cherry-pick of [gitea#32542](https://github.com/go-gitea/gitea/pull/32542). This makes /login/oauth/authorize behave the same way as the /login/oauth/userinfo endpoint. Previously, `name` property of the returned OIDCToken used to depend on the UI.DefaultShowFullName setting (I don't think this is desired behavior). Even worse, the `userinfo` endpoint can return basically the same data, but the `name` value there always returned `FullName`, even if it's empty (no fallback to `Name`). A few notes: I'm not sure what branch to target with this PR, please correct me if I'm chose the wrong one. The deleted lines in the tests are duplicates, there's a copy of the whole thing just below, the only difference being the `Name` field (used to test the dependency on the UI.DefaultShowFullName setting) ## Checklist The [contributor guide](https://forgejo.org/docs/next/contributor/) contains information that will be helpful to first time contributors. 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 - I added test coverage for Go changes... - [ ] in their respective `*_test.go` for unit tests. - [ ] in the `tests/integration` directory if it involves interactions with a live Forgejo server. - I added test coverage for JavaScript changes... - [ ] in `web_src/js/*.test.js` if it can be unit tested. - [ ] in `tests/e2e/*.test.e2e.js` if it requires interactions with a live Forgejo server (see also the [developer guide for JavaScript testing](https://codeberg.org/forgejo/forgejo/src/branch/forgejo/tests/e2e/README.md#end-to-end-tests)). ### Documentation - [ ] I created a pull request [to the documentation](https://codeberg.org/forgejo/docs) to explain to Forgejo users how to use this change. - [ ] I did not document these changes and I do not expect someone else to do it. ### Release notes - [ ] I do not want this change to show in the release notes. - [ ] I want the title to show in the release notes with a link to this pull request. - [ ] I want the content of the `release-notes/<pull request number>.md` to be be used for the release notes instead of the title. Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/6071 Reviewed-by: Earl Warren <earl-warren@noreply.codeberg.org> Reviewed-by: Gusted <gusted@noreply.codeberg.org> Co-authored-by: Baltazár Radics <baltazar.radics@gmail.com> Co-committed-by: Baltazár Radics <baltazar.radics@gmail.com>
85 lines
2.9 KiB
Go
85 lines
2.9 KiB
Go
// Copyright 2021 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package auth
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"code.gitea.io/gitea/models/auth"
|
|
"code.gitea.io/gitea/models/db"
|
|
"code.gitea.io/gitea/models/unittest"
|
|
user_model "code.gitea.io/gitea/models/user"
|
|
"code.gitea.io/gitea/modules/timeutil"
|
|
"code.gitea.io/gitea/services/auth/source/oauth2"
|
|
|
|
"github.com/golang-jwt/jwt/v5"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func createAndParseToken(t *testing.T, grant *auth.OAuth2Grant) *oauth2.OIDCToken {
|
|
signingKey, err := oauth2.CreateJWTSigningKey("HS256", make([]byte, 32))
|
|
require.NoError(t, err)
|
|
assert.NotNil(t, signingKey)
|
|
|
|
response, terr := newAccessTokenResponse(db.DefaultContext, grant, signingKey, signingKey)
|
|
assert.Nil(t, terr)
|
|
assert.NotNil(t, response)
|
|
|
|
parsedToken, err := jwt.ParseWithClaims(response.IDToken, &oauth2.OIDCToken{}, func(token *jwt.Token) (any, error) {
|
|
assert.NotNil(t, token.Method)
|
|
assert.Equal(t, signingKey.SigningMethod().Alg(), token.Method.Alg())
|
|
return signingKey.VerifyKey(), nil
|
|
})
|
|
require.NoError(t, err)
|
|
assert.True(t, parsedToken.Valid)
|
|
|
|
oidcToken, ok := parsedToken.Claims.(*oauth2.OIDCToken)
|
|
assert.True(t, ok)
|
|
assert.NotNil(t, oidcToken)
|
|
|
|
return oidcToken
|
|
}
|
|
|
|
func TestNewAccessTokenResponse_OIDCToken(t *testing.T) {
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
|
|
|
grants, err := auth.GetOAuth2GrantsByUserID(db.DefaultContext, 3)
|
|
require.NoError(t, err)
|
|
assert.Len(t, grants, 1)
|
|
|
|
// Scopes: openid
|
|
oidcToken := createAndParseToken(t, grants[0])
|
|
assert.Empty(t, oidcToken.Name)
|
|
assert.Empty(t, oidcToken.PreferredUsername)
|
|
assert.Empty(t, oidcToken.Profile)
|
|
assert.Empty(t, oidcToken.Picture)
|
|
assert.Empty(t, oidcToken.Website)
|
|
assert.Empty(t, oidcToken.UpdatedAt)
|
|
assert.Empty(t, oidcToken.Email)
|
|
assert.False(t, oidcToken.EmailVerified)
|
|
|
|
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 5})
|
|
grants, err = auth.GetOAuth2GrantsByUserID(db.DefaultContext, user.ID)
|
|
require.NoError(t, err)
|
|
assert.Len(t, grants, 1)
|
|
|
|
// Scopes: openid profile email
|
|
oidcToken = createAndParseToken(t, grants[0])
|
|
assert.Equal(t, "User Five", oidcToken.Name)
|
|
assert.Equal(t, "user5", oidcToken.PreferredUsername)
|
|
assert.Equal(t, "https://try.gitea.io/user5", oidcToken.Profile)
|
|
assert.Equal(t, "https://try.gitea.io/assets/img/avatar_default.png", oidcToken.Picture)
|
|
assert.Equal(t, "", oidcToken.Website)
|
|
assert.Equal(t, timeutil.TimeStamp(0), oidcToken.UpdatedAt)
|
|
assert.Equal(t, "user5@example.com", oidcToken.Email)
|
|
assert.True(t, oidcToken.EmailVerified)
|
|
}
|
|
|
|
func TestEncodeCodeChallenge(t *testing.T) {
|
|
// test vector from https://datatracker.ietf.org/doc/html/rfc7636#page-18
|
|
codeChallenge, err := encodeCodeChallenge("dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk")
|
|
require.NoError(t, err)
|
|
assert.Equal(t, "E9Melhoa2OwvFrEMTJguCHaoeK1t8URWbuGJSstw-cM", codeChallenge)
|
|
}
|