There can be web applications internally calling the OAuth secured APIs. Once web applications are published and accessed through the AppManager gateway, user is authenticated using the SAML SSO. In this feature, we can use the same SAML token generated by the App Manager gateway and get an OAuth2 access token by calling the token endpoints used by these APIs.
The diagram below depicts this scenario.
Prerequisites
Make sure that following prerequisites are met before following the instructions on publishing the web application for obtaining an OAuth2 access token.
Configuring the identity provider of App Manager
App Manager uses WSO2 Identity Server as the default service provider for SAML SSO authentication. You need to create a service provider in IDP for each web application that is published through the App Manager (i.e to get the SAML SSO authentication for web applications).
If a particular web application is registered to obtain a OAuth2 token using the SAML response generated from the SSO authentication, when creating the service provider for this web application, it needs to give the following mandatory details in service provider.
- Enable Response Signing
- Enable Assertion Signing
- Enable Audience Restriction
You need to add API provider’s token endpoint as a Audience Restrict parameter.
See the below image for configuring WSO2 Identity Server.
Configuring the API provider of App Manager
WSO2 App Manager uses WSO2 API-Manager as the API Provider. In order to provide a OAuth token using the SAML token provided by the IDP of App Manager (WSO2 IS), APIManager needs to include, IS as a trusted IDP provider. Configure WSO2 APIManager as follows.
Publishing the web application for obtaining an OAuth2 access token
If a particular web application needs accessing OAuth secured APIs internally, it needs to provide the following OAuth parameters of the APIs when publishing the web application in AppManager, as depicted below.
- API Token Endpoint - URL of token endpoint used by the APIs.
- API Consumer Key - the consumer keys of the OAuth APIs.
- API Consumer Secret - the consumer secret keys of the OAuth APIs.
- API Name - alias name for the APIs.
Once the web application is created on App Manager, it will wrap these details and generate a new consumer/secret key pair for the web application [WCk1, WCSk1]. Actual web application can use this consumer/secret key pair generated by the App Manager publisher, when it need to get an access token to call the registered APIs.
You can see the consumer/secret key pair generated by App Manager, in the overview page of published applications as depicted in the below image.
xxxxxx insert image here xxxxxx
Invoking App Manager Token API from web app
App Manager itself provides a token API. Web applications need to call this token API with the consumer/secret key pair [WCk1, WCSk1] provided by the App Manager, when they need to get an access token for a particular API.
This token API is deployed in the <PRODUCT_HOME>/repository/deployment/server/synapse-configs/default/api/_TokenAPI_.xml
file. If App Manager is running with a port offset, then the port defined inside the _TokenAPI_.xml
file needs to be changed accordingly.
Use the following parameters and values to invoke AppManager Token API from the web application, to obtain an access token.
String apiAlias = "pizzashack"; String applicationToken = base64Encode(WCk1 + ":" + WCSk1); String payload = "grant_type=SAML2&scope=" + samlTokenId + "," + apiAlias; httpClient.doPost("http://localhost:8280/token", applicationToken, payload, "application/x-www-form-urlencoded");
- apiAlias - the alias name given when registering the API details in App Manager publisher.
- applicationToken - the base64 encoded value of consumer/secret key pair provided by App Manager.
- String payload - the payload needs to send the grant type and the scope value. Grant type need to be set as SAML2. Scope should contain the value of
samlTokenId
cookie andapiAlias
as comma separated strings. - httpClient.doPost - the token API provided by App Manager. Change it with the define port offset accordingly.