Skip to content

Commit 30c07cd

Browse files
Add xfcc header auth (#77)
1 parent 86e9e83 commit 30c07cd

File tree

4 files changed

+35
-15
lines changed

4 files changed

+35
-15
lines changed

acdc-ws/app/utils/Authorization.scala

+20-12
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,26 @@ class Authorization(private var authorizationSettings: AuthorizationSettings) {
2121
import Authorization._
2222

2323
def getRoles(request: Request[_]): List[String] = {
24-
authorizationSettings.authEnabled match {
25-
case true => getKeyRoles(request.headers.get(authorizationSettings.authHeader))
26-
case false => List(Admin)
24+
(authorizationSettings.apiKeyAuthEnabled, authorizationSettings.xfccKeyAuthEnabled) match {
25+
case (true, true) =>
26+
if (getKeyRoles(request.headers.get(authorizationSettings.apiKeyAuthHeader)) ==
27+
getXfccRoles(request.headers.get(authorizationSettings.xfccAuthHeader))) {
28+
List(Admin)
29+
} else {
30+
List.empty
31+
}
32+
case (true, false) => getKeyRoles(request.headers.get(authorizationSettings.apiKeyAuthHeader))
33+
case (false, true) => getXfccRoles(request.headers.get(authorizationSettings.xfccAuthHeader))
34+
case (false, false) => List(Admin)
35+
}
36+
}
37+
38+
private def getXfccRoles(key: Option[String]) = {
39+
key match {
40+
case Some(xfcc) =>
41+
if (xfcc.contains(authorizationSettings.xfccMustContain)) { List(Admin) }
42+
else { List.empty }
43+
case None => List.empty
2744
}
2845
}
2946

@@ -34,17 +51,8 @@ class Authorization(private var authorizationSettings: AuthorizationSettings) {
3451
}
3552
}
3653

37-
def checkAuthorization(request: Request[_]): Boolean =
38-
request.headers
39-
.get(authorizationSettings.authHeader)
40-
.map(validateKey)
41-
.getOrElse(!authorizationSettings.authEnabled)
42-
4354
def refreshDelay: Option[FiniteDuration] = authorizationSettings.ttl.map(_.second)
4455

45-
private def validateKey(key: String): Boolean =
46-
authorizationSettings.keyRoles.contains(convertToSha256(key))
47-
4856
def reloadSettings(): this.type = {
4957
ConfigFactory.invalidateCaches()
5058
authorizationSettings = AuthorizationSettings()

acdc-ws/app/utils/AuthorizationSettings.scala

+8-2
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,15 @@ import com.typesafe.config.{Config, ConfigFactory, ConfigList}
1717

1818
class AuthorizationSettings private (config: Config) {
1919

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

22-
def authEnabled: Boolean = config.getBoolean(s"enabled")
22+
def apiKeyAuthEnabled: Boolean = config.getBoolean("enabled")
23+
24+
def xfccAuthHeader: String = config.getString("xfcc.header-name")
25+
26+
def xfccKeyAuthEnabled: Boolean = config.getBoolean("xfcc.enabled")
27+
28+
def xfccMustContain: String = config.getString("xfcc.must-contain")
2329

2430
def keyRoles: Map[String, List[String]] = {
2531
val userRoles = for {

acdc-ws/conf/application.conf

+6
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ acdc.auth = {
2222
user = [ ${?MCE_ENV_X_API_USER1} , ${?MCE_ENV_X_API_USER2} ]
2323
admin = [ ${?MCE_ENV_X_API_ADMIN1} , ${?MCE_ENV_X_API_ADMIN2} ]
2424
}
25+
xfcc = {
26+
enabled = false
27+
header-name = ${?XFCC_HEADER_NAME}
28+
must-contain = ${?XFCC_MUST_CONTAIN}
29+
}
30+
2531
# referesh settings every x seconds, setting it to null will make it never refresh. Note also
2632
# it only works if auth is specified as an external source config, setting it along with play's
2733
# setting will prevent it from being reloaded, and the reload/cache is handled at playframework

version.sbt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
ThisBuild / version := "0.10.1"
1+
ThisBuild / version := "0.11.0"

0 commit comments

Comments
 (0)