Over a million developers have joined DZone.

How to Improve OAuth Security With HMAC Validation

DZone's Guide to

How to Improve OAuth Security With HMAC Validation

We explain a type of attack that's possible to carry out on systems using OAuth and how to mitigate them using keyed-Hash Message Authentication Code Validation.

· Security Zone ·
Free Resource

Discover how to provide active runtime protection for your web applications from known and unknown vulnerabilities including Remote Code Execution Attacks.

Today, everyone focuses on advanced security measures and preventing all possible attacks on their enterprise software. This post will explain a type of attack that's possible to carry out on systems using OAuth and how you can mitigate them by adding keyed-Hash Message Authentication Code (HMAC) validation to the OAuth token. This specific solution can be used when your enterprise platform is built with WSO2 API Manager and WSO2 Identity Server.

The main use case this would address is preventing miss-use of expired OAuth tokens or randomly generated OAuth tokens. Stolen or randomly generated tokens can be used to employ DoS/DDoS attacks effectively.

If an attacker uses random tokens to send API requests, API Manager and IS will be trying to verify the token and it will go through the critical path of verification. This is a costly transaction and it can cause high latencies and instability in WSO2 APIM (WSO2 API Manager) and WSO2 IS (WSO2 Identity Server) clusters. Implementation of this particular solution is done using extensions developed for standard extension points of WSO2 IS and WSO2 APIM.

WSO2 IS Extension — OAuth Token Generator Extension

Please find more information on developing OAuth token generator extensions here. Code for this particular solution can be found in my GitHub repository here.

You can engage this handler by adding the following entry to your IS_HOME/repository/conf/identity.xml.


This extension is responsible for enhancing the OAuth token with HMAC (Hash-based Message Authentication Code) so that the above-discussed attacks will be less effective. The following two parts will be added to the token in addition to the default token created in WSO2 IS.

  • HMAC
  • Expiry timestamp

Access Token Format

Token has 3 parts, delimited by “.”

Part I — Original access token issued from WSO2 Identity Server.

Part II — Hex value for token expiry time.

Part III — HMAC calculation of (‘Part I’ + ‘.’ + ‘Part II’).

<Part I>.<Part II>.<Part III>

For Example:


WSO2 APIM Extension — HMAC and Timestamp Verification Handler

Please refer to this document to understand how to develop and engage WSO2 API handler extensions. You can find the code for APIM handler on GitHub, here.

You can engage this handler by adding the following entry before “#foreach($handler in $handlers)” line of “velocity_template.xml” file. You can find this file at APIM_HOME/repository/resources/api_templates/ directory.

<handler class=“com.sample.lahiru.wso2.hmac.handler.HMACTokenValidatorHandler”/>

This custom handler verifies the HMAC of the token before it tries to authenticate using a default authentication handler, which will be an expensive operation usually. It will also make sure the token is not expired. These verifications will avoid any API calls to WSO2 IS, in case the token is expired or HMAC is invalid.

HMAC validation handler calculates the HMAC using Part I and Part II (see access token format), extracted from the token and validates by comparing that value with the HMAC value included in the token (Part III).

Find out how Waratek’s award-winning application security platform can improve the security of your new and legacy applications and platforms with no false positives, code changes or slowing your application.

security ,oauth 2 ,denial of service ,ddos attacks ,hmac

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}