OAuth2 Provider Module Example
The OAuth2 Provider module supports a Mule runtime engine (Mule) application to be configured as an Authentication Manager in an OAuth2 authorization framework. By using this module, the application can register clients, authenticate registered clients, grant tokens, validate tokens, and delete clients. The following example shows how to configure the Create client and Validate token operations, that create and validate client credentials.
-
In Studio, drag the HTTP Listener operation to the Studio canvas.
-
Set Path to
/createClient
. -
Set the Connector configuration to your desired HTTP configuration.
-
Drag the Create client operation alongside the HTTP Listener operation.
-
Set Module configuration to your desired OAuth2 provider module configuration.
-
Set the following parameters:
-
Client id:
attributes.headers.client_id
-
Type:
CONFIDENTIAL
-
Secret:
attributes.headers.client_secret
-
Client name:
attributes.headers.client_name
-
Description:
””
-
Principal:
””
-
Redirect uris:
”demo.com”
-
Authorized grant types:
”CLIENT_CREDENTIALS”
-
-
Drag a Set Payload component alongside the Create client operation.
-
Set Value to
Client Created
. -
Drag a new HTTP Listener source to a new flow.
-
Set Path to
/validate
. -
Drag a Validate token operation alongside the Listener source.
-
Set Module configuration to your desired OAuth2 provider module configuration.
-
Set Access token to
(attributes.headers['authorization'] splitBy ' ')[1]
. -
Drag a Transform Message component alongside the Validate token operation.
-
In the output display, add the following DataWeave code:
%dw 2.0 output application/json --- { "Message": "Successfully validated" }
-
Save and run your Mule app.
Test your Mule App
To test the Mule app, run the following commands:
http://oauthtest01.us-e2.cloudhub.io/createClient
Method: GET
Param: client_id, client_secret, client_name
For example, setting the GET parameters fields with the following values client_id = 123
, client_secret = 1234
, client_name = abc
Returns the following result:
{
"Message": "Client Created"
}
http://oauthtest01.us-e2.cloudhub.io/token
Method: POST
Param: client_id, client_secret, grant_type
For example, setting the POST parameters fields with the following values: client_id = 123
, client_secret = 1234
, grant_type = CLIENT_CREDENTIALS
Returns the following result:
{"access_token":"s143Barj7xpQP3_AGWvHRyZxFNEcgJ55XB1I3Yquj8kgem2qxqa5TpouLG3gQCr2sj170Ci1MqLd5gzF4AUpRQ",
"token_type":"Bearer",
"expires_in":86400}
Another option is to run the validate command:
http://oauthtest01.us-e2.cloudhub.io/validate
Method: GET
Param: Authorization
-
Put the
token_type
received from the response data which is "Bearer". -
Add a space.
-
Add the
access_token
received from the response data.
For example, the Authorization equals to the Bearer:
Bearer s143Barj7xpQP3_AGWvHRyZxFNEcgJ55XB1I3Yquj8kgem2qxqa5TpouLG3gQCr2sj170Ci1MqLd5gzF4AUpRQ
Returns the following result:
{
"Message": "Successfully validated"
}
If the response is not valid, the result is the following:
The token received: s143BarjxpQP3_AGWvHRyZxFNEcgJ55XB1I3Yquj8kgem2qxqa5TpouLG3gQCr2sj170Ci1MqLd5gzF4AUpRQ, is not valid.