-
-
Notifications
You must be signed in to change notification settings - Fork 367
/
flow_authorize_code_auth_test.go
179 lines (170 loc) · 5.81 KB
/
flow_authorize_code_auth_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
173
174
175
176
177
178
179
// Copyright © 2024 Ory Corp
// SPDX-License-Identifier: Apache-2.0
package oauth2
import (
"context"
"net/url"
"strings"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/ory/fosite"
"github.com/ory/fosite/storage"
)
func parseUrl(uu string) *url.URL {
u, _ := url.Parse(uu)
return u
}
func TestAuthorizeCode_HandleAuthorizeEndpointRequest(t *testing.T) {
for k, strategy := range map[string]CoreStrategy{
"hmac": hmacshaStrategy,
} {
t.Run("strategy="+k, func(t *testing.T) {
store := storage.NewMemoryStore()
handler := AuthorizeExplicitGrantHandler{
CoreStorage: store,
AuthorizeCodeStrategy: strategy,
Config: &fosite.Config{
AudienceMatchingStrategy: fosite.DefaultAudienceMatchingStrategy,
ScopeStrategy: fosite.HierarchicScopeStrategy,
},
}
for _, c := range []struct {
handler AuthorizeExplicitGrantHandler
areq *fosite.AuthorizeRequest
description string
expectErr error
expect func(t *testing.T, areq *fosite.AuthorizeRequest, aresp *fosite.AuthorizeResponse)
}{
{
handler: handler,
areq: &fosite.AuthorizeRequest{
ResponseTypes: fosite.Arguments{""},
Request: *fosite.NewRequest(),
},
description: "should pass because not responsible for handling an empty response type",
},
{
handler: handler,
areq: &fosite.AuthorizeRequest{
ResponseTypes: fosite.Arguments{"foo"},
Request: *fosite.NewRequest(),
},
description: "should pass because not responsible for handling an invalid response type",
},
{
handler: handler,
areq: &fosite.AuthorizeRequest{
ResponseTypes: fosite.Arguments{"code"},
Request: fosite.Request{
Client: &fosite.DefaultClient{
ResponseTypes: fosite.Arguments{"code"},
RedirectURIs: []string{"http://asdf.com/cb"},
},
},
RedirectURI: parseUrl("http://asdf.com/cb"),
},
description: "should fail because redirect uri is not https",
expectErr: fosite.ErrInvalidRequest,
},
{
handler: handler,
areq: &fosite.AuthorizeRequest{
ResponseTypes: fosite.Arguments{"code"},
Request: fosite.Request{
Client: &fosite.DefaultClient{
ResponseTypes: fosite.Arguments{"code"},
RedirectURIs: []string{"https://asdf.com/cb"},
Audience: []string{"https://www.ory.sh/api"},
},
RequestedAudience: []string{"https://www.ory.sh/not-api"},
},
RedirectURI: parseUrl("https://asdf.com/cb"),
},
description: "should fail because audience doesn't match",
expectErr: fosite.ErrInvalidRequest,
},
{
handler: handler,
areq: &fosite.AuthorizeRequest{
ResponseTypes: fosite.Arguments{"code"},
Request: fosite.Request{
Client: &fosite.DefaultClient{
ResponseTypes: fosite.Arguments{"code"},
RedirectURIs: []string{"https://asdf.de/cb"},
Audience: []string{"https://www.ory.sh/api"},
},
RequestedAudience: []string{"https://www.ory.sh/api"},
GrantedScope: fosite.Arguments{"a", "b"},
Session: &fosite.DefaultSession{
ExpiresAt: map[fosite.TokenType]time.Time{fosite.AccessToken: time.Now().UTC().Add(time.Hour)},
},
RequestedAt: time.Now().UTC(),
},
State: "superstate",
RedirectURI: parseUrl("https://asdf.de/cb"),
},
description: "should pass",
expect: func(t *testing.T, areq *fosite.AuthorizeRequest, aresp *fosite.AuthorizeResponse) {
code := aresp.GetParameters().Get("code")
assert.NotEmpty(t, code)
assert.Equal(t, strings.Join(areq.GrantedScope, " "), aresp.GetParameters().Get("scope"))
assert.Equal(t, areq.State, aresp.GetParameters().Get("state"))
assert.Equal(t, fosite.ResponseModeQuery, areq.GetResponseMode())
},
},
{
handler: AuthorizeExplicitGrantHandler{
CoreStorage: store,
AuthorizeCodeStrategy: strategy,
Config: &fosite.Config{
ScopeStrategy: fosite.HierarchicScopeStrategy,
AudienceMatchingStrategy: fosite.DefaultAudienceMatchingStrategy,
OmitRedirectScopeParam: true,
},
},
areq: &fosite.AuthorizeRequest{
ResponseTypes: fosite.Arguments{"code"},
Request: fosite.Request{
Client: &fosite.DefaultClient{
ResponseTypes: fosite.Arguments{"code"},
RedirectURIs: []string{"https://asdf.de/cb"},
Audience: []string{"https://www.ory.sh/api"},
},
RequestedAudience: []string{"https://www.ory.sh/api"},
GrantedScope: fosite.Arguments{"a", "b"},
Session: &fosite.DefaultSession{
ExpiresAt: map[fosite.TokenType]time.Time{fosite.AccessToken: time.Now().UTC().Add(time.Hour)},
},
RequestedAt: time.Now().UTC(),
},
State: "superstate",
RedirectURI: parseUrl("https://asdf.de/cb"),
},
description: "should pass but no scope in redirect uri",
expect: func(t *testing.T, areq *fosite.AuthorizeRequest, aresp *fosite.AuthorizeResponse) {
code := aresp.GetParameters().Get("code")
assert.NotEmpty(t, code)
assert.Empty(t, aresp.GetParameters().Get("scope"))
assert.Equal(t, areq.State, aresp.GetParameters().Get("state"))
assert.Equal(t, fosite.ResponseModeQuery, areq.GetResponseMode())
},
},
} {
t.Run("case="+c.description, func(t *testing.T) {
aresp := fosite.NewAuthorizeResponse()
err := c.handler.HandleAuthorizeEndpointRequest(context.Background(), c.areq, aresp)
if c.expectErr != nil {
require.EqualError(t, err, c.expectErr.Error())
} else {
require.NoError(t, err)
}
if c.expect != nil {
c.expect(t, c.areq, aresp)
}
})
}
})
}
}