-
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.
keycloak compatible for SaaS and self-hosted (#14)
fix #3
- Loading branch information
Showing
8 changed files
with
130 additions
and
32 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
import http from "./http"; | ||
import {KeycloakConfig} from "keycloak-js"; | ||
|
||
type AuthTypeResponse = { type: 'ST' } | (KeycloakConfig & { type: 'Bearer' }) | ||
type AuthTypeResponse = { type: 'ST' } | (KeycloakConfig & { type: 'Bearer', saas: boolean }) | ||
export function getAppInfo() { | ||
return http.get<AuthTypeResponse>('/info') | ||
} |
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
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
35 changes: 35 additions & 0 deletions
35
backend/src/main/scala/com/timzaak/fornet/keycloak/KeycloakJWTSaaSAuthStrategy.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,35 @@ | ||
package com.timzaak.fornet.keycloak | ||
|
||
import org.scalatra.auth.ScentrySupport | ||
import org.scalatra.auth.strategy.BasicAuthSupport | ||
import com.typesafe.scalalogging.LazyLogging | ||
import org.keycloak.TokenVerifier | ||
import com.typesafe.scalalogging.Logger | ||
import very.util.keycloak.{ JWKTokenVerifier, KeycloakJWTAuthStrategy } | ||
import very.util.web.auth.AuthStrategy | ||
|
||
import scala.util.{ Failure, Success } | ||
|
||
class KeycloakJWTSaaSAuthStrategy( | ||
jwkTokenVerifier: JWKTokenVerifier, | ||
) extends AuthStrategy[String] | ||
with LazyLogging { | ||
|
||
def name: String = KeycloakJWTAuthStrategy.name | ||
|
||
def adminAuth(token: String): Option[String] = { | ||
jwkTokenVerifier.verify(token) match { | ||
case Success(accessToken) => | ||
Some(accessToken.getSubject) | ||
case Failure(exception) => | ||
logger.debug(s"bad token:$token", exception) | ||
None | ||
} | ||
} | ||
|
||
// SaaS do not support Client SSO Login | ||
def clientAuth(token: String): Option[String] = { | ||
None | ||
} | ||
|
||
} |
56 changes: 56 additions & 0 deletions
56
backend/src/main/scala/com/timzaak/fornet/keycloak/KeycloakJWTSaaSCompatAuthStrategy.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,56 @@ | ||
package com.timzaak.fornet.keycloak | ||
|
||
import org.scalatra.auth.ScentrySupport | ||
import org.scalatra.auth.strategy.BasicAuthSupport | ||
import com.typesafe.scalalogging.LazyLogging | ||
import org.keycloak.TokenVerifier | ||
import com.typesafe.scalalogging.Logger | ||
import very.util.keycloak.{ JWKTokenVerifier, KeycloakJWTAuthStrategy } | ||
import very.util.web.auth.AuthStrategy | ||
|
||
import scala.util.{ Failure, Success } | ||
|
||
class KeycloakJWTSaaSCompatAuthStrategy( | ||
jwkTokenVerifier: JWKTokenVerifier, | ||
adminRole: Option[String], | ||
clientRole: Option[String] | ||
) extends AuthStrategy[String] | ||
with LazyLogging { | ||
// JWT | ||
def name: String = KeycloakJWTAuthStrategy.name | ||
|
||
def adminAuth(token: String): Option[String] = { | ||
jwkTokenVerifier.verify(token) match { | ||
case Success(accessToken) => | ||
if (adminRole.fold(true)(role => accessToken.getRealmAccess.getRoles.contains(role))) { | ||
Some(adminRole.getOrElse("admin")) | ||
} else { | ||
logger.info( | ||
s"the user:${accessToken.getSubject} could not pass admin auth" | ||
) | ||
None | ||
} | ||
case Failure(exception) => | ||
logger.debug(s"bad token:$token", exception) | ||
None | ||
} | ||
} | ||
|
||
def clientAuth(token: String): Option[String] = { | ||
jwkTokenVerifier.verify(token) match { | ||
case Success(accessToken) => | ||
if (clientRole.fold(true)(role => accessToken.getRealmAccess.getRoles.contains(role))) { | ||
Some(accessToken.getSubject) | ||
} else { | ||
logger.info( | ||
s"the user:${accessToken.getSubject} could not pass client auth" | ||
) | ||
None | ||
} | ||
case Failure(exception) => | ||
logger.debug(s"bad token:$token", exception) | ||
None | ||
} | ||
} | ||
|
||
} |
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