-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfireauth_test.go
More file actions
210 lines (151 loc) · 4.42 KB
/
Copy pathfireauth_test.go
File metadata and controls
210 lines (151 loc) · 4.42 KB
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
package fireauth
import (
"fmt"
"net/http"
"net/http/httptest"
"time"
"github.com/benbjohnson/clock"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"gopkg.in/jose.v1/jws"
"gopkg.in/jose.v1/jwt"
)
var _ = Describe("fireauth", func() {
var (
firebase *FireAuth
mockClock *clock.Mock
err error
)
BeforeEach(func() {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set(HeaderCacheControl, "..., max-age=19008, ...")
fmt.Fprintln(w, jsonKeys)
}))
defer ts.Close()
mockClock = clock.NewMock()
mockClock.Set(time.Date(2016, time.February, 02, 8, 0, 0, 0, time.UTC))
fb := &FireAuth{
ProjectID: "ridesharelogger",
KeyURL: ts.URL,
IssPrefix: IssPrefix,
Clock: mockClock,
}
err = fb.UpdatePublicKeys()
Expect(err).ToNot(HaveOccurred())
firebase = fb
})
Describe("validate", func() {
var (
userID string
claims jwt.Claims
)
Context("valid token", func() {
BeforeEach(func() {
claimTimeOverride := &claimTimeOverride{
exp: time.Now().Unix() + 1000,
iat: mockClock.Now().Unix() - 1000,
}
firebase.setClaimTimeOverride(claimTimeOverride)
userID, claims, err = firebase.Verify(token)
})
It("should not thow an error", func() {
Expect(err).ToNot(HaveOccurred())
})
It("should return correct user ID", func() {
Expect(userID).To(Equal("TE19E2gU2aUxZP4t02mLW3VCMF63"))
})
It("should return 9 claims", func() {
Expect(len(claims)).To(Equal(9))
})
})
Context("token not signed by active keys", func() {
BeforeEach(func() {
claimTimeOverride := &claimTimeOverride{
exp: time.Now().Unix() + 1000,
iat: mockClock.Now().Unix() - 1000,
}
firebase.setClaimTimeOverride(claimTimeOverride)
_, _, err = firebase.Verify(token2)
})
It("should not thow an error", func() {
Expect(err).To(Equal(ErrRSAVerification))
})
})
Context("expired token", func() {
BeforeEach(func() {
claimTimeOverride := &claimTimeOverride{
exp: time.Now().Unix() - 1000,
iat: mockClock.Now().Unix() - 1000,
}
firebase.setClaimTimeOverride(claimTimeOverride)
_, _, err = firebase.Verify(token)
})
It("should thow a token is expired error", func() {
Expect(err).To(Equal(jwt.ErrTokenIsExpired))
})
})
Context("token not yet issued", func() {
BeforeEach(func() {
claimTimeOverride := &claimTimeOverride{
exp: time.Now().Unix() + 1000,
iat: mockClock.Now().Unix() + 1000,
}
firebase.setClaimTimeOverride(claimTimeOverride)
_, _, err = firebase.Verify(token)
})
It("should thow a token not issued yet error", func() {
Expect(err).To(Equal(ErrNotIssuedYet))
})
})
Context("invalid token", func() {
BeforeEach(func() {
_, claims, err = firebase.Verify("invalid token")
})
It("should throw ErrNotCompact error", func() {
Expect(err).To(HaveOccurred())
Expect(err).To(Equal(jws.ErrNotCompact))
})
It("should return nil claims", func() {
Expect(claims).To(BeNil())
})
})
})
Describe("update stale keys", func() {
BeforeEach(func() {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set(HeaderCacheControl, "..., max-age=1337, ...")
fmt.Fprintln(w, jsonKeys2)
}))
defer ts.Close()
firebase.KeyURL = ts.URL
mockClock.Set(time.Date(2017, time.February, 02, 8, 0, 0, 0, time.UTC))
firebase.Verify(token)
})
Specify("max-age should now be 1337", func() {
maxAge := firebase.keyExpire - mockClock.Now().Unix()
Expect(maxAge).To(Equal(int64(1337)))
})
Specify("Firebase should now have 2 keys", func() {
Expect(len(firebase.publicKeys)).To(Equal(2))
})
})
Describe("non stale keys don't need updated", func() {
BeforeEach(func() {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set(HeaderCacheControl, "..., max-age=1337, ...")
fmt.Fprintln(w, jsonKeys2)
}))
defer ts.Close()
firebase.KeyURL = ts.URL
mockClock.Set(time.Date(2016, time.February, 02, 8, 0, 0, 0, time.UTC))
firebase.Verify(token)
})
Specify("max-age should still be 19008", func() {
maxAge := firebase.keyExpire - mockClock.Now().Unix()
Expect(maxAge).To(Equal(int64(19008)))
})
Specify("Firebase should still have 4 keys", func() {
Expect(len(firebase.publicKeys)).To(Equal(4))
})
})
})