Skip to content

Commit

Permalink
Apply scalafmt
Browse files Browse the repository at this point in the history
  • Loading branch information
ScalaWilliam committed Aug 17, 2021
1 parent 7d4ef49 commit 21113f9
Show file tree
Hide file tree
Showing 11 changed files with 51 additions and 41 deletions.
5 changes: 3 additions & 2 deletions common/src/main/scala/tsec/common/ManagedRandom.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ trait ManagedRandom {
* [[https://tersesystems.com/2015/12/17/the-right-way-to-use-securerandom/]]
*/
private[tsec] val cachedRand: SecureRandom = {
val r = SecureRandom.getInstance(if ( scala.util.Properties.isWin ) ManagedRandom.WinRandom else ManagedRandom.UnixURandom)
val r =
SecureRandom.getInstance(if (scala.util.Properties.isWin) ManagedRandom.WinRandom else ManagedRandom.UnixURandom)
r.nextBytes(new Array[Byte](20)) //Force reseed
r
}
Expand All @@ -20,6 +21,6 @@ trait ManagedRandom {
}

object ManagedRandom {
private[ManagedRandom] val WinRandom = "Windows-PRNG"
private[ManagedRandom] val WinRandom = "Windows-PRNG"
private[ManagedRandom] val UnixURandom = "NativePRNGNonBlocking"
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import cats.syntax.all._
import io.circe.Json
import io.circe.parser.decode
import io.circe.syntax._
import io.circe.{Encoder, Decoder}
import io.circe.{Decoder, Encoder}
import org.http4s._
import tsec.cipher.symmetric.{IvGen, _}
import tsec.cipher.symmetric.jca._
Expand Down Expand Up @@ -99,8 +99,8 @@ object AuthEncryptedCookie {

object Internal {
import io.circe.generic.semiauto._
implicit def decoder[T : Decoder]: Decoder[Internal[T]] = deriveDecoder[Internal[T]]
implicit def encoder[T : Encoder]: Encoder[Internal[T]] = deriveEncoder[Internal[T]]
implicit def decoder[T: Decoder]: Decoder[Internal[T]] = deriveDecoder[Internal[T]]
implicit def encoder[T: Encoder]: Encoder[Internal[T]] = deriveEncoder[Internal[T]]
}

def build[A, Id](
Expand Down Expand Up @@ -418,10 +418,10 @@ object EncryptedCookieAuthenticator {
for {
cookieId <- F.delay(UUID.randomUUID())
now <- F.delay(Instant.now())
expiry = now.plusSeconds(settings.expiryDuration.toSeconds)
lastTouched = settings.maxIdle.map(_ => now)
messageBodyJson = AuthEncryptedCookie.Internal(cookieId, body, expiry, lastTouched).asJson : Json
messageBody = messageBodyJson.printWith(JWTPrinter)
expiry = now.plusSeconds(settings.expiryDuration.toSeconds)
lastTouched = settings.maxIdle.map(_ => now)
messageBodyJson = AuthEncryptedCookie.Internal(cookieId, body, expiry, lastTouched).asJson: Json
messageBody = messageBodyJson.printWith(JWTPrinter)
encrypted <- AEADCookieEncryptor.signAndEncrypt[F, A](messageBody, generateAAD(messageBody), key)
} yield AuthEncryptedCookie.build[A, I](cookieId, encrypted, body, expiry, lastTouched, settings)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import cats.data.OptionT
import cats.effect.Sync
import cats.instances.string._
import cats.syntax.all._
import io.circe.{Encoder, Decoder}
import org.http4s.{Request, Header, ResponseCookie, Response, HttpDate}
import io.circe.{Decoder, Encoder}
import org.http4s.{Header, HttpDate, Request, Response, ResponseCookie}
import org.typelevel.ci.CIString
import tsec.authentication.internal._
import tsec.common._
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,6 @@ package object authentication {
)
}


private[tsec] implicit val InstantLongEncoder: Encoder[Instant] = new Encoder[Instant] {
def apply(a: Instant): Json = Json.fromLong(a.getEpochSecond)
}
Expand Down
4 changes: 2 additions & 2 deletions tsec-http4s/src/main/scala/tsec/authorization/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import scala.reflect.ClassTag
package object authorization {

type AuthGroup[G] = AuthGroup.Type[G]

object AuthGroup {
type Type[A] <: Array[A]

Expand All @@ -23,7 +23,7 @@ package object authorization {
}
def fromSeq[G: ClassTag](seq: Seq[G]): AuthGroup[G] = unsafeApply[G](seq.distinct.toArray)
def unsafeFromSeq[G: ClassTag](seq: Seq[G]): AuthGroup[G] = unsafeApply(seq.toArray)
def empty[G: ClassTag]: AuthGroup[G] = unsafeApply[G](Array.empty[G])
def empty[G: ClassTag]: AuthGroup[G] = unsafeApply[G](Array.empty[G])
}

/** A simple typeclass that allows us to propagate information that is required for authorization */
Expand Down
12 changes: 6 additions & 6 deletions tsec-http4s/src/main/scala/tsec/csrf/TSecCSRF.scala
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,12 @@ final class TSecCSRF[F[_], A] private[tsec] (

private[tsec] def checkCSRF(r: Request[F], service: HttpRoutes[F]): F[Response[F]] =
(for {
c1 <- cookieFromRequest[F](cookieName, r)
c2 <- OptionT.fromOption[F](r.headers.get(org.typelevel.ci.CIString(headerName)).map(_.head.value))
raw1 <- extractRaw(CSRFToken(c1.content))
raw2 <- extractRaw(CSRFToken(c2))
res: Response[F] <- if (isEqual(raw1, raw2)) service(r) else OptionT.none
newToken <- OptionT.liftF(signToken(raw1)) //Generate a new token to guard against BREACH.
c1 <- cookieFromRequest[F](cookieName, r)
c2 <- OptionT.fromOption[F](r.headers.get(org.typelevel.ci.CIString(headerName)).map(_.head.value))
raw1 <- extractRaw(CSRFToken(c1.content))
raw2 <- extractRaw(CSRFToken(c2))
res: Response[F] <- if (isEqual(raw1, raw2)) service(r) else OptionT.none
newToken <- OptionT.liftF(signToken(raw1)) //Generate a new token to guard against BREACH.
} yield res.addCookie(ResponseCookie(name = cookieName, content = newToken)))
.getOrElse(Response[F](Status.Unauthorized))

Expand Down
2 changes: 1 addition & 1 deletion tsec-http4s/src/main/scala/tsec/csrf/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ package object csrf {
object CSRFToken extends ManagedRandom {
type Token <: String

def apply(s: String): CSRFToken = s.asInstanceOf[CSRFToken]
def apply(s: String): CSRFToken = s.asInstanceOf[CSRFToken]
def subst[F[_]](value: F[String]): F[CSRFToken] = value.asInstanceOf[F[CSRFToken]]

def generateHexBase(tokenLength: Int = 32): String = {
Expand Down
24 changes: 17 additions & 7 deletions tsec-http4s/src/test/scala/tsec/authentication/CSRFSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,11 @@ class CSRFSpec extends TestSpec {

val (origToken, origRaw, response, newToken, newRaw) =
(for {
t1 <- OptionT.liftF[IO, CSRFToken](tsecCSRF.generateToken)
raw1 <- tsecCSRF.extractRaw(t1)
resp <- tsecCSRF.validate()(dummyService)(passThroughRequest.addCookie(RequestCookie(tsecCSRF.cookieName, t1)))
t1 <- OptionT.liftF[IO, CSRFToken](tsecCSRF.generateToken)
raw1 <- tsecCSRF.extractRaw(t1)
resp <- tsecCSRF.validate()(dummyService)(
passThroughRequest.addCookie(RequestCookie(tsecCSRF.cookieName, t1))
)
cookie <- OptionT.fromOption[IO](resp.cookies.find(_.name == tsecCSRF.cookieName))
raw2 <- tsecCSRF.extractRaw(CSRFToken(cookie.content))
} yield (t1, raw1, resp, CSRFToken(cookie.content), raw2))
Expand All @@ -90,7 +92,9 @@ class CSRFSpec extends TestSpec {
(for {
token <- OptionT.liftF(tsecCSRF.generateToken)
res <- tsecCSRF.validate()(dummyService)(
dummyRequest.withHeaders(Headers.apply(Header.Raw(CIString(tsecCSRF.headerName), token))).addCookie(tsecCSRF.cookieName, token)
dummyRequest
.withHeaders(Headers.apply(Header.Raw(CIString(tsecCSRF.headerName), token)))
.addCookie(tsecCSRF.cookieName, token)
)
} yield res).getOrElse(orElse).unsafeRunSync().status mustBe Status.Ok
}
Expand Down Expand Up @@ -124,7 +128,9 @@ class CSRFSpec extends TestSpec {
token1 <- OptionT.liftF(tsecCSRF.generateToken)
token2 <- OptionT.liftF(tsecCSRF.generateToken)
res <- tsecCSRF.validate()(dummyService)(
dummyRequest.withHeaders(Headers.apply(Header.Raw(CIString(tsecCSRF.headerName), token1))).addCookie(tsecCSRF.cookieName, token2)
dummyRequest
.withHeaders(Headers.apply(Header.Raw(CIString(tsecCSRF.headerName), token1)))
.addCookie(tsecCSRF.cookieName, token2)
)
} yield res).getOrElse(orElse).unsafeRunSync().status mustBe Status.Unauthorized
}
Expand All @@ -134,7 +140,9 @@ class CSRFSpec extends TestSpec {
token <- OptionT.liftF(tsecCSRF.generateToken)
raw1 <- tsecCSRF.extractRaw(token)
res <- tsecCSRF.validate()(dummyService)(
dummyRequest.withHeaders(Headers.apply(Header.Raw(CIString(tsecCSRF.headerName), token))).addCookie(tsecCSRF.cookieName, token)
dummyRequest
.withHeaders(Headers.apply(Header.Raw(CIString(tsecCSRF.headerName), token)))
.addCookie(tsecCSRF.cookieName, token)
)
r <- OptionT.fromOption[IO](res.cookies.find(_.name == tsecCSRF.cookieName).map(_.content))
raw2 <- tsecCSRF.extractRaw(CSRFToken(r))
Expand All @@ -146,7 +154,9 @@ class CSRFSpec extends TestSpec {
token1 <- OptionT.liftF(tsecCSRF.generateToken)
token2 <- OptionT.liftF(tsecCSRF.generateToken)
res <- tsecCSRF.validate()(dummyService)(
dummyRequest.withHeaders(Headers.apply(Header.Raw(CIString(tsecCSRF.headerName), token1))).addCookie(tsecCSRF.cookieName, token2)
dummyRequest
.withHeaders(Headers.apply(Header.Raw(CIString(tsecCSRF.headerName), token1)))
.addCookie(tsecCSRF.cookieName, token2)
)
} yield res).getOrElse(Response.notFound).unsafeRunSync()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import cats.syntax.either._
import io.circe.generic.auto._
import io.circe.parser.decode
import org.http4s.headers.`Set-Cookie`
import org.http4s.{Response, RequestCookie, Request}
import org.http4s.{Request, RequestCookie, Response}
import tsec.cipher.symmetric.jca._
import tsec.cookies.{AEADCookieEncryptor, AEADCookie}
import tsec.cookies.{AEADCookie, AEADCookieEncryptor}
import tsec.keygen.symmetric.IdKeyGen

import scala.concurrent.duration._
Expand Down Expand Up @@ -39,7 +39,7 @@ class EncryptedCookieAuthenticatorSpec extends RequestAuthenticatorSpec {
store: BackingStore[IO, UUID, AuthEncryptedCookie[A, Int]]
): AuthSpecTester[AuthEncryptedCookie[A, Int]] = {
implicit val instance: AADEncryptor[IO, A, SecretKey] = cipherAPI.genEncryptor[IO]
implicit val stategy: IvGen[IO, A] = cipherAPI.defaultIvStrategy[IO]
implicit val stategy: IvGen[IO, A] = cipherAPI.defaultIvStrategy[IO]

val dummyStore = dummyBackingStore[IO, Int, DummyUser](_.id)
val authenticator = EncryptedCookieAuthenticator.withBackingStore[IO, Int, DummyUser, A](
Expand Down Expand Up @@ -84,7 +84,7 @@ class EncryptedCookieAuthenticatorSpec extends RequestAuthenticatorSpec {
idKeyGen: IdKeyGen[A, SecretKey]
): AuthSpecTester[AuthEncryptedCookie[A, Int]] = {
implicit val instance: AADEncryptor[IO, A, SecretKey] = cipherAPI.genEncryptor[IO]
implicit val stategy: IvGen[IO, A] = cipherAPI.defaultIvStrategy[IO]
implicit val stategy: IvGen[IO, A] = cipherAPI.defaultIvStrategy[IO]

val dummyStore = dummyBackingStore[IO, Int, DummyUser](_.id)
val secretKey = cipherAPI.unsafeGenerateKey
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,12 @@ class EncryptedCookieAuthenticatorTests extends EncryptedCookieAuthenticatorSpec
} yield List(cookie, update, renew, refresh, expire)

program.unsafeRunSync().forall { aec =>

settings.cookieName === aec.name &&
settings.secure === aec.secure &&
settings.httpOnly === aec.httpOnly &&
settings.domain === aec.domain &&
settings.path === aec.path &&
settings.extension === aec.extension
settings.cookieName === aec.name &&
settings.secure === aec.secure &&
settings.httpOnly === aec.httpOnly &&
settings.domain === aec.domain &&
settings.path === aec.path &&
settings.extension === aec.extension

} mustBe true

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ import cats.effect.unsafe.implicits.global

class SignedCookieAuthenticatorTests extends RequestAuthenticatorSpec {

private val cookieName = "hi"
implicit def cookieBackingStore[A]: BackingStore[IO, UUID, AuthenticatedCookie[A, Int]] = dummyBackingStore[IO, UUID, AuthenticatedCookie[A, Int]](_.id)
private val cookieName = "hi"
implicit def cookieBackingStore[A]: BackingStore[IO, UUID, AuthenticatedCookie[A, Int]] =
dummyBackingStore[IO, UUID, AuthenticatedCookie[A, Int]](_.id)

def genAuthenticator[A](
implicit keyGenerator: IdKeyGen[A, MacSigningKey],
Expand Down

0 comments on commit 21113f9

Please sign in to comment.