Last active
May 10, 2021 11:29
-
-
Save julien-truffaut/73620c67135e3217a35317558f8e5e12 to your computer and use it in GitHub Desktop.
Auth middleware issue
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] = ??? | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
) | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
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 | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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