simplify oauth Protocol.Secret

pull/9281/head
Niklas Fiekas 2021-06-27 20:15:38 +02:00
parent e1ff8a1504
commit 48f7111448
6 changed files with 13 additions and 27 deletions

View File

@ -202,7 +202,7 @@ final class Challenge(
Action.async { req =>
import cats.implicits._
val scopes = List(OAuthScope.Challenge.Write)
(get("token1", req) map AccessToken.Id, get("token2", req) map AccessToken.Id).mapN {
(get("token1", req) map AccessToken.Id.apply, get("token2", req) map AccessToken.Id.apply).mapN {
env.oAuth.server.authBoth(scopes)
} ?? {
_ flatMap {

View File

@ -23,14 +23,11 @@ case class AccessToken(
object AccessToken {
val idSize = 16
case class Id(value: String) extends AnyVal {
def isPersonal = value.lengthIs == idSize
case class Id(value: String) extends AnyVal
object Id {
def random() = Id(s"lio_${SecureRandom.nextString(32)}")
}
def makeId = Id(SecureRandom nextString idSize)
case class ForAuth(userId: User.ID, scopes: List[OAuthScope])
case class WithApp(token: AccessToken, app: OAuthApp)

View File

@ -15,7 +15,7 @@ final class AccessTokenApi(colls: OauthColls)(implicit ec: scala.concurrent.Exec
def create(granted: AccessTokenRequest.Granted): Fu[AccessToken] = {
val token = AccessToken(
id = AccessToken.Id(Protocol.Secret.random("lio_").value),
id = AccessToken.Id.random(),
publicId = BSONObjectID.generate(),
clientId = PersonalToken.clientId, // TODO
userId = granted.userId,

View File

@ -13,7 +13,7 @@ final class AuthorizationApi(val coll: Coll)(implicit ec: scala.concurrent.Execu
val code = Protocol.AuthorizationCode.random()
coll.insert.one(
PendingAuthorizationBSONHandler write PendingAuthorization(
code.secret.hashed,
code.hashed,
request.clientId,
request.user,
request.redirectUri,
@ -27,7 +27,7 @@ final class AuthorizationApi(val coll: Coll)(implicit ec: scala.concurrent.Execu
def consume(
request: AccessTokenRequest.Prepared
): Fu[Validated[Protocol.Error, AccessTokenRequest.Granted]] =
coll.findAndModify($doc(F.hashedCode -> request.code.secret.hashed), coll.removeModifier) map {
coll.findAndModify($doc(F.hashedCode -> request.code.hashed), coll.removeModifier) map {
_.result[PendingAuthorization]
.toValid(Protocol.Error.AuthorizationCodeInvalid)
.ensure(Protocol.Error.AuthorizationCodeExpired)(_.expires.isAfter(DateTime.now()))

View File

@ -29,7 +29,7 @@ object OAuthForm {
) {
def make(user: lila.user.User) =
AccessToken(
id = AccessToken.makeId,
id = AccessToken.Id.random(),
publicId = BSONObjectID.generate(),
clientId = PersonalToken.clientId,
userId = user.id,

View File

@ -10,23 +10,12 @@ import io.lemonlabs.uri.AbsoluteUrl
import lila.common.SecureRandom
object Protocol {
case class Secret(value: String) {
def hashed: String = Algo.sha256(value).hex
override def toString = "Secret(***)"
override def equals(other: Any) = other match {
case other: Secret => hashed == other.hashed
case _ => false
}
override def hashCode = hashed.hashCode()
case class AuthorizationCode(secret: String) extends AnyVal {
def hashed = Algo.sha256(secret).hex
override def toString = "AuthorizationCode(***)"
}
object Secret {
def random(prefix: String) = Secret(s"$prefix${SecureRandom.nextString(32)}")
}
case class AuthorizationCode(secret: Secret) extends AnyVal
object AuthorizationCode {
def apply(value: String): AuthorizationCode = AuthorizationCode(Secret(value))
def random() = AuthorizationCode(Secret.random("liu_"))
def random() = AuthorizationCode(s"liu_${SecureRandom.nextString(32)}")
}
case class ClientId(value: String) extends AnyVal
@ -91,7 +80,7 @@ object Protocol {
def code(code: AuthorizationCode, state: Option[State]): String = value
.withQueryString(
"code" -> Some(code.secret.value),
"code" -> Some(code.secret),
"state" -> state.map(_.value)
)
.toString