Version Affected: All
Overview
This article explains how to send a one-time passcode (OTP) to a phone by text message (SMS) with the SecureAuth Identity Provider (IdP) Authentication API, using Postman to build and sign the request. The API sends the OTP to the phone number in the request. Unless the realm is set to validate OTPs itself, the API also returns the same OTP in its response, so the calling application can check the code the user enters.
The Authentication API accepts only signed requests. This article signs each request with two headers: X-SA-Ext-Date, which holds the time of the request, and Authorization, which holds a hash-based message authentication code (HMAC) signature made with the realm's Application Key. Postman has no built-in authorization type for this signature, so a pre-request script builds both headers before each request is sent.
Before you start, you need Postman, administrator access to the IdP Web Admin, and a user who exists in the realm's data store.
Enable the API on the Realm
- In the IdP Web Admin, open the realm that will handle the API requests and select its API tab.
- In the API Key section, select Enable API for this realm.
- Click Generate Credentials, then use Select & Copy to copy the Application ID and the Application Key. If the realm already has credentials that an application uses, copy those instead: new credentials replace them when you save, and that application can no longer authenticate.
- Under API Permissions, select Enable Authentication API.
- Leave Enable Authentication API Validation cleared. In 23.07 and earlier, this setting is the OTP Validation Property list; leave it unset. When this setting is on, the IdP keeps the OTP for its own validation endpoint and leaves it out of the response.
- Click Save.
Create the Postman Collection and Its Variables
- In Postman, create a new collection, for example SecureAuth Authentication API.
- Open the collection's Variables tab and add the five variables below. Variable names are case-sensitive and must match exactly; for example, appID is not the same as appId.
- baseUrl: the IdP server's address with no trailing slash, for example https://idp.example.com
- appID: the realm's Application ID
- appKey: the realm's Application Key
- currentDate: leave empty; the script sets it
- signature: leave empty; the script sets it
- Save the collection.
Create the Request
- In the collection, add a new request. Set the method to POST and enter the address below, replacing <realm> with the realm's path, such as secureauth2.
{{baseUrl}}/<realm>/api/v2/authOlder examples use api/v1/auth, which accepts the same request. This article uses api/v2, as the Authentication API guide does for IdP 9.2 and later.
- On the Headers tab, add these two headers:
- Key X-SA-Ext-Date, value {{currentDate}}
- Key Authorization, value Basic {{signature}}, with a space after Basic
- On the Body tab, select raw, set the type to JSON, and enter the body below. Replace <username> with the user's ID and <phone-number> with the number to send the OTP to, including the country code, for example +15555550100. Setting the type to JSON makes Postman send the Content-Type: application/json header that the API needs.
{
"user_id": "<username>",
"type": "sms",
"token": "<phone-number>"
}The token field sends the OTP to that number even if it is not stored in the user's profile.
Add the Pre-Request Script
- Open the request's Scripts tab and select Before request. In older Postman versions, this is the Pre-request Script tab.
- Paste in the script below, then save the request.
var moment = require('moment');
var CryptoJS = require('crypto-js');
// The request date for the X-SA-Ext-Date header: UTC, with milliseconds
pm.collectionVariables.set('currentDate', moment().utc().format('ddd, DD MMM YYYY HH:mm:ss.SSS') + ' GMT');
// The request path, from the realm onward, for example /secureauth2/api/v2/auth
var fullUrl = pm.variables.replaceIn(pm.request.url.toString());
var host = pm.collectionVariables.get('baseUrl');
var path = '/' + fullUrl.replace(host, '').replace(/^\/+/, '');
// The string to sign: method, date, App ID and path, one per line, then the body if there is one
var parts = [
pm.request.method,
pm.collectionVariables.get('currentDate'),
pm.collectionVariables.get('appID'),
path
];
var body = (pm.request.body && pm.request.body.raw) ? pm.variables.replaceIn(pm.request.body.raw) : '';
if (body) {
parts.push(body);
}
// HMAC-SHA256 of that string, keyed with the hex-decoded App Key, then Base64
var hmac = CryptoJS.HmacSHA256(
CryptoJS.enc.Utf8.parse(parts.join('\n')),
CryptoJS.enc.Hex.parse(pm.collectionVariables.get('appKey'))
).toString(CryptoJS.enc.Base64);
// The Authorization header value: Base64 of AppID:HMAC
pm.collectionVariables.set('signature', require('btoa')(pm.collectionVariables.get('appID') + ':' + hmac));The script runs before every send. It sets currentDate to the current time in Coordinated Universal Time (UTC), with milliseconds. It then joins the method, that date, the Application ID, the request path and the body, when there is one, with line breaks, and signs the result with HMAC-SHA256, using the Application Key decoded from hexadecimal. Finally, it sets signature to the Base64 encoding of the Application ID and that signature, joined by a colon. Because each send gets a new date and signature, you can send the request again without changing anything.
Send the Request
Click Send. A successful request returns 200 OK with a body like the one below, and the phone receives the same OTP by text message.
{
"otp": "123456",
"status": "valid",
"message": "",
"user_id": "<username>"
}If the response has no otp field, Enable Authentication API Validation (the OTP Validation Property in 23.07 and earlier) is set on the realm.
To send the OTP to a phone number that is already in the user's profile, replace "token": "<phone-number>" in the body with the profile's phone field, for example "factor_id": "Phone1". To list a user's factor IDs, duplicate the request, set the method to GET, change the address to {{baseUrl}}/<realm>/api/v2/users/<username>/factors, and clear the text on the Body tab.
If the request fails, the message field in the response says why:
- Invalid credentials. The signature did not match. Check that appKey is the realm's Application Key exactly, and that the address starts with {{baseUrl}}, because the script works out the request path by removing baseUrl from the address.
- AppId is unknown. appID is not this realm's Application ID.
- Clock skew of message is outside threshold. The computer's clock is too far from the IdP server's clock. Correct the time and send the request again.
- Authentication header has been seen before. The same signature was sent twice. Send the request again; the script makes a new signature each time.
If Postman shows TypeError: Cannot read properties of undefined (reading 'length') instead of sending the request, a collection variable is missing or its name does not match the script exactly.
Special Considerations
- Protect the Application Key. The Application ID and Application Key let anyone call this realm's API, including sending OTPs to any phone number. Treat the key like a password, and do not share or export the collection while it holds the key. If the key is exposed, generate new credentials on the realm's API tab and update every application that uses them.
- Each request counts toward multi-factor throttling. If multi-factor throttling is turned on for the realm, every request counts as one attempt for the user named in the request. Once the user reaches the limit, the API returns Multi-factor limit reached instead of sending an OTP, and if throttling is set to lock the account, the user's account is locked as well. Use a test account while you set this up.
- Realms that Login for Endpoints uses. Older releases mark the OTP Validation Property as required for Login for Endpoints, so do not clear Enable Authentication API Validation on a realm that Login for Endpoints uses. Enable the API on a separate realm for this integration instead.
SecureAuth Knowledge Base Articles provide information based on specific use cases and may not apply to all appliances or configurations. Be advised that these instructions could cause harm to the environment if not followed correctly or if they do not apply to the current use case.
Customers are responsible for their own due diligence prior to utilizing this information and agree that SecureAuth is not liable for any issues caused by misconfiguration directly or indirectly related to SecureAuth products.
Comments
0 comments
Please sign in to leave a comment.