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 AppManager publisher, when it need to get an access token to call the registered APIs.
You can see the consumer/secret key pair generated by AppManager, once you go to the overview page of published application. See the image bellow.
xxxxxx insert image here xxxxxx
[2] AppManager itself provides a token API. Web applications need to call this token API with the consumer, secret key pair [WCk1, WCSk1] given by the AppManager, when they need to get an access token for a particular API.
Note: This token API is deployed in the location of {AppM_HOME}/repository/deployment/server/synapse-configs/default/api/_TokenAPI_.xml. If AppManager is running with some port offset, then the port defined inside _TokenAPI_.xml need to be changed accordingly.
Example of invoking AppManager Token API from Web-App to get an access token is given below.
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 need to be the alias name given when registering the API details in AppManager publisher.
- applicationToken is the base64 encoded value of consumer/secret key pair provided by AppManager.
- In the payload, it need 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 and apiAlias as comma separated strings.
- " http://localhost:8280/token " is the token API provided by AppManager. It need to change with port offset accordingly.