From e755822ee9d31ecedbd6c2bdd82fde46d8cf3009 Mon Sep 17 00:00:00 2001 From: Hongwei Date: Thu, 30 Mar 2023 11:11:44 +0200 Subject: [PATCH 1/4] refactor/removed unused props defaultAuthProvider --- README.md | 11 +++++------ src/main/resources/props/sample.props.template | 3 --- src/main/scala/bootstrap/liftweb/Boot.scala | 4 ---- src/main/scala/code/lib/ObpAPI.scala | 2 -- 4 files changed, 5 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index c7a6151a..f6435ca1 100644 --- a/README.md +++ b/README.md @@ -74,7 +74,7 @@ Note: You may need to add the pluginGroup to the $HOME/.m2/settings.xml * Navigate to test/scala/RunWebApp. You may see a Setup Scala SDK link. Click this and check Scala 2.11.8 or so. -* In src/main/resources/props create a \.props (or default.props) for development. For localhost set api_hostname=http://127.0.0.1:8080, defaultAuthProvider=http://127.0.0.1:8080, base_url=http://localhost:8082, dev.port=8082 and oauth keys (i.e. obp_consumer_key and obp_consumer_secret) obtained from the api (at /consumer-registration) to run [OBP-API](https://github.com/OpenBankProject/OBP-API) in your local environment. +* In src/main/resources/props create a \.props (or default.props) for development. For localhost set api_hostname=http://127.0.0.1:8080, base_url=http://localhost:8082, dev.port=8082 and oauth keys (i.e. obp_consumer_key and obp_consumer_secret) obtained from the api (at /consumer-registration) to run [OBP-API](https://github.com/OpenBankProject/OBP-API) in your local environment. * Now **Rebuild** the project so everything is compiled. At this point you may need to select the SDK, see above. @@ -124,12 +124,11 @@ The keys are obtained by registering as a developer on the Open Bank Project API All in all, a props file could look something like: -api_hostname=https://api.openbankproject.com/api \ -defaultAuthProvider=https://api.openbankproject.com/api +api_hostname=https://api.openbankproject.com/api + ### OR -The base url of the api to use for local: \ -api_hostname=http://127.0.0.1:8080 \ -defaultAuthProvider=http://127.0.0.1:8080 +The base url of the api to use for local: +api_hostname=http://127.0.0.1:8080 obp_consumer_key=uodsifnodsfifdsliufdsliufdsfdsfsdfsx \ obp_secret_key=iuesbfiyvglxzgifg7eisgei7fglesfi \ diff --git a/src/main/resources/props/sample.props.template b/src/main/resources/props/sample.props.template index fe102c98..204faca7 100644 --- a/src/main/resources/props/sample.props.template +++ b/src/main/resources/props/sample.props.template @@ -8,9 +8,6 @@ # The base url of the api to use (e.g. https://apisandbox.openbankproject.com) api_hostname=FILL_ME_IN -# For now, this value should be identical to the value of api_hostname -defaultAuthProvider=FILL_ME_IN - # These are the oauth keys obtained from the api (at /consumer-registration) obp_consumer_key=FILL_ME_IN obp_secret_key=FILL_ME_IN diff --git a/src/main/scala/bootstrap/liftweb/Boot.scala b/src/main/scala/bootstrap/liftweb/Boot.scala index 9fc34e3d..2bda893c 100755 --- a/src/main/scala/bootstrap/liftweb/Boot.scala +++ b/src/main/scala/bootstrap/liftweb/Boot.scala @@ -166,10 +166,6 @@ class Boot extends MdcLoggable{ firstChoicePropsDir.toList.flatten ::: secondChoicePropsDir.toList.flatten } - if(Helper.getPropsValue("defaultAuthProvider").isEmpty) { - throw new Exception("defaultAuthProvider must be specified in the props file!") - } - def check(bool: Boolean) : Box[LiftResponse] = { if(bool){ Empty diff --git a/src/main/scala/code/lib/ObpAPI.scala b/src/main/scala/code/lib/ObpAPI.scala index d4bcee2f..7da3f4a0 100644 --- a/src/main/scala/code/lib/ObpAPI.scala +++ b/src/main/scala/code/lib/ObpAPI.scala @@ -46,8 +46,6 @@ object ObpAPI extends Loggable { implicit val formats = DefaultFormats val dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ") - val defaultProvider = Helper.getPropsValue("defaultAuthProvider").getOrElse("") - val userNotFoundError = "user (\\S+) at provider (\\S+) not found".r final val AccountUrlPath = "/accounts/" From 594d6578f5a9e8e4cbb52e3fe4b61ad74ba534bf Mon Sep 17 00:00:00 2001 From: Hongwei Date: Tue, 18 Apr 2023 18:49:21 +0800 Subject: [PATCH 2/4] refactor/tweaked the backend error messages --- src/main/scala/code/snippet/ApiExplorer.scala | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/scala/code/snippet/ApiExplorer.scala b/src/main/scala/code/snippet/ApiExplorer.scala index d694e1bb..84d8c772 100644 --- a/src/main/scala/code/snippet/ApiExplorer.scala +++ b/src/main/scala/code/snippet/ApiExplorer.scala @@ -1046,7 +1046,7 @@ WIP to add comments on resource docs. This code copied from Sofit. val jsEnabledBtn = s"jQuery('input[name=$name]').removeAttr('disabled')" //We call the getApiCollectionsForCurrentUser endpoint again, to make sure we already created or delete the record there. val apiFavouriteCollection = ObpAPI.getApiCollection("Favourites") - val errorMessage = if(apiFavouriteCollection.isInstanceOf[Failure]) apiFavouriteCollection.asInstanceOf[Failure].messageChain else "" + val errorMessage = if(apiFavouriteCollection.isInstanceOf[Failure]) "Error happened on backend: "+apiFavouriteCollection.asInstanceOf[Failure].messageChain else "" if(apiFavouriteCollection.isInstanceOf[Failure]){ // If the user is not logged in, we do not need call any apis calls. (performance enhancement) @@ -1060,7 +1060,7 @@ WIP to add comments on resource docs. This code copied from Sofit. }else{ if (myWebpageOperationIds.contains(favouritesOperationIdFromWebpage)) { //If we already have this operationId, we need to delete it val deletedBox = ObpAPI.deleteMyApiCollectionEndpoint("Favourites",favouritesOperationIdFromWebpage) - val deleteErrorMessage = if(deletedBox.isInstanceOf[Failure]) deletedBox.asInstanceOf[Failure].messageChain else "" + val deleteErrorMessage = if(deletedBox.isInstanceOf[Failure]) "Error happened on backend: "+ deletedBox.asInstanceOf[Failure].messageChain else "" if (deletedBox.isInstanceOf[Failure]){ SetHtml(s"favourites_error_message_${favouritesOperationIdFromWebpage}", Text(deleteErrorMessage)) & Run(jsEnabledBtn) @@ -1069,7 +1069,7 @@ WIP to add comments on resource docs. This code copied from Sofit. } } else {//If we do not have this operationId, we need to create it. val createdBox = ObpAPI.createMyApiCollectionEndpoint("Favourites",favouritesOperationIdFromWebpage) - val createdErrorMessage = if(createdBox.isInstanceOf[Failure]) createdBox.asInstanceOf[Failure].messageChain else "" + val createdErrorMessage = if(createdBox.isInstanceOf[Failure]) "Error happened on backend: "+ createdBox.asInstanceOf[Failure].messageChain else "" if (createdBox.isInstanceOf[Failure]){ SetHtml(s"favourites_error_message_${favouritesOperationIdFromWebpage}", Text(createdErrorMessage)) & Run(jsEnabledBtn) @@ -1565,7 +1565,7 @@ WIP to add comments on resource docs. This code copied from Sofit. ResourceStyleCss #> s"${DisplayEqualNone}" & ResourceErrorStyleCss #> s"${DisplayEqualBlock}" & ContentBoxHeadline #> { - allResourcesBox.asInstanceOf[Failure].msg + "Error happened on backend: "+allResourcesBox.asInstanceOf[Failure].msg }& { if(allResourcesBox.asInstanceOf[Failure].msg.contains("CanReadResourceDoc")){ From befd4da0cfcf022497e031441960a8d74f2e099e Mon Sep 17 00:00:00 2001 From: Hongwei Date: Wed, 19 Apr 2023 18:40:02 +0800 Subject: [PATCH 3/4] feature/added the error handling for the oauth --- src/main/scala/code/lib/OAuthClient.scala | 16 +++++++++++++--- src/main/scala/code/snippet/Login.scala | 17 +++++++++++++---- 2 files changed, 26 insertions(+), 7 deletions(-) diff --git a/src/main/scala/code/lib/OAuthClient.scala b/src/main/scala/code/lib/OAuthClient.scala index 96941231..f8ea3613 100644 --- a/src/main/scala/code/lib/OAuthClient.scala +++ b/src/main/scala/code/lib/OAuthClient.scala @@ -189,10 +189,20 @@ object OAuthClient extends MdcLoggable { logger.debug("redirect says: credential.provider: " + credential.provider) logger.debug("redirect says: oauthcallbackUrl: " + oauthcallbackUrl) credential.consumer.setMessageSigner(new HmacSha256MessageSigner()) - val authUrl = provider.oAuthProvider.retrieveRequestToken(credential.consumer, oauthcallbackUrl) - logger.debug("redirect says: authUrl: " + authUrl) + val authUrlBox = tryo {provider.oAuthProvider.retrieveRequestToken(credential.consumer, oauthcallbackUrl)} + + if(authUrlBox.isInstanceOf[Failure]) { + val errorMessage = "Critical exception happened on the backend: " + authUrlBox.asInstanceOf[Failure].messageChain + logger.error(errorMessage) + throw new Exception(errorMessage) + } else if(authUrlBox.isEmpty){ + logger.error("Critical exception happened on backend: oauth callback Url is empty! Please check the consumer key and secret first.") + throw new Exception("Critical exception happened on backend: oauth callback Url is empty! Please check the consumer key and secret first.") + } else{ + logger.debug("redirect says: authUrlBox: " + authUrlBox.head) + S.redirectTo(authUrlBox.head) + } - S.redirectTo(authUrl) } def redirectToConnectBankAccount() = { diff --git a/src/main/scala/code/snippet/Login.scala b/src/main/scala/code/snippet/Login.scala index 07385221..bfd0f1f4 100644 --- a/src/main/scala/code/snippet/Login.scala +++ b/src/main/scala/code/snippet/Login.scala @@ -35,10 +35,10 @@ package code.snippet import net.liftweb.http.js.JsCmd import net.liftweb.util.Helpers import Helpers._ -import net.liftweb.http.SHtml +import net.liftweb.http.{ResponseShortcutException, SHtml} import code.lib.{OAuthClient, ObpAPI} import net.liftweb.common.Box -import net.liftweb.http.js.JsCmds.Noop +import net.liftweb.http.js.JsCmds.{Alert, Noop} class Login { private def getDisplayNameOfUser(): Box[String] = { @@ -69,8 +69,17 @@ class Login { ".logged-in *" #> "" & "#start-login [onclick]" #> { def actionJS: JsCmd = { - OAuthClient.redirectToOauthLogin() - Noop + try { + OAuthClient.redirectToOauthLogin() + } + catch { + //this is the Liftweb redirect mechanism, it will throw the ResponseShortcutException. + case e: ResponseShortcutException => + OAuthClient.redirectToOauthLogin() + Noop + case e: Throwable => + Alert(e.getMessage) + } } SHtml.onEvent((s: String) => actionJS) } From cf123149b034f845ec90f4cdfcabf4738b0d8beb Mon Sep 17 00:00:00 2001 From: Hongwei Date: Fri, 21 Apr 2023 20:29:16 +0800 Subject: [PATCH 4/4] docfix/added the list of Endpoints that API Explorer used to README.md --- README.md | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/README.md b/README.md index f6435ca1..57105767 100644 --- a/README.md +++ b/README.md @@ -133,3 +133,27 @@ api_hostname=http://127.0.0.1:8080 obp_consumer_key=uodsifnodsfifdsliufdsliufdsfdsfsdfsx \ obp_secret_key=iuesbfiyvglxzgifg7eisgei7fglesfi \ base_url=http://localhost:8082 + +## The list of Endpoints that API Explorer needs to function + +### Before Login: +Get API Info (root): /obp/v4.0.0/root \ +Get Banks: /obp/v3.1.0/banks \ +Get Resource Docs: /obp/v4.0.0/resource-docs/OBPv5.1.0/obp \ +Get Glossary of the API: /obp/v3.0.0/api/glossary \ +Get all Authentication Type Validations - public: /obp/v4.0.0/endpoints/authentication-type-validations \ +Get all JSON Schema Validations: /obp/v4.0.0/endpoints/json-schema-validations \ +Get Message Docs: /obp/v2.2.0/message-docs/CONNECTOR + +### After Login +Oauth1.0: /oauth/initiate \ +Oauth1.0: /oauth/token + +Get private accounts at all banks (Authenticated access): /obp/v1.2.1/accounts/private \ +Get Entitlements for the current User: /obp/v3.0.0/my/entitlements \ +Get Entitlement Requests for the current User: /obp/v3.0.0/my/entitlement-requests \ +Get My Spaces: /obp/v4.0.0/my/spaces \ +Get Api Collection Endpoints: /obp/v4.0.0/api-collections//api-collection-endpoints \ +Get My Api Collection Endpoints: /obp/v4.0.0/my/api-collections/Favourites/api-collection-endpoints \ +Get My Api Collections: /obp/v4.0.0/my/api-collections \ +Get User (Current): /obp/v3.0.0/users/current \ No newline at end of file