HMAC Authentication
HMAC Authentication Explanation for Access To API
See javascript, php and shell script example scripts for API calls
To access the API end points authentication is required. Without it, you will receive an error that contains, among other things, this text:
Message: Authorization header required
To avoid this message and have proper authentication a particular hmac algorithm needs to be implemented at runtime before each request.
The algorithm needs to set 3 request headers:
Date
Content-Type
Authorization
Request headers needed for authentication
Date
The Date
header should be set to the datetime before the request was sent as ISO 8601 string.
This date must be used as well in the Authorization
header explained here. An example value would be: 2023-03-09T14:11:32.044Z
Javascript example:
(new Date()).toISOString()
Content-Type
This header indicates what data is sent with the request. If request is sent, and it isn't clear what to use for Content-Type
, say, for a GET
request, one can simply use:
application/json
This content-type must be used as well in the Authorization
header explained here.
Authorization
This is the most complicated authentication request header.
It contains multiple parts that are concatenated together (more to that later on).
The header contains these variables:
provider
is as well referenced by others as 'auth-scheme'. A simple string provided by gotom. Default value is "gotom_app_api".user
is a simple string provided by gotomsignature
is a specially encoded token
How to create the signature
part?
signature
part?To create the signature
6 parts are needed:
- Request method: e.g.
GET
orPOST
etc - An md5 hash of the request body. Javascript example would be
CryptoJS.MD5(requestBody).toString()
- Request body: If the request body is empty or not string, empty string should be used for the md5 hash.
- Content-Type: the same string from the other header.
- Date: the same string from the other header.
- Custom headers: this must always be empty string.
- The url path with query as string. So without domain, http scheme and port, e.g. only
/app-api/graph-export/download/41
- Secret: string provided from gotom.
These 7 parts now must be processed in 3 steps.
- Concat the signature parts 1-6 together (without the 7. part), maintaining the correct order, using a newline
\n
between the parts. Javascript example:
const digestParts = [requestMethod, hashedRequestBody, contentType, date, customHeaders, path];
const step1ProcessSignature = digestParts.join('\n');
- On the result from step 1, apply the Hmac SHA1 generation with the provided secret. Javascript example:
const step2ProcessSignature = CryptoJS.HmacSHA1(step1ProcessSignature, secret);
- Base64 encode the result from step 2. The result of this last step, is what is called here, the signature. Javascript example:
const signature = step2ProcessSignature.toString(CryptoJS.enc.Base64);
Putting it together to form a valid Authorization header
The Authorization
header value can now be assembled using the 3 variables described earlier.
<foo>
is used to indicate a variable with the name foo
:
<provider> <user>:<signature>
An example value would be:
gotomprovider johndoe:Ax3lBxQyKTTXEdwUIEv4M/MSzmU=
Updated 27 days ago