...
The Key Manager decouples the OAuth client and access token management from the rest of its operations, so that you can plug in a third-party OAuth provider for managing OAuth clients and access tokens. When working with an external Key Manager, you need to extend the required Key Manager interface(s), which are explained below, based on your requirements.
- Key Manager interface - This interface handles functionalities functionalities of the API Store. It contains methods to create, update, get, and delete OAuth2 applications, to map the existing consumer keys and secrets, and to generate the application access tokens. For more information, see Extending the Key Manager Interface.
- Key Validation handler - This interface handles functionalities of the Key Manager component. This interface It contains methods to implement at the API runtime to validate the token, subscriptions, and scopes, and also to generate JSON Web Tokens (JWTs). For more information, see Extending Key Validation.
...
In this guide, we explain how to integrate WSO2 API Store with an external Identity and Access Management (IAM) by using the Surf OAuth Authorization Server, which is an open source IAM, to manage the OAuth clients and tokens required by WSO2 API Manager. We have a sample client implementation that consumes APIs exposed by Surf OAuth.
...
Download the binary located here and deploy it in a Tomcat server.
Info Alternatively, you can build the OAuth Server from scratch and start the server by issuing the mvn jetty:run command in the
api-authorization-server-war
folder. Detailed Detailed steps for building and starting the server are provided at here.Tip Tip: The Surf OAuth Web web application that you just downloaded has the following customizations:
- The
apis.application.properties
file is copied to the classpath. - All the URLs starting with
localhost
are replaced by the loop back IP (127.0.0.1). org.surfnet.oaaas.noop.NoopAuthenticator
authenticator is set as the default authenticator.- Token expiry time is increased to 99999 seconds. This ensures that the tokens issued for the Web web client lasts last several months.
- The
Move the Web web application to the ROOT context to ensure that the Surf Oauth Web web applications works work on Tomcat.
Code Block rm -rf tomcat7/webapps/ROOT mv tomcat7/webapps/surf-oauth tomcat7/webapps/ROOT
- Access
http://127.0.0.1:8080/
to see the following page:
The server is now up and running. - Create a Resource ServerFollow the steps below to create a resource server.
- In Surf OAuth UI, click the Resource Servers link where all the OAuth clients are grouped together.
- Register a resource server representing WSO2 API Manager.
- Add two scopes named
test
andscope1
and save your changes.
You will use them when creating clients.
The front end is now registered as a distinct client with the authorization server.
- Create Follow the steps to create an OAuth Client.
- Click the Access TokenTokens link and note all the tokens issued for the Web web client.
These tokens are obtained at the time you sign in, by a Javascript client running on the browser. The same token is then used for subsequent operations.
- Pick an active access token from the above list.
You use it to create clients through WSO2 API Manager. - Get a registration endpoint that is needed to register the client.
As Surf OAuth doesn’t support a spec-compliant client registration yet, you can use an endpoint with similar capabilities. For example, as shown below, you can enable Developer Tools in Google Chrome to see the URL and the request:
- Click the Access TokenTokens link and note all the tokens issued for the Web web client.
...
Build the demo.client available at https://github.com/Rajith90/surf-oauth-demo/tree/v2.1.0.
Copy the JARs JAR files that you built into in to the
<API-M_HOME>/repository/components/lib
directory.Note If you are setting up a distributed environment, Copy the JARs copy and paste the JAR files that you built into in to the respective directories given below of in the Key Manager node and the Store node respectively.
API Key Manager -
<API-M_KEY_MANAGER_HOME>/repository/components/lib
API Store -
<API-M_STORE_HOME>/repository/components/lib
Uncomment the
<APIKeyManager>
element in the/repository/conf/api-manager.xml
file, which is in the API Key Manager and API Store and change the values based on your third-party implementation.Tip Tip: Be sure to replace the
<RegistrationEndpoint>
and<AccessToken>
elements with the client registration endpoint and the access token you obtained earlier in step 7 and 6. ConsumerKey and Secret should be that of the created Resource Serverresource server. Also change the<hostname>
in the<IntrospectionURL>
accordingly.Note The
nl.surfnet.demo.SurfOAuthClient
class, which is mentioned in the following example, extends the Key Manager interface.Code Block title Example <APIKeyManager> <KeyManagerClientImpl>nl.surfnet.demo.SurfOAuthClient</KeyManagerClientImpl> <Configuration> <RegistrationEndpoint><Give the client registration endpoint you got in step 7></RegistrationEndpoint> <AccessToken><Give the access token you got in step 6></AccessToken> <IntrospectionURL>http://<hostname>:port/v1/tokeninfo</IntrospectionURL> <ConsumerKey>xxx</ConsumerKey> <ConsumerSecret>xxx</ConsumerSecret> </Configuration> </APIKeyManager>
Tip For a sample on Key Manager implementation, see the WSO2 default Key Manager implementation.
...
- Start WSO2 API Manager.
Sign in to the WSO2 API Store and create an application.
Code Block curl -k -X POST -c cookies https://localhost:9443/store/site/blocks/user/login/ajax/login.jag -d 'action=login&username=admin&password=admin' curl -k -X POST -b cookies https://localhost:9443/store/site/blocks/application/application-add/ajax/application-add.jag -d 'action=addApplication&application=SurfClientApp&tier=Unlimited&description=&callbackUrl='
Register an OAuth client of the type PRODUCTION in the authorization server.
As shown below, you need to send the specific parameters required by the OAuth Server in JSON.Code Block curl -k -X POST -b cookies https://localhost:9443/store/site/blocks/subscription/subscription-add/ajax/subscription-add.jag -d 'action=generateApplicationKey&application=SurfClientApp&authorizedDomains=ALL&keytype=PRODUCTION&validityTime=3600&jsonParams={"scopes":["test"],"contactName":"John Doe","contactEmail":"john@doe.com"}'
- Go to the Client Applications link in the Surf OAuth UI.
Note the newly created client listed there.
You have now created an application and registered an OAuth Client corresponding to it. - Validate tokens by subscribing to a SurfClient application and obtaining a token.
- Sign in to the API Publisher and deploy the sample API (
PizzaShackAPI
) if you haven't done so already.
Assuming you still have the OAuth client created earlier, subscribe to this API as follows:
Code Block curl -k -X POST -b cookies https://localhost:9443/store/site/blocks/subscription/subscription-add/ajax/subscription-add.jag -d 'action=addAPISubscription&name=PizzaShackAPI&version=1.0.0&provider=admin&tier=Unlimited&applicationName=SurfClientApp'
Let's obtain a token from the OAuth Provider.
Go to the Edit view of the OAuth client and make sure the
client_credentials
grant type is enabled, and a token expiration time is specified.
Obtain a token.
Replace<ConsumerKey:ConsumerSecret>
with the Base64 encodedConsumerKey:ConsumerSecret
of the client application you just created.Code Block curl -k -d "grant_type=client_credentials&scope=test" -H "Authorization: Basic <ConsumerKey:ConsumerSecret>" -H "Content-Type: application/x-www-form-urlencoded" https://localhost:8243/token
- Update the token endpoint in the
<API-M_GATEWAY_HOME>/repository/deployment/server/synapse-configs/default/api/_TokenAPI_.xml
file accordingly. - Update the revoke endpoint in the
<API-M_GATEWAY_HOME>/repository/deployment/server/synapse-configs/default/api/_RevokeAPI_.xml
file accordingly. If you use the authorization code grant type to generate tokens, update the authorize endpoint in the
<API-M_GATEWAY_HOME>/repository/deployment/server/synapse-configs/default/api/AuthorizeAPI.xml
file accordingly.Info The Token endpoint format for above e, f, and g steps is: http://<surf-hostname>:port/v1/token. A sample change done in _TokenAPI_.xml is as follows:
Code Block language xml <api xmlns="http://ws.apache.org/ns/synapse" name="_WSO2AMTokenAPI_" context="/token"> ... <endpoint> <http uri-template="http://<surf-hostname>:port/v1/token"> <timeout> <duration>60000</duration> <responseAction>fault</responseAction> </timeout> </http> </endpoint> ... </api>
Invoke the API using the token obtained.
Code Block curl -k -H "Authorization: Bearer 02316379-8c19-4d72-94d1-6306ea2703a4" "https://localhost:8243/pizzashack/1.0.0/menu"
- Sign in to the API Publisher and deploy the sample API (