Skip to content

Commit 9c1808d

Browse files
committed
Add tests for microsoft oidc provider id_token.
1 parent be931e3 commit 9c1808d

File tree

1 file changed

+112
-0
lines changed

1 file changed

+112
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
// Copyright © 2023 Ory Corp
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package oidc_test
5+
6+
import (
7+
"context"
8+
"net/http"
9+
"net/http/httptest"
10+
"testing"
11+
"time"
12+
13+
_ "embed"
14+
15+
"github.com/golang-jwt/jwt/v4"
16+
"github.com/stretchr/testify/assert"
17+
"github.com/stretchr/testify/require"
18+
19+
"github.com/ory/kratos/internal"
20+
"github.com/ory/kratos/selfservice/strategy/oidc"
21+
)
22+
23+
func TestMicrosoftVerify(t *testing.T) {
24+
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
25+
w.WriteHeader(200)
26+
w.Write(publicJWKS)
27+
}))
28+
29+
tsOtherJWKS := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
30+
w.WriteHeader(200)
31+
w.Write(publicJWKS2)
32+
}))
33+
makeClaims := func(aud string) jwt.RegisteredClaims {
34+
return jwt.RegisteredClaims{
35+
Issuer: "https://login.microsoftonline.com/tenant_id/v2.0",
36+
Subject: "[email protected]",
37+
Audience: jwt.ClaimStrings{aud},
38+
ExpiresAt: jwt.NewNumericDate(time.Now().Add(24 * time.Hour)),
39+
}
40+
}
41+
t.Run("case=successful verification", func(t *testing.T) {
42+
_, reg := internal.NewFastRegistryWithMocks(t)
43+
apple := oidc.NewProviderMicrosoft(&oidc.Configuration{
44+
ClientID: "com.example.app",
45+
Tenant: "tenant_id",
46+
}, reg).(*oidc.ProviderMicrosoft)
47+
apple.JWKSUrl = ts.URL
48+
token := createIdToken(t, makeClaims("com.example.app"))
49+
50+
c, err := apple.Verify(context.Background(), token)
51+
require.NoError(t, err)
52+
assert.Equal(t, "[email protected]", c.Email)
53+
assert.Equal(t, "[email protected]", c.Subject)
54+
assert.Equal(t, "https://login.microsoftonline.com/tenant_id/v2.0", c.Issuer)
55+
})
56+
57+
t.Run("case=fails due to client_id mismatch", func(t *testing.T) {
58+
_, reg := internal.NewFastRegistryWithMocks(t)
59+
apple := oidc.NewProviderMicrosoft(&oidc.Configuration{
60+
ClientID: "com.example.app",
61+
Tenant: "tenant_id",
62+
}, reg).(*oidc.ProviderMicrosoft)
63+
apple.JWKSUrl = ts.URL
64+
token := createIdToken(t, makeClaims("com.different-example.app"))
65+
66+
_, err := apple.Verify(context.Background(), token)
67+
require.Error(t, err)
68+
assert.Equal(t, `token audience didn't match allowed audiences: [com.example.app] oidc: expected audience "com.example.app" got ["com.different-example.app"]`, err.Error())
69+
})
70+
71+
t.Run("case=fails due to jwks mismatch", func(t *testing.T) {
72+
_, reg := internal.NewFastRegistryWithMocks(t)
73+
apple := oidc.NewProviderMicrosoft(&oidc.Configuration{
74+
ClientID: "com.example.app",
75+
Tenant: "tenant_id",
76+
}, reg).(*oidc.ProviderMicrosoft)
77+
apple.JWKSUrl = tsOtherJWKS.URL
78+
token := createIdToken(t, makeClaims("com.example.app"))
79+
80+
_, err := apple.Verify(context.Background(), token)
81+
require.Error(t, err)
82+
assert.Equal(t, "failed to verify signature: failed to verify id token signature", err.Error())
83+
})
84+
85+
t.Run("case=fails due to wrong issuer tenant", func(t *testing.T) {
86+
_, reg := internal.NewFastRegistryWithMocks(t)
87+
apple := oidc.NewProviderMicrosoft(&oidc.Configuration{
88+
ClientID: "com.example.app",
89+
Tenant: "wrong_tenant_id",
90+
}, reg).(*oidc.ProviderMicrosoft)
91+
apple.JWKSUrl = tsOtherJWKS.URL
92+
token := createIdToken(t, makeClaims("com.example.app"))
93+
94+
_, err := apple.Verify(context.Background(), token)
95+
require.Error(t, err)
96+
assert.Equal(t, "oidc: id token issued by a different provider, expected \"https://login.microsoftonline.com/wrong_tenant_id/v2.0\" got \"https://login.microsoftonline.com/tenant_id/v2.0\"", err.Error())
97+
})
98+
99+
t.Run("case=succeedes with additional id token audience", func(t *testing.T) {
100+
_, reg := internal.NewFastRegistryWithMocks(t)
101+
apple := oidc.NewProviderMicrosoft(&oidc.Configuration{
102+
ClientID: "something.else.app",
103+
Tenant: "tenant_id",
104+
AdditionalIDTokenAudiences: []string{"com.example.app"},
105+
}, reg).(*oidc.ProviderMicrosoft)
106+
apple.JWKSUrl = ts.URL
107+
token := createIdToken(t, makeClaims("com.example.app"))
108+
109+
_, err := apple.Verify(context.Background(), token)
110+
require.NoError(t, err)
111+
})
112+
}

0 commit comments

Comments
 (0)