forked from ory/fosite
/
oauth2_integration_test.go
238 lines (212 loc) · 8.06 KB
/
oauth2_integration_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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
package fosite_test
import (
"github.com/go-errors/errors"
"github.com/golang/mock/gomock"
"github.com/gorilla/mux"
. "github.com/ory-am/fosite"
"github.com/ory-am/fosite/enigma"
"github.com/ory-am/fosite/handler/core/explicit"
"github.com/ory-am/fosite/handler/core/strategy"
. "github.com/ory-am/fosite/internal"
"github.com/parnurzeal/gorequest"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"golang.org/x/net/context"
goauth2 "golang.org/x/oauth2"
"net/http"
"net/http/httptest"
"net/url"
"testing"
"time"
)
var clientID = "foo"
var clientSecret = "barbarbarbarbar"
var ts *httptest.Server
var mockStore *MockStorage
var mockClient *MockClient
var mockAuthStore *MockAuthorizeCodeGrantStorage
var mockAuthReq *MockAuthorizeRequester
var mockHasher *MockHasher
var defaultStrategy = &strategy.HMACSHAStrategy{
Enigma: &enigma.HMACSHAEnigma{
GlobalSecret: []byte("super-global-secret"),
},
}
func TestFosite(t *testing.T) {
ctrl := gomock.NewController(t)
mockStore = NewMockStorage(ctrl)
mockClient = NewMockClient(ctrl)
mockAuthStore = NewMockAuthorizeCodeGrantStorage(ctrl)
mockAuthReq = NewMockAuthorizeRequester(ctrl)
mockHasher = NewMockHasher(ctrl)
defer ctrl.Finish()
authExplicitHandler := &explicit.AuthorizeExplicitGrantTypeHandler{
AccessTokenStrategy: defaultStrategy,
RefreshTokenStrategy: defaultStrategy,
AuthorizeCodeStrategy: defaultStrategy,
Store: mockAuthStore,
}
oauth2 := NewFosite(mockStore)
oauth2.Hasher = mockHasher
oauth2.AuthorizeEndpointHandlers.Add("code", authExplicitHandler)
oauth2.TokenEndpointHandlers.Add("code", authExplicitHandler)
oauth2TestAuthorizeCodeWorkFlow(oauth2, t, func() {
mockStore = NewMockStorage(ctrl)
mockAuthReq = NewMockAuthorizeRequester(ctrl)
mockClient = NewMockClient(ctrl)
mockAuthStore = NewMockAuthorizeCodeGrantStorage(ctrl)
mockHasher = NewMockHasher(ctrl)
oauth2.Hasher = mockHasher
oauth2.Store = mockStore
authExplicitHandler.Store = mockAuthStore
})
}
func oauth2TestAuthorizeCodeWorkFlow(oauth2 OAuth2Provider, t *testing.T, refreshMocks func()) {
const workingClientID = "foo"
const workingClientSecret = "secretsecretsecretsecret"
const state = "secure-random-state"
var workingClientHashedSecret = []byte("$2a$10$rUQDYblu3fytMb9aQ3soh.yKNe.17spWcY9fUkkvI9Nv7U1NJCMV2")
var session = &struct {
UserID string
}{
UserID: "foo",
}
router := mux.NewRouter()
router.HandleFunc("/auth", authEndpoint(t, oauth2, session))
router.HandleFunc("/cb", cbEndpoint(t))
router.HandleFunc("/token", tokenEndpoint(t, oauth2))
ts = httptest.NewServer(router)
defer ts.Close()
for k, c := range []struct {
conf goauth2.Config
state string
expectBody string
expectStatusCode int
expectPath string
expectedTokenError bool
mock func()
}{
{
conf: goauth2.Config{
ClientID: clientID,
ClientSecret: clientSecret,
RedirectURL: ts.URL + "/cb",
Endpoint: goauth2.Endpoint{
AuthURL: ts.URL + "/auth",
},
},
state: state,
mock: func() {
mockStore.EXPECT().GetClient(gomock.Eq(clientID)).AnyTimes().Return(nil, errors.New("foo"))
mockClient.EXPECT().GetHashedSecret().AnyTimes().Return(workingClientHashedSecret)
mockClient.EXPECT().GetRedirectURIs().AnyTimes().Return([]string{ts.URL + "/cb"})
mockAuthStore.EXPECT().GetAuthorizeCodeSession(gomock.Any(), gomock.Any()).AnyTimes().Return(nil, errors.New("foo"))
},
expectStatusCode: http.StatusOK,
expectPath: "/auth",
expectBody: "{\n\t\"name\": \"invalid_client\",\n\t\"description\": \"Client authentication failed (e.g., unknown client, no client authentication included, or unsupported authentication method)\"\n}",
expectedTokenError: true,
},
{
conf: goauth2.Config{
ClientID: clientID,
ClientSecret: clientSecret,
RedirectURL: ts.URL + "/cb",
Endpoint: goauth2.Endpoint{
AuthURL: ts.URL + "/auth",
TokenURL: ts.URL + "/token",
},
},
state: state,
mock: func() {
mockStore.EXPECT().GetClient(gomock.Eq(clientID)).AnyTimes().Return(mockClient, nil)
mockHasher.EXPECT().Compare(gomock.Any(), gomock.Any()).Return(nil)
mockClient.EXPECT().GetHashedSecret().AnyTimes().Return(workingClientHashedSecret)
mockClient.EXPECT().GetRedirectURIs().AnyTimes().Return([]string{ts.URL + "/cb"})
mockAuthStore.EXPECT().GetAuthorizeCodeSession(gomock.Any(), gomock.Any()).AnyTimes().Return(nil, errors.New("foo"))
},
expectStatusCode: http.StatusOK,
expectPath: "/cb",
expectBody: "error: invalid_scope",
expectedTokenError: true,
},
{
conf: goauth2.Config{
ClientID: clientID,
ClientSecret: clientSecret,
Scopes: []string{DefaultRequiredScopeName},
RedirectURL: ts.URL + "/cb",
Endpoint: goauth2.Endpoint{
AuthURL: ts.URL + "/auth",
TokenURL: ts.URL + "/token",
},
},
state: state,
mock: func() {
mockStore.EXPECT().GetClient(gomock.Eq(clientID)).AnyTimes().Return(mockClient, nil)
mockHasher.EXPECT().Compare(gomock.Any(), gomock.Any()).Return(nil)
mockClient.EXPECT().GetHashedSecret().AnyTimes().Return(workingClientHashedSecret)
mockClient.EXPECT().GetRedirectURIs().AnyTimes().Return([]string{ts.URL + "/cb"})
mockAuthStore.EXPECT().CreateAuthorizeCodeSession(gomock.Any(), gomock.Any()).Return(nil)
mockAuthStore.EXPECT().GetAuthorizeCodeSession(gomock.Any(), gomock.Any()).AnyTimes().Return(nil, errors.New("foo"))
},
expectStatusCode: http.StatusOK,
expectPath: "/cb",
expectBody: "code: ok",
expectedTokenError: true,
},
{
conf: goauth2.Config{
ClientID: clientID,
ClientSecret: clientSecret,
RedirectURL: ts.URL + "/cb",
Scopes: []string{DefaultRequiredScopeName},
Endpoint: goauth2.Endpoint{
AuthURL: ts.URL + "/auth",
TokenURL: ts.URL + "/token",
},
},
state: state,
mock: func() {
mockStore.EXPECT().GetClient(gomock.Eq(clientID)).AnyTimes().Return(mockClient, nil)
mockHasher.EXPECT().Compare(gomock.Any(), gomock.Any()).Return(nil)
mockClient.EXPECT().GetID().AnyTimes().Return(clientID)
mockClient.EXPECT().GetHashedSecret().AnyTimes().Return(workingClientHashedSecret)
mockClient.EXPECT().GetRedirectURIs().AnyTimes().Return([]string{ts.URL + "/cb"})
mockAuthStore.EXPECT().CreateAuthorizeCodeSession(gomock.Any(), gomock.Any()).AnyTimes().Return(nil)
mockAuthStore.EXPECT().GetAuthorizeCodeSession(gomock.Any(), gomock.Any()).AnyTimes().Return(mockAuthReq, nil)
mockAuthStore.EXPECT().CreateAccessTokenSession(gomock.Any(), gomock.Any()).AnyTimes().Return(nil)
mockAuthStore.EXPECT().CreateRefreshTokenSession(gomock.Any(), gomock.Any()).AnyTimes().Return(nil)
mockAuthStore.EXPECT().DeleteAuthorizeCodeSession(gomock.Any()).AnyTimes().Return(nil)
mockAuthReq.EXPECT().GetClient().AnyTimes().Return(mockClient)
mockAuthReq.EXPECT().GetRequestedAt().AnyTimes().Return(time.Now())
mockAuthReq.EXPECT().GetRequestForm().AnyTimes().Return(url.Values{})
mockAuthReq.EXPECT().GetSession().AnyTimes().Return(nil)
mockAuthReq.EXPECT().GetScopes().Return([]string{DefaultRequiredScopeName})
},
expectStatusCode: http.StatusOK,
expectPath: "/cb",
expectBody: "code: ok",
expectedTokenError: false,
},
// TODO add a ton of tests for RFC conform tests. use factories! See https://github.com/ory-am/fosite/issues/13
} {
refreshMocks()
c.mock()
authurl := c.conf.AuthCodeURL(c.state)
req := gorequest.New()
resp, body, errs := req.Get(authurl).End()
require.Len(t, errs, 0, "%s", errs)
assert.Equal(t, c.expectPath, resp.Request.URL.Path)
assert.Equal(t, c.expectBody, body)
assert.Equal(t, c.expectStatusCode, resp.StatusCode)
authorizeCode := resp.Request.URL.Query().Get("code")
token, err := c.conf.Exchange(context.Background(), authorizeCode)
assert.Equal(t, c.expectedTokenError, err != nil, "%d: %s", k, err)
if !c.expectedTokenError {
assert.NotNil(t, token)
}
t.Logf("Got token %s", token)
t.Logf("Passed test case %d", k)
}
}