This repository has been archived by the owner on Nov 29, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 55
/
EncryptedCookieAuthenticator.scala
490 lines (436 loc) · 17.3 KB
/
EncryptedCookieAuthenticator.scala
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
package tsec.authentication
import java.time.Instant
import java.util.UUID
import cats.data.OptionT
import cats.effect.Sync
import cats.syntax.all._
import io.circe.generic.auto._
import io.circe.parser.decode
import io.circe.syntax._
import io.circe.{Decoder, Encoder}
import org.http4s._
import tsec.cipher.symmetric.{IvGen, _}
import tsec.cipher.symmetric.jca._
import tsec.common._
import tsec.cookies._
import tsec.hashing.jca._
import tsec.jwt.JWTPrinter
import scala.concurrent.duration.FiniteDuration
sealed abstract class EncryptedCookieAuthenticator[F[_]: Sync, I, V, A]
extends Authenticator[F, I, V, AuthEncryptedCookie[A, I]]
sealed abstract class StatefulECAuthenticator[F[_]: Sync, I, V, A] private[tsec] (
val expiry: FiniteDuration,
val maxIdle: Option[FiniteDuration]
) extends EncryptedCookieAuthenticator[F, I, V, A] {
def withKey(newKey: SecretKey[A]): StatefulECAuthenticator[F, I, V, A]
def withSettings(settings: TSecCookieSettings): StatefulECAuthenticator[F, I, V, A]
def withIdentityStore(newStore: IdentityStore[F, I, V]): StatefulECAuthenticator[F, I, V, A]
def withTokenStore(
newStore: BackingStore[F, UUID, AuthEncryptedCookie[A, I]]
): StatefulECAuthenticator[F, I, V, A]
}
sealed abstract class StatelessECAuthenticator[F[_]: Sync, I, V, A] private[tsec] (
val expiry: FiniteDuration,
val maxIdle: Option[FiniteDuration]
) extends EncryptedCookieAuthenticator[F, I, V, A] {
def withKey(newKey: SecretKey[A]): StatelessECAuthenticator[F, I, V, A]
def withSettings(settings: TSecCookieSettings): StatelessECAuthenticator[F, I, V, A]
def withIdentityStore(newStore: IdentityStore[F, I, V]): StatelessECAuthenticator[F, I, V, A]
}
/** An authenticated cookie implementation
*
* @param id the cookie id
* @param content the raw cookie: The full thing, including the nonce
* @param identity The id of what
* @param expiry
* @param lastTouched
* @tparam A
* @tparam Id
*/
final case class AuthEncryptedCookie[A, Id](
id: UUID,
name: String,
content: AEADCookie[A],
identity: Id,
expiry: Instant,
lastTouched: Option[Instant],
secure: Boolean,
httpOnly: Boolean = true,
domain: Option[String] = None,
path: Option[String] = None,
extension: Option[String] = None
) {
def toCookie = ResponseCookie(
name,
content,
Some(HttpDate.unsafeFromInstant(expiry)),
None,
domain,
path,
secure,
httpOnly,
extension
)
}
object AuthEncryptedCookie {
implicit def auth[A, Id] = new AuthToken[AuthEncryptedCookie[A, Id]] {
def expiry(a: AuthEncryptedCookie[A, Id]): Instant = a.expiry
def lastTouched(a: AuthEncryptedCookie[A, Id]): Option[Instant] = a.lastTouched
}
final case class Internal[Id](id: UUID, messageId: Id, expiry: Instant, lastTouched: Option[Instant])
def build[A, Id](
id: UUID,
content: AEADCookie[A],
messageId: Id,
expiry: Instant,
lastTouched: Option[Instant],
settings: TSecCookieSettings
): AuthEncryptedCookie[A, Id] =
AuthEncryptedCookie[A, Id](
id,
settings.cookieName,
content,
messageId,
expiry,
lastTouched,
settings.secure,
settings.httpOnly,
settings.domain,
settings.path,
settings.extension
)
def build[A, Id](
internal: Internal[Id],
content: AEADCookie[A],
settings: TSecCookieSettings
): AuthEncryptedCookie[A, Id] =
AuthEncryptedCookie[A, Id](
internal.id,
settings.cookieName,
content,
internal.messageId,
internal.expiry,
internal.lastTouched,
settings.secure,
settings.httpOnly,
settings.domain,
settings.path,
settings.extension
)
}
object EncryptedCookieAuthenticator {
private[tsec] def isExpired[I, A](
internal: AuthEncryptedCookie[A, I],
now: Instant,
maxIdle: Option[FiniteDuration]
): Boolean =
!internal.isExpired(now) && !maxIdle.exists(internal.isTimedOut(now, _))
/** The default Encrypted cookie Authenticator, with a backing store.
*
* @param settings the cookie settings
* @param tokenStore the token backing store
* @param identityStore the backing store of the identity
* @param key the symmetric signing key
* @tparam F the effect type
* @tparam A the symmetric key algorithm to use authenticated encryption
* @tparam I the id type for a user
* @tparam V the expected user type, V aka value
* @return An encrypted cookie authenticator
*/
def withBackingStore[F[_], I, V, A: AES](
settings: TSecCookieSettings,
tokenStore: BackingStore[F, UUID, AuthEncryptedCookie[A, I]],
identityStore: IdentityStore[F, I, V],
key: SecretKey[A]
)(
implicit F: Sync[F],
instance: JAuthEncryptor[F, A],
ivStrat: IvGen[F, A]
): StatefulECAuthenticator[F, I, V, A] =
new StatefulECAuthenticator[F, I, V, A](settings.expiryDuration, settings.maxIdle) {
/** Return a new instance with a modified key */
def withKey(newKey: SecretKey[A]): StatefulECAuthenticator[F, I, V, A] =
withBackingStore(
settings,
tokenStore,
identityStore,
newKey
)
/** Return a new instance with modified settings */
def withSettings(settings: TSecCookieSettings): StatefulECAuthenticator[F, I, V, A] =
withBackingStore(
settings,
tokenStore,
identityStore,
key
)
/** Return a new instance with a different identity store */
def withIdentityStore(newStore: IdentityStore[F, I, V]): StatefulECAuthenticator[F, I, V, A] =
withBackingStore(
settings,
tokenStore,
newStore,
key
)
/** Return a new instance with a different token store */
def withTokenStore(
newStore: BackingStore[F, UUID, AuthEncryptedCookie[A, I]]
): StatefulECAuthenticator[F, I, V, A] =
withBackingStore(
settings,
newStore,
identityStore,
key
)
/** Generate our AAD: A sort of nonce to use for authentication with our encryption
*
*/
private def generateAAD(message: String) =
AAD(SHA1.hashPure((message + Instant.now.toEpochMilli).utf8Bytes))
/** Validate the cookie against the raw representation, and
* validate against possibly expiration or timeout
*
*/
private def validateCookie(
internal: AuthEncryptedCookie[A, I],
raw: AEADCookie[A],
now: Instant
): Boolean =
internal.content === raw && isExpired[I, A](internal, now, settings.maxIdle)
/** lift the validation onto an optionT
*
*/
private def validateAndRefresh(
internal: AuthEncryptedCookie[A, I],
raw: AEADCookie[A],
now: Instant
): F[AuthEncryptedCookie[A, I]] =
if (validateCookie(internal, raw, now)) refresh(internal) else F.raiseError(AuthenticationFailure)
def extractRawOption(request: Request[F]): Option[String] =
unliftedCookieFromRequest(settings.cookieName, request).map(_.content)
def parseRaw(raw: String, request: Request[F]): OptionT[F, SecuredRequest[F, V, AuthEncryptedCookie[A, I]]] =
OptionT(
(for {
now <- F.delay(Instant.now())
coerced = AEADCookie[A](raw)
contentRaw <- AEADCookieEncryptor.retrieveFromSigned[F, A](coerced, key)
tokenId <- uuidFromRaw[F](contentRaw)
authed <- tokenStore.get(tokenId).orAuthFailure
refreshed <- validateAndRefresh(authed, coerced, now)
identity <- identityStore.get(authed.identity).orAuthFailure
} yield SecuredRequest(request, identity, refreshed).some)
.handleError(_ => None)
)
/** Create a new cookie from the id field of a particular user.
*
*/
def create(body: I): F[AuthEncryptedCookie[A, I]] =
for {
cookieId <- F.delay(UUID.randomUUID())
now <- F.delay(Instant.now())
expiry = now.plusSeconds(settings.expiryDuration.toSeconds)
lastTouched = settings.maxIdle.map(_ => now)
messageBody = cookieId.toString
encrypted <- AEADCookieEncryptor.signAndEncrypt[F, A](messageBody, generateAAD(messageBody), key)
cookie <- F.pure(AuthEncryptedCookie.build[A, I](cookieId, encrypted, body, expiry, lastTouched, settings))
_ <- tokenStore.put(cookie)
} yield cookie
/** Update our authenticator in the backing store.
*
*/
def update(authenticator: AuthEncryptedCookie[A, I]): F[AuthEncryptedCookie[A, I]] =
tokenStore.update(authenticator)
/** Discard our authenticator from the backing store
*
*/
def discard(authenticator: AuthEncryptedCookie[A, I]): F[AuthEncryptedCookie[A, I]] =
tokenStore.delete(authenticator.id).map(_ => authenticator.copy(expiry = Instant.EPOCH))
/** Renew, aka reset both the expiry as well as the last touched (if present) value
*
*/
def renew(authenticator: AuthEncryptedCookie[A, I]): F[AuthEncryptedCookie[A, I]] =
F.delay(Instant.now()).flatMap { now =>
settings.maxIdle match {
case Some(_) =>
val updated = authenticator.copy[A, I](
lastTouched = Some(now),
expiry = now.plusSeconds(settings.expiryDuration.toSeconds)
)
tokenStore.update(updated).map(_ => updated)
case None =>
val updated = authenticator.copy[A, I](
expiry = now.plusSeconds(settings.expiryDuration.toSeconds)
)
tokenStore.update(updated).map(_ => updated)
}
}
/** Touch our authenticator. Only used for sliding window expiration. Otherwise, it will be a no-op.
*
*/
def refresh(authenticator: AuthEncryptedCookie[A, I]): F[AuthEncryptedCookie[A, I]] =
settings.maxIdle match {
case Some(_) =>
F.delay(Instant.now())
.flatMap(
now =>
tokenStore.update(
authenticator.copy[A, I](
lastTouched = Some(now)
)
)
)
case None =>
F.pure(authenticator)
}
def embed(response: Response[F], authenticator: AuthEncryptedCookie[A, I]): Response[F] =
response.addCookie(authenticator.toCookie)
def afterBlock(response: Response[F], authenticator: AuthEncryptedCookie[A, I]): OptionT[F, Response[F]] =
settings.maxIdle match {
case Some(_) =>
OptionT.pure[F](response.addCookie(authenticator.toCookie))
case None =>
OptionT.pure[F](response)
}
}
/**
* Generate a stateless cookie authenticator that stores the authentication data, but not a token, in a backing store.
* Since we have no way to verify that the cookie's expiration and sliding window haven't been modified,
* we encrypt it as part of the contents
*
*/
def stateless[F[_], I: Decoder: Encoder, V, A: AES](
settings: TSecCookieSettings,
identityStore: IdentityStore[F, I, V],
key: SecretKey[A]
)(
implicit F: Sync[F],
instance: JAuthEncryptor[F, A],
ivStrat: IvGen[F, A]
): StatelessECAuthenticator[F, I, V, A] =
new StatelessECAuthenticator[F, I, V, A](settings.expiryDuration, settings.maxIdle) {
def withKey(newKey: SecretKey[A]): StatelessECAuthenticator[F, I, V, A] =
stateless[F, I, V, A](
settings,
identityStore,
newKey
)
def withSettings(settings: TSecCookieSettings): StatelessECAuthenticator[F, I, V, A] =
stateless[F, I, V, A](
settings,
identityStore,
key
)
def withIdentityStore(newStore: IdentityStore[F, I, V]): StatelessECAuthenticator[F, I, V, A] =
stateless[F, I, V, A](
settings,
newStore,
key
)
private def generateAAD(message: String) =
AAD(SHA1.hashPure((message + Instant.now.toEpochMilli).utf8Bytes))
/** Inside a stateless cookie, we do not have a backing store, thus checking with the AuthEncryptedCookie is
* useless
*
*/
private def validateAndRefresh(
internal: AuthEncryptedCookie[A, I],
now: Instant
): OptionT[F, AuthEncryptedCookie[A, I]] =
if (isExpired(internal, now, settings.maxIdle)) OptionT.liftF(refresh(internal)) else OptionT.none
def extractRawOption(request: Request[F]): Option[String] =
unliftedCookieFromRequest(settings.cookieName, request).map(_.content)
/** Unfortunately, parseRaw is not enough **/
def parseRaw(raw: String, request: Request[F]): OptionT[F, SecuredRequest[F, V, AuthEncryptedCookie[A, I]]] =
(for {
now <- OptionT.liftF(F.delay(Instant.now()))
rawCookie <- cookieFromRequest[F](settings.cookieName, request)
coerced = AEADCookie[A](rawCookie.content)
contentRaw <- OptionT.liftF(AEADCookieEncryptor.retrieveFromSigned[F, A](coerced, key))
internal <- OptionT.liftF(F.fromEither(decode[AuthEncryptedCookie.Internal[I]](contentRaw)))
authed = AuthEncryptedCookie.build[A, I](internal, coerced, settings)
refreshed <- validateAndRefresh(authed, now)
identity <- identityStore.get(authed.identity)
} yield SecuredRequest(request, identity, refreshed))
.handleErrorWith(_ => OptionT.none)
/** Create our cookie
* In the case of encrypted cookies, we cannot trust the client to avoid tampering.
* Thus, we unfortunately have to encrypt the expiry data as well
*
*/
def create(body: I): F[AuthEncryptedCookie[A, I]] =
for {
cookieId <- F.delay(UUID.randomUUID())
now <- F.delay(Instant.now())
expiry = now.plusSeconds(settings.expiryDuration.toSeconds)
lastTouched = settings.maxIdle.map(_ => now)
messageBody = AuthEncryptedCookie.Internal(cookieId, body, expiry, lastTouched).asJson.pretty(JWTPrinter)
encrypted <- AEADCookieEncryptor.signAndEncrypt[F, A](messageBody, generateAAD(messageBody), key)
} yield AuthEncryptedCookie.build[A, I](cookieId, encrypted, body, expiry, lastTouched, settings)
/** Reader's note:
* Since this is a stateless authenticator, update must carry information about expiry and sliding window info
* since this is something that could be manipulated by an attacker.
*
* We sincerely don't want this, thus renew and refresh also rely on update.
* */
def update(authenticator: AuthEncryptedCookie[A, I]): F[AuthEncryptedCookie[A, I]] = {
val serialized = AuthEncryptedCookie
.Internal(authenticator.id, authenticator.identity, authenticator.expiry, authenticator.lastTouched)
.asJson
.pretty(JWTPrinter)
for {
encrypted <- AEADCookieEncryptor.signAndEncrypt[F, A](serialized, generateAAD(serialized), key)
} yield authenticator.copy[A, I](content = encrypted)
}
/** In this case, since we have no backing store, we can invalidate the token.
* This doesn't work entirely if the person _doesn't_ set the token afterwards.
*
* @param authenticator
* @return
*/
def discard(authenticator: AuthEncryptedCookie[A, I]): F[AuthEncryptedCookie[A, I]] =
F.delay(authenticator.copy[A, I](content = AEADCookie[A]("invalid"), expiry = Instant.EPOCH))
/** Renew all of our cookie's possible expirations.
* If there is a timeout, refresh that as well. otherwise, simply update the expiry.
*
*/
def renew(authenticator: AuthEncryptedCookie[A, I]): F[AuthEncryptedCookie[A, I]] =
F.delay(Instant.now()).flatMap { now =>
settings.maxIdle match {
case Some(idleTime) =>
update(
authenticator.copy[A, I](
lastTouched = Some(now),
expiry = now.plusSeconds(settings.expiryDuration.toSeconds)
)
)
case None =>
update(
authenticator.copy[A, I](
expiry = now.plusSeconds(settings.expiryDuration.toSeconds)
)
)
}
}
def refresh(authenticator: AuthEncryptedCookie[A, I]): F[AuthEncryptedCookie[A, I]] =
settings.maxIdle match {
case Some(_) =>
F.delay(
authenticator.copy[A, I](
lastTouched = Some(Instant.now())
)
)
.flatMap(update)
case None =>
F.pure(authenticator)
}
def embed(response: Response[F], authenticator: AuthEncryptedCookie[A, I]): Response[F] =
response.addCookie(authenticator.toCookie)
def afterBlock(response: Response[F], authenticator: AuthEncryptedCookie[A, I]): OptionT[F, Response[F]] =
settings.maxIdle match {
case Some(_) =>
OptionT.pure[F](response.addCookie(authenticator.toCookie))
case None =>
OptionT.pure[F](response)
}
}
}