Skip to content

Instantly share code, notes, and snippets.

View bandrzejczak's full-sized avatar

Bartek Andrzejczak bandrzejczak

View GitHub Profile
class Impersonator(config: KeycloakConfig)
(implicit sttpBackend: SttpBackend[Future, Nothing], ec: ExecutionContext)
case class KeycloakConfig(
authServerUrl: String,
realm: String,
impersonatorUsername: String,
impersonatorPassword: String,
clientId: String,
redirectUri: String
)
import scalacache._
import scala.concurrent.ExecutionContext.Implicits.global
import scalacache.modes.scalaFuture._
implicit val cache: Cache[Token] = ???
def obtainTokenFor(userId: String): Future[(Token, Duration)] = ???
val userId = "123"
def cachingWithTTL[T, M[_]](keyParts: Any*)(value: => M[(T, Duration)])
(implicit cache: Cache[T], mode: Mode[M], flags: Flags): M[T] = {
import mode._
M.flatMap(get(keyParts:_*)) {
case Some(found) => M.pure(found)
case None =>
M.flatMap(value) {
case (v, ttl) =>
M.map(put(keyParts:_*)(v, Some(ttl)))(_ => v)
}
$rootScope.authData = {};
$http.jsonp("http://localhost:9000/test?callback=JSON_CALLBACK")
.success(function (response) {
$rootScope.authData.token = response.token;
$rootScope.authData.username = response.username;
});
$rootScope.userLogout = function () {
$cookies.remove('X-Authorization-Token');
$interval.cancel(updateTokenInterval);
keycloak.logout();
};
var updateTokenInterval = $interval(function () {
// refresh token if it's valid for less then 15 minutes
keycloak.updateToken(15)
.success(function (refreshed) {
if (refreshed) {
$cookies.put('X-Authorization-Token', keycloak.token);
}
});
}, 10000);
$cookies.put('X-Authorization-Token', keycloak.token);
authorized { token =>
// the part of the API that we need to be checked for authorization
}
implicit val system = ActorSystem("sso-system")
implicit val actorMaterializer = ActorMaterializer()
val oauth2 = new OAuth2Authorization(
Logging(system, getClass),
new KeycloakTokenVerifier(
KeycloakDeploymentBuilder.build(
getClass.getResourceAsStream("/keycloak.json")
)
)