forked from lichess-org/lila
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
57 additions
and
5 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package lila.security | ||
|
||
import org.joda.time.DateTime | ||
import play.api.mvc.RequestHeader | ||
|
||
import lila.common.Iso | ||
import lila.db.dsl._ | ||
import lila.user.{ User, UserRepo } | ||
|
||
object OAuth { | ||
case class AccessTokenId(value: String) extends AnyVal | ||
} | ||
|
||
private final class OAuthServer( | ||
tokenColl: Coll, | ||
clientColl: Coll | ||
) { | ||
|
||
import OAuth._ | ||
|
||
private implicit val tokenIdHandler = stringAnyValHandler[AccessTokenId](_.value, AccessTokenId.apply) | ||
|
||
def activeUser(req: RequestHeader): Fu[Option[User]] = { | ||
req.headers get "Authorization" map (_.split(" ", 2)) | ||
} ?? { | ||
case Array("Bearer", accessToken) => activeUser(AccessTokenId(accessToken)) | ||
case _ => fuccess(none) | ||
} | ||
|
||
def activeUser(token: AccessTokenId): Fu[Option[User]] = | ||
tokenColl.primitiveOne[User.ID]($doc( | ||
"access_token_id" -> token, | ||
"expireDate" $gt DateTime.now | ||
), "user_id") flatMap { _ ?? UserRepo.byId } | ||
} |
This file contains 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