| 1 |
package oidc |
| 2 |
|
| 3 |
import ( |
| 4 |
"encoding/json" |
| 5 |
"testing" |
| 6 |
"time" |
| 7 |
|
| 8 |
jose "github.com/go-jose/go-jose/v4" |
| 9 |
"github.com/stretchr/testify/assert" |
| 10 |
"golang.org/x/text/language" |
| 11 |
) |
| 12 |
|
| 13 |
var ( |
| 14 |
tokenClaimsData = TokenClaims{ |
| 15 |
Issuer: "zitadel", |
| 16 |
Subject: "[email protected]", |
| 17 |
Audience: Audience{"foo", "bar"}, |
| 18 |
Expiration: 12345, |
| 19 |
IssuedAt: 12000, |
| 20 |
JWTID: "900", |
| 21 |
AuthorizedParty: "[email protected]", |
| 22 |
Nonce: "6969", |
| 23 |
AuthTime: 12000, |
| 24 |
NotBefore: 12000, |
| 25 |
AuthenticationContextClassReference: "something", |
| 26 |
AuthenticationMethodsReferences: []string{"some", "methods"}, |
| 27 |
ClientID: "777", |
| 28 |
SignatureAlg: jose.ES256, |
| 29 |
} |
| 30 |
accessTokenData = &AccessTokenClaims{ |
| 31 |
TokenClaims: tokenClaimsData, |
| 32 |
Scopes: []string{"email", "phone"}, |
| 33 |
Claims: map[string]any{ |
| 34 |
"foo": "bar", |
| 35 |
}, |
| 36 |
} |
| 37 |
idTokenData = &IDTokenClaims{ |
| 38 |
TokenClaims: tokenClaimsData, |
| 39 |
NotBefore: 12000, |
| 40 |
AccessTokenHash: "acthashhash", |
| 41 |
CodeHash: "hashhash", |
| 42 |
SessionID: "666", |
| 43 |
UserInfoProfile: userInfoData.UserInfoProfile, |
| 44 |
UserInfoEmail: userInfoData.UserInfoEmail, |
| 45 |
UserInfoPhone: userInfoData.UserInfoPhone, |
| 46 |
Address: userInfoData.Address, |
| 47 |
Claims: map[string]any{ |
| 48 |
"foo": "bar", |
| 49 |
}, |
| 50 |
} |
| 51 |
introspectionResponseData = &IntrospectionResponse{ |
| 52 |
Active: true, |
| 53 |
Scope: SpaceDelimitedArray{"email", "phone"}, |
| 54 |
ClientID: "777", |
| 55 |
TokenType: "idtoken", |
| 56 |
Expiration: 12345, |
| 57 |
IssuedAt: 12000, |
| 58 |
NotBefore: 12000, |
| 59 |
Subject: "[email protected]", |
| 60 |
Audience: Audience{"foo", "bar"}, |
| 61 |
Issuer: "zitadel", |
| 62 |
JWTID: "900", |
| 63 |
Username: "muhlemmer", |
| 64 |
UserInfoProfile: userInfoData.UserInfoProfile, |
| 65 |
UserInfoEmail: userInfoData.UserInfoEmail, |
| 66 |
UserInfoPhone: userInfoData.UserInfoPhone, |
| 67 |
Address: userInfoData.Address, |
| 68 |
Claims: map[string]any{ |
| 69 |
"foo": "bar", |
| 70 |
}, |
| 71 |
} |
| 72 |
userInfoData = &UserInfo{ |
| 73 |
Subject: "[email protected]", |
| 74 |
UserInfoProfile: UserInfoProfile{ |
| 75 |
Name: "Tim Möhlmann", |
| 76 |
GivenName: "Tim", |
| 77 |
FamilyName: "Möhlmann", |
| 78 |
MiddleName: "Danger", |
| 79 |
Nickname: "muhlemmer", |
| 80 |
Profile: "https://github.com/muhlemmer", |
| 81 |
Picture: "https://avatars.githubusercontent.com/u/5411563?v=4", |
| 82 |
Website: "https://zitadel.com", |
| 83 |
Gender: "male", |
| 84 |
Birthdate: "1st of April", |
| 85 |
Zoneinfo: "Europe/Amsterdam", |
| 86 |
Locale: NewLocale(language.Dutch), |
| 87 |
UpdatedAt: 1, |
| 88 |
PreferredUsername: "muhlemmer", |
| 89 |
}, |
| 90 |
UserInfoEmail: UserInfoEmail{ |
| 91 |
Email: "[email protected]", |
| 92 |
EmailVerified: true, |
| 93 |
}, |
| 94 |
UserInfoPhone: UserInfoPhone{ |
| 95 |
PhoneNumber: "+1234567890", |
| 96 |
PhoneNumberVerified: true, |
| 97 |
}, |
| 98 |
Address: &UserInfoAddress{ |
| 99 |
Formatted: "Sesame street 666\n666-666, Smallvile\nMoon", |
| 100 |
StreetAddress: "Sesame street 666", |
| 101 |
Locality: "Smallvile", |
| 102 |
Region: "Outer space", |
| 103 |
PostalCode: "666-666", |
| 104 |
Country: "Moon", |
| 105 |
}, |
| 106 |
Claims: map[string]any{ |
| 107 |
"foo": "bar", |
| 108 |
}, |
| 109 |
} |
| 110 |
jwtProfileAssertionData = &JWTProfileAssertionClaims{ |
| 111 |
PrivateKeyID: "8888", |
| 112 |
PrivateKey: []byte("qwerty"), |
| 113 |
Issuer: "zitadel", |
| 114 |
Subject: "[email protected]", |
| 115 |
Audience: Audience{"foo", "bar"}, |
| 116 |
Expiration: 12345, |
| 117 |
IssuedAt: 12000, |
| 118 |
Claims: map[string]any{ |
| 119 |
"foo": "bar", |
| 120 |
}, |
| 121 |
} |
| 122 |
) |
| 123 |
|
| 124 |
func TestTokenClaims(t *testing.T) { |
| 125 |
claims := tokenClaimsData |
| 126 |
|
| 127 |
assert.Equal(t, claims.Issuer, tokenClaimsData.GetIssuer()) |
| 128 |
assert.Equal(t, claims.Subject, tokenClaimsData.GetSubject()) |
| 129 |
assert.Equal(t, []string(claims.Audience), tokenClaimsData.GetAudience()) |
| 130 |
assert.Equal(t, claims.Expiration.AsTime(), tokenClaimsData.GetExpiration()) |
| 131 |
assert.Equal(t, claims.IssuedAt.AsTime(), tokenClaimsData.GetIssuedAt()) |
| 132 |
assert.Equal(t, claims.Nonce, tokenClaimsData.GetNonce()) |
| 133 |
assert.Equal(t, claims.AuthTime.AsTime(), tokenClaimsData.GetAuthTime()) |
| 134 |
assert.Equal(t, claims.AuthorizedParty, tokenClaimsData.GetAuthorizedParty()) |
| 135 |
assert.Equal(t, claims.SignatureAlg, tokenClaimsData.GetSignatureAlgorithm()) |
| 136 |
assert.Equal(t, claims.AuthenticationContextClassReference, tokenClaimsData.GetAuthenticationContextClassReference()) |
| 137 |
|
| 138 |
claims.SetSignatureAlgorithm(jose.ES384) |
| 139 |
assert.Equal(t, jose.ES384, claims.SignatureAlg) |
| 140 |
} |
| 141 |
|
| 142 |
func TestNewAccessTokenClaims(t *testing.T) { |
| 143 |
want := &AccessTokenClaims{ |
| 144 |
TokenClaims: TokenClaims{ |
| 145 |
Issuer: "zitadel", |
| 146 |
Subject: "[email protected]", |
| 147 |
Audience: Audience{"foo"}, |
| 148 |
Expiration: 12345, |
| 149 |
ClientID: "foo", |
| 150 |
JWTID: "900", |
| 151 |
}, |
| 152 |
} |
| 153 |
|
| 154 |
got := NewAccessTokenClaims( |
| 155 |
want.Issuer, want.Subject, nil, |
| 156 |
want.Expiration.AsTime(), want.JWTID, "foo", time.Second, |
| 157 |
) |
| 158 |
|
| 159 |
// test if the dynamic timestamps are around now, |
| 160 |
// allowing for a delta of 1, just in case we flip on |
| 161 |
// either side of a second boundary. |
| 162 |
nowMinusSkew := NowTime() - 1 |
| 163 |
assert.InDelta(t, int64(nowMinusSkew), int64(got.IssuedAt), 1) |
| 164 |
assert.InDelta(t, int64(nowMinusSkew), int64(got.NotBefore), 1) |
| 165 |
|
| 166 |
// Make equal not fail on dynamic timestamp |
| 167 |
got.IssuedAt = 0 |
| 168 |
got.NotBefore = 0 |
| 169 |
|
| 170 |
assert.Equal(t, want, got) |
| 171 |
} |
| 172 |
|
| 173 |
func TestIDTokenClaims_GetAccessTokenHash(t *testing.T) { |
| 174 |
assert.Equal(t, idTokenData.AccessTokenHash, idTokenData.GetAccessTokenHash()) |
| 175 |
} |
| 176 |
|
| 177 |
func TestIDTokenClaims_SetUserInfo(t *testing.T) { |
| 178 |
want := IDTokenClaims{ |
| 179 |
TokenClaims: TokenClaims{ |
| 180 |
Subject: userInfoData.Subject, |
| 181 |
}, |
| 182 |
UserInfoProfile: userInfoData.UserInfoProfile, |
| 183 |
UserInfoEmail: userInfoData.UserInfoEmail, |
| 184 |
UserInfoPhone: userInfoData.UserInfoPhone, |
| 185 |
Address: userInfoData.Address, |
| 186 |
Claims: map[string]any{ |
| 187 |
"foo": "bar", |
| 188 |
}, |
| 189 |
} |
| 190 |
|
| 191 |
var got IDTokenClaims |
| 192 |
got.SetUserInfo(userInfoData) |
| 193 |
|
| 194 |
assert.Equal(t, want, got) |
| 195 |
} |
| 196 |
|
| 197 |
func TestNewIDTokenClaims(t *testing.T) { |
| 198 |
want := &IDTokenClaims{ |
| 199 |
TokenClaims: TokenClaims{ |
| 200 |
Issuer: "zitadel", |
| 201 |
Subject: "[email protected]", |
| 202 |
Audience: Audience{"foo", "[email protected]"}, |
| 203 |
Expiration: 12345, |
| 204 |
AuthTime: 12000, |
| 205 |
Nonce: "6969", |
| 206 |
AuthenticationContextClassReference: "something", |
| 207 |
AuthenticationMethodsReferences: []string{"some", "methods"}, |
| 208 |
AuthorizedParty: "[email protected]", |
| 209 |
ClientID: "[email protected]", |
| 210 |
}, |
| 211 |
} |
| 212 |
|
| 213 |
got := NewIDTokenClaims( |
| 214 |
want.Issuer, want.Subject, want.Audience, |
| 215 |
want.Expiration.AsTime(), |
| 216 |
want.AuthTime.AsTime().Add(time.Second), |
| 217 |
want.Nonce, want.AuthenticationContextClassReference, |
| 218 |
want.AuthenticationMethodsReferences, want.AuthorizedParty, |
| 219 |
time.Second, |
| 220 |
) |
| 221 |
|
| 222 |
// test if the dynamic timestamp is around now, |
| 223 |
// allowing for a delta of 1, just in case we flip on |
| 224 |
// either side of a second boundary. |
| 225 |
nowMinusSkew := NowTime() - 1 |
| 226 |
assert.InDelta(t, int64(nowMinusSkew), int64(got.IssuedAt), 1) |
| 227 |
|
| 228 |
// Make equal not fail on dynamic timestamp |
| 229 |
got.IssuedAt = 0 |
| 230 |
|
| 231 |
assert.Equal(t, want, got) |
| 232 |
} |
| 233 |
|
| 234 |
func TestIDTokenClaims_GetUserInfo(t *testing.T) { |
| 235 |
want := &UserInfo{ |
| 236 |
Subject: idTokenData.Subject, |
| 237 |
UserInfoProfile: idTokenData.UserInfoProfile, |
| 238 |
UserInfoEmail: idTokenData.UserInfoEmail, |
| 239 |
UserInfoPhone: idTokenData.UserInfoPhone, |
| 240 |
Address: idTokenData.Address, |
| 241 |
Claims: idTokenData.Claims, |
| 242 |
} |
| 243 |
got := idTokenData.GetUserInfo() |
| 244 |
assert.Equal(t, want, got) |
| 245 |
} |
| 246 |
|
| 247 |
func TestIDTokenClaims_UnmarshalJSON_StringAMR(t *testing.T) { |
| 248 |
var got IDTokenClaims |
| 249 |
err := json.Unmarshal([]byte(`{"iss":"zitadel","sub":"[email protected]","aud":"foo","exp":12345,"iat":12000,"amr":"pwd"}`), &got) |
| 250 |
assert.NoError(t, err) |
| 251 |
assert.Equal(t, AuthenticationMethodsReferences{"pwd"}, got.AuthenticationMethodsReferences) |
| 252 |
} |
| 253 |
|
| 254 |
func TestIntrospectionResponse_UnmarshalJSON_StringAMR(t *testing.T) { |
| 255 |
var got IntrospectionResponse |
| 256 |
err := json.Unmarshal([]byte(`{"active":true,"sub":"[email protected]","amr":"pwd"}`), &got) |
| 257 |
assert.NoError(t, err) |
| 258 |
assert.Equal(t, AuthenticationMethodsReferences{"pwd"}, got.AuthenticationMethodsReferences) |
| 259 |
} |
| 260 |
|
| 261 |
func TestNewLogoutTokenClaims(t *testing.T) { |
| 262 |
want := &LogoutTokenClaims{ |
| 263 |
Issuer: "zitadel", |
| 264 |
Subject: "[email protected]", |
| 265 |
Audience: Audience{"foo", "[email protected]"}, |
| 266 |
Expiration: 12345, |
| 267 |
JWTID: "jwtID", |
| 268 |
Events: map[string]any{ |
| 269 |
"http://schemas.openid.net/event/backchannel-logout": struct{}{}, |
| 270 |
}, |
| 271 |
SessionID: "sessionID", |
| 272 |
Claims: nil, |
| 273 |
} |
| 274 |
|
| 275 |
got := NewLogoutTokenClaims( |
| 276 |
want.Issuer, |
| 277 |
want.Subject, |
| 278 |
want.Audience, |
| 279 |
want.Expiration.AsTime(), |
| 280 |
want.JWTID, |
| 281 |
want.SessionID, |
| 282 |
1*time.Second, |
| 283 |
) |
| 284 |
|
| 285 |
// test if the dynamic timestamp is around now, |
| 286 |
// allowing for a delta of 1, just in case we flip on |
| 287 |
// either side of a second boundary. |
| 288 |
nowMinusSkew := NowTime() - 1 |
| 289 |
assert.InDelta(t, int64(nowMinusSkew), int64(got.IssuedAt), 1) |
| 290 |
|
| 291 |
// Make equal not fail on dynamic timestamp |
| 292 |
got.IssuedAt = 0 |
| 293 |
|
| 294 |
assert.Equal(t, want, got) |
| 295 |
} |