Skip to content

Commit

Permalink
Merge pull request #2321 from hongwei1/develop
Browse files Browse the repository at this point in the history
refactor/added the locale guard for all resourceDocs endpoints
  • Loading branch information
simonredfern authored Nov 13, 2023
2 parents e4f1b85 + de51b8e commit 0eb7fc1
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import code.api.v3_0_0.OBPAPI3_0_0
import code.api.v3_1_0.OBPAPI3_1_0
import code.api.v4_0_0.{APIMethods400, OBPAPI4_0_0}
import code.apicollectionendpoint.MappedApiCollectionEndpointsProvider
import code.util.Helper.{MdcLoggable, ObpS}
import code.util.Helper.{MdcLoggable, ObpS, SILENCE_IS_GOLDEN}
import com.github.dwickern.macros.NameOf.nameOf
import com.openbankproject.commons.model.{BankId, ListResult, User}
import com.openbankproject.commons.model.enums.ContentParam.{ALL, DYNAMIC, STATIC}
Expand Down Expand Up @@ -396,7 +396,7 @@ trait ResourceDocsAPIMethods extends MdcLoggable with APIMethods220 with APIMeth
| You may filter this endpoint using the 'content' url parameter, e.g. ?content=dynamic
| if set content=dynamic, only show dynamic endpoints, if content=static, only show the static endpoints. if omit this parameter, we will show all the endpoints.
|
| You may need some other language resource docs, now we support i18n language tag , e.g. ?locale=zh_CN
| You may need some other language resource docs, now we support en_GB and es_ES at the moment.
|
| You can filter with api-collection-id, but api-collection-id can not be used with others together. If api-collection-id is used in URL, it will ignore all other parameters.
|
Expand All @@ -410,7 +410,7 @@ trait ResourceDocsAPIMethods extends MdcLoggable with APIMethods220 with APIMeth
|${getObpApiRoot}/v4.0.0$endpointBankIdPath/resource-docs/v4.0.0/obp
|${getObpApiRoot}/v4.0.0$endpointBankIdPath/resource-docs/v4.0.0/obp?tags=Account,Bank
|${getObpApiRoot}/v4.0.0$endpointBankIdPath/resource-docs/v4.0.0/obp?functions=getBanks,bankById
|${getObpApiRoot}/v4.0.0$endpointBankIdPath/resource-docs/v4.0.0/obp?locale=zh_CN
|${getObpApiRoot}/v4.0.0$endpointBankIdPath/resource-docs/v4.0.0/obp?locale=es_ES
|${getObpApiRoot}/v4.0.0$endpointBankIdPath/resource-docs/v4.0.0/obp?content=static,dynamic,all
|${getObpApiRoot}/v4.0.0$endpointBankIdPath/resource-docs/v4.0.0/obp?api-collection-id=4e866c86-60c3-4268-a221-cb0bbf1ad221
|
Expand Down Expand Up @@ -540,6 +540,13 @@ trait ResourceDocsAPIMethods extends MdcLoggable with APIMethods220 with APIMeth
}
requestedApiVersion <- NewStyle.function.tryons(s"$InvalidApiVersionString $requestedApiVersionString", 400, callContext) {ApiVersionUtils.valueOf(requestedApiVersionString)}
_ <- Helper.booleanToFuture(s"$ApiVersionNotSupported $requestedApiVersionString", 400, callContext)(versionIsAllowed(requestedApiVersion))
_ <- if (locale.isDefined) {
Helper.booleanToFuture(failMsg = s"$InvalidLocale Current Locale is ${locale.get}" intern(), cc = cc.callContext) {
APIUtil.obpLocaleValidation(locale.get) == SILENCE_IS_GOLDEN
}
} else {
Future.successful(true)
}
cacheKey = APIUtil.createResourceDocCacheKey(
None,
requestedApiVersionString,
Expand Down Expand Up @@ -637,6 +644,13 @@ trait ResourceDocsAPIMethods extends MdcLoggable with APIMethods220 with APIMeth
case false => anonymousAccess(cc)
case true => authenticatedAccess(cc) // If set resource_docs_requires_role=true, we need check the authentication
}
_ <- if (locale.isDefined) {
Helper.booleanToFuture(failMsg = s"$InvalidLocale Current Locale is ${locale.get}" intern(), cc = cc.callContext) {
APIUtil.obpLocaleValidation(locale.get) == SILENCE_IS_GOLDEN
}
} else {
Future.successful(true)
}
(_, callContext) <- NewStyle.function.getBank(BankId(bankId), Option(cc))
_ <- resourceDocsRequireRole match {
case false => Future()
Expand Down
20 changes: 10 additions & 10 deletions obp-api/src/main/scala/code/api/util/APIUtil.scala
Original file line number Diff line number Diff line change
Expand Up @@ -969,6 +969,14 @@ object APIUtil extends MdcLoggable with CustomJsonFormats{
}
}

/** only support en_GB, es_ES at the moment, will support more later*/
def obpLocaleValidation(value:String): String ={
if(value.equalsIgnoreCase("es_Es")|| value.equalsIgnoreCase("en_GB"))
SILENCE_IS_GOLDEN
else
ErrorMessages.ConsumerKeyIsInvalid
}

/** only A-Z, a-z, 0-9, all allowed characters for password and max length <= 512 */
/** also support space now */
def basicPasswordValidation(value:String): String ={
Expand Down Expand Up @@ -4882,16 +4890,8 @@ object APIUtil extends MdcLoggable with CustomJsonFormats{
apiCollectionIdParam: Option[String],
isVersion4OrHigher: Option[Boolean],
isStaticResource: Option[Boolean],
) = (requestedApiVersionString
+ bankId
+ tags
+ partialFunctions
+ locale.toString
+ contentParam
+ apiCollectionIdParam
+ isVersion4OrHigher
+ isStaticResource
).intern()
) = s"requestedApiVersionString:$requestedApiVersionString-bankId:$bankId-tags:$tags-partialFunctions:$partialFunctions-locale:${locale.toString}" +
s"-contentParam:$contentParam-apiCollectionIdParam:$apiCollectionIdParam-isVersion4OrHigher:$isVersion4OrHigher-isStaticResource:$isStaticResource".intern()


}
1 change: 1 addition & 0 deletions obp-api/src/main/scala/code/api/util/ErrorMessages.scala
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ object ErrorMessages {
val InvalidHttpMethod = "OBP-10037: Incorrect http_method."
val InvalidHttpProtocol = "OBP-10038: Incorrect http_protocol."
val ServiceIsTooBusy = "OBP-10040: The Service is too busy, please try it later."
val InvalidLocale = "OBP-10041: This locale is not supported. Only the following can be used: en_GB , es_ES."

// General Sort and Paging
val FilterSortDirectionError = "OBP-10023: obp_sort_direction parameter can only take two values: DESC or ASC!" // was OBP-20023
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -597,7 +597,7 @@ object JSONFactory1_4_0 extends MdcLoggable{
val userDefinedEndpointTags = getAllEndpointTagsBox(rd.operationId).map(endpointTag =>ResourceDocTag(endpointTag.tagName))
val resourceDocWithUserDefinedEndpointTags: ResourceDoc = rd.copy(tags = userDefinedEndpointTags++ rd.tags)

val cacheKey = (resourceDocWithUserDefinedEndpointTags.operationId + locale + isVersion4OrHigher).intern()
val cacheKey = s"operationId:${resourceDocWithUserDefinedEndpointTags.operationId}-locale:$locale- isVersion4OrHigher:$isVersion4OrHigher".intern()
val cacheValueFromRedis = Caching.getLocalisedResourceDocCache(cacheKey)

if(cacheValueFromRedis.isDefined){
Expand Down

0 comments on commit 0eb7fc1

Please sign in to comment.