This repository has been archived by the owner on Mar 18, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
tokens_test.go
249 lines (199 loc) · 8.03 KB
/
tokens_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
239
240
241
242
243
244
245
246
247
248
249
// This Source Code Form is subject to the terms of the Mozilla Public
// License, version 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
package oauth2
import (
"bytes"
"encoding/json"
"net/http"
"net/http/httptest"
"net/url"
"testing"
"github.com/hooklift/oauth2/providers/test"
"github.com/hooklift/oauth2/types"
)
func AuthzGrantTokenRequestTest(t *testing.T, grantType, authzCode string) *http.Request {
// http://tools.ietf.org/html/rfc6749#section-4.1.3
queryStr := url.Values{
"grant_type": {grantType},
"code": {authzCode},
"redirect_uri": {"https://example.com/oauth2/callback"},
"client_id": {"test_client_id"},
}
buffer := bytes.NewBufferString(queryStr.Encode())
req, err := http.NewRequest("POST", "https://example.com/oauth2/tokens", buffer)
ok(t, err)
req.Header.Set("Content-type", "application/x-www-form-urlencoded")
return req
}
// TestAccessToken tests a happy path for getting access tokens in accordance with
// http://tools.ietf.org/html/rfc6749#section-4.1.3 and
// http://tools.ietf.org/html/rfc6749#section-4.1.4
func TestAuthzGrantTokenRequest(t *testing.T) {
cfg, authzCode := getTestAuthzCode(t)
req := AuthzGrantTokenRequestTest(t, "authorization_code", authzCode)
req.SetBasicAuth("testclient", "testclient")
w := httptest.NewRecorder()
IssueToken(w, req, cfg)
// http://tools.ietf.org/html/rfc6749#section-4.1.4
accessToken := types.Token{}
err := json.Unmarshal(w.Body.Bytes(), &accessToken)
ok(t, err)
//log.Printf("%s", w.Body.String())
equals(t, "bearer", accessToken.Type)
equals(t, "600", accessToken.ExpiresIn)
assert(t, accessToken.RefreshToken != "", "we were expecting a refresh token.")
// Tests that cache headers are being sent when generating access tokens using
// authorization grant codes.
equals(t, "no-store", w.Header().Get("Cache-Control"))
equals(t, "no-cache", w.Header().Get("Pragma"))
equals(t, "0", w.Header().Get("Expires"))
}
// TestClientAuthRequired tests that client is required to always authenticate in order
// to request access tokens.
func TestAuthzGrantClientAuthRequired(t *testing.T) {
cfg, authzCode := getTestAuthzCode(t)
req := AuthzGrantTokenRequestTest(t, "authorization_code", authzCode)
w := httptest.NewRecorder()
IssueToken(w, req, cfg)
// Tests for a 400 instead of 401 in accordance to http://tools.ietf.org/html/rfc6749#section-5.1
equals(t, http.StatusBadRequest, w.Code)
appErr := types.AuthzError{}
err := json.Unmarshal(w.Body.Bytes(), &appErr)
ok(t, err)
equals(t, "unauthorized_client", appErr.Code)
}
// TestResourceOwnerCredentialsGrant tests happy path for http://tools.ietf.org/html/rfc6749#section-4.3
func TestResourceOwnerCredentialsGrant(t *testing.T) {
cfg := setupTest()
cfg.provider = test.NewProvider(true)
queryStr := url.Values{
"grant_type": {"password"},
"username": {"test_user"},
"password": {"test_password"},
}
buffer := bytes.NewBufferString(queryStr.Encode())
req, err := http.NewRequest("POST", "https://example.com/oauth2/tokens", buffer)
ok(t, err)
req.Header.Set("Content-type", "application/x-www-form-urlencoded")
req.SetBasicAuth("testclient", "testclient")
w := httptest.NewRecorder()
IssueToken(w, req, cfg)
accessToken := types.Token{}
err = json.Unmarshal(w.Body.Bytes(), &accessToken)
ok(t, err)
//log.Printf("%s", w.Body.String())
equals(t, "bearer", accessToken.Type)
equals(t, "600", accessToken.ExpiresIn)
assert(t, accessToken.RefreshToken != "", "we were expecting a refresh token.")
// Tests that cache headers are being sent when generating tokens using
// resource owner credentials.
equals(t, "no-store", w.Header().Get("Cache-Control"))
equals(t, "no-cache", w.Header().Get("Pragma"))
equals(t, "0", w.Header().Get("Expires"))
}
// TestClientCredentialsGrant tests happy path for http://tools.ietf.org/html/rfc6749#section-4.4
func TestClientCredentialsGrant(t *testing.T) {
cfg := setupTest()
cfg.provider = test.NewProvider(true)
queryStr := url.Values{
"grant_type": {"client_credentials"},
}
buffer := bytes.NewBufferString(queryStr.Encode())
req, err := http.NewRequest("POST", "https://example.com/oauth2/tokens", buffer)
ok(t, err)
req.Header.Set("Content-type", "application/x-www-form-urlencoded")
req.SetBasicAuth("testclient", "testclient")
w := httptest.NewRecorder()
IssueToken(w, req, cfg)
accessToken := types.Token{}
err = json.Unmarshal(w.Body.Bytes(), &accessToken)
ok(t, err)
//log.Printf("%s", w.Body.String())
equals(t, "bearer", accessToken.Type)
equals(t, "600", accessToken.ExpiresIn)
// A refresh token SHOULD NOT be included.
equals(t, "", accessToken.RefreshToken)
// Tests that cache headers are being sent when generating tokens using
// client credentials.
equals(t, "no-store", w.Header().Get("Cache-Control"))
equals(t, "no-cache", w.Header().Get("Pragma"))
equals(t, "0", w.Header().Get("Expires"))
}
// TestRefreshToken tests happy path for http://tools.ietf.org/html/rfc6749#section-6
func TestRefreshToken(t *testing.T) {
cfg := setupTest()
provider := test.NewProvider(true)
cfg.provider = provider
noAuthzGrant := types.Grant{
Scopes: types.Scopes{
types.Scope{ID: "identity"},
},
}
accessToken, err := provider.GenToken(noAuthzGrant, types.Client{
ID: "test_client_id",
}, true, cfg.tokenExpiration)
ok(t, err)
queryStr := url.Values{
"grant_type": {"refresh_token"},
"refresh_token": {accessToken.RefreshToken},
"scope": {"identity"},
}
buffer := bytes.NewBufferString(queryStr.Encode())
req, err := http.NewRequest("POST", "https://example.com/oauth2/tokens", buffer)
ok(t, err)
req.Header.Set("Content-type", "application/x-www-form-urlencoded")
req.SetBasicAuth("testclient", "testclient")
w := httptest.NewRecorder()
IssueToken(w, req, cfg)
token := types.Token{}
err = json.Unmarshal(w.Body.Bytes(), &token)
ok(t, err)
//log.Printf("%s", w.Body.String())
equals(t, "bearer", token.Type)
equals(t, "600", token.ExpiresIn)
assert(t, accessToken.Value != token.Value, "We got the same access token, it should be different!")
assert(t, token.Value != "", "We were expecting to get a token and instead we got: %s", token.Value)
assert(t, token.RefreshToken != "", "we were expecting a refresh token.")
assert(t, token.RefreshToken != accessToken.RefreshToken, "We got the same refresh token, it should be different!")
// Tests that cache headers are being sent when refreshing tokens
equals(t, "no-store", w.Header().Get("Cache-Control"))
equals(t, "no-cache", w.Header().Get("Pragma"))
equals(t, "0", w.Header().Get("Expires"))
}
// TestAuthzCodeOwnership tests that the authorization code was issued to the client
// requesting the access token.
func TestAuthzCodeOwnership(t *testing.T) {
cfg, authzCode := getTestAuthzCode(t)
req := AuthzGrantTokenRequestTest(t, "authorization_code", authzCode)
req.SetBasicAuth("boo", "boo")
w := httptest.NewRecorder()
IssueToken(w, req, cfg)
// http://tools.ietf.org/html/rfc6749#section-4.1.4
authzErr := types.AuthzError{}
//log.Printf("%s", w.Body.String())
err := json.Unmarshal(w.Body.Bytes(), &authzErr)
ok(t, err)
equals(t, "invalid_grant", authzErr.Code)
equals(t, "Grant code was generated for a different redirect URI.", authzErr.Description)
}
// TestRevokeToken tests happy path for revoking refresh and access tokens.
// In accordance with https://tools.ietf.org/html/rfc7009
func TestRevokeToken(t *testing.T) {
cfg, authzCode := getTestAuthzCode(t)
r1 := AuthzGrantTokenRequestTest(t, "authorization_code", authzCode)
r1.SetBasicAuth("testclient", "testclient")
w := httptest.NewRecorder()
IssueToken(w, r1, cfg)
// http://tools.ietf.org/html/rfc6749#section-4.1.4
accessToken := types.Token{}
err := json.Unmarshal(w.Body.Bytes(), &accessToken)
ok(t, err)
r2, err := http.NewRequest("DELETE", "https://example.com/oauth2/tokens/"+accessToken.Value, nil)
ok(t, err)
r2.Header.Set("Content-type", "application/x-www-form-urlencoded")
r2.SetBasicAuth("testclient", "testclient")
w2 := httptest.NewRecorder()
RevokeToken(w2, r2, cfg)
equals(t, http.StatusOK, w2.Code)
}