DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • GraphQL vs REST — Which Is Better?
  • What Developers Need to Know About IP API Privacy in Mobile Apps (2025 Update)
  • How to Build a New API Quickly Using Spring Boot and Maven
  • Running a Mobile App API Locally With Docker and Postman

Trending

  • Using LLMs to Automate Data Cleaning and Transformation Pipelines
  • Integrating AI-Driven Decision-Making in Agile Frameworks: A Deep Dive into Real-World Applications and Challenges
  • A Scalable Framework for Enterprise Salesforce Optimization: Turning Outcomes Into an Operating System
  • Observability in Spring Boot 4
  1. DZone
  2. Data Engineering
  3. Databases
  4. Configuring Custom OAuth 2.0 Grant Type in WSO2 API-M 4.X

Configuring Custom OAuth 2.0 Grant Type in WSO2 API-M 4.X

This article explains how we can configure a custom OAuth 2.0 grant type known as the “mobile” grant type in WSO2 APIM.

By 
Suman Mohan user avatar
Suman Mohan
·
Dec. 20, 22 · Tutorial
Likes (1)
Comment
Save
Tweet
Share
3.5K Views

Join the DZone community and get the full member experience.

Join For Free

This article explains how we can configure a custom OAuth 2.0 grant type known as the “mobile” grant type in WSO2 APIM. This mobile grant type is like the password grant type, where a mobile number will be passed as a parameter instead of a username and password.

Prerequisites:

  1. Apache Maven.
  2. Download the required source code from GitHub: https://github.com/wso2/samples-is/tree/master/oauth2/custom-grant

Custom-grant contains the source code for the mobile grant type that can be enhanced as per our requirement to configure the new grant type in WSO2 APIM.

Implementation:

Changes To Be Made for Customizing the Mobile Grant Type Handler (MobileGrant.java):

1.  Change the isValidMobileNumber() method logic to check if a mobile no. is valid. Earlier, it would just check and pass nos. starting with 033.

Java
 
private boolean isValidMobileNumber(String mobileNumber){
//(0/91): number starts with (0/91)  
//[7-9]: starting of the number may contain a digit between 0 to 9  
//[0-9]: then contains digits 0 to 9  
Pattern ptrn = Pattern.compile("(0/91)?[7-9][0-9]{9}");  
//the matcher() method creates a matcher that will match the given input against this pattern  
Matcher match = ptrn.matcher(mobileNumber);  
//returns a boolean value  
return (match.find() && match.group().equals(mobileNumber));
}


2. Change the issue() method to generate a valid JWT token. Earlier implementation of this method would generate some random opaque token.

Java
 
public OAuth2AccessTokenRespDTO issue(OAuthTokenReqMessageContext tokReqMsgCtx) throws IdentityOAuth2Exception {
    
    // calling super
    OAuth2AccessTokenRespDTO tokenRespDTO1 =  super.issue(tokReqMsgCtx);
    return tokenRespDTO1;
}


NOTE: We can enhance the mobile grant type code as per our requirements. Here it's just customized to check if a mobile number passed as a parameter is valid or not and to generate a valid JWT token in response.

Steps to Configure the New Mobile Grant Type:

1. After doing the required customization in MobileGrant.java, run the below Apache Maven command in the custom-grant folder using the command line.

 
mvn clean install


2. Place the generated .jar file under the API-M path <APIM_HOME>/repository/components/lib.

3. Add the following configuration in <APIM_HOME>/repository/conf/deployment.toml file.

 
[[oauth.custom_grant_type]]
name="mobile"
grant_handler="org.wso2.sample.identity.oauth2.grant.mobile.MobileGrant"
grant_validator="org.wso2.sample.identity.oauth2.grant.mobile.MobileGrantValidator"
[oauth.custom_grant_type.properties]
IdTokenAllowed=true


4. Restart the APIM server.

Testing:

Now, the new mobile grant type can be seen in Devportal under all the applications.

Testing

We can’t generate the access token using the new mobile grant type from Devportal, as Generate Access Token button gets enabled only on choosing the Client Credentials grant type.

We can get the access token using the new mobile grant type only by invoking the token API from a rest client. Copy the Consumer Key (Username) and Secret (Password) for any application (TestMobile in this case) from Devportal, and invoke the token API using Postman or any other rest client as shown below.

Authorization Tab

Under the Authorization tab, provide the Consumer Key and Secret, as seen in the above screenshot.

access_token

grant_type and mobileNumber are the 2 request parameters to be passed for invoking token API with mobile grant type, as seen above. The access_token we get in the response can be used to invoke any API that this application (TestMobile) has subscribed to.

TestMobile application has subscribed to PizzaShack API, and we can invoke it using the access_token we got above, as seen below.

PizzaShack API

If you enter some invalid mobile number as a parameter, you will get a 400 Bad Request error, as seen below.

400 Bad Request error

CURL command to invoke token API using the mobile grant type:

 
curl -k -X POST https://localhost:9443/oauth2/token -d "grant_type=mobile&mobileNumber=8251165672" -H "Authorization: Basic Base64(consumer-key:consumer-secret)"


API Apache Maven mobile app

Opinions expressed by DZone contributors are their own.

Related

  • GraphQL vs REST — Which Is Better?
  • What Developers Need to Know About IP API Privacy in Mobile Apps (2025 Update)
  • How to Build a New API Quickly Using Spring Boot and Maven
  • Running a Mobile App API Locally With Docker and Postman

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook