forked from ory/fosite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoidc_explicit_test.go
156 lines (142 loc) · 4.86 KB
/
oidc_explicit_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// Copyright © 2024 Ory Corp
// SPDX-License-Identifier: Apache-2.0
package integration_test
import (
"context"
"fmt"
"io"
"net/http"
"strings"
"testing"
"time"
"github.com/ory/fosite/internal/gen"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"golang.org/x/oauth2"
"github.com/ory/fosite"
"github.com/ory/fosite/compose"
"github.com/ory/fosite/handler/openid"
"github.com/ory/fosite/token/jwt"
)
func newIDSession(j *jwt.IDTokenClaims) *defaultSession {
return &defaultSession{
DefaultSession: &openid.DefaultSession{
Claims: j,
Headers: &jwt.Headers{},
Subject: j.Subject,
},
}
}
func TestOpenIDConnectExplicitFlow(t *testing.T) {
f := compose.ComposeAllEnabled(&fosite.Config{
GlobalSecret: []byte("some-secret-thats-random-some-secret-thats-random-")}, fositeStore, gen.MustRSAKey())
for k, c := range []struct {
description string
setup func(oauthClient *oauth2.Config) string
authStatusCode int
authCodeURL string
session *defaultSession
expectAuthErr string
expectTokenErr string
}{
{
session: newIDSession(&jwt.IDTokenClaims{Subject: "peter"}),
description: "should pass",
setup: func(oauthClient *oauth2.Config) string {
oauthClient.Scopes = []string{"openid"}
return oauthClient.AuthCodeURL("12345678901234567890") + "&nonce=11234123"
},
authStatusCode: http.StatusOK,
},
{
session: newIDSession(&jwt.IDTokenClaims{Subject: "peter"}),
description: "should fail because nonce is not long enough",
setup: func(oauthClient *oauth2.Config) string {
oauthClient.Scopes = []string{"openid"}
return oauthClient.AuthCodeURL("12345678901234567890") + "&nonce=1"
},
authStatusCode: http.StatusOK,
expectTokenErr: "insufficient_entropy",
},
{
session: newIDSession(&jwt.IDTokenClaims{Subject: "peter"}),
description: "should fail because state is not long enough",
setup: func(oauthClient *oauth2.Config) string {
oauthClient.Scopes = []string{"openid"}
return oauthClient.AuthCodeURL("123") + "&nonce=1234567890"
},
expectAuthErr: "invalid_state",
authStatusCode: http.StatusNotAcceptable, // code from internal test callback handler when error occurs
},
{
session: newIDSession(&jwt.IDTokenClaims{
Subject: "peter",
RequestedAt: time.Now().UTC(),
AuthTime: time.Now().Add(time.Second).UTC(),
}),
description: "should pass",
setup: func(oauthClient *oauth2.Config) string {
oauthClient.Scopes = []string{"openid"}
return oauthClient.AuthCodeURL("12345678901234567890") + "&nonce=1234567890&prompt=login"
},
authStatusCode: http.StatusOK,
},
{
session: newIDSession(&jwt.IDTokenClaims{
Subject: "peter",
RequestedAt: time.Now().UTC(),
AuthTime: time.Now().Add(-time.Minute).UTC(),
}),
description: "should fail because authentication was in the past",
setup: func(oauthClient *oauth2.Config) string {
oauthClient.Scopes = []string{"openid"}
return oauthClient.AuthCodeURL("12345678901234567890") + "&nonce=1234567890&prompt=login"
},
authStatusCode: http.StatusNotAcceptable, // code from internal test callback handler when error occurs
expectAuthErr: "login_required",
},
{
session: newIDSession(&jwt.IDTokenClaims{
Subject: "peter",
RequestedAt: time.Now().UTC(),
AuthTime: time.Now().Add(-time.Minute).UTC(),
}),
description: "should pass because authorization was in the past and no login was required",
setup: func(oauthClient *oauth2.Config) string {
oauthClient.Scopes = []string{"openid"}
return oauthClient.AuthCodeURL("12345678901234567890") + "&nonce=1234567890&prompt=none"
},
authStatusCode: http.StatusOK,
},
} {
t.Run(fmt.Sprintf("case=%d/description=%s", k, c.description), func(t *testing.T) {
ts := mockServer(t, f, c.session)
defer ts.Close()
oauthClient := newOAuth2Client(ts)
fositeStore.Clients["my-client"].(*fosite.DefaultClient).RedirectURIs[0] = ts.URL + "/callback"
resp, err := http.Get(c.setup(oauthClient))
require.NoError(t, err)
defer resp.Body.Close()
body, _ := io.ReadAll(resp.Body)
require.Equal(t, c.authStatusCode, resp.StatusCode, "Got response: %s", body)
if resp.StatusCode >= 400 {
assert.Equal(t, c.expectAuthErr, strings.Replace(string(body), "error: ", "", 1))
}
if c.expectAuthErr != "" {
assert.Empty(t, resp.Request.URL.Query().Get("code"))
}
if resp.StatusCode == http.StatusOK {
time.Sleep(time.Second)
token, err := oauthClient.Exchange(context.Background(), resp.Request.URL.Query().Get("code"))
if c.expectTokenErr != "" {
require.Error(t, err)
assert.True(t, strings.Contains(err.Error(), c.expectTokenErr), err.Error())
} else {
require.NoError(t, err)
assert.NotEmpty(t, token.AccessToken)
assert.NotEmpty(t, token.Extra("id_token"))
}
}
})
}
}