Introduction
This article explains how to prototype Mule OAuth 2.0 client application with grant type of "Client Credentials", which is most popular grant type for Mule integration. Most modern APIs enforce OAuth2.0 security policies. OAuth2.0 has the following Grant Types (The detailed explanation can be found here):- Authorization Code
- PKCE
- Client Credentials
- Device Code
- Refresh Token
- Legacy: Implicit Flow
- Legacy: Password Grant
- Using postman to retrieve access token
- Prototyping retrieve access token using cURL
- Mule flow to retrieve access token
- Sample flow using the access token with caching scope
OAuth 2.0 With Client Credential Grant Type
In order to access the OAuth2.0 enabled APIs, we first have to retrieve the access token from the Identity Providers. Then we can access the API by passing the access token. The parameters for Client Credential can be the following:- grant_type (required)
- scope (optional)
- client_id (required)
- client_secret (required)
1 | echo -n "${CLIENT_ID}:${CLIENT_SECRET}" | base64 |
1 | echo -n "${CLIENT_ID}:${CLIENT_SECRET}" | openssl enc -base64 |
Using Postman to Retrieve Access Token
The postman is the best tool to do prototyping for the OAuth 2 client. The following snapshot shows the setup of the Postman:for body: for Headers: for Authorization:
cURL Solution
Once we have the postman, the solution of cUrl is very straight forward.1 2 3 4 5 6 7 8 9 10 11 12 13 | $ cat oauth2-client.sh #!/bin/bash # CLIENT_ID=MY-CLIENT-ID-GOES-HERE-WITHOUT-QUOTE CLIENT_SECRET=YOUR-CLIENT-SECRET-GOES-HERE-WITHOUT-QUOTE OAUTH_HEADER=$( echo -n "${CLIENT_ID}:${CLIENT_SECRET}" | base64) -H "Content-Type: application/x-www-form-urlencoded" \ -H "Authorization: Basic ${OAUTH_HEADER}" \ -XPOST https: //login .microsoftonline.com /keurig .onmicrosoft.com /oauth2/v2 .0 /token |
Mule Application Solution - Retrieve access_token
The mule application flow for retrieving access token is the following: The Data-Weave transformation code is the following:1 2 3 4 5 6 7 | %dw 2.0 output application/x-www-form-urlencoded --- { grant_type: "client_credentials" , } |
1 2 3 4 5 6 7 | < http:request method = "POST" doc:name = "Request" doc:id = "65997138-84c0-48b3-8347-68abf386b3a1" config-ref = "HTTP_Request_configuration" path = "/keurig.onmicrosoft.com/oauth2/v2.0/token" > < http:headers > <!--[CDATA[#[output application/java --- { "Content-Type" : "application/x-www-form-urlencoded" }]]]--> </ http:headers > </ http:request > |
1 2 3 4 5 6 7 8 9 10 | < http:request-config name = "HTTP_Request_configuration" doc:name = "HTTP Request configuration" doc:id = "fb6e2c7d-010f-4141-bafe-a4a50c4fc540" > < http:request-connection protocol = "${oauth2.protocol}" host = "${oauth2.host}" port = "${oauth2.port}" > < reconnection > < reconnect frequency = "5000" > </ reconnect ></ reconnection > < http:authentication > < http:basic-authentication username = "${secure::oauth2.client.id}" password = "${secure::oauth2.client.secret}" > </ http:basic-authentication ></ http:authentication > </ http:request-connection > </ http:request-config > |
1 2 3 4 5 6 | { "token_type" : "Bearer" , "expires_in" : 3599, "ext_expires_in" : 3599, "access_token" : "eyJ0eXAi......" } |
I am impressed. I don't think Ive met anyone who knows as much about this subject as you do. You are truly well informed and very intelligent. You wrote something that people could understand and made the subject intriguing for everyone. Really, great blog you have got here
ReplyDeleteBCOM 1st Year Exam TimeTable 2020
BCOM 2nd Year Exam Schedule 2020
BCOM 3rd Year Exam Date Sheet 2020
Do you realize there's a 12 word sentence you can speak to your partner... that will induce intense emotions of love and impulsive attractiveness to you buried inside his chest?
ReplyDeleteBecause deep inside these 12 words is a "secret signal" that triggers a man's instinct to love, worship and care for you with all his heart...
====> 12 Words That Fuel A Man's Desire Instinct
This instinct is so hardwired into a man's mind that it will make him try harder than ever before to to be the best lover he can be.
Matter-of-fact, fueling this powerful instinct is so binding to getting the best ever relationship with your man that the moment you send your man one of the "Secret Signals"...
...You will soon notice him open his soul and heart for you in such a way he's never expressed before and he will recognize you as the only woman in the universe who has ever truly tempted him.
Thank you for sharing wonderful information with us to get some idea about that content.
ReplyDeleteMulesoft Training in Hyderabad
Mulesoft Online Training
Great work. Your article was very helpful. Thanks for sharing valuable points.
ReplyDeleteMulesoft Online Training india
Mulesoft Online Training in Hyderabad
Mulesoft Online Training Hyderabad
Mule ESB Training
Learn Mulesoft Online
Mulesoft Training Courses
Mulesoft Online Course
Online Mulesoft Training
Mulesoft Certification
Learn Mulesoft
Mulesoft Online Training