2021-08-19 12:11:30 -04:00
|
|
|
// Copyright 2021 The Gitea Authors. All rights reserved.
|
2022-11-27 13:20:29 -05:00
|
|
|
// SPDX-License-Identifier: MIT
|
2021-08-19 12:11:30 -04:00
|
|
|
|
2022-01-02 08:12:35 -05:00
|
|
|
package auth
|
2021-08-19 12:11:30 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2022-01-02 08:12:35 -05:00
|
|
|
"code.gitea.io/gitea/models/auth"
|
2022-05-20 10:08:52 -04:00
|
|
|
"code.gitea.io/gitea/models/db"
|
2021-11-12 09:36:47 -05:00
|
|
|
"code.gitea.io/gitea/models/unittest"
|
2021-11-24 04:49:20 -05:00
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2024-11-27 02:48:32 -05:00
|
|
|
"code.gitea.io/gitea/modules/timeutil"
|
2021-08-19 12:11:30 -04:00
|
|
|
"code.gitea.io/gitea/services/auth/source/oauth2"
|
|
|
|
|
2023-07-19 05:57:10 -04:00
|
|
|
"github.com/golang-jwt/jwt/v5"
|
2021-08-19 12:11:30 -04:00
|
|
|
"github.com/stretchr/testify/assert"
|
2024-07-30 15:41:10 -04:00
|
|
|
"github.com/stretchr/testify/require"
|
2021-08-19 12:11:30 -04:00
|
|
|
)
|
|
|
|
|
2022-01-02 08:12:35 -05:00
|
|
|
func createAndParseToken(t *testing.T, grant *auth.OAuth2Grant) *oauth2.OIDCToken {
|
2021-08-25 16:50:38 -04:00
|
|
|
signingKey, err := oauth2.CreateJWTSigningKey("HS256", make([]byte, 32))
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, err)
|
2021-08-19 12:11:30 -04:00
|
|
|
assert.NotNil(t, signingKey)
|
|
|
|
|
2022-05-20 10:08:52 -04:00
|
|
|
response, terr := newAccessTokenResponse(db.DefaultContext, grant, signingKey, signingKey)
|
2021-08-19 12:11:30 -04:00
|
|
|
assert.Nil(t, terr)
|
|
|
|
assert.NotNil(t, response)
|
|
|
|
|
2023-07-04 14:36:08 -04:00
|
|
|
parsedToken, err := jwt.ParseWithClaims(response.IDToken, &oauth2.OIDCToken{}, func(token *jwt.Token) (any, error) {
|
2021-08-19 12:11:30 -04:00
|
|
|
assert.NotNil(t, token.Method)
|
|
|
|
assert.Equal(t, signingKey.SigningMethod().Alg(), token.Method.Alg())
|
|
|
|
return signingKey.VerifyKey(), nil
|
|
|
|
})
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, err)
|
2021-08-19 12:11:30 -04:00
|
|
|
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) {
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
2021-08-19 12:11:30 -04:00
|
|
|
|
2022-05-20 10:08:52 -04:00
|
|
|
grants, err := auth.GetOAuth2GrantsByUserID(db.DefaultContext, 3)
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, err)
|
2021-08-19 12:11:30 -04:00
|
|
|
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)
|
|
|
|
|
2022-08-15 22:22:25 -04:00
|
|
|
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 5})
|
2022-05-20 10:08:52 -04:00
|
|
|
grants, err = auth.GetOAuth2GrantsByUserID(db.DefaultContext, user.ID)
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, err)
|
2021-08-19 12:11:30 -04:00
|
|
|
assert.Len(t, grants, 1)
|
|
|
|
|
2022-06-16 18:29:54 -04:00
|
|
|
// Scopes: openid profile email
|
|
|
|
oidcToken = createAndParseToken(t, grants[0])
|
2024-11-27 02:48:32 -05:00
|
|
|
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)
|
2021-08-19 12:11:30 -04:00
|
|
|
}
|
2024-04-18 03:55:08 -04:00
|
|
|
|
|
|
|
func TestEncodeCodeChallenge(t *testing.T) {
|
|
|
|
// test vector from https://datatracker.ietf.org/doc/html/rfc7636#page-18
|
|
|
|
codeChallenge, err := encodeCodeChallenge("dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk")
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, err)
|
2024-04-18 03:55:08 -04:00
|
|
|
assert.Equal(t, "E9Melhoa2OwvFrEMTJguCHaoeK1t8URWbuGJSstw-cM", codeChallenge)
|
|
|
|
}
|