Skip to content

Commit

Permalink
Merge pull request #2288 from constantine2nd/develop
Browse files Browse the repository at this point in the history
Echo headers, locale
  • Loading branch information
simonredfern authored Oct 9, 2023
2 parents 860a33f + a6fc64a commit 5af1625
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 28 deletions.
6 changes: 3 additions & 3 deletions obp-api/src/main/scala/bootstrap/liftweb/Boot.scala
Original file line number Diff line number Diff line change
Expand Up @@ -665,9 +665,9 @@ class Boot extends MdcLoggable {
ObpS.param(PARAM_LOCALE) match {
case Full(requestedLocale) if requestedLocale != null && APIUtil.checkShortString(requestedLocale)==SILENCE_IS_GOLDEN => {
val computedLocale: Locale = I18NUtil.computeLocale(requestedLocale)
val sessionId = S.session.map(_.uniqueId).openOr("")
AuthUser.updateComputedLocale(sessionId, computedLocale.toString())
S.addCookie(HTTPCookie(localeCookieName, requestedLocale))
// Simon: if we are not using resource_user.last_used_local we don't need to set it. It is not returned in the Agent User endpoint. Thus, for now, we don't need to set it in the database.
// val sessionId = S.session.map(_.uniqueId).openOr("")
// AuthUser.updateComputedLocale(sessionId, computedLocale.toString())
computedLocale
}
case _ => currentLocale
Expand Down
4 changes: 2 additions & 2 deletions obp-api/src/main/scala/code/api/util/APIUtil.scala
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ object APIUtil extends MdcLoggable with CustomJsonFormats{

def hasDirectLoginHeader(authorization: Box[String]): Boolean = hasHeader("DirectLogin", authorization)

def has2021DirectLoginHeader(requestHeaders: List[HTTPParam]): Boolean = requestHeaders.find(_.name == "DirectLogin").isDefined
def has2021DirectLoginHeader(requestHeaders: List[HTTPParam]): Boolean = requestHeaders.find(_.name.toLowerCase == "DirectLogin".toLowerCase()).isDefined

def hasAuthorizationHeader(requestHeaders: List[HTTPParam]): Boolean = requestHeaders.find(_.name == "Authorization").isDefined

Expand Down Expand Up @@ -688,7 +688,7 @@ object APIUtil extends MdcLoggable with CustomJsonFormats{
getPropsAsBoolValue("echo_request_headers", defaultValue = false)
(callContext, echoRequestHeaders) match {
case (Some(cc), true) =>
CustomResponseHeaders(cc.requestHeaders.map(item => (s"ECHO_${item.name}", item.values.head)))
CustomResponseHeaders(cc.requestHeaders.map(item => (s"echo_${item.name}", item.values.head)))
case _ =>
CustomResponseHeaders(Nil)
}
Expand Down
20 changes: 0 additions & 20 deletions obp-api/src/main/scala/code/snippet/WebUI.scala
Original file line number Diff line number Diff line change
Expand Up @@ -66,34 +66,14 @@ class WebUI extends MdcLoggable{
}

def currentPage = {
def replaceLocale(replacement: String) = {
ObpS.queryString.isDefined match {
case true =>
ObpS.queryString.exists(_.contains("locale=")) match {
case true =>
val queryString = ObpS.queryString
queryString.map(
_.replaceAll("locale=en_GB", replacement)
.replaceAll("locale=es_ES", replacement)
)
case false =>
ObpS.queryString.map(i => i + s"&$replacement")
}
case false =>
Full(s"$replacement")
}
}.getOrElse("")

val supportedLocales = APIUtil.getPropsValue("supported_locales","en_GB,es_ES").split(",")
def displayLanguage(locale: String) = {
val hyphenLocale = locale.replace("_", "-")
if (supportedLocales.contains(locale) || supportedLocales.contains(hyphenLocale) ) {""} else {"none"}
}
val page = Constant.HostName + ObpS.uri
val language = I18NUtil.currentLocale().getLanguage()

"#es a [href]" #> scala.xml.Unparsed(s"${page}?${replaceLocale("locale=es_ES")}") &
"#en a [href]" #> scala.xml.Unparsed(s"${page}?${replaceLocale("locale=en_GB")}") &
"#es a [style]" #> s"display: ${displayLanguage("es_ES")}" &
"#locale_separator [style]" #> {if(supportedLocales.size == 1) "display: none" else ""} &
"#en a [style]" #> s"display: ${displayLanguage("en_GB")}" &
Expand Down
7 changes: 7 additions & 0 deletions obp-api/src/main/webapp/media/js/website.js
Original file line number Diff line number Diff line change
Expand Up @@ -441,3 +441,10 @@ function copyConsumerRegistrationResultToClipboard(element) {
// Revert the original values of text and icon after 2 seconds
revertTextAndClassDeferred(titleText, iconClass);
}

function setCookie(cName, cValue) {
if(cName) {
document.cookie = cName + "=" + cValue + ";" + "SameSite=Lax";
location.reload();
}
}
4 changes: 2 additions & 2 deletions obp-api/src/main/webapp/templates-hidden/default.html
Original file line number Diff line number Diff line change
Expand Up @@ -244,9 +244,9 @@
<br>
<div class="language-tag" data-lift="WebUI.currentPage">
<span><a href="#">Language</a></span>
<span><a id="en" href="/">EN</a></span>
<span><a onclick="setCookie('SELECTED_LOCALE', 'en_GB')" id="en" href="/">EN</a></span>
<span id="locale_separator">|</span>
<span><a id="es" href="/">ES</a></span>
<span><a onclick="setCookie('SELECTED_LOCALE', 'es_ES')" id="es" href="/">ES</a></span>
</div>
<div id="copyright">
<a href="http://openbankproject.com"><lift:loc locid="open_bank_project_is">Open Bank Project is &copy;2011 - </lift:loc> <span id="copyright-year" data-lift="WebUI.currentYearText">2018</span> </a> <a href="http://tesobe.com"><lift:loc locid="and_commercial_licenses">TESOBE and distributed under the AGPL and commercial licenses. </lift:loc></a>
Expand Down
40 changes: 39 additions & 1 deletion obp-api/src/test/scala/code/api/DirectLoginTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,7 @@ class DirectLoginTest extends ServerSetup with BeforeAndAfter {



scenario("Test oly last issued token is valid", ApiEndpoint2) {
scenario("Test the last issued token is valid as well as a previous one", ApiEndpoint2) {

When("The header and credentials are good")
val request = directLoginRequest
Expand Down Expand Up @@ -489,6 +489,44 @@ class DirectLoginTest extends ServerSetup with BeforeAndAfter {
}


scenario("Test DirectLogin header value is case insensitive", ApiEndpoint2) {

When("The header and credentials are good")
val request = directLoginRequest
val response = makePostRequestAdditionalHeader(request, "", validHeaders)
var token = ""
Then("We should get a 201 - OK and a token")
response.code should equal(201)
response.body match {
case JObject(List(JField(name, JString(value)))) =>
name should equal("token")
value.length should be > 0
token = value
case _ => fail("Expected a token")
}

val headerWithToken = ("dIreCtLoGin", "token=%s".format(token))
val validHeadersWithToken = List(accessControlOriginHeader, headerWithToken)
When("When we use the token to get current user and it should work - New Style")
val requestCurrentUserNewStyle = baseRequest / "obp" / "v3.0.0" / "users" / "current"
val responseCurrentUserNewStyle = makeGetRequest(requestCurrentUserNewStyle, validHeadersWithToken)
And("We should get a 200")
responseCurrentUserNewStyle.code should equal(200)
val currentUserNewStyle = responseCurrentUserNewStyle.body.extract[UserJsonV300]
currentUserNewStyle.username shouldBe USERNAME

When("When we issue a new token")
makePostRequestAdditionalHeader(request, "", validHeaders)
Then("The previous one should be valid")
val secondResponse = makeGetRequest(requestCurrentUserNewStyle, validHeadersWithToken)
And("We should get a 200")
secondResponse.code should equal(200)
// assertResponse(failedResponse, DirectLoginInvalidToken)


}


}

private def assertResponse(response: APIResponse, expectedErrorMessage: String): Unit = {
Expand Down

0 comments on commit 5af1625

Please sign in to comment.