OBP-API/src/main/scala/code/api/util/APIUtil.scala

797 lines
32 KiB
Scala
Raw Normal View History

/**
2016-05-13 16:38:40 +00:00
* Open Bank Project - API
* Copyright (C) 2011-2016, TESOBE Ltd
2016-05-13 16:38:40 +00:00
**
*This program is free software: you can redistribute it and/or modify
*it under the terms of the GNU Affero General Public License as published by
*the Free Software Foundation, either version 3 of the License, or
*(at your option) any later version.
**
*This program is distributed in the hope that it will be useful,
*but WITHOUT ANY WARRANTY; without even the implied warranty of
*MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
*GNU Affero General Public License for more details.
**
*You should have received a copy of the GNU Affero General Public License
*along with this program. If not, see <http://www.gnu.org/licenses/>.
**
*Email: contact@tesobe.com
*TESOBE Ltd
2016-05-13 16:38:40 +00:00
*Osloerstrasse 16/17
*Berlin 13359, Germany
**
*This product includes software developed at
*TESOBE (http://www.tesobe.com/)
* by
*Simon Redfern : simon AT tesobe DOT com
*Stefan Bethge : stefan AT tesobe DOT com
*Everett Sochowski : everett AT tesobe DOT com
*Ayoub Benali: ayoub AT tesobe DOT com
*
*/
2014-09-10 15:13:33 +00:00
package code.api.util
2013-06-14 08:35:34 +00:00
import java.io.InputStream
2016-05-13 16:38:40 +00:00
import code.api.Constant._
import code.api.DirectLogin
import code.api.OAuthHandshake._
2013-06-14 08:35:34 +00:00
import code.api.v1_2.ErrorMessage
2016-07-20 08:40:12 +00:00
import code.customer.Customer
2016-06-17 09:19:05 +00:00
import code.entitlement.Entitlement
2014-09-10 15:13:33 +00:00
import code.metrics.APIMetrics
import code.model._
2016-05-13 16:38:40 +00:00
import dispatch.url
import net.liftweb.common.{Empty, _}
2014-09-10 15:13:33 +00:00
import net.liftweb.http.js.JE.JsRaw
import net.liftweb.http.js.JsExp
import net.liftweb.http._
import net.liftweb.json.JsonAST.JValue
2016-05-13 16:38:40 +00:00
import net.liftweb.json.{Extraction, parse}
import net.liftweb.mapper.By
2013-06-14 08:35:34 +00:00
import net.liftweb.util.Helpers._
import net.liftweb.util.{Helpers, Props, SecurityHelpers}
import scala.xml.{Elem, XML}
import scala.collection.mutable.ArrayBuffer
2016-05-13 16:38:40 +00:00
import scala.collection.JavaConverters._
import code.util.Helper.SILENCE_IS_GOLDEN
2016-03-02 12:51:13 +00:00
2016-02-21 10:55:02 +00:00
object ErrorMessages {
2016-02-21 09:50:53 +00:00
2016-03-05 07:04:41 +00:00
// Infrastructure / config messages
val HostnameNotSpecified = "OBP-00001: Hostname not specified. Could not get hostname from Props. Please edit your props file. Here are some example settings: hostname=http://127.0.0.1:8080 or hostname=https://www.example.com"
2016-02-21 09:50:53 +00:00
2016-03-05 07:04:41 +00:00
// General messages
val InvalidJsonFormat = "OBP-10001: Incorrect json format."
val InvalidNumber = "OBP-10002: Invalid Number. Could not convert value to a number."
val InvalidISOCurrencyCode = "OBP-10003: Invalid Currency Value. It should be three letters ISO Currency Code. "
2016-02-21 09:50:53 +00:00
// Authentication / Authorisation / User messages
val UserNotLoggedIn = "OBP-20001: User not logged in. Authentication is required!"
2016-02-21 09:50:53 +00:00
2016-03-14 09:14:42 +00:00
val DirectLoginMissingParameters = "OBP-20002: These DirectLogin parameters are missing: "
val DirectLoginInvalidToken = "OBP-20003: This DirectLogin token is invalid or expired: "
2016-03-16 04:15:00 +00:00
val InvalidLoginCredentials = "OBP-20004: Invalid login credentials. Check username/password."
2016-03-14 09:14:42 +00:00
2017-01-22 15:42:59 +00:00
val UserNotFoundById = "OBP-20005: User not found. Please specify a valid value for USER_ID."
val UserDoesNotHaveRole = "OBP-20006: User does not have a role "
2016-06-28 08:22:54 +00:00
val UserNotFoundByEmail = "OBP-20007: User not found by email."
2016-03-14 09:14:42 +00:00
val InvalidConsumerKey = "OBP-20008: Invalid Consumer Key."
val InvalidConsumerCredentials = "OBP-20009: Invalid consumer credentials"
val InsufficientAuthorisationToCreateBranch = "OBP-20010: Insufficient authorisation to Create Branch offered by the bank. The Request could not be created because you don't have access to CanCreateBranch."
2016-02-21 09:50:53 +00:00
val InvalidValueLength = "OBP-20010: Value too long"
val InvalidValueCharacters = "OBP-20011: Value contains invalid characters"
val InvalidDirectLoginParameters = "OBP-20012: Invalid direct login parameters"
2016-12-31 15:16:24 +00:00
val UsernameHasBeenLocked = "OBP-20013: The account has been locked, please contact administrator !"
2016-12-30 23:12:12 +00:00
2016-02-21 09:50:53 +00:00
// Resource related messages
val BankNotFound = "OBP-30001: Bank not found. Please specify a valid value for BANK_ID."
val CustomerNotFound = "OBP-30002: Customer not found. Please specify a valid value for CUSTOMER_NUMBER."
val CustomerNotFoundByCustomerId = "OBP-30002: Customer not found. Please specify a valid value for CUSTOMER_ID."
2016-02-21 09:50:53 +00:00
val AccountNotFound = "OBP-30003: Account not found. Please specify a valid value for ACCOUNT_ID."
2016-04-17 02:40:22 +00:00
val CounterpartyNotFound = "OBP-30004: Counterparty not found. The BANK_ID / ACCOUNT_ID specified does not exist on this server."
val ViewNotFound = "OBP-30005: View not found for Account. Please specify a valid value for VIEW_ID"
2016-02-21 09:50:53 +00:00
val CustomerNumberAlreadyExists = "OBP-30006: Customer Number already exists. Please specify a different value for BANK_ID or CUSTOMER_NUMBER."
2016-10-05 13:19:41 +00:00
val CustomerAlreadyExistsForUser = "OBP-30007: The User is already linked to a Customer at the bank specified by BANK_ID"
val CustomerDoNotExistsForUser = "OBP-30008: User is not linked to a Customer at the bank specified by BANK_ID"
val AtmNotFoundByAtmId = "OBP-30009: ATM not found. Please specify a valid value for ATM_ID."
2016-11-24 16:25:00 +00:00
val BranchNotFoundByBranchId = "OBP-300010: Branch not found. Please specify a valid value for BRANCH_ID."
val ProductNotFoundByProductCode = "OBP-30011: Product not found. Please specify a valid value for PRODUCT_CODE."
2017-01-22 15:42:59 +00:00
val CounterpartyNotFoundByIban = "OBP-30012: Counterparty not found. Please specify a valid value for IBAN."
val CounterpartyBeneficiaryPermit = "OBP-30013: The account can not send money to the Counterparty. Please set the Counterparty 'isBeneficiary' true first"
val CounterpartyAlreadyExists = "OBP-30014: Counterparty already exists. Please specify a different value for BANK_ID or ACCOUNT_ID or VIEW_ID or NAME."
val CreateBranchInsertError = "OBP-30015: Could not insert the Branch"
val CreateBranchUpdateError = "OBP-30016: Could not update the Branch"
2017-01-22 15:42:59 +00:00
val CounterpartyNotFoundByCounterpartyId = "OBP-30017: Counterparty not found. Please specify a valid value for COUNTERPARTY_ID."
val BankAccountNotFound = "OBP-30018: Bank Account not found. Please specify valid values for BANK_ID and ACCOUNT_ID. "
val ConsumerNotFound = "OBP-30019: Consumer not found. Please specify the valid values for CONSUMER_ID and CONSUMER_KEY."
val MeetingsNotSupported = "OBP-30101: Meetings are not supported on this server."
val MeetingApiKeyNotConfigured = "OBP-30102: Meeting provider API Key is not configured."
val MeetingApiSecretNotConfigured = "OBP-30103: Meeting provider Secret is not configured."
2016-07-04 14:13:48 +00:00
val MeetingNotFound = "OBP-30104: Meeting not found."
2016-11-02 07:43:56 +00:00
val InvalidAccountInitialBalance = "OBP-30104: Invalid Number. Initial balance must be a number, e.g 1000.00"
val InvalidAccountBalanceCurrency = "OBP-30105: Invalid Balance Currency."
val InvalidAccountBalanceAmount = "OBP-30106: Invalid Balance Amount."
val InvalidUserId = "OBP-30107: Invalid User Id."
val InvalidAccountType = "OBP-30108: Invalid Account Type."
val InitialBalanceMustBeZero = "OBP-30109: Initial Balance of Account must be Zero (0)."
val InvalidAccountIdFormat = "OBP-30110: Invalid Account Id. The ACCOUNT_ID should only contain 0-9/a-z/A-Z/'-'/'.'/'_', the length should be smaller than 255."
val InvalidBankIdFormat = "OBP-30111: Invalid Bank Id. The BANK_ID should only contain 0-9/a-z/A-Z/'-'/'.'/'_', the length should be smaller than 255."
val ConnectorEmptyResponse = "OBP-30200: Connector cannot return the data we requested."
val InvalidGetBankAccountsConnectorResponse = "OBP-30201: Connector did not return the set of accounts we requested."
val InvalidGetBankAccountConnectorResponse = "OBP-30202: Connector did not return the account we requested."
val InvalidGetTransactionConnectorResponse = "OBP-30203: Connector did not return the transaction we requested."
2016-08-02 13:49:35 +00:00
val EntitlementIsBankRole = "OBP-30205: This entitlement is a Bank Role. Please set bank_id to a valid bank id."
val EntitlementIsSystemRole = "OBP-30206: This entitlement is a System Role. Please set bank_id to empty string."
val InvalidGetTransactionsConnectorResponse = "OBP-30204: Connector did not return the set of transactions we requested."
// Transaction related messages:
val InvalidTransactionRequestType = "OBP-40001: Invalid value for TRANSACTION_REQUEST_TYPE"
val InsufficientAuthorisationToCreateTransactionRequest = "OBP-40002: Insufficient authorisation to create TransactionRequest. The Transaction Request could not be created because you don't have access to the owner view of the from account and you don't have access to canCreateAnyTransactionRequest."
val InvalidTransactionRequestCurrency = "OBP-40003: Transaction Request Currency must be the same as From Account Currency."
val InvalidTransactionRequestId = "OBP-40004: Transaction Request Id not found."
val InsufficientAuthorisationToCreateTransactionType = "OBP-40005: Insufficient authorisation to Create Transaction Type offered by the bank. The Request could not be created because you don't have access to CanCreateTransactionType."
2017-01-22 15:42:59 +00:00
val CreateTransactionTypeInsertError = "OBP-40006: Could not insert Transaction Type: Non unique BANK_ID / SHORT_CODE"
val CreateTransactionTypeUpdateError = "OBP-40007: Could not update Transaction Type: Non unique BANK_ID / SHORT_CODE"
}
object APIUtil extends Loggable {
2013-06-14 08:35:34 +00:00
implicit val formats = net.liftweb.json.DefaultFormats
implicit def errorToJson(error: ErrorMessage): JValue = Extraction.decompose(error)
val headers = ("Access-Control-Allow-Origin","*") :: Nil
2013-06-14 08:35:34 +00:00
def httpMethod : String =
S.request match {
case Full(r) => r.request.method
case _ => "GET"
}
def isThereDirectLoginHeader : Boolean = {
2016-01-30 11:13:04 +00:00
S.request match {
case Full(a) => a.header("Authorization") match {
case Full(parameters) => parameters.contains("DirectLogin")
2016-01-30 11:13:04 +00:00
case _ => false
}
case _ => false
}
}
2013-06-14 08:35:34 +00:00
def isThereAnOAuthHeader : Boolean = {
S.request match {
case Full(a) => a.header("Authorization") match {
case Full(parameters) => parameters.contains("OAuth")
case _ => false
}
case _ => false
}
}
def registeredApplication(consumerKey: String): Boolean = {
Consumer.find(By(Consumer.key, consumerKey)) match {
case Full(application) => application.isActive
case _ => false
}
}
def logAPICall = {
if(Props.getBool("write_metrics", false)) {
val user =
if (isThereAnOAuthHeader) {
getUser match {
case Full(u) => Full(u)
case _ => Empty
}
} else if (Props.getBool("allow_direct_login", true) && isThereDirectLoginHeader) {
DirectLogin.getUser match {
case Full(u) => Full(u)
case _ => Empty
}
} else {
Empty
}
2016-03-05 07:04:41 +00:00
// TODO This should use Elastic Search or Kafka not an RDBMS
val u = user.orNull
val userId = if (u != null) u.userId else "null"
2016-07-26 12:12:49 +00:00
val userName = if (u != null) u.name else "null"
var appName = "null"
var developerEmail = "null"
for (c <- getConsumer) {
appName = c.name.get
developerEmail = c.developerEmail.get
}
APIMetrics.apiMetrics.vend.saveMetric(userId, S.uriAndQueryString.getOrElse(""), (now: TimeSpan), userName, appName, developerEmail)
}
}
2013-06-14 08:35:34 +00:00
/*
Return the git commit. If we can't for some reason (not a git root etc) then log and return ""
*/
2013-06-14 08:35:34 +00:00
def gitCommit : String = {
val commit = try {
2013-06-14 08:35:34 +00:00
val properties = new java.util.Properties()
logger.debug("Before getResourceAsStream git.properties")
2013-06-14 08:35:34 +00:00
properties.load(getClass().getClassLoader().getResourceAsStream("git.properties"))
logger.debug("Before get Property git.commit.id")
2013-06-14 08:35:34 +00:00
properties.getProperty("git.commit.id", "")
} catch {
case e : Throwable => {
logger.warn("gitCommit says: Could not return git commit. Does resources/git.properties exist?")
logger.error(s"Exception in gitCommit: $e")
"" // Return empty string
}
2013-06-14 08:35:34 +00:00
}
commit
2013-06-14 08:35:34 +00:00
}
def noContentJsonResponse : JsonResponse =
JsonResponse(JsRaw(""), headers, Nil, 204)
2013-06-14 08:35:34 +00:00
def successJsonResponse(json: JsExp, httpCode : Int = 200) : JsonResponse =
JsonResponse(json, headers, Nil, httpCode)
def createdJsonResponse(json: JsExp, httpCode : Int = 201) : JsonResponse =
JsonResponse(json, headers, Nil, httpCode)
def acceptedJsonResponse(json: JsExp, httpCode : Int = 202) : JsonResponse =
JsonResponse(json, headers, Nil, httpCode)
2013-06-14 08:35:34 +00:00
def errorJsonResponse(message : String = "error", httpCode : Int = 400) : JsonResponse =
JsonResponse(Extraction.decompose(ErrorMessage(message)), headers, Nil, httpCode)
2013-06-14 08:35:34 +00:00
def notImplementedJsonResponse(message : String = "Not Implemented", httpCode : Int = 501) : JsonResponse =
JsonResponse(Extraction.decompose(ErrorMessage(message)), headers, Nil, httpCode)
2015-07-17 18:27:26 +00:00
def oauthHeaderRequiredJsonResponse : JsonResponse =
JsonResponse(Extraction.decompose(ErrorMessage("Authentication via OAuth is required")), headers, Nil, 400)
2013-06-14 08:35:34 +00:00
/** check the currency ISO code from the ISOCurrencyCodes.xml file */
def isValidCurrencyISOCode(currencyCode: String): Boolean = {
//just for initialization the Elem variable
var xml: Elem = <html/>
LiftRules.getResource("/media/xml/ISOCurrencyCodes.xml").map{ url =>
val input: InputStream = url.openStream()
xml = XML.load(input)
}
val stringArray = (xml \ "Currency" \ "CurrencyCode").map(_.text).mkString(" ").split("\\s+")
stringArray.contains(currencyCode)
}
/** Check the id values from GUI, such as ACCOUNT_ID, BANK_ID ... */
def isValidID(id :String):Boolean= {
val regex = """^([A-Za-z0-9\-_.]+)$""".r
id match {
case regex(e) if(e.length<256) => true
case _ => false
}
}
/** These three functions check rather than assert. I.e. they are silent if OK and return an error message if not.
* They do not throw an exception on failure thus they are not assertions
*/
2017-01-18 11:57:17 +00:00
/** only A-Z, a-z and max length <= 512 */
def checkMediumAlpha(value:String): String ={
val valueLength = value.length
val regex = """^([A-Za-z]+)$""".r
value match {
case regex(e) if(valueLength <= 512) => SILENCE_IS_GOLDEN
case regex(e) if(valueLength > 512) => ErrorMessages.InvalidValueLength
case _ => ErrorMessages.InvalidValueCharacters
}
}
2017-01-18 11:57:17 +00:00
/** only A-Z, a-z, 0-9 and max length <= 512 */
def checkMediumAlphaNumeric(value:String): String ={
val valueLength = value.length
val regex = """^([A-Za-z0-9]+)$""".r
value match {
case regex(e) if(valueLength <= 512) => SILENCE_IS_GOLDEN
case regex(e) if(valueLength > 512) => ErrorMessages.InvalidValueLength
case _ => ErrorMessages.InvalidValueCharacters
}
}
/** only A-Z, a-z, 0-9, all allowed characters for password and max length <= 512 */
def checkMediumPassword(value:String): String ={
val valueLength = value.length
val regex = """^([A-Za-z0-9!"#$%&'\(\)*+,-./:;<=>?@\\[\\\\]^_\\`{|}~]+)$""".r
value match {
case regex(e) if(valueLength <= 512) => SILENCE_IS_GOLDEN
case regex(e) if(valueLength > 512) => ErrorMessages.InvalidValueLength
case _ => ErrorMessages.InvalidValueCharacters
}
}
2017-01-22 15:42:59 +00:00
/** only A-Z, a-z, 0-9, -, _, ., @, and max length <= 512 */
def checkMediumString(value:String): String ={
val valueLength = value.length
val regex = """^([A-Za-z0-9\-._@]+)$""".r
value match {
case regex(e) if(valueLength <= 512) => SILENCE_IS_GOLDEN
case regex(e) if(valueLength > 512) => ErrorMessages.InvalidValueLength
case _ => ErrorMessages.InvalidValueCharacters
}
}
/** Import this object's methods to add signing operators to dispatch.Request */
object OAuth {
import javax.crypto
import dispatch.{Req => Request}
import net.liftweb.util.Helpers
2014-09-10 15:13:33 +00:00
import org.apache.http.protocol.HTTP.UTF_8
import scala.collection.Map
2016-05-13 16:38:40 +00:00
import scala.collection.immutable.{TreeMap, Map => IMap}
case class ReqData (
url: String,
method: String,
body: String,
body_encoding: String,
headers: Map[String, String],
query_params: Map[String,String],
form_params: Map[String,String]
)
case class Consumer(key: String, secret: String)
case class Token(value: String, secret: String)
object Token {
def apply[T <: Any](m: Map[String, T]): Option[Token] = List("oauth_token", "oauth_token_secret").flatMap(m.get) match {
case value :: secret :: Nil => Some(Token(value.toString, secret.toString))
case _ => None
}
}
/** @return oauth parameter map including signature */
def sign(method: String, url: String, user_params: Map[String, Any], consumer: Consumer, token: Option[Token], verifier: Option[String], callback: Option[String]) = {
val oauth_params = IMap(
"oauth_consumer_key" -> consumer.key,
"oauth_signature_method" -> "HMAC-SHA1",
"oauth_timestamp" -> (System.currentTimeMillis / 1000).toString,
"oauth_nonce" -> System.nanoTime.toString,
"oauth_version" -> "1.0"
) ++ token.map { "oauth_token" -> _.value } ++
verifier.map { "oauth_verifier" -> _ } ++
callback.map { "oauth_callback" -> _ }
val encoded_ordered_params = (
new TreeMap[String, String] ++ (user_params ++ oauth_params map %%)
) map { case (k, v) => k + "=" + v } mkString "&"
val message =
%%(method.toUpperCase :: url :: encoded_ordered_params :: Nil)
val SHA1 = "HmacSHA1"
val key_str = %%(consumer.secret :: (token map { _.secret } getOrElse "") :: Nil)
val key = new crypto.spec.SecretKeySpec(bytes(key_str), SHA1)
val sig = {
val mac = crypto.Mac.getInstance(SHA1)
mac.init(key)
2016-05-13 16:38:40 +00:00
base64Encode(mac.doFinal(bytes(message)))
}
oauth_params + ("oauth_signature" -> sig)
}
/** Out-of-band callback code */
val oob = "oob"
/** Map with oauth_callback set to the given url */
def callback(url: String) = IMap("oauth_callback" -> url)
//normalize to OAuth percent encoding
private def %% (str: String): String = {
val remaps = ("+", "%20") :: ("%7E", "~") :: ("*", "%2A") :: Nil
(encode_%(str) /: remaps) { case (str, (a, b)) => str.replace(a,b) }
}
private def %% (s: Seq[String]): String = s map %% mkString "&"
private def %% (t: (String, Any)): (String, String) = (%%(t._1), %%(t._2.toString))
private def bytes(str: String) = str.getBytes(UTF_8)
/** Add OAuth operators to dispatch.Request */
implicit def Request2RequestSigner(r: Request) = new RequestSigner(r)
/** @return %-encoded string for use in URLs */
def encode_% (s: String) = java.net.URLEncoder.encode(s, org.apache.http.protocol.HTTP.UTF_8)
/** @return %-decoded string e.g. from query string or form body */
def decode_% (s: String) = java.net.URLDecoder.decode(s, org.apache.http.protocol.HTTP.UTF_8)
class RequestSigner(rb: Request) {
2016-05-13 16:38:40 +00:00
private val r = rb.toRequest
@deprecated("use <@ (consumer, callback) to pass the callback in the header for a request-token request")
def <@ (consumer: Consumer): Request = sign(consumer, None, None, None)
/** sign a request with a callback, e.g. a request-token request */
def <@ (consumer: Consumer, callback: String): Request = sign(consumer, None, None, Some(callback))
/** sign a request with a consumer, token, and verifier, e.g. access-token request */
def <@ (consumer: Consumer, token: Token, verifier: String): Request =
sign(consumer, Some(token), Some(verifier), None)
/** sign a request with a consumer and a token, e.g. an OAuth-signed API request */
def <@ (consumer: Consumer, token: Token): Request = sign(consumer, Some(token), None, None)
2013-07-19 12:37:06 +00:00
def <@ (consumerAndToken: Option[(Consumer,Token)]): Request = {
consumerAndToken match {
case Some(cAndt) => sign(cAndt._1, Some(cAndt._2), None, None)
case _ => rb
}
}
/** Sign request by reading Post (<<) and query string parameters */
private def sign(consumer: Consumer, token: Option[Token], verifier: Option[String], callback: Option[String]) = {
2016-05-13 16:38:40 +00:00
val oauth_url = r.getUrl.split('?')(0)
2016-05-13 16:38:40 +00:00
val query_params = r.getQueryParams.asScala.groupBy(_.getName).mapValues(_.map(_.getValue)).map {
case (k, v) => k -> v.toString
}
val form_params = r.getFormParams.asScala.groupBy(_.getName).mapValues(_.map(_.getValue)).map {
case (k, v) => k -> v.toString
}
val body_encoding = r.getBodyEncoding
var body = new String()
if (r.getByteData != null )
body = new String(r.getByteData)
val oauth_params = OAuth.sign(r.getMethod, oauth_url,
query_params ++ form_params,
consumer, token, verifier, callback)
2016-05-13 16:38:40 +00:00
def createRequest( reqData: ReqData ): Request = {
val rb = url(reqData.url)
.setMethod(reqData.method)
.setBodyEncoding(reqData.body_encoding)
.setBody(reqData.body) <:< reqData.headers
if (reqData.query_params.nonEmpty)
rb <<? reqData.query_params
rb
}
2016-05-13 16:38:40 +00:00
createRequest( ReqData(
oauth_url,
r.getMethod,
body,
body_encoding,
IMap("Authorization" -> ("OAuth " + oauth_params.map {
2016-05-13 16:38:40 +00:00
case (k, v) => encode_%(k) + "=\"%s\"".format(encode_%(v.toString))
}.mkString(",") )),
query_params,
form_params
))
}
}
}
2015-07-25 12:31:15 +00:00
/*
Used to document API calls / resources.
TODO Can we extract apiVersion, apiFunction, requestVerb and requestUrl from partialFunction?
2015-07-25 12:31:15 +00:00
*/
// Used to tag Resource Docs
case class ResourceDocTag(tag: String)
// Use the *singular* case. for both the variable name and string.
// e.g. "This call is Payment related"
val apiTagTransactionRequest = ResourceDocTag("TransactionRequest")
2016-02-09 10:45:59 +00:00
val apiTagApiInfo = ResourceDocTag("APIInfo")
val apiTagBank = ResourceDocTag("Bank")
val apiTagAccount = ResourceDocTag("Account")
2016-02-09 10:45:59 +00:00
val apiTagPublicData = ResourceDocTag("PublicData")
val apiTagPrivateData = ResourceDocTag("PrivateData")
val apiTagTransaction = ResourceDocTag("Transaction")
val apiTagMetaData = ResourceDocTag("MetaData")
val apiTagView = ResourceDocTag("View")
val apiTagEntitlement = ResourceDocTag("Entitlement")
2016-02-09 10:45:59 +00:00
val apiTagOwnerRequired = ResourceDocTag("OwnerViewRequired")
val apiTagCounterparty = ResourceDocTag("Counterparty")
val apiTagKyc = ResourceDocTag("KYC")
val apiTagCustomer = ResourceDocTag("Customer")
val apiTagOnboarding = ResourceDocTag("Onboarding")
val apiTagUser = ResourceDocTag("User")
val apiTagMeeting = ResourceDocTag("Meeting")
val apiTagExperimental = ResourceDocTag("Experimental")
val apiTagPerson = ResourceDocTag("Person")
2017-01-22 15:42:59 +00:00
case class Catalogs(core: Boolean = false, psd2: Boolean = false, obwg: Boolean = false)
val Core = true
val PSD2 = true
val OBWG = true
val notCore = false
val notPSD2 = false
val notOBWG = false
// Used to document the API calls
case class ResourceDoc(
partialFunction : PartialFunction[Req, Box[User] => Box[JsonResponse]],
apiVersion: String, // TODO: Constrain to certain strings?
apiFunction: String, // The partial function that implements this resource. Could use it to link to the source code that implements the call
requestVerb: String, // GET, POST etc. TODO: Constrain to GET, POST etc.
requestUrl: String, // The URL (not including /obp/vX.X). Starts with / No trailing slash. TODO Constrain the string?
summary: String, // A summary of the call (originally taken from code comment) SHOULD be under 120 chars to be inline with Swagger
description: String, // Longer description (originally taken from github wiki)
exampleRequestBody: JValue, // An example of the body required (maybe empty)
successResponseBody: JValue, // A successful response body
errorResponseBodies: List[JValue], // Possible error responses
catalogs: Catalogs,
tags: List[ResourceDocTag]
)
// Define relations between API end points. Used to create _links in the JSON and maybe later for API Explorer browsing
case class ApiRelation(
fromPF : PartialFunction[Req, Box[User] => Box[JsonResponse]],
toPF : PartialFunction[Req, Box[User] => Box[JsonResponse]],
rel : String
)
// Populated from Resource Doc and ApiRelation
case class InternalApiLink(
fromPF : PartialFunction[Req, Box[User] => Box[JsonResponse]],
toPF : PartialFunction[Req, Box[User] => Box[JsonResponse]],
rel : String,
requestUrl: String
)
// Used to pass context of current API call to the function that generates links for related Api calls.
case class DataContext(
user : Box[User],
bankId : Option[BankId],
accountId: Option[AccountId],
viewId: Option[ViewId],
counterpartyId: Option[CounterpartyId],
transactionId: Option[TransactionId]
)
2016-03-02 12:51:13 +00:00
case class CallerContext(
caller : PartialFunction[Req, Box[User] => Box[JsonResponse]]
)
case class CodeContext(
2016-03-02 12:51:13 +00:00
resourceDocsArrayBuffer : ArrayBuffer[ResourceDoc],
relationsArrayBuffer : ArrayBuffer[ApiRelation]
)
2016-03-05 07:04:41 +00:00
case class ApiLink(
2016-03-02 12:51:13 +00:00
rel: String,
href: String
)
case class LinksJSON(
_links: List[ApiLink]
)
case class ResultAndLinksJSON(
result : JValue,
_links: List[ApiLink]
)
def createResultAndLinksJSON(result : JValue, links : List[ApiLink] ) : ResultAndLinksJSON = {
new ResultAndLinksJSON(
result,
links
)
}
/*
Returns a string showed to the developer
*/
def authenticationRequiredMessage(authRequired: Boolean) : String =
authRequired match {
case true => "Authentication is Mandatory"
case false => "Authentication is Optional"
}
2016-03-05 07:04:41 +00:00
def apiVersionWithV(apiVersion : String) : String = {
// TODO Define a list of supported versions (put in Constant) and constrain the input
// Append v and replace _ with .
s"v${apiVersion.replaceAll("_",".")}"
}
def fullBaseUrl : String = {
val crv = CurrentReq.value
val apiPathZeroFromRequest = crv.path.partPath(0)
if (apiPathZeroFromRequest != ApiPathZero) throw new Exception("Configured ApiPathZero is not the same as the actual.")
val path = s"$HostName/$ApiPathZero"
path
}
// Modify URL replacing placeholders for Ids
def contextModifiedUrl(url: String, context: DataContext) = {
2016-03-05 07:04:41 +00:00
// Potentially replace BANK_ID
val url2: String = context.bankId match {
case Some(x) => url.replaceAll("BANK_ID", x.value)
case _ => url
}
val url3: String = context.accountId match {
// Take care *not* to change OTHER_ACCOUNT_ID HERE
case Some(x) => url2.replaceAll("/ACCOUNT_ID", s"/${x.value}").replaceAll("COUNTERPARTY_ID", x.value)
case _ => url2
}
val url4: String = context.viewId match {
case Some(x) => url3.replaceAll("VIEW_ID", {x.value})
case _ => url3
}
val url5: String = context.counterpartyId match {
// Change OTHER_ACCOUNT_ID or COUNTERPARTY_ID
case Some(x) => url4.replaceAll("OTHER_ACCOUNT_ID", x.value).replaceAll("COUNTERPARTY_ID", x.value)
case _ => url4
}
val url6: String = context.transactionId match {
case Some(x) => url5.replaceAll("TRANSACTION_ID", x.value)
case _ => url5
}
2016-03-05 07:04:41 +00:00
// Add host, port, prefix, version.
// not correct because call could be in other version
val fullUrl = s"$fullBaseUrl$url6"
fullUrl
}
2016-03-02 12:51:13 +00:00
def getApiLinkTemplates(callerContext: CallerContext,
codeContext: CodeContext
) : List[InternalApiLink] = {
// Relations of the API version where the caller is defined.
val relations = codeContext.relationsArrayBuffer.toList
// Resource Docs
// Note: This doesn't allow linking to calls in earlier versions of the API
// TODO: Fix me
val resourceDocs = codeContext.resourceDocsArrayBuffer
2016-03-02 12:51:13 +00:00
val pf = callerContext.caller
val internalApiLinks: List[InternalApiLink] = for {
relation <- relations.filter(r => r.fromPF == pf)
toResourceDoc <- resourceDocs.find(rd => rd.partialFunction == relation.toPF)
}
yield new InternalApiLink(
pf,
toResourceDoc.partialFunction,
relation.rel,
2016-03-05 07:04:41 +00:00
// Add the vVersion to the documented url
s"/${apiVersionWithV(toResourceDoc.apiVersion)}${toResourceDoc.requestUrl}"
)
internalApiLinks
}
2016-03-02 12:51:13 +00:00
// This is not currently including "templated" attribute
def halLinkFragment (link: ApiLink) : String = {
"\"" + link.rel +"\": { \"href\": \"" +link.href + "\" }"
}
// Since HAL links can't be represented via a case class, (they have dynamic attributes rather than a list) we need to generate them here.
def buildHalLinks(links: List[ApiLink]): JValue = {
val halLinksString = links match {
case head :: tail => tail.foldLeft("{"){(r: String, c: ApiLink) => ( r + " " + halLinkFragment(c) + " ," ) } + halLinkFragment(head) + "}"
case Nil => "{}"
}
parse(halLinksString)
}
// Returns API links (a list of them) that have placeholders (e.g. BANK_ID) replaced by values (e.g. ulster-bank)
def getApiLinks(callerContext: CallerContext, codeContext: CodeContext, dataContext: DataContext) : List[ApiLink] = {
val templates = getApiLinkTemplates(callerContext, codeContext)
// Replace place holders in the urls like BANK_ID with the current value e.g. 'ulster-bank' and return as ApiLinks for external consumption
2016-03-02 12:51:13 +00:00
val links = templates.map(i => ApiLink(i.rel,
contextModifiedUrl(i.requestUrl, dataContext) )
)
links
}
2016-03-02 12:51:13 +00:00
// Returns links formatted at objects.
def getHalLinks(callerContext: CallerContext, codeContext: CodeContext, dataContext: DataContext) : JValue = {
val links = getApiLinks(callerContext, codeContext, dataContext)
getHalLinksFromApiLinks(links)
}
2016-03-02 12:51:13 +00:00
def getHalLinksFromApiLinks(links: List[ApiLink]) : JValue = {
val halLinksJson = buildHalLinks(links)
halLinksJson
}
def isSuperAdmin(user_id: String) : Boolean = {
val user_ids = Props.get("super_admin_user_ids", "super_admin_user_ids is not defined").split(",").map(_.trim).toList
user_ids.filter(_ == user_id).length > 0
}
def hasEntitlement(bankId: String, userId: String, role: ApiRole): Boolean = {
2016-06-17 09:19:05 +00:00
!Entitlement.entitlement.vend.getEntitlement(bankId, userId, role.toString).isEmpty
}
// Function checks does a user specified by a parameter userId has at least one role provided by a parameter roles at a bank specified by a parameter bankId
// i.e. does user has assigned at least one role from the list
def hasAtLeastOneEntitlement(bankId: String, userId: String, roles: List[ApiRole]): Boolean = {
val list: List[Boolean] = for (role <- roles) yield {
!Entitlement.entitlement.vend.getEntitlement(if (role.requiresBankId == true) bankId else "", userId, role.toString).isEmpty
}
list.exists(_ == true)
}
// Function checks does a user specified by a parameter userId has all roles provided by a parameter roles at a bank specified by a parameter bankId
// i.e. does user has assigned all roles from the list
def hasAllEntitlements(bankId: String, userId: String, roles: List[ApiRole]): Boolean = {
val list: List[Boolean] = for (role <- roles) yield {
!Entitlement.entitlement.vend.getEntitlement(if (role.requiresBankId == true) bankId else "", userId, role.toString).isEmpty
}
list.forall(_ == true)
}
2016-07-20 08:40:12 +00:00
def getCustomers(ids: List[String]): List[Customer] = {
val customers = {
for {id <- ids
c = Customer.customerProvider.vend.getCustomerByCustomerId(id)
u <- c
} yield {
u
}
}
customers
}
def getAutocompleteValue: String = {
Props.get("autocomplete_at_login_form_enabled", "false") match {
case "true" => "on"
case "false" => "off"
case _ => "off"
}
}
2013-06-14 08:35:34 +00:00
}