Contact Us 1-800-596-4880

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.

OAuth provider studio flow
  1. In Studio, drag the HTTP Listener operation to the Studio canvas.

  2. Set Path to /createClient.

  3. Set the Connector configuration to your desired HTTP configuration.

  4. Drag the Create client operation alongside the HTTP Listener operation.

  5. Set Module configuration to your desired OAuth2 provider module configuration.

  6. 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”

  7. Drag a Set Payload component alongside the Create client operation.

  8. Set Value to Client Created.

  9. Drag a new HTTP Listener source to a new flow.

  10. Set Path to /validate.

  11. Drag a Validate token operation alongside the Listener source.

  12. Set Module configuration to your desired OAuth2 provider module configuration.

  13. Set Access token to (attributes.headers['authorization'] splitBy ' ')[1].

  14. Drag a Transform Message component alongside the Validate token operation.

  15. In the output display, add the following DataWeave code:

    %dw 2.0
    output application/json
    ---
    {
    	"Message": "Successfully validated"
    }
  16. 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

  1. Put the token_type received from the response data which is "Bearer".

  2. Add a space.

  3. 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.
View on GitHub