OAuth2.0 Grants — Cheatsheet for WSO2 Identity Server or Asgardeo  by@dinali

OAuth2.0 Grants — Cheatsheet for WSO2 Identity Server or Asgardeo

This is a fully OAuth2.0 and OpenID. It is available to download and use for the first time in the U.S. to test your knowledge of the system. Use the site to test and use the content of this article to help you with the latest in-depth search for the best online services available at http://www.mailonline.com/shoppingopping.org/soucy.com. The fee is free and does not need to be included in the form of payment.
Dinali HackerNoon profile picture


WSO2 Identity Server(IAM) is a fully OAuth2.0 and OpenID Connect compliant CIAM product that can run an on-prem or private cloud. Asgardeo is the new IDaaS(CIAM Cloud solution) offered by WSO2 to cater to all your developer-friendly CIAM needs. Both our CIAM products allow users to easily experience Single Sign-On with OpenID connect and delegate access to resources through OAuth2.0.

Asagrdeo is now open for early adoption - you can create an account for you/your organization for free and try out!

This blog will give you a simpler view of the most commonly used grant types how we can try them out even without an actual sample application like the playground.

To try out these grant types, you need to first register an OAuth2.0/OpenID connect application (with a test callback URL and get the client-id and secret).

WSO2 Identity Server - Service Provider configurations

WSO2 Identity Server - Service Provider configurations

Sample Sp name — test-sp

Callback URL used — http://testsp/callback

Main Grant Types and its Request Formats

Authorization Code Grant

This grant type is a redirection-based grant type. This grant type uses a browser redirection call as the first call to receive the code after authentication and secondly, a backchannel call to get the access token using the code received from the first browser call.

1. Browser request: Copy-paste this sample request on the browser and get the authorization code from the browser to use in the second curl command

https://{hostname:por}or{Asgardeo URL}/oauth2/authorize?response_type=code&client_id=<client-id>&nonce=oidc&redirect_uri=<call-back url>&scope=openid

Adding **scope=openid **will give you the id token in the second token response. Simply the OAuth2.0 request becomes an OpenID Connect request.

2. Back-channel request: curl command

curl -k -v --user <client-id>:<secret> 
-d "grant_type=authorization_code&code=<code recieved from first/above request>&redirect_uri=<call-back url>&scope=openid" 
https://{IDM URL or Asgardeo URL}/oauth2/token

Currently, there is a public client version of the authorization code grant supported by WSO2 for single-page applications and mobile applications, which can be used instead of a client-credential grant. There, we do not need the client secret in the backchannel call. This is a custom grant offered by WSO2 for better security.

Moreover, for better security, you can use PKCE for Single Page Applications or public clients.

Implicit Grant

This grant is fully browser-based; once you copy-paste this sample request, you will receive the token and id-token in the browser response after the authentication. A refresh token is not available.

https://{IDM URL or Asgardeo URL}/oauth2/authorize?response_type=id_token token&client_id=<client-id>&nonce=oidc&redirect_uri=<call-back url>&scope=openid

A sample response to this request will look as follows:


By changing the response_type= id_token token parameter, we can get a different response with both token and id_token

Password Grant

This grant is useful in a scenario where you need to use Basic Authentication behavior. This is the only REST API provided by OAuth2.0 specification similar to username: password authentication. Although many use this for Native clients, it is always recommended to use the native browser when you want to achieve single-sign-on.

curl -u <client id>:<client secret> 
-k -d "grant_type=password&username=<username>&password=<password>&scope=openid" 
-H "Content-Type:application/x-www-form-urlencoded" 
https://{IDM or Asgardeo host URL}/oauth2/token

Client-Credential Grant

This grant is used for public clients like mobile applications and devices to generate application-based access tokens - this is no more considered a secure grant type for public clients. But solutions can be built by using this grant type for the purposes like machine-to-machine access delegation.

curl -u <client id>:<client secret> 
-k -d "grant_type=client_credentials&scope=openid" 
-H "Content-Type:application/x-www-form-urlencoded" 
https://{IDM or Asgardeo host URL}/oauth2/token

Refresh Grant

Refresh grant is used to renew an access token before it expires. With the access token, the refresh token also can be renewed. Authorization code grant, bearer grants, and password grant will provide a refresh token to renew the access token.

curl -k -d "grant_type=refresh_token&refresh_token=<refresh_token>" 
-H "Authorization: Basic <Base64Encoded(Client_Id:Client_Secret)>"
-H "Content-Type: application/x-www-form-urlencoded" 
https://{IDM or Asgardeo host URL}/oauth2/token

Kerberos Grant

Kerberos is a security protocol that has support built into various operating systems and open-source distributions (e.g., Ubuntu, Windows, RedHat, Open Solaris, etc.). Additionally, a majority of browsers support some Kerberos functions as well. A refresh token is available.

WSO2 Identity Server and Asgardeo uses the Kerberos OAuth2.0 grant type to allow organizations to exchange a Kerberos ticket for an OAuth2.0 token. This allows organizations to re-use their existing Keberos infrastructure and adopt OAuth 2.0

curl -v -X POST 
-H "Authorization: Basic <base64 encoded client id:client secret value>"
-k -d "grant_type=kerberos&kerberos_realm=<kerberos realm>&kerberos_token=<kerberos token>" 
-H "Content-Type:application/x-www-form-urlencoded" 
https://{IDM or Asgardeo host URL}/oauth2/token

JWT Bearer Grant

If you have a JWT token or an ID token, you can exchange the JWT token for an access token through this grant.

curl -i -X POST -u <CLIENT_ID>:<Client_SECRET> -k 
-d 'grant_type=urn:ietf:params:oauth:grant-type:jwt-bearer&assertion=<JWT>' 
-H 'Content-Type: application/x-www-form-urlencoded' 
https://{IDM or Asgardeo host URL}/oauth2/token

Other important requests that you need when you work with OAuth2.0 or OpenID Connect

Introspect an Access Token

When an Opaque Access token is received to a server by a client, the server should check whether this access token is valid or not. For that, they use the introspection endpoint of the token issuer.

curl -k -u <USERNAME>:<PASSWORD> 
-H 'Content-Type: application/x-www-form-urlencoded' -X POST 
--data 'token=<ACCESS_TOKEN>' https://{IDM or Asgardeo host URL}/oauth2/introspect

The response will be similar to below:

{"exp":1464161560,"username":"[email protected]","scope":"test1 test2","active":true,"token_type":"Bearer","client_id":"rgfKVdnMQnJSSr_pKFTxj3apiwYa","iat":1464157960}

If there are any scopes bound to the access token, you will get that information as well when calling the introspection endpoint.

Revoke Access Token

For security reasons, it is important to revoke access tokens. There is an expiration time for each access token, but through revoke, you can invalidate the token at any time.

curl -X POST — basic -u “<client id>:<client secret>” 
-H “Content-Type: application/x-www-form-urlencoded;charset=UTF-8” 
-k -d “token=<token to revoke>&token_type_hint=access_token” 
https://{IDM or Asgardeo host URL}/oauth2/revoke

Get User info (Id-token information)

When a client uses the OAuth2.0 protocol to get the user’s information, the client exposes an endpoint called the user-info endpoint.

curl -k -H “Authorization: Bearer <Acess_token>” 
https://{IDM or Asgardeo host URL}/oauth2/userinfo?schema=openid

This covers pretty much all the request formats which you need when you try OAuth2.0 and OpenID Connect in WSO2 Identity Server (IAM).

I hope this will help you as a cheat sheet in your developer journey to try out different OAuth2.0 grant types with WSO2 Identity Server or Asgardeo!!!

react to story with heart
react to story with light
react to story with boat
react to story with money

Related Stories

. . . comments & more!