forked from ory/fosite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
oidc_explicit_test.go
172 lines (158 loc) · 5.52 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*
* Copyright © 2015-2018 Aeneas Rekkas <[email protected]>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* @author Aeneas Rekkas <[email protected]>
* @copyright 2015-2018 Aeneas Rekkas <[email protected]>
* @license Apache-2.0
*
*/
package integration_test
import (
"fmt"
"io/ioutil"
"net/http"
"strings"
"testing"
"time"
"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/internal"
"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(new(compose.Config), fositeStore, []byte("some-secret-thats-random-some-secret-thats-random-"), internal.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, _ := ioutil.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(oauth2.NoContext, resp.Request.URL.Query().Get("code"))
if c.expectTokenErr != "" {
assert.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"))
}
}
})
}
}