-
Notifications
You must be signed in to change notification settings - Fork 105
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement akka-http-session RefreshTokenStorage backed by an actor (fix
#372)
- Loading branch information
1 parent
376bbea
commit 9bc90cb
Showing
5 changed files
with
86 additions
and
11 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
69 changes: 69 additions & 0 deletions
69
server/src/main/scala/com.olegych.scastie.web/oauth2/InMemoryRefreshTokenStorage.scala
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,69 @@ | ||
package com.olegych.scastie.web.oauth2 | ||
|
||
import com.softwaremill.session.{ | ||
RefreshTokenData, | ||
RefreshTokenStorage, | ||
RefreshTokenLookupResult | ||
} | ||
import akka.actor.{Actor, ActorSystem, Props} | ||
import akka.pattern.ask | ||
import akka.util.Timeout | ||
|
||
import scala.concurrent.Future | ||
import scala.concurrent.duration._ | ||
import scala.collection.mutable | ||
|
||
import java.util.UUID | ||
|
||
private[oauth2] case class SessionStorage(session: UUID, | ||
tokenHash: String, | ||
expires: Long) | ||
|
||
class ActorRefreshTokenStorage(system: ActorSystem) | ||
extends RefreshTokenStorage[UUID] { | ||
import system.dispatcher | ||
implicit private val timeout = Timeout(10.seconds) | ||
private val impl = system.actorOf(Props(new ActorRefreshTokenStorageImpl())) | ||
|
||
def lookup(selector: String): Future[Option[RefreshTokenLookupResult[UUID]]] = | ||
(impl ? Lookup(selector)).mapTo[Option[RefreshTokenLookupResult[UUID]]] | ||
|
||
def store(data: RefreshTokenData[UUID]): Future[Unit] = { | ||
impl ! Store(data) | ||
Future.successful(()) | ||
} | ||
def remove(selector: String): Future[Unit] = { | ||
impl ! Remove(selector) | ||
Future.successful(()) | ||
} | ||
def schedule[S](after: Duration)(op: => Future[S]): Unit = { | ||
after match { | ||
case finite: FiniteDuration => system.scheduler.scheduleOnce(finite)(op) | ||
case _: Duration.Infinite => () | ||
} | ||
} | ||
} | ||
|
||
private[oauth2] case class Lookup(selector: String) | ||
private[oauth2] case class Store(data: RefreshTokenData[UUID]) | ||
private[oauth2] case class Remove(selector: String) | ||
|
||
class ActorRefreshTokenStorageImpl() extends Actor { | ||
private val storage = mutable.Map[String, SessionStorage]() | ||
override def receive: Receive = { | ||
case Lookup(selector) => | ||
val lookupResult = | ||
storage | ||
.get(selector) | ||
.map( | ||
s => | ||
RefreshTokenLookupResult(s.tokenHash, s.expires, () => s.session) | ||
) | ||
sender ! lookupResult | ||
case Store(data) => | ||
storage.put(data.selector, | ||
SessionStorage(data.forSession, data.tokenHash, data.expires)) | ||
case Remove(selector) => | ||
storage.remove(selector) | ||
} | ||
} |
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