|
| 1 | +package code.api.util |
| 2 | + |
| 3 | +import code.api.RequestHeader |
| 4 | +import com.openbankproject.commons.model.User |
| 5 | +import net.liftweb.common.{Box, Empty, Failure} |
| 6 | +import net.liftweb.http.provider.HTTPParam |
| 7 | + |
| 8 | +object BerlinGroupCheck { |
| 9 | + |
| 10 | + // Parse mandatory headers from a comma-separated string |
| 11 | + private val berlinGroupMandatoryHeaders: List[String] = APIUtil.getPropsValue("berlin_group_mandatory_headers", defaultValue = "X-Request-ID,PSU-IP-Address,PSU-Device-ID,PSU-Device-Name") |
| 12 | + .split(",") |
| 13 | + .map(_.trim.toLowerCase) |
| 14 | + .toList.filterNot(_.isEmpty) |
| 15 | + private val berlinGroupMandatoryHeaderConsent = APIUtil.getPropsValue("berlin_group_mandatory_header_consent", defaultValue = "TPP-Redirect-URL") |
| 16 | + .split(",") |
| 17 | + .map(_.trim.toLowerCase) |
| 18 | + .toList.filterNot(_.isEmpty) |
| 19 | + |
| 20 | + private def validateHeaders(verb: String, url: String, reqHeaders: List[HTTPParam], forwardResult: (Box[User], Option[CallContext])): (Box[User], Option[CallContext]) = { |
| 21 | + val headerMap = reqHeaders.map(h => h.name.toLowerCase -> h).toMap |
| 22 | + val missingHeaders = if(url.contains("berlin-group") && url.endsWith("/consent")) |
| 23 | + berlinGroupMandatoryHeaders.filterNot(headerMap.contains) |
| 24 | + else |
| 25 | + (berlinGroupMandatoryHeaders ++ berlinGroupMandatoryHeaderConsent).filterNot(headerMap.contains) |
| 26 | + |
| 27 | + if (missingHeaders.isEmpty) { |
| 28 | + forwardResult // All mandatory headers are present |
| 29 | + } else { |
| 30 | + (Failure(s"Missing mandatory headers: ${missingHeaders.mkString(", ")}"), forwardResult._2) |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + def validate(body: Box[String], verb: String, url: String, reqHeaders: List[HTTPParam], forwardResult: (Box[User], Option[CallContext])): (Box[User], Option[CallContext]) = { |
| 35 | + validateHeaders(verb, url, reqHeaders, forwardResult) match { |
| 36 | + case (user, _) if user.isDefined || user == Empty => // All good. Chain another check |
| 37 | + // Verify signed request (Berlin Group) |
| 38 | + BerlinGroupSigning.verifySignedRequest(body, verb, url, reqHeaders, forwardResult) |
| 39 | + case forwardError => // Forward error case |
| 40 | + forwardError |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | +} |
0 commit comments