Authentication

All interactions between your connector service and the Amazon Yojaka product are via RESTful API calls. This page documents how you can secure these API calls and authenticate your connector service with the Amazon Yojaka product.

AWS Signature Version 4

AWS Signature Version 4 (shortened to AWS SigV4) is a standard mechanism used to add authentication information to AWS requests sent over HTTP. A detailed description of this authentication mechanism can be found here.

Amazon Yojaka leverages the same authentication mechanism for adding authentication information to RESTful API calls made by connectors to Amazon Yojaka.

Note

To authenticate requests to the Amazon Yojaka APIs using the AWS SigV4 mechanism, you will need the access keys of the AWS IAM user that you created during the Amazon Web Services Setup step and the ARN of the AWS IAM role provided during the Post On-boarding Steps step.

How to sign requests

The exact process for signing your Amazon Yojaka API call are documented here. You will need the following information while signing the canonical request to the API:

Region

The region you will need to specify while signing the request will vary based on the country/area in which you are invoking Amazon Yojaka APIs. Refer to the table below to find the region to use.

Country / Area

Region

IN

eu-west-1

NA

us-east-1

Service

To invoke the Amazon Yojaka APIs, the service to use while signing the request is always execute-api.

Tip

AWS SigV4 signing of your requests to the Amazon Yojaka APIs are automatically handled for you if you use the Amazon Yojaka SDK that is available or most common programming languages. Refer to the SDKs documentation for more details.

Sample Code

Below is some sample code for authenticating a request to the Amazon Yojaka APIs in multiple programming languages.

Java

The code below uses the standard Apache HttpComponents library and an additional library available here.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import com.amazonaws.auth.AWS4Signer;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.auth.STSAssumeRoleSessionCredentialsProvider;
import com.amazonaws.http.AWSRequestSigningApacheInterceptor;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.securitytoken.AWSSecurityTokenService;
import com.amazonaws.services.securitytoken.AWSSecurityTokenServiceClientBuilder;
import org.apache.http.client.HttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.client.utils.URIBuilder;


    public static void invokeAmazonYojaka() {
        // Create the AWS SigV4 signer
        AWS4Signer aws4Signer = new AWS4Signer();

        // Set the service name and region
        aws4Signer.setServiceName("execute-api");
        aws4Signer.setRegionName("eu-west-1");

        // Create an AWS STS client using the AWS IAM user credentials
        AWSSecurityTokenService awsSecurityTokenService = AWSSecurityTokenServiceClientBuilder.standard()
            .withRegion(Regions.EU_WEST_1)
            // Use the access key and secret access key of the AWS IAM user that you have on-boarded with
            // Amazon Yojaka
            .withCredentials(new AWSStaticCredentialsProvider(
                    new BasicAWSCredentials("AWS_ACCESS_KEY_ID", "AWS_SECRET_ACCESS_KEY")))
            .build();

        // Create an AWS STS assume role credentials provider that will assume the role assigned to your
        // connector
        STSAssumeRoleSessionCredentialsProvider stsAssumeRoleSessionCredentialsProvider =
            // Use the ARN of the AWS IAM role created by the Amazon Yojaka team as part of on-boarding
            new STSAssumeRoleSessionCredentialsProvider.Builder("AWS_IAM_ROLE_ARN", "MyConnector")
                    // Use the AWS STS client created above
                    .withStsClient(awsSecurityTokenService)
                    // The maximum duration of the assume role credentials is 1 hour
                    // This credentials provider class will automatically refresh the session credentials
                    // upon expiry
                    .withRoleSessionDurationSeconds(4 * 60 * 60)
                    .build();

        // Create the AWS SigV4 signing interceptor
        AWSRequestSigningApacheInterceptor awsRequestSigningApacheInterceptor =
            new AWSRequestSigningApacheInterceptor("execute-api", aws4Signer,
                stsAssumeRoleSessionCredentialsProvider);

        // Create the HTTP client
        HttpClient httpClient = HttpClients.custom()
            .addInterceptorLast(awsRequestSigningApacheInterceptor)
            .build();

        // Use the HTTP client to invoke Amazon Yojaka APIs using the HTTP client object
        // Build a request to invoke the UpdateInventory API of the Amazon Yojaka Sandbox
        URIBuilder uriBuilder = new URIBuilder();
        uriBuilder.setScheme("https");
        uriBuilder.setHost("api.sandbox.dub.yojaka.xp.sellers.a2z.com");
        uriBuilder.setPort(443);
        uriBuilder.setPath("v1/inventories");
        // Replace with the appropriate values for locationId, skuId, quantity and inventoryUpdateSequence
        uriBuilder.setCustomQuery("locationId=896e68a8-990f-4f5a-99a2-f9877a7ef36d&skuId=TestSku1"
            + "&quantity=19976&inventoryUpdateSequence=2");

        HttpPut httpPut = new HttpPut(uriBuilder.build());
        httpPut.addHeader("Accept", "application/json");
        // Replace with an appropriate LWA access token
        httpPut.addHeader("X-Amz-Access-Token", "Atza|IwE...");

        // Invoke the method
        HttpResponse httpResponse = httpClient.execute(httpPut);

        // Use the response as appropriate for your application
    }

Note

Use the credentials of the AWS IAM user you created during the Amazon Web Services Setup step to sign your HTTP request in the code above.

C#

The code below uses standard C# programming language constructs and uses an additional library available here.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Text.Json;
using System.Threading.Tasks;
using Amazon.Runtime;
using Amazon.SecurityToken;
using Amazon.SecurityToken.Model;
using Amazon;

using Aws4RequestSigner;

    private static async Task<Dictionary<string, object>> InvokeAmazonYojaka()
    {

        // Create an AWS Credentials object, using the access key and secret access key of the AWS IAM user
        // that you have on-boarded with Yojaka
        AWSCredentials credentials = new Credentials
        {
            AccessKeyId = "AWS_ACCESS_KEY_ID",
            SecretAccessKey = "AWS_SECRET_ACCESS_KEY"
        };

        // Create an AWS STS client using these credentials
        AmazonSecurityTokenServiceClient stsClient =
                new AmazonSecurityTokenServiceClient(credentials, RegionEndpoint.EUWest1);

        // Invoke the AssumeRole operation on this STS Client that will assume the role assigned to your connector
        var apiCallerAssumedRoleResult = stsClient.AssumeRoleAsync(new AssumeRoleRequest
        {
            // Use the ARN of the AWS IAM role created by the Amazon Yojaka team as part of on-boarding
            RoleArn = "AWS_IAM_ROLE_ARN",
            RoleSessionName = "MyConnector",
            // The maximum duration of the temporary security credentials is 1 hour
            DurationSeconds = 3600
        }).Result;

        string AWS_ACCESS_KEY_ID = apiCallerAssumedRoleResult.Credentials.AccessKeyId;
        string AWS_SECRET_ACCESS_KEY = apiCallerAssumedRoleResult.Credentials.SecretAccessKey;
        string AWS_SECURITY_TOKEN = apiCallerAssumedRoleResult.Credentials.SessionToken;

        // Create the AWS SigV4 signer with the AccessKeyId and SecretAccessKey obtained above
        var signer = new AWS4RequestSigner(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY);

        // Build a request to invoke the UpdateInventory API of the Amazon Yojaka Sandbox
        UriBuilder uriBuilder = new UriBuilder
        {
            Scheme = "https",
            Host = "api.sandbox.dub.yojaka.xp.sellers.a2z.com",
            Port = 443,
            Path = "v1/inventories",
            // Replace with the appropriate values for locationId, skuId, quantity and inventoryUpdateSequence
            Query = "locationId=896e68a8-990f-4d5a-9944-f9877a7ef36d&skuId=Sku1&quantity=5&inventoryUpdateSequence=2"
        };

        var request = new HttpRequestMessage
        {
            Method = HttpMethod.Put,
            RequestUri = uriBuilder.Uri,
            Headers = {
                { "Accept", "application/json" },
                // Replace with an appropriate LWA access token
                { "X-Amz-Access-Token", "Atza|IwE..." },
                { "X-Amz-Security-Token", AWS_SECURITY_TOKEN }
            }
        };

        // Sign the request for the eu-west-1 region
        request = await signer.Sign(request, "execute-api", "eu-west-1");

        // Invoke the method
        var client = new HttpClient();
        var response = await client.SendAsync(request);
        response.EnsureSuccessStatusCode();

        // Use the response as appropriate for your application
        return JsonSerializer.Deserialize<Dictionary<string, object>>(await response.Content.ReadAsStringAsync());
    }

Note

Use the credentials of the AWS IAM user you created during the Amazon Web Services Setup step to sign your HTTP request in the code above.

PHP

The code below uses AWS PHP SDK and Guzzle HTTP Client library.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<?php
require 'vendor/autoload.php';

use Aws\Signature\SignatureV4;
use Aws\Credentials\Credentials;
use Aws\Sts\StsClient;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Psr7\Request;

// Create an AWS Credentials object, using the access key and secret access key of the AWS IAM user
// that you have on-boarded with Yojaka
$awsCredentials = new Credentials('AWS_ACCESS_KEY_ID', 'AWS_SECRET_ACCESS_KEY');

// Create an AWS STS client using these credentials
$stsClient = new StsClient([
    'version' => '2011-06-15',
    'credentials' => $awsCredentials,
    'region' => 'eu-west-1'
]);

// Use the ARN of the AWS IAM role created by the Amazon Yojaka team as part of on-boarding
$arn = "AWS_IAM_ROLE_ARN";
$sessionName = "connector-session";

// Invoke the AssumeRole operation on this STS Client that will assume the role assigned to your connector
$result = $stsClient->AssumeRole([
      'RoleArn' => $arn,
      'RoleSessionName' => $sessionName,
]);

// Create an AWS credentials object using the aws access key, secret key and session token from the assumed role
$awsAssumedRoleCredentials = new Credentials(
    $result['Credentials']['AccessKeyId'],
    $result['Credentials']['SecretAccessKey'],
    $result['Credentials']['SessionToken']
);

// Instantiate AWS SignatureV4 client for the eu-west-1 region
$signer = new SignatureV4('execute-api', 'eu-west-1');

// Replace with an appropriate LWA access token
$headers = ['x-amz-access-token' => 'Atza|IwE...'];
$request = new Request('GET', 'https://api.sandbox.dub.yojaka.xp.sellers.a2z.com/v1/inventories?locationId=0687b6dd-598d-4f3f-a9be-b9c4bee4334&skuId=Sku1', $headers);

// Use the AWS SigV4 signer to sign the HTTP request using credentials from the Assumed Role.
$request = $signer->signRequest($request, $awsAssumedRoleCredentials);

// Invoke the method
$client = new Client(['base_uri' => 'api.sandbox.dub.yojaka.xp.sellers.a2z.com']);
$response = $client->send($request, ['debug' => true]);

// Use the response as appropriate for your application
echo $response->getBody();

On this page