Authentication via Signed Stringπ°
Depending on the REST resource being used, you may be required to use one of two authentication types, API authentication or User authentication. The type of authentication required for each resource is listed as part of that resource's documentation
API authentication
If the REST resource you're calling requires API authentication, you must include these HTTP headers with your application's HTTP requests. They are:
-
X-Imagen-API-Key: The value must be your application's API key.
-
X-Imagen-API-Signature: The value must be a base64 digest of anΒ HMAC-SHA256Β hash prepended with the stringΒ HMAC-SHA256. The hashed content is a concatenation of a string (seeΒ Generating an Imagen API signature) and is signed with your application'sΒ secret access key.
-
X-Imagen-DateΒ orΒ Date: The value must be the time of the HTTP request inΒ RFC-7231, IMF-fixdateΒ format. This format requires time values in Coordinated Universal Time (UTC), although the format indicates UTC by the three-letter abbreviation for Greenwich Mean Time, "GMT", a predecessor of the UTC name. Required because Imagen will only accept a request if its time stamp is within Β±5 minutes of the time (UTC) at which it reaches the service.
-
(Optional)Β Content-Length: Size of the request body, in decimal number of OCTETs, as perΒ RFC-7230.
-
(Optional)Β Content-MD5: base64 of 128-bit MD5 digest of the request body, as perΒ RFC-1864.
-
(Optional)Β Content-Type: Media type of the request body, as perΒ RFC-7231.
Note:X-Imagen-DateΒ is included as some HTTP client libraries do not allow the developer to set theΒ DateΒ HTTP header manually or read its value for inclusion in theΒ X-Imagen-API-Signature. If bothΒ X-Imagen-DateΒ andΒ DateΒ are set, Imagen will take the value ofΒ X-Imagen-DateΒ and ignoreΒ Date. If the 'Date' header (https://tools.ietf.org/html/rfc7231#section-7.1.1.2) is used instead of 'X-Imagen-Date', then Imagen will ignore it unless it uses theΒ RFC-7231, IMF-fixdateΒ format. An example of a time stamp in the correct format is: 'Wed, 03 May 2015 09:04:08 GMT'. Two digits are required for all date and time numbers except for the year, which must be 4 digits. A single space between components is also required. If the format for the 'X-Imagen-Date' header is wrong your request will be rejected with an explanatory error message, so for ease of debugging, we recommend using the 'X-Imagen-Date' header, whether the 'Date' header is also used or not.
Generating an Imagen API signature
The value ofΒ X-Imagen-API-SignatureΒ is a base64 digest of anΒ HMAC-SHA256Β hash. The hashed content is a concatenation of the following string (see StringToSign) and is signed with your application'sΒ secret access key.
The values of the standard HTTP headers must be included in the string in the order shown, without header names. If an optional header is not being supplied with the request then the value must be left blank.
StringToSign = HTTPVERB + "\n" +
Content-Length + "\n" +
Content-MD5 + "\n" +
Content-Type + "\n" +
Date + "\n" +
CanonicalizedResource;
StringToSign summary
-
\n: All new-line characters shown are required within the signature string, even if the value preceding them is empty.
-
HTTPVERB: The HTTP method of your request e.g.Β POST,Β GET,Β PUT. It should always be uppercase.
-
Content-Length: The value of your request'sΒ Content-LengthΒ HTTP header, as perΒ RFC-7230. If not set, leave blank.
-
Content-MD5: The value of your request'sΒ Content-MD5Β HTTP header, as perΒ RFC-1864. If not set, leave blank.
-
Content-Type: The value of your request'sΒ Content-TypeΒ HTTP header (e.g.Β application/json), as perΒ RFC-7231. If not set, leave blank.
-
Date: The value of your request'sΒ X-Imagen-DateΒ orΒ DateΒ HTTP header inΒ RFC-7231, IMF-fixdateΒ format.
-
CanonicalizedResource: The Imagen REST resource being targeted by the request. For example, the canonicalized resource ofΒ https://example.com/core/v1/applicationΒ isΒ /core/v1/application
Example StringToSign before hashing
GET
Tue, 23 Jun 2015 12:54:48 GMT
/core/v1/application
Note:Β Content-LengthΒ andΒ Content-MD5Β values are empty as we have not included these HTTP headers in our request.
Example X-Imagen-API-Signature
Signing our StringToSign with the secret access keyΒ ujeQhWRMGY3YfK4vARjUGm9dMZ5lCoxtCMX64vsTΒ and prepending it withΒ HMAC-SHA256Β produces the followingΒ X-Imagen-API-SignatureΒ value:
HMAC-SHA256 4Xk9nftZ1Vr5OlHF4Wrxm5pisgY5WUHsS0bKNjzUJpE=
User authentication
If the REST resource you're calling requires user authentication, you must include two HTTP headers with your application's HTTP requests. They are:
-
X-Imagen-API-Key: The value must be your application's API key.
-
Authorization: Bearer {userAccessToken}:Β {userAccessToken}Β is anΒ Access tokenΒ granted to your application by a user that has authorized it to perform a specific set of actions on their behalf. AnΒ Access tokenΒ is granted by way of OAuth 2.0.
OAuth in Imagen
If you're developing an Imagen application, it's likely that you'll need to make use of an Imagen REST resource that requires user authentication, which necessitates the usage of anΒ Access token. AnΒ Access tokenΒ represents an Imagen user that has given permission for your application to perform actions on their behalf. To get anΒ Access tokenΒ for a user, your application must make use of Imagen's OAuth 2.0 implementation.
Imagen OAuth glossary
-
Authorization code: If a user authorizes your application to perform actions on their behalf, your application will be passed anΒ Authorization codeΒ that must be converted into anΒ Access tokenΒ within 30 seconds. After 30 seconds has elapsed, theΒ Authorization codeΒ will become invalid.
-
Access token: Used as part of the Authorization HTTP header when making an HTTP request to an Imagen REST resource that requires user authentication. AnΒ Access tokenΒ never expires, but can be revoked via ImagenWeb.
Supported OAuth grant types
-
Authorization code: The most common OAuth 2.0 grant type and most suited to applications where client credentials are stored securely.Β Authorization codeΒ grant type involves the user granting the client anΒ Authorization codeΒ that will then be exchanged for anΒ Access token. This must be used withΒ WebΒ applications.
-
Implicit: For applications unable to store client credentials securely (such as a mobile or JavaScript application), implicit grant type should be used. This is similar to theΒ Authorization codeΒ grant type except that the request returns anΒ Access tokenΒ instead of anΒ Authorization code. This must be used withΒ InstalledΒ applications.
Authorization code URL summary
-
imagenweb: URL to your instance of ImagenWeb.
-
apiKey: Your application'sΒ API Key.
-
redirectURI: Your application's redirect URI.
-
state: This will be sent back to your application as a query string parameter as part of the HTTP response from ImagenWeb, which can be used to verify that the responder is indeed ImagenWeb.
-
scope: A space separated list of scopes that are used to inform the user what your application intends to do on their behalf. The configuration of an application's scope is delegated to an Imagen administrator.
Authorization code grant type
Getting an Access token
To get anΒ Access tokenΒ for your application, you must first get anΒ Authorization code, which is returned after a user agrees to/authorizes your application. This agreement is made on ImagenWeb and then the resultantΒ Authorization codeΒ is returned to your application's Redirect URI as a query string parameter namedΒ code.
Getting an Authorization code
To get the aforementionedΒ Authorization code, your application must redirect the user to the following URL:
https://{imagenweb}/oauth/authorize?response_type=code&client_id={apiKey}&redirect_uri={redirectURI}&state={state}&scope={scope}
A successful authorization will pass the application theΒ Authorization codeΒ as a query string parameter namedΒ codeΒ to your application's Redirect URI:
{redirectURI}?code=xyz&state={state}
Converting an Authorization code into an Access token
Once your application has been granted anΒ Authorization code, it has 30 seconds to convert it into anΒ Access token.
To achieve this, it will need to make an HTTP POST request to the following URL:
https://{imagenweb}/oauth/token
With the following POST parameters:
grant_type=authorization_code&code={AuthorizationCode}
In order to authenticate your application with the OAuth 2.0 server, this HTTP request must also include a Basic authentication HTTP header, where the username is your application'sΒ API KeyΒ and password is theΒ Secret access key.
In summary, the equivalent cURL command for this HTTP POST request should look like:
curl -u {APIKey}:{SecretAccessKey} https://{imagenweb}/oauth/token -d 'grant_type=authorization_code&code={AuthorizationCode}'
A successfulΒ Access tokenΒ request will return a standardΒ Access tokenΒ in JSON format:
{"access_token":"03807cb390319329bdf6c777d4dfae9c0d3b3c35","token_type":"bearer","scope":null}
At this point, your application has anΒ Access token.
Implicit grant type
Getting an Access token
To get anΒ Access token, your application must redirect the user to the following URL:
https://{imagenweb}/oauth/authorize?response_type=token&client_id={apiKey}&redirect_uri={redirectURI}&state={state}&scope={scope}
If the redirect_uri is a URL, anΒ Access tokenΒ will be returned in the JSON format:
{"access_token":"03807cb390319329bdf6c777d4dfae9c0d3b3c35","token_type":"bearer","scope":null}
Other supported Redirect URIs
-
urn:ietf:wg:oauth:2.0:oob: TheΒ Access tokenΒ will be returned in the title bar of the browser and the web page will prompt the user to copy and paste the code into their application. This is useful when the application is unable to listen on an HTTP port.
-
urn:ietf:wg:oauth:2.0:oob:auto: TheΒ Access tokenΒ will be returned in the title bar of the browser and the web page will instruct the user to close the window. This is useful when your application reads theΒ Access tokenΒ from the title of the HTML page, but is unable to close the page itself.
Note: It is recommended that you make use of theΒ Download JSONΒ option for your application, which can be found on the Your applications page on ImagenWeb. The JSON file contains your application'sΒ API Key,Β Secret access key, Redirect URI and scopes.
Note: AnΒ Access tokenΒ never expires, but can be revoked via ImagenWeb. If it is revoked, a newΒ Access tokenΒ must be regenerated.