2013-12-23 11:39:18 +00:00
/* *
2016-05-13 16:38:40 +00:00
* Open Bank Project - API
2016-11-06 10:48:59 +00:00
* 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
2016-11-06 10:48:59 +00:00
* 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
*
2013-12-23 11:39:18 +00:00
*/
2014-09-10 15:13:33 +00:00
package code.api.util
2013-06-14 08:35:34 +00:00
2016-05-13 16:38:40 +00:00
import code.api.Constant._
2016-06-06 19:29:18 +00:00
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
2016-02-29 23:26:47 +00:00
import code.model._
2016-05-13 16:38:40 +00:00
import dispatch.url
2016-06-06 19:29:18 +00:00
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
2016-05-13 16:38:40 +00:00
import net.liftweb.http. { CurrentReq , JsonResponse , Req , S }
2016-01-30 13:33:33 +00:00
import net.liftweb.json.JsonAST.JValue
2016-05-13 16:38:40 +00:00
import net.liftweb.json. { Extraction , parse }
2016-08-11 17:17:43 +00:00
import net.liftweb.mapper.By
2013-06-14 08:35:34 +00:00
import net.liftweb.util.Helpers._
2016-06-06 19:29:18 +00:00
import net.liftweb.util. { Helpers , Props , SecurityHelpers }
2013-06-14 08:35:34 +00:00
2016-02-29 23:26:47 +00:00
import scala.collection.mutable.ArrayBuffer
2016-05-13 16:38:40 +00:00
import scala.collection.JavaConverters._
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
2016-03-12 16:36:03 +00:00
val InvalidJsonFormat = "OBP-10001: Incorrect json format."
val InvalidNumber = "OBP-10002: Invalid Number. Could not convert value to a number."
2016-02-21 09:50:53 +00:00
2016-05-24 19:35:52 +00:00
// Authentication / Authorisation / User messages
2016-02-21 20:53:18 +00:00
val UserNotLoggedIn = "OBP-20001: User not logged in. Authentication is required!"
2016-02-21 09:50:53 +00:00
2016-05-24 19:35:52 +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
2016-05-24 19:35:52 +00:00
val UserNotFoundById = "OBP-20005: User not found by User Id."
2016-06-14 14:07:53 +00:00
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
2016-07-22 16:20:02 +00:00
val InvalidConsumerKey = "OBP-20008: Invalid Consumer Key."
2016-11-01 15:31:47 +00:00
val InvalidConsumerCredentials = "OBP-20009: Invalid consumer credentials"
2016-02-21 09:50:53 +00:00
// Resource related messages
2016-03-06 08:35:13 +00:00
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."
2016-06-10 06:45:35 +00:00
val CustomerNotFoundByCustomerId = "OBP-30002: Customer not found. Please specify a valid value for CUSTOMER_ID."
2016-02-21 09:50:53 +00:00
2016-03-06 08:35:13 +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."
2016-03-06 08:35:13 +00:00
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
2016-05-21 12:49:44 +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"
2016-05-21 12:49:44 +00:00
2016-05-22 12:01:37 +00:00
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-05-22 12:01:37 +00:00
2016-07-03 11:41:55 +00:00
2016-11-02 07:43:56 +00:00
val InvalidAccountInitialBalance = "OBP-30104: Invalid Number. Initial balance must be a number, e.g 1000.00"
2016-07-03 11:41:55 +00: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)."
2016-08-02 11:27:04 +00:00
val ConnectorEmptyResponse = "OBP-30200: Connector cannot return the data we requested."
2016-08-01 13:02:21 +00:00
val InvalidGetBankAccountsConnectorResponse = "OBP-30201: Connector did not return the set of accounts we requested."
2016-08-01 14:08:45 +00:00
val InvalidGetBankAccountConnectorResponse = "OBP-30202: Connector did not return the account we requested."
2016-08-01 14:42:49 +00:00
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."
2016-08-02 07:45:24 +00:00
val InvalidGetTransactionsConnectorResponse = "OBP-30204: Connector did not return the set of transactions we requested."
2016-08-01 13:02:21 +00:00
2016-07-03 11:41:55 +00:00
2016-04-27 15:13:16 +00:00
// Transaction related messages:
val InvalidTransactionRequestType = "OBP-40001: Invalid value for TRANSACTION_REQUEST_TYPE"
2016-06-17 08:44:32 +00:00
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."
2016-11-08 18:28:36 +00:00
val InvalidTransactionRequestCurrency = "OBP-40003: Transaction Request Currency must be the same as From Account Currency."
val InvalidTransactionRequestId = "OBP-40004: Transaction Request Id not found."
2016-11-20 11:22:23 +00:00
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."
val CreateTransactionTypeInsertError = "OBP-40006: Could not insert Transaction Type: Non unique bankId / shortCode"
val CreateTransactionTypeUpdateError = "OBP-40007: Could not update Transaction Type: Non unique bankId / shortCode"
2016-07-03 11:41:55 +00:00
2016-02-21 09:21:01 +00:00
}
2015-05-12 10:48:15 +00:00
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 )
2013-07-16 09:12:44 +00:00
val headers = ( "Access-Control-Allow-Origin" , "*" ) : : Nil
2016-03-06 08:35:13 +00:00
2013-06-14 08:35:34 +00:00
def httpMethod : String =
S . request match {
case Full ( r ) => r . request . method
case _ => "GET"
}
2016-01-30 13:33:33 +00:00
def isThereDirectLoginHeader : Boolean = {
2016-01-30 11:13:04 +00:00
S . request match {
2016-01-30 13:33:33 +00:00
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
}
}
2016-08-11 17:17:43 +00:00
def registeredApplication ( consumerKey : String ) : Boolean = {
Consumer . find ( By ( Consumer . key , consumerKey ) ) match {
case Full ( application ) => application . isActive
case _ => false
}
}
2015-05-30 15:17:18 +00:00
def logAPICall = {
if ( Props . getBool ( "write_metrics" , false ) ) {
2016-06-06 19:29:18 +00:00
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
2016-06-08 07:39:32 +00:00
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 )
2015-05-30 15:17:18 +00:00
}
}
2013-06-14 08:35:34 +00:00
2015-05-12 10:48:15 +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 = {
2015-05-12 10:48:15 +00:00
val commit = try {
2013-06-14 08:35:34 +00:00
val properties = new java . util . Properties ( )
2015-05-12 10:48:15 +00:00
logger . debug ( "Before getResourceAsStream git.properties" )
2013-06-14 08:35:34 +00:00
properties . load ( getClass ( ) . getClassLoader ( ) . getResourceAsStream ( "git.properties" ) )
2015-05-12 10:48:15 +00:00
logger . debug ( "Before get Property git.commit.id" )
2013-06-14 08:35:34 +00:00
properties . getProperty ( "git.commit.id" , "" )
2015-05-12 10:48:15 +00:00
} 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
}
2015-05-12 10:48:15 +00:00
commit
2013-06-14 08:35:34 +00:00
}
def noContentJsonResponse : JsonResponse =
2013-07-16 09:12:44 +00:00
JsonResponse ( JsRaw ( "" ) , headers , Nil , 204 )
2013-06-14 08:35:34 +00:00
def successJsonResponse ( json : JsExp , httpCode : Int = 200 ) : JsonResponse =
2015-10-02 21:25:15 +00:00
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 =
2013-07-16 09:12:44 +00:00
JsonResponse ( json , headers , Nil , httpCode )
2013-06-14 08:35:34 +00:00
def errorJsonResponse ( message : String = "error" , httpCode : Int = 400 ) : JsonResponse =
2013-07-16 09:12:44 +00:00
JsonResponse ( Extraction . decompose ( ErrorMessage ( message ) ) , headers , Nil , httpCode )
2013-06-14 08:35:34 +00:00
2015-10-22 13:27:41 +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 =
2013-07-16 09:12:44 +00:00
JsonResponse ( Extraction . decompose ( ErrorMessage ( "Authentication via OAuth is required" ) ) , headers , Nil , 400 )
2013-06-14 08:35:34 +00:00
2013-07-08 13:38:32 +00:00
/* * Import this object's methods to add signing operators to dispatch.Request */
object OAuth {
import javax.crypto
2015-04-24 17:17:09 +00:00
import dispatch. { Req => Request }
2013-07-08 13:38:32 +00:00
import net.liftweb.util.Helpers
2014-09-10 15:13:33 +00:00
import org.apache.http.protocol.HTTP.UTF_8
2015-04-24 17:17:09 +00:00
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 ]
)
2013-07-08 13:38:32 +00:00
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 ) ) )
2013-07-08 13:38:32 +00:00
}
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" ) : : ( " % 7 E " , " ~ " ) : : ( " * " , " % 2 A " ) : : 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
2013-07-08 13:38:32 +00:00
@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
}
}
2013-07-08 13:38:32 +00:00
/* * 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
2013-07-08 13:38:32 +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 )
2013-07-08 13:38:32 +00:00
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
2013-07-08 13:38:32 +00:00
rb
}
2016-05-13 16:38:40 +00:00
createRequest ( ReqData (
oauth_url ,
r . getMethod ,
body ,
body_encoding ,
2013-07-08 13:38:32 +00:00
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
) )
2013-07-08 13:38:32 +00:00
}
}
}
2015-07-25 12:31:15 +00:00
/*
2016-01-17 15:07:36 +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
*/
2016-02-08 20:58:11 +00:00
2016-02-29 23:26:47 +00:00
// Used to tag Resource Docs
2016-02-08 20:58:11 +00:00
case class ResourceDocTag ( tag : String )
2016-06-25 02:23:48 +00:00
// Use the *singular* case. for both the variable name and string.
// e.g. "This call is Payment related"
2016-06-27 07:34:51 +00:00
val apiTagTransactionRequest = ResourceDocTag ( "TransactionRequest" )
2016-02-09 10:45:59 +00:00
val apiTagApiInfo = ResourceDocTag ( "APIInfo" )
2016-06-25 02:23:48 +00:00
val apiTagBank = ResourceDocTag ( "Bank" )
val apiTagAccount = ResourceDocTag ( "Account" )
2016-02-09 10:45:59 +00:00
val apiTagPublicData = ResourceDocTag ( "PublicData" )
val apiTagPrivateData = ResourceDocTag ( "PrivateData" )
2016-06-25 02:23:48 +00:00
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" )
2016-06-25 02:23:48 +00:00
val apiTagCounterparty = ResourceDocTag ( "Counterparty" )
2016-02-08 20:58:11 +00:00
val apiTagKyc = ResourceDocTag ( "KYC" )
val apiTagCustomer = ResourceDocTag ( "Customer" )
2016-05-22 12:01:37 +00:00
val apiTagOnboarding = ResourceDocTag ( "Onboarding" )
val apiTagUser = ResourceDocTag ( "User" )
val apiTagMeeting = ResourceDocTag ( "Meeting" )
2016-06-25 02:23:48 +00:00
val apiTagExperimental = ResourceDocTag ( "Experimental" )
2016-07-27 20:52:09 +00:00
val apiTagPerson = ResourceDocTag ( "Person" )
2016-02-08 20:58:11 +00:00
2016-02-29 23:26:47 +00:00
// Used to document the API calls
2015-08-28 10:36:47 +00:00
case class ResourceDoc (
2016-02-29 23:26:47 +00:00
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
isCore : Boolean ,
isPSD2 : Boolean ,
2016-04-24 07:07:14 +00:00
isOBWG : Boolean ,
2016-02-29 23:26:47 +00:00
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 ] ]
)
2016-02-29 23:26:47 +00:00
case class CodeContext (
2016-03-02 12:51:13 +00:00
resourceDocsArrayBuffer : ArrayBuffer [ ResourceDoc ] ,
relationsArrayBuffer : ArrayBuffer [ ApiRelation ]
2016-02-29 23:26:47 +00:00
)
2016-03-05 07:04:41 +00:00
2016-02-29 23:26:47 +00:00
case class ApiLink (
2016-03-02 12:51:13 +00:00
rel : String ,
href : String
2016-02-29 23:26:47 +00:00
)
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
)
}
2016-01-20 21:29:43 +00:00
2016-07-15 00:24:38 +00:00
/*
Returns a string showed to the developer
*/
2016-01-20 21:29:43 +00:00
def authenticationRequiredMessage ( authRequired : Boolean ) : String =
2016-07-15 00:24:38 +00:00
authRequired match {
case true => "Authentication is Mandatory"
case false => "Authentication is Optional"
2016-01-20 21:29:43 +00:00
}
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
}
2016-02-29 23:26:47 +00:00
// Modify URL replacing placeholders for Ids
def contextModifiedUrl ( url : String , context : DataContext ) = {
2016-03-05 07:04:41 +00:00
// Potentially replace BANK_ID
2016-02-29 23:26:47 +00:00
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-02-29 23:26:47 +00:00
}
2016-03-02 12:51:13 +00:00
def getApiLinkTemplates ( callerContext : CallerContext ,
codeContext : CodeContext
2016-02-29 23:26:47 +00:00
) : List [ InternalApiLink ] = {
2016-03-12 16:36:03 +00:00
// Relations of the API version where the caller is defined.
2016-02-29 23:26:47 +00:00
val relations = codeContext . relationsArrayBuffer . toList
2016-03-12 16:36:03 +00:00
// Resource Docs
// Note: This doesn't allow linking to calls in earlier versions of the API
// TODO: Fix me
2016-02-29 23:26:47 +00:00
val resourceDocs = codeContext . resourceDocsArrayBuffer
2016-03-02 12:51:13 +00:00
val pf = callerContext . caller
2016-02-29 23:26:47 +00:00
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 } "
2016-02-29 23:26:47 +00:00
)
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 )
2016-02-29 23:26:47 +00:00
// 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-02-29 23:26:47 +00:00
}
2016-01-20 21:29:43 +00:00
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-01-20 21:29:43 +00:00
2016-03-02 12:51:13 +00:00
def getHalLinksFromApiLinks ( links : List [ ApiLink ] ) : JValue = {
val halLinksJson = buildHalLinks ( links )
halLinksJson
}
2016-06-03 15:01:55 +00:00
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
2016-06-03 15:01:55 +00:00
}
2016-10-18 09:36:08 +00:00
// 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
2016-10-17 07:21:46 +00:00
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 )
}
2016-10-18 09:36:08 +00:00
// 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
}
2013-06-14 08:35:34 +00:00
}