Skip to content

Instantly share code, notes, and snippets.

@julien-truffaut
Last active May 10, 2021 11:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save julien-truffaut/73620c67135e3217a35317558f8e5e12 to your computer and use it in GitHub Desktop.
Save julien-truffaut/73620c67135e3217a35317558f8e5e12 to your computer and use it in GitHub Desktop.
Auth middleware issue
trait Auth {
def authenticate(token: Token): Future[User]
}
class OktaAuth(client: RestClient) extends Auth {
def authenticate(token: Token): Future[User] = ???
}
class MockAuth(users: Map[Token, User]) extends Auth {
def authenticate(token: Token): Future[User] = ???
}
class CachedAuth(cache: Cache, underlying: Auth) extends Auth {
def authenticate(token: Token): Future[User] = ???
}
object AuthMiddleware {
def checkUser(request: Request, auth: Auth, env: Env) =
(env, auth) match {
case (Prod, _: MockAuth) => InternalServerError()
case _ => // doesn't handle CachedAuth!
request.headers.get("Authorization") match {
case None => reject(Forbidden())
case Some(token) =>
auth.authenticate(token).map(user =>
provide(user)
)
}
}
}
{
auth : {
type : "Okta"
baseUrl : "https://okta.com/auth0"
api-key : "X28fd-OS0-8S"
}
}
{
auth : {
type : "Mock"
users : {
"bob" : "abcd"
"eda" : "xxxx"
}
}
}
{
auth : {
type : "Cached"
underlying : {
type : "Okta"
baseUrl : "https://okta.com/auth0"
api-key : "X28fd-OS0-8S"
},
cacheSize : 100
}
}
sealed trait Env {
object Env {
case object Local extends Env
case object UAT extends Env
ase object Prod extends Env
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment