API HMAC Authentication (2024)

Introduction

HMAC (hash-based message authentication code) is used to verify that a request is coming from an expected source and that the request has not been tampered with in transit. This is achieved by including both a public (key-identifier) and private key (key-secret) in each message, the latter of which is only known to the server and client. Using these values, the client will generate a unique HMAC (a hashed code) representing its request to the server. The client sends that HMAC to the server, along with a timestamp and all the arguments and values it was going to send anyway. The server (CrowdTwist) gets the request and re-generates its own unique HMAC based on the submitted values using the same methods the client used. The server (CrowdTwist) will then compare the two HMACs, if they are equal, then the server trusts the client, and processes the request.

1) HMAC does not require an API key in the request URL, but does use a key in the header. The key is the CrowdTwist public HMAC key.

2) All calls to api[client_id].crowdtwist.com are supported via HMAC, but calls to pos[client_id].crowdtwist.com are not supported.

3) HMAC is not supported on the Legacy Auth Sign-In and Sign-Out endpoints.

Creating a Request

Once HMAC has been enabled, all incoming API calls will require HMAC request headers, which will be detailed below.

  • X-CT-Authorization: This header must follow the convention “CTApiV2Auth [public key]:[signature]”, where [public key] is the HMAC public key and [signature] is the HMAC hash that requires several steps to generate.
  • Content-Type: This header must be “application/json” for POST and PUT requests.
  • X-CT-Timestamp:This header must be a UNIX timestamp and must be within 15 minutes of the current time. This timestamp is included to prevent replay attacks.
X-CT-Authorization: CTApiV2Auth publickey:signatureX-CT-Timestamp: 1505325876486
X-CT-Authorization: CTApiV2Auth publickey:signatureX-CT-Timestamp: 1505325876486

Creating the Signature

To create the [signature] value in the X-CT-Authorization header, several steps need to be followed.

  • [signature] = Base64(HMAC-SHA-256(PrivateKey, StringToSign ) )

The PrivateKey along with a StringToSign body will need to be HMAC-SHA-256 hashed. Then, that resulting hash will be Base64 encoded to create the signature. This signature is then added to the X-CT-Authorization header along with the public key as seen in the example.

To start, the StringToSign parameter will be created by appending the strings below separated by a new-line (“\n”) character.

StringToSign = HTTP-Verb + "\n" +Content-MD5 + "\n" +Content-Type + "\n" +Timestamp + "\n" +RequestURI
  • HTTP Verb:the HTTP method (GET, POST,PUT, etc.)
  • Content-MD5:an MD5 hash of the exact request body (JSON Format)
  • Content-Type:Currently, the only value supported for POST/PUT requests is application/json
  • Timestamp:the UNIX timestamp
  • RequestURI:the URL with the query string parameters. This is not the full path, just the path and query string after domain.
    • For example, the RequestURI for https://api.crowdtwist.com/v2/activities, would be /v2/activities.

Sample PUT Request

To generate the signature the below process needs to be followed:

signature = Base64(HMAC-SHA-256(PrivateKey, StringToSign))

To create the StringToSign, the request body will need to be MD5 hashed.

{
“email_address”: “testing@crowdtwist.com”,
“postal_code”: “10010”,
“last_name”: “test”
}

MD5 value:

d8d84b98493543c9d51bda8c7c73f622

Next, the StringToSign is created by adding the MD5 hash.

StringToSign value:

PUT
d8d84b98493543c9d51bda8c7c73f622
application/json
1505759963477
/v2/users/11116703

After forming the StringToSign, the HMAC SHA-256 hashing algorithm using the private key is applied.

HMAC SHA-256 value:

3a9805afa6ec3850b80273c61a06a11702c38b868cc986e8d981b4a63d2356f2

To create the signature the above HMAC SHA-256 value will be Base64 encoded using UTF8.

Base 64 value:

M2E5ODA1YWZhNmVjMzg1MGI4MDI3M2M2MWEwNmExMTcwMmMzOGI4NjhjYzk4NmU4ZDk4MWI0YTYzZDIzNTZmMg==

The public key, and the signature are sent in the X-CT-Authorization header.

<!--?php$curl = curl_init();curl_setopt_array($curl, array( CURLOPT_URL =--> "https://api.crowdtwist.com/v2/users/11116703", CURLOPT_RETURNTRANSFER => true, CURLOPT_ENCODING => "", CURLOPT_MAXREDIRS => 10, CURLOPT_TIMEOUT => 30, CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, CURLOPT_CUSTOMREQUEST => "PUT", CURLOPT_POSTFIELDS => "{\n \"email_address\": \"testing@crowdtwist.com\",\n \"postal_code\": \"10010\",\n \"last_name\": \"test\"\n}", CURLOPT_HTTPHEADER => array( "cache-control: no-cache", "content-type: application/json", "x-ct-authorization: CTApiV2Auth publickey:signature", "x-ct-timestamp: 1505325876486" ),));$response = curl_exec($curl);$err = curl_error($curl);curl_close($curl);if ($err) { echo "cURL Error #:" . $err;} else { echo $response;}
<!--?php$curl = curl_init();curl_setopt_array($curl, array( CURLOPT_URL =--> "https://api.crowdtwist.com/v2/users/11116703", CURLOPT_RETURNTRANSFER => true, CURLOPT_ENCODING => "", CURLOPT_MAXREDIRS => 10, CURLOPT_TIMEOUT => 30, CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, CURLOPT_CUSTOMREQUEST => "PUT", CURLOPT_POSTFIELDS => "{\n \"email_address\": \"testing@crowdtwist.com\",\n \"postal_code\": \"10010\",\n \"last_name\": \"test\"\n}", CURLOPT_HTTPHEADER => array( "cache-control: no-cache", "content-type: application/json", "x-ct-authorization: CTApiV2Auth publickey:signature", "x-ct-timestamp: 1505325876486" ),));$response = curl_exec($curl);$err = curl_error($curl);curl_close($curl);if ($err) { echo "cURL Error #:" . $err;} else { echo $response;}

Sample Authentication Request

Following similar steps as seen above, a sample Authentication request will appear as follows:

POST https://api.crowdtwist.com/v2/user_auth_sign_inHeadersX-CT-Authorization CTApiV2Auth ABCl3y7r0s5ukCXz5lCJOCrTZ427pjp5: YTUyNDU0MTc1YTg1MTZiN2IyMTc2Mzc5ZTA2YTlkN2Q1ZmEwNzAyYzM4ZmM0NWUzZWY2M2JmMWE1NzQ2YzBjMA==Content-Type application/jsonX-CT-Timestamp 1437604131Body{ "redirect" : "http://test-rewards.crowdtwist.com", "username" : "AliceTwist", "verified" : 1}
POST https://api.crowdtwist.com/v2/user_auth_sign_inHeadersX-CT-Authorization CTApiV2Auth ABCl3y7r0s5ukCXz5lCJOCrTZ427pjp5: YTUyNDU0MTc1YTg1MTZiN2IyMTc2Mzc5ZTA2YTlkN2Q1ZmEwNzAyYzM4ZmM0NWUzZWY2M2JmMWE1NzQ2YzBjMA==Content-Type application/jsonX-CT-Timestamp 1437604131 Body{ "redirect" : "http://test-rewards.crowdtwist.com", "username" : "AliceTwist", "verified" : 1}

First, to generate the StringToSign the above request body will be MD5 hashed to produce the below value:

  • de26bd80b53577dbe47738239d23f0b3

This value will then be added the StringToSign which will have the below format

POST
de26bd80b53577dbe47738239d23f0b3
application/json
1437604131
/v2/user_auth_sign_in

Taking the above StringToSign along with the private key (ABttp1b92Tb65445rmZL835f263n1q4Y) and applying the HMAC-SHA-256 algorithm will result in the below string:

  • a52454175a8516b7b2176379e06a9d7d5fa0702c38fc45e3ef63bf1a5746c0c0

Lastly, this value will then need to be Base64 encoded, resulting in the following value:

  • YTUyNDU0MTc1YTg1MTZiN2IyMTc2Mzc5ZTA2YTlkN2Q1ZmEwNzAyYzM4ZmM0NWUzZWY2M2JmMWE1NzQ2YzBjMA==

With the signature complete, the request can be sent with the appropriate HMAC Headers and a valid response will be returned.

Sample GET Request

Following similar steps as seen above, a sample GET request will appear as follows:

GET https://api.crowdtwist.com/v2/activitiesHeadersX-CT-Authorization CTApiV2Auth ABCl3y7r0s5ukCXz5lCJOCrTZ427pjp5:YmQ0YTgyY2QzMTlhYmFiZTU3ZDBhODIyMDQ5YWU4OTg1MDI5ZjgyMjM3NTA5ZDNmMDkxYzgyY2JjN2E2OTQ1Yw==X-CT-Timestamp 1437659826 
GET https://api.crowdtwist.com/v2/activities HeadersX-CT-Authorization CTApiV2Auth ABCl3y7r0s5ukCXz5lCJOCrTZ427pjp5:YmQ0YTgyY2QzMTlhYmFiZTU3ZDBhODIyMDQ5YWU4OTg1MDI5ZjgyMjM3NTA5ZDNmMDkxYzgyY2JjN2E2OTQ1Yw==X-CT-Timestamp 1437659826 

Since there is no request body, the StringToSign value can be immediately generated with the below format:

GET

1437659826
/v2/activities

Applying the HMAC-SHA-256 algorithm with the private key (ABttp1b92Tb65445rmZL835f263n1q4Y) and the string to sign results in the below string:

  • bd4a82cd319ababe57d0a822049ae8985029f82237509d3f091c82cbc7a6945c

Next, this hash is then Base64 encoded, which results in the following value:

  • YmQ0YTgyY2QzMTlhYmFiZTU3ZDBhODIyMDQ5YWU4OTg1MDI5ZjgyMjM3NTA5ZDNmMDkxYzgyY2JjN2E2OTQ1Yw==

This value is then placed in the X-CT-Authorization header as the [signature] to receive a valid response.

Error Conditions

Below are the possible HMAC specific error conditions; all responses are in JSON format.

Invalid HMAC header – Possibly due to missing http header or http header in unexpected format.

Response

{ "error": "hmac_verification_failed", "message": "Invalid hmac header."}
{ "error": "hmac_verification_failed", "message": "Invalid hmac header."}

HMAC signature mismatch – The client generated hash value and the server generated hash value does not match.

Response

{ "error": "hmac_verification_failed", "message": "Hmac signature mismatch."}
{ "error": "hmac_verification_failed", "message": "Hmac signature mismatch."}

HMAC timestamp expired – The client generated hash value and the server generated hash value matches, but the timestamp exceeded the acceptable limit. The timestamp must be within 15 minutes of the current time.

Response

{ "error": "hmac_verification_failed", "message": "Hmac timestamp expired."}
{ "error": "hmac_verification_failed", "message": "Hmac timestamp expired."}
API HMAC Authentication (2024)

FAQs

What is HMAC in API? ›

Hash-based message authentication code (or HMAC) is a cryptographic authentication technique that uses a hash function and a secret key.

How to authenticate HMAC? ›

This is achieved by including both a public (key-identifier) and private key (key-secret) in each message, the latter of which is only known to the server and client. Using these values, the client will generate a unique HMAC (a hashed code) representing its request to the server.

What is the HMAC authentication policy? ›

In the typical use of HMAC, a sender of a message sends a message and its HMAC to a receiver, and the receiver can use the HMAC along with the shared secret key to authenticate the message. This policy is a Standard policy and can be deployed to any environment type.

What is the best practice of HMAC authentication? ›

3 Best practices for HMAC

To use HMAC effectively and securely, it's recommended to use a strong hash function, such as SHA-256, SHA-384, or SHA-512. A secure key should be generated randomly, stored securely, and rotated periodically. The key should be long enough to prevent brute-force attacks.

What is the difference between HMAC and JWT? ›

A JWT (when using HMAC as the signing scheme) is basically just an HMAC message where the message data is a JSON object. The interesting thing about the JWT system is that the sender and the receiver of the JWT are typically the same entity, that is, the webserver.

What are the disadvantages of HMAC? ›

The drawbacks of HMAC

The biggest drawback is HMAC's reliance on a secret key. If this key is shared with an unauthorized user, it can compromise the integrity of messages. This means it's essential to have processes in place to securely store and manage any secret keys you're using.

What is the difference between SHA-256 and HMAC? ›

HMAC stands for "Hash-based Message Authentication Code," and SHA-256 refers to the Secure Hash Algorithm 256-bit variant. HMAC-SHA-256 combines the strength of SHA-256's cryptographic hashing with HMAC's ability to authenticate the integrity and source of a message.

How do I authenticate API access? ›

Authentication is typically done by requiring the client to provide some form of credentials – such as a user name and password, an OAuth token, or a JSON Web Token (JWT). As an API owner, you can implement authentication in Apigee using policies.

Is HMAC the same as MAC? ›

HMAC is a specific construction for MACs that is based on a cryptographic hash function. It provides enhanced security compared to traditional MAC algorithms by incorporating additional steps in the computation of the tag. The main difference between a MAC and HMAC lies in the way the tag is computed.

Is HMAC authentication Secure? ›

HMAC authentication offers several advantages: Security: The shared secret adds an extra layer of security. Even if an attacker intercepts the request and response, they won't be able to reverse-engineer the secret key.

What is the difference between HMAC and oauth2? ›

HMAC is for authentication that determining who you are, https is for security of transport that ensure on one in the middle can see the content of your transport. Oauth 2 authorization server use secret key or password determining who you are.

How to generate a HMAC key? ›

Create an HMAC key
  1. In the Google Cloud console, go to the Cloud Storage Settings page. Go to Settings.
  2. Select the Interoperability tab.
  3. Click add_box Create a key for a service account.
  4. Select the service account you want the HMAC key to be associated with.
  5. Click Create key.

How to use HMAC in API? ›

In HMAC authentication, every request is independently established using a cryptographic hash function. For each API request, the client computes a hashed "signature" using a secret key and submits it in the Authorization header. Your first step is to create a new system user - called a Data API Agent User.

How does HMAC authentication work? ›

HMAC (Hash-based Message Authentication Code) is a type of a message authentication code (MAC) that is acquired by executing a cryptographic hash function on the data (that is) to be authenticated and a secret shared key. Like any of the MAC, it is used for both data integrity and authentication.

What is the secret key in HMAC? ›

The secret cryptographic key is what enables a user to make an encrypted message readable after it has been encrypted by an algorithm. In an HMAC transaction, the client and server must agree on the secret key. This provides a way to decode messages, which must stay secret, to maintain the transaction's integrity.

What is HMAC key used for? ›

An HMAC key is a type of credential associated with an account, typically a service account. You use an HMAC key to create signatures using the HMAC-SHA256 signing algorithm. The signatures you create are then included in requests to the Cloud Storage XML API.

What is the difference between SHA256 and HMAC? ›

HMAC stands for "Hash-based Message Authentication Code," and SHA-256 refers to the Secure Hash Algorithm 256-bit variant. HMAC-SHA-256 combines the strength of SHA-256's cryptographic hashing with HMAC's ability to authenticate the integrity and source of a message.

What is the difference between HMAC key and AES key? ›

AES encryption uses the key to encrypt the data, while HMAC uses the key to generate a message authentication code (MAC) for the data. In fact, it is common practice to use the same key for both encryption and authentication, as it simplifies key management and can improve performance.

What is the difference between HMAC and MAC? ›

In a MAC algorithm, the tag is typically computed by applying a cryptographic function directly to the message and the secret key. In contrast, HMAC uses a more complex construction that involves two passes of the hash function, along with the use of inner and outer padding.

Top Articles
Do You Need a College Degree to Be a Real Estate Agent?
Import Google Drive API Data to Google Sheets [2024] | API Connector
Davita Internet
Ffxiv Palm Chippings
Research Tome Neltharus
Valley Fair Tickets Costco
Mohawkind Docagent
Emmalangevin Fanhouse Leak
Mndot Road Closures
Erskine Plus Portal
13 The Musical Common Sense Media
World Cup Soccer Wiki
Craigslist Heavy Equipment Knoxville Tennessee
Edible Arrangements Keller
Slag bij Plataeae tussen de Grieken en de Perzen
Oscar Nominated Brings Winning Profile to the Kentucky Turf Cup
Love In The Air Ep 9 Eng Sub Dailymotion
Leader Times Obituaries Liberal Ks
Committees Of Correspondence | Encyclopedia.com
Huntersville Town Billboards
Timeforce Choctaw
Ford F-350 Models Trim Levels and Packages
Routing Number For Radiant Credit Union
Bn9 Weather Radar
Sofia the baddie dog
City Of Durham Recycling Schedule
Urbfsdreamgirl
Truvy Back Office Login
Table To Formula Calculator
Sandals Travel Agent Login
Orange Park Dog Racing Results
Neteller Kasiinod
Maths Open Ref
DIY Building Plans for a Picnic Table
Have you seen this child? Caroline Victoria Teague
Steven Batash Md Pc Photos
Tamil Play.com
Atlantic Broadband Email Login Pronto
Spinning Gold Showtimes Near Emagine Birch Run
Oreillys Federal And Evans
Asian Grocery Williamsburg Va
Afspraak inzien
Directions To 401 East Chestnut Street Louisville Kentucky
Academic important dates - University of Victoria
Gpa Calculator Georgia Tech
Housing Intranet Unt
T&Cs | Hollywood Bowl
St Vrain Schoology
Online College Scholarships | Strayer University
Understanding & Applying Carroll's Pyramid of Corporate Social Responsibility
Unpleasant Realities Nyt
Tyrone Unblocked Games Bitlife
Latest Posts
Article information

Author: Foster Heidenreich CPA

Last Updated:

Views: 6605

Rating: 4.6 / 5 (56 voted)

Reviews: 87% of readers found this page helpful

Author information

Name: Foster Heidenreich CPA

Birthday: 1995-01-14

Address: 55021 Usha Garden, North Larisa, DE 19209

Phone: +6812240846623

Job: Corporate Healthcare Strategist

Hobby: Singing, Listening to music, Rafting, LARPing, Gardening, Quilting, Rappelling

Introduction: My name is Foster Heidenreich CPA, I am a delightful, quaint, glorious, quaint, faithful, enchanting, fine person who loves writing and wants to share my knowledge and understanding with you.