forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth_test.go
248 lines (214 loc) · 7.52 KB
/
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
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
package uaa_test
import (
"errors"
"fmt"
"net/http"
"net/url"
. "code.cloudfoundry.org/cli/api/uaa"
"code.cloudfoundry.org/cli/api/uaa/constant"
"code.cloudfoundry.org/cli/api/uaa/uaafakes"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
. "github.com/onsi/gomega/ghttp"
)
var _ = Describe("Auth", func() {
var (
client *Client
credentials map[string]string
origin string
grantType constant.GrantType
accessToken string
refreshToken string
executeErr error
fakeConfig *uaafakes.FakeConfig
)
BeforeEach(func() {
fakeConfig = NewTestConfig()
client = NewTestUAAClientAndStore(fakeConfig)
})
Describe("Authenticate", func() {
JustBeforeEach(func() {
accessToken, refreshToken, executeErr = client.Authenticate(credentials, origin, grantType)
})
When("no errors occur", func() {
When("the grant type is password", func() {
var response string
BeforeEach(func() {
response = `{
"access_token":"some-access-token",
"refresh_token":"some-refresh-token"
}`
credentials = map[string]string{
"username": "some-username",
"password": "some-password",
}
grantType = constant.GrantTypePassword
})
When("origin is not set", func() {
BeforeEach(func() {
origin = ""
server.AppendHandlers(
CombineHandlers(
verifyRequestHost(TestAuthorizationResource),
VerifyRequest(http.MethodPost, "/oauth/token", ""),
VerifyHeaderKV("Content-Type", "application/x-www-form-urlencoded"),
VerifyHeaderKV("Authorization", "Basic Y2xpZW50LWlkOmNsaWVudC1zZWNyZXQ="),
VerifyBody([]byte("grant_type=password&password=some-password&username=some-username")),
RespondWith(http.StatusOK, response),
))
})
It("authenticates with the credentials provided", func() {
Expect(executeErr).NotTo(HaveOccurred())
Expect(accessToken).To(Equal("some-access-token"))
Expect(refreshToken).To(Equal("some-refresh-token"))
})
})
When("origin is set", func() {
BeforeEach(func() {
origin = "some-fake-origin"
expectedQuery := "login_hint=%7B%22origin%22%3A%22" + origin + "%22%7D"
server.AppendHandlers(
CombineHandlers(
verifyRequestHost(TestAuthorizationResource),
VerifyRequest(http.MethodPost, "/oauth/token", expectedQuery),
VerifyHeaderKV("Content-Type", "application/x-www-form-urlencoded"),
VerifyHeaderKV("Authorization", "Basic Y2xpZW50LWlkOmNsaWVudC1zZWNyZXQ="),
VerifyBody([]byte("grant_type=password&password=some-password&username=some-username")),
RespondWith(http.StatusOK, response),
))
})
It("authenticates with the credentials provided", func() {
Expect(executeErr).NotTo(HaveOccurred())
Expect(accessToken).To(Equal("some-access-token"))
Expect(refreshToken).To(Equal("some-refresh-token"))
})
})
When("additional prompts are answered", func() {
BeforeEach(func() {
credentials = map[string]string{
"username": "some-username",
"password": "some-password",
"mfaCode": "some-mfa-code",
"customPrompt": "some-custom-value",
}
expectedValues := url.Values{
"username": []string{"some-username"},
"password": []string{"some-password"},
"mfaCode": []string{"some-mfa-code"},
"customPrompt": []string{"some-custom-value"},
}
server.AppendHandlers(
CombineHandlers(
VerifyForm(expectedValues),
RespondWith(http.StatusOK, response),
),
)
})
It("sends all the prompts to the UAA", func() {
Expect(executeErr).NotTo(HaveOccurred())
Expect(accessToken).To(Equal("some-access-token"))
Expect(refreshToken).To(Equal("some-refresh-token"))
})
})
})
When("the grant type is client credentials", func() {
BeforeEach(func() {
response := `{
"access_token":"some-access-token"
}`
credentials = map[string]string{
"client_id": "some-client-id",
"client_secret": "some-client-secret",
}
origin = ""
grantType = constant.GrantTypeClientCredentials
server.AppendHandlers(
CombineHandlers(
verifyRequestHost(TestAuthorizationResource),
VerifyRequest(http.MethodPost, "/oauth/token"),
VerifyHeaderKV("Content-Type", "application/x-www-form-urlencoded"),
VerifyHeaderKV("Authorization"),
VerifyBody([]byte(fmt.Sprintf("client_id=%s&client_secret=%s&grant_type=%s", credentials["client_id"], credentials["client_secret"], grantType))),
RespondWith(http.StatusOK, response),
))
})
It("authenticates with the credentials provided", func() {
Expect(executeErr).NotTo(HaveOccurred())
Expect(accessToken).To(Equal("some-access-token"))
Expect(refreshToken).To(BeEmpty())
})
})
})
When("an error occurs", func() {
var response string
BeforeEach(func() {
response = `{
"error": "some-error",
"error_description": "some-description"
}`
server.AppendHandlers(
CombineHandlers(
verifyRequestHost(TestAuthorizationResource),
VerifyRequest(http.MethodPost, "/oauth/token"),
RespondWith(http.StatusTeapot, response),
))
})
It("returns the error", func() {
Expect(executeErr).To(MatchError(RawHTTPStatusError{
StatusCode: http.StatusTeapot,
RawResponse: []byte(response),
}))
})
})
})
Describe("Revoke", func() {
var (
jtiFromToken string
testToken string
actualError error
)
JustBeforeEach(func() {
actualError = client.Revoke(testToken)
})
When("the call to revoke succeeds", func() {
BeforeEach(func() {
testToken = "eyJhbGciOiJIUzI1NiJ9.eyJqdGkiOiI1NWZiOTVlM2M5OTY0MmYxODQxMTUyZWIwNmFjYTM4NiIsInJldm9jYWJsZSI6dHJ1ZX0.EYvzQDsCqXRO0r1dowPRYIgeqP_V320v1WbLTG5y6iA"
jtiFromToken = "55fb95e3c99642f1841152eb06aca386"
server.AppendHandlers(
CombineHandlers(
VerifyRequest(http.MethodDelete, fmt.Sprintf("/oauth/token/revoke/%s", jtiFromToken)),
VerifyHeaderKV("Authorization: Bearer "+testToken),
RespondWith(http.StatusOK, nil),
))
})
It("makes a call to find the jti and uses that jti to revoke the token", func() {
Expect(actualError).To(BeNil())
Expect(len(server.ReceivedRequests())).To(Equal(1))
Expect(server.ReceivedRequests()[0].RequestURI).To(Equal("/oauth/token/revoke/55fb95e3c99642f1841152eb06aca386"))
})
})
When("the call to revoke the token fails", func() {
BeforeEach(func() {
testToken = "eyJhbGciOiJIUzI1NiJ9.eyJqdGkiOiI1NWZiOTVlM2M5OTY0MmYxODQxMTUyZWIwNmFjYTM4NiIsInJldm9jYWJsZSI6dHJ1ZX0.EYvzQDsCqXRO0r1dowPRYIgeqP_V320v1WbLTG5y6iA"
jtiFromToken = "55fb95e3c99642f1841152eb06aca386"
server.AppendHandlers(
CombineHandlers(
VerifyRequest(http.MethodDelete, fmt.Sprintf("/oauth/token/revoke/%s", jtiFromToken)),
VerifyHeaderKV("Authorization: Bearer "+testToken),
RespondWith(http.StatusForbidden, nil),
))
})
It("returns that failure", func() {
Expect(actualError).To(MatchError(RawHTTPStatusError{StatusCode: 403, RawResponse: []byte{}}))
})
})
When("the token contains no jti", func() {
BeforeEach(func() {
testToken = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c"
})
It("returns an error saying it could not parse the jti", func() {
Expect(actualError).To(Equal(errors.New("could not parse jti from payload")))
})
})
})
})