2023-01-23 07:11:40 +00:00
|
|
|
package jwt
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-02-28 20:20:58 +00:00
|
|
|
"encoding/json"
|
2023-01-23 07:11:40 +00:00
|
|
|
"errors"
|
|
|
|
"testing"
|
2023-02-28 20:20:58 +00:00
|
|
|
"time"
|
2023-01-23 07:11:40 +00:00
|
|
|
|
2023-02-28 20:20:58 +00:00
|
|
|
"github.com/golang/mock/gomock"
|
|
|
|
"github.com/h2non/gock"
|
2023-01-23 07:11:40 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2023-02-28 20:20:58 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2023-01-23 07:11:40 +00:00
|
|
|
"github.com/zitadel/oidc/v2/pkg/oidc"
|
|
|
|
"golang.org/x/oauth2"
|
|
|
|
"golang.org/x/text/language"
|
2023-02-28 20:20:58 +00:00
|
|
|
"gopkg.in/square/go-jose.v2"
|
2023-01-23 07:11:40 +00:00
|
|
|
|
2023-02-28 20:20:58 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/crypto"
|
2023-03-14 19:20:38 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/domain"
|
2023-01-23 07:11:40 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestSession_FetchUser(t *testing.T) {
|
|
|
|
type fields struct {
|
2023-02-28 20:20:58 +00:00
|
|
|
name string
|
|
|
|
issuer string
|
|
|
|
jwtEndpoint string
|
|
|
|
keysEndpoint string
|
|
|
|
headerName string
|
|
|
|
encryptionAlg func(t *testing.T) crypto.EncryptionAlgorithm
|
|
|
|
httpMock func(issuer string)
|
|
|
|
authURL string
|
2023-03-28 11:28:56 +00:00
|
|
|
tokens *oidc.Tokens[*oidc.IDTokenClaims]
|
2023-01-23 07:11:40 +00:00
|
|
|
}
|
|
|
|
type want struct {
|
|
|
|
err func(error) bool
|
|
|
|
id string
|
|
|
|
firstName string
|
|
|
|
lastName string
|
|
|
|
displayName string
|
|
|
|
nickName string
|
|
|
|
preferredUsername string
|
|
|
|
email string
|
|
|
|
isEmailVerified bool
|
|
|
|
phone string
|
|
|
|
isPhoneVerified bool
|
|
|
|
preferredLanguage language.Tag
|
|
|
|
avatarURL string
|
|
|
|
profile string
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
fields fields
|
|
|
|
want want
|
|
|
|
}{
|
|
|
|
{
|
2023-02-28 20:20:58 +00:00
|
|
|
name: "no tokens",
|
|
|
|
fields: fields{
|
|
|
|
issuer: "https://jwt.com",
|
|
|
|
jwtEndpoint: "https://auth.com/jwt",
|
|
|
|
keysEndpoint: "https://jwt.com/keys",
|
|
|
|
headerName: "jwt-header",
|
|
|
|
encryptionAlg: func(t *testing.T) crypto.EncryptionAlgorithm {
|
|
|
|
return crypto.CreateMockEncryptionAlg(gomock.NewController(t))
|
|
|
|
},
|
|
|
|
httpMock: func(issuer string) {
|
|
|
|
gock.New(issuer).
|
|
|
|
Get("/keys").
|
|
|
|
Reply(200).
|
|
|
|
JSON(keys(t))
|
|
|
|
},
|
|
|
|
},
|
2023-01-23 07:11:40 +00:00
|
|
|
want: want{
|
|
|
|
err: func(err error) bool {
|
|
|
|
return errors.Is(err, ErrNoTokens)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2023-02-28 20:20:58 +00:00
|
|
|
name: "invalid token",
|
2023-01-23 07:11:40 +00:00
|
|
|
fields: fields{
|
2023-02-28 20:20:58 +00:00
|
|
|
issuer: "https://jwt.com",
|
|
|
|
jwtEndpoint: "https://auth.com/jwt",
|
|
|
|
keysEndpoint: "https://jwt.com/keys",
|
|
|
|
headerName: "jwt-header",
|
|
|
|
encryptionAlg: func(t *testing.T) crypto.EncryptionAlgorithm {
|
|
|
|
return crypto.CreateMockEncryptionAlg(gomock.NewController(t))
|
|
|
|
},
|
|
|
|
httpMock: func(issuer string) {
|
|
|
|
gock.New(issuer).
|
|
|
|
Get("/keys").
|
|
|
|
Reply(200).
|
|
|
|
JSON(keys(t))
|
|
|
|
},
|
2023-01-23 07:11:40 +00:00
|
|
|
authURL: "https://auth.com/jwt?authRequestID=testState",
|
2023-03-28 11:28:56 +00:00
|
|
|
tokens: &oidc.Tokens[*oidc.IDTokenClaims]{
|
2023-02-28 20:20:58 +00:00
|
|
|
Token: &oauth2.Token{},
|
|
|
|
IDToken: "invalidToken",
|
2023-01-23 07:11:40 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
want: want{
|
2023-02-28 20:20:58 +00:00
|
|
|
err: func(err error) bool {
|
|
|
|
return errors.Is(err, ErrInvalidToken)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "successful fetch",
|
|
|
|
fields: fields{
|
|
|
|
issuer: "https://jwt.com",
|
|
|
|
jwtEndpoint: "https://auth.com/jwt",
|
|
|
|
keysEndpoint: "https://jwt.com/keys",
|
|
|
|
headerName: "jwt-header",
|
|
|
|
encryptionAlg: func(t *testing.T) crypto.EncryptionAlgorithm {
|
|
|
|
return crypto.CreateMockEncryptionAlg(gomock.NewController(t))
|
|
|
|
},
|
|
|
|
httpMock: func(issuer string) {
|
|
|
|
gock.New(issuer).
|
|
|
|
Get("/keys").
|
|
|
|
Reply(200).
|
|
|
|
JSON(keys(t))
|
|
|
|
},
|
|
|
|
authURL: "https://auth.com/jwt?authRequestID=testState",
|
2023-03-28 11:28:56 +00:00
|
|
|
tokens: &oidc.Tokens[*oidc.IDTokenClaims]{
|
2023-02-28 20:20:58 +00:00
|
|
|
Token: &oauth2.Token{},
|
|
|
|
IDToken: idToken(t, "https://jwt.com"),
|
2023-03-28 11:28:56 +00:00
|
|
|
IDTokenClaims: &oidc.IDTokenClaims{
|
|
|
|
TokenClaims: oidc.TokenClaims{
|
|
|
|
Subject: "sub",
|
|
|
|
},
|
|
|
|
UserInfoProfile: oidc.UserInfoProfile{
|
|
|
|
Picture: "picture",
|
|
|
|
Name: "firstname lastname",
|
|
|
|
GivenName: "firstname",
|
|
|
|
FamilyName: "lastname",
|
|
|
|
Nickname: "nickname",
|
|
|
|
PreferredUsername: "username",
|
|
|
|
Profile: "profile",
|
|
|
|
Locale: oidc.NewLocale(language.English),
|
|
|
|
},
|
|
|
|
UserInfoEmail: oidc.UserInfoEmail{
|
|
|
|
Email: "email",
|
|
|
|
EmailVerified: oidc.Bool(true),
|
|
|
|
},
|
|
|
|
UserInfoPhone: oidc.UserInfoPhone{
|
|
|
|
PhoneNumber: "phone",
|
|
|
|
PhoneNumberVerified: true,
|
|
|
|
},
|
|
|
|
},
|
2023-01-23 07:11:40 +00:00
|
|
|
},
|
2023-02-28 20:20:58 +00:00
|
|
|
},
|
|
|
|
want: want{
|
2023-01-23 07:11:40 +00:00
|
|
|
id: "sub",
|
|
|
|
firstName: "firstname",
|
|
|
|
lastName: "lastname",
|
|
|
|
displayName: "firstname lastname",
|
|
|
|
nickName: "nickname",
|
|
|
|
preferredUsername: "username",
|
|
|
|
email: "email",
|
|
|
|
isEmailVerified: true,
|
|
|
|
phone: "phone",
|
|
|
|
isPhoneVerified: true,
|
|
|
|
preferredLanguage: language.English,
|
|
|
|
avatarURL: "picture",
|
|
|
|
profile: "profile",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2023-02-28 20:20:58 +00:00
|
|
|
defer gock.Off()
|
|
|
|
tt.fields.httpMock(tt.fields.issuer)
|
2023-01-23 07:11:40 +00:00
|
|
|
a := assert.New(t)
|
|
|
|
|
2023-02-28 20:20:58 +00:00
|
|
|
provider, err := New(
|
|
|
|
tt.fields.name,
|
|
|
|
tt.fields.issuer,
|
|
|
|
tt.fields.jwtEndpoint,
|
|
|
|
tt.fields.keysEndpoint,
|
|
|
|
tt.fields.headerName,
|
|
|
|
tt.fields.encryptionAlg(t),
|
|
|
|
)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2023-01-23 07:11:40 +00:00
|
|
|
session := &Session{
|
2023-02-28 20:20:58 +00:00
|
|
|
Provider: provider,
|
|
|
|
AuthURL: tt.fields.authURL,
|
|
|
|
Tokens: tt.fields.tokens,
|
2023-01-23 07:11:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
user, err := session.FetchUser(context.Background())
|
|
|
|
if tt.want.err != nil && !tt.want.err(err) {
|
|
|
|
a.Fail("invalid error", err)
|
|
|
|
}
|
|
|
|
if tt.want.err == nil {
|
|
|
|
a.NoError(err)
|
|
|
|
a.Equal(tt.want.id, user.GetID())
|
|
|
|
a.Equal(tt.want.firstName, user.GetFirstName())
|
|
|
|
a.Equal(tt.want.lastName, user.GetLastName())
|
|
|
|
a.Equal(tt.want.displayName, user.GetDisplayName())
|
|
|
|
a.Equal(tt.want.nickName, user.GetNickname())
|
|
|
|
a.Equal(tt.want.preferredUsername, user.GetPreferredUsername())
|
2023-03-14 19:20:38 +00:00
|
|
|
a.Equal(domain.EmailAddress(tt.want.email), user.GetEmail())
|
2023-01-23 07:11:40 +00:00
|
|
|
a.Equal(tt.want.isEmailVerified, user.IsEmailVerified())
|
2023-03-14 19:20:38 +00:00
|
|
|
a.Equal(domain.PhoneNumber(tt.want.phone), user.GetPhone())
|
2023-01-23 07:11:40 +00:00
|
|
|
a.Equal(tt.want.isPhoneVerified, user.IsPhoneVerified())
|
|
|
|
a.Equal(tt.want.preferredLanguage, user.GetPreferredLanguage())
|
|
|
|
a.Equal(tt.want.avatarURL, user.GetAvatarURL())
|
|
|
|
a.Equal(tt.want.profile, user.GetProfile())
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2023-02-28 20:20:58 +00:00
|
|
|
|
|
|
|
func idToken(t *testing.T, issuer string) string {
|
|
|
|
claims := oidc.NewIDTokenClaims(
|
|
|
|
issuer,
|
|
|
|
"sub",
|
|
|
|
[]string{"clientID"},
|
|
|
|
time.Now().Add(1*time.Hour),
|
|
|
|
time.Now().Add(-1*time.Minute),
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
nil,
|
|
|
|
"clientID",
|
|
|
|
0,
|
|
|
|
)
|
2023-03-28 11:28:56 +00:00
|
|
|
claims.UserInfoProfile = oidc.UserInfoProfile{
|
|
|
|
GivenName: "firstname",
|
|
|
|
FamilyName: "lastname",
|
|
|
|
Name: "firstname lastname",
|
|
|
|
Nickname: "nickname",
|
|
|
|
PreferredUsername: "username",
|
|
|
|
Locale: oidc.NewLocale(language.English),
|
|
|
|
Picture: "picture",
|
|
|
|
Profile: "profile",
|
|
|
|
}
|
|
|
|
claims.UserInfoEmail = oidc.UserInfoEmail{
|
|
|
|
Email: "email",
|
|
|
|
EmailVerified: oidc.Bool(true),
|
|
|
|
}
|
|
|
|
claims.UserInfoPhone = oidc.UserInfoPhone{
|
|
|
|
PhoneNumber: "phone",
|
|
|
|
PhoneNumberVerified: true,
|
|
|
|
}
|
|
|
|
|
2023-02-28 20:20:58 +00:00
|
|
|
privateKey, err := crypto.BytesToPrivateKey([]byte(`-----BEGIN RSA PRIVATE KEY-----
|
|
|
|
MIIEpAIBAAKCAQEAs38btwb3c7r0tMaQpGvBmY+mPwMU/LpfuPoC0k2t4RsKp0fv
|
|
|
|
40SMl50CRrHgk395wch8PMPYbl3+8TtYAJuyrFALIj3Ff1UcKIk0hOH5DDsfh7/q
|
|
|
|
2wFuncTmS6bifYo8CfSq2vDGnM7nZnEvxY/MfSydZdcmIqlkUpfQmtzExw9+tSe5
|
|
|
|
Dxq6gn5JtlGgLgZGt69r5iMMrTEGhhVAXzNuMZbmlCoBru+rC8ITlTX/0V1ZcsSb
|
|
|
|
L8tYWhthyu9x6yjo1bH85wiVI4gs0MhU8f2a+kjL/KGZbR14Ua2eo6tonBZLC5DH
|
|
|
|
WM2TkYXgRCDPufjcgmzN0Lm91E4P8KvBcvly6QIDAQABAoIBAQCPj1nbSPcg2KZe
|
|
|
|
73FAD+8HopyUSSK//1AP4eXfzcEECVy77g0u9+R6XlkzsZCsZ4g6NN8ounqfyw3c
|
|
|
|
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DOHb99FqdzK
|
|
|
|
sqcedQKBgQCOmgbqxGsnT2WQhoOdzln+NOo6Tx+FveLLqat2KzpY59W4noeI2Awn
|
|
|
|
HfIQgWUAW9dsjVVOXMP1jhq8U9hmH/PFWA11V/iCdk1NTxZEw87VAOeWuajpdDHG
|
|
|
|
+iex349j8h2BcQ4Zd0FWu07gGFnS/yuDJPn6jBhRusdieEcxLRjTKg==
|
|
|
|
-----END RSA PRIVATE KEY-----
|
|
|
|
`))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
signer, err := jose.NewSigner(jose.SigningKey{Key: privateKey, Algorithm: "RS256"}, &jose.SignerOptions{})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
data, err := json.Marshal(claims)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
jws, err := signer.Sign(data)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
idToken, err := jws.CompactSerialize()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
return idToken
|
|
|
|
}
|
|
|
|
|
|
|
|
func keys(t *testing.T) *jose.JSONWebKeySet {
|
|
|
|
privateKey, err := crypto.BytesToPublicKey([]byte(`-----BEGIN PUBLIC KEY-----
|
|
|
|
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAs38btwb3c7r0tMaQpGvB
|
|
|
|
mY+mPwMU/LpfuPoC0k2t4RsKp0fv40SMl50CRrHgk395wch8PMPYbl3+8TtYAJuy
|
|
|
|
rFALIj3Ff1UcKIk0hOH5DDsfh7/q2wFuncTmS6bifYo8CfSq2vDGnM7nZnEvxY/M
|
|
|
|
fSydZdcmIqlkUpfQmtzExw9+tSe5Dxq6gn5JtlGgLgZGt69r5iMMrTEGhhVAXzNu
|
|
|
|
MZbmlCoBru+rC8ITlTX/0V1ZcsSbL8tYWhthyu9x6yjo1bH85wiVI4gs0MhU8f2a
|
|
|
|
+kjL/KGZbR14Ua2eo6tonBZLC5DHWM2TkYXgRCDPufjcgmzN0Lm91E4P8KvBcvly
|
|
|
|
6QIDAQAB
|
|
|
|
-----END PUBLIC KEY-----
|
|
|
|
`))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
return &jose.JSONWebKeySet{Keys: []jose.JSONWebKey{{Key: privateKey, Algorithm: "RS256", Use: oidc.KeyUseSignature}}}
|
|
|
|
}
|