Skip to content

Commit

Permalink
Add xfcc header auth (#77)
Browse files Browse the repository at this point in the history
  • Loading branch information
garden-of-delete authored Nov 22, 2024
1 parent 86e9e83 commit 30c07cd
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 15 deletions.
32 changes: 20 additions & 12 deletions acdc-ws/app/utils/Authorization.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,26 @@ class Authorization(private var authorizationSettings: AuthorizationSettings) {
import Authorization._

def getRoles(request: Request[_]): List[String] = {
authorizationSettings.authEnabled match {
case true => getKeyRoles(request.headers.get(authorizationSettings.authHeader))
case false => List(Admin)
(authorizationSettings.apiKeyAuthEnabled, authorizationSettings.xfccKeyAuthEnabled) match {
case (true, true) =>
if (getKeyRoles(request.headers.get(authorizationSettings.apiKeyAuthHeader)) ==
getXfccRoles(request.headers.get(authorizationSettings.xfccAuthHeader))) {
List(Admin)
} else {
List.empty
}
case (true, false) => getKeyRoles(request.headers.get(authorizationSettings.apiKeyAuthHeader))
case (false, true) => getXfccRoles(request.headers.get(authorizationSettings.xfccAuthHeader))
case (false, false) => List(Admin)
}
}

private def getXfccRoles(key: Option[String]) = {
key match {
case Some(xfcc) =>
if (xfcc.contains(authorizationSettings.xfccMustContain)) { List(Admin) }
else { List.empty }
case None => List.empty
}
}

Expand All @@ -34,17 +51,8 @@ class Authorization(private var authorizationSettings: AuthorizationSettings) {
}
}

def checkAuthorization(request: Request[_]): Boolean =
request.headers
.get(authorizationSettings.authHeader)
.map(validateKey)
.getOrElse(!authorizationSettings.authEnabled)

def refreshDelay: Option[FiniteDuration] = authorizationSettings.ttl.map(_.second)

private def validateKey(key: String): Boolean =
authorizationSettings.keyRoles.contains(convertToSha256(key))

def reloadSettings(): this.type = {
ConfigFactory.invalidateCaches()
authorizationSettings = AuthorizationSettings()
Expand Down
10 changes: 8 additions & 2 deletions acdc-ws/app/utils/AuthorizationSettings.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,15 @@ import com.typesafe.config.{Config, ConfigFactory, ConfigList}

class AuthorizationSettings private (config: Config) {

def authHeader: String = config.getString(s"header-name")
def apiKeyAuthHeader: String = config.getString(s"header-name")

def authEnabled: Boolean = config.getBoolean(s"enabled")
def apiKeyAuthEnabled: Boolean = config.getBoolean("enabled")

def xfccAuthHeader: String = config.getString("xfcc.header-name")

def xfccKeyAuthEnabled: Boolean = config.getBoolean("xfcc.enabled")

def xfccMustContain: String = config.getString("xfcc.must-contain")

def keyRoles: Map[String, List[String]] = {
val userRoles = for {
Expand Down
6 changes: 6 additions & 0 deletions acdc-ws/conf/application.conf
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ acdc.auth = {
user = [ ${?MCE_ENV_X_API_USER1} , ${?MCE_ENV_X_API_USER2} ]
admin = [ ${?MCE_ENV_X_API_ADMIN1} , ${?MCE_ENV_X_API_ADMIN2} ]
}
xfcc = {
enabled = false
header-name = ${?XFCC_HEADER_NAME}
must-contain = ${?XFCC_MUST_CONTAIN}
}

# referesh settings every x seconds, setting it to null will make it never refresh. Note also
# it only works if auth is specified as an external source config, setting it along with play's
# setting will prevent it from being reloaded, and the reload/cache is handled at playframework
Expand Down
2 changes: 1 addition & 1 deletion version.sbt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
ThisBuild / version := "0.10.1"
ThisBuild / version := "0.11.0"

0 comments on commit 30c07cd

Please sign in to comment.