Zig
Zig
AWS Security Token Service (STS) GetSessionToken
See more AWS Security Token Service Examples
Returns a set of temporary credentials for an AWS account or IAM user.Chilkat Zig Downloads
const std = @import("std");
const chilkat = @import("chilkat");
pub fn main(init: std.process.Init) !void {
const alloc = init.arena.allocator();
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
const rest = try chilkat.Rest.init();
defer rest.deinit();
// Connect to the Amazon AWS REST server.
// such as https://sts.us-west-2.amazonaws.com/
const b_tls = true;
const port = 443;
const b_auto_reconnect = true;
try rest.connect("sts.us-west-2.amazonaws.com", port, b_tls, b_auto_reconnect);
// Provide AWS credentials for the REST call.
const auth_aws = try chilkat.AuthAws.init();
defer auth_aws.deinit();
auth_aws.setAccessKey("AWS_ACCESS_KEY");
auth_aws.setSecretKey("AWS_SECRET_KEY");
// the region should match our URL above..
// See https://docs.aws.amazon.com/IAM/latest/UserGuide/id_credentials_temp_enable-regions.html
auth_aws.setRegion("us-west-2");
auth_aws.setServiceName("sts");
rest.setAuthAws(auth_aws) catch {};
rest.addQueryParam("Version", "2011-06-15") catch {};
rest.addQueryParam("Action", "GetSessionToken") catch {};
rest.addQueryParam("DurationSeconds", "3600") catch {};
const response_xml = rest.fullRequestNoBody(alloc, "GET", "/") catch {
std.debug.print("{s}\n", .{try rest.getLastErrorText(alloc)});
return;
};
// A successful response will have a status code equal to 200.
if (rest.getResponseStatusCode() != 200) {
std.debug.print("response status code = {d}\n", .{rest.getResponseStatusCode()});
std.debug.print("response status text = {s}\n", .{try rest.getResponseStatusText(alloc)});
std.debug.print("response header: {s}\n", .{try rest.getResponseHeader(alloc)});
std.debug.print("response body: {s}\n", .{response_xml});
return;
}
// Examine the successful XML response (shown below)
const xml = try chilkat.Xml.init();
defer xml.deinit();
xml.loadXml(response_xml) catch {};
std.debug.print("{s}\n", .{try xml.getXml(alloc)});
// Sample response:
// <?xml version="1.0" encoding="utf-8"?>
// <GetSessionTokenResponse xmlns="https://sts.amazonaws.com/doc/2011-06-15/">
// <GetSessionTokenResult>
// <Credentials>
// <AccessKeyId>AS........T4N</AccessKeyId>
// <SecretAccessKey>05W........ARPMr</SecretAccessKey>
// <SessionToken>IQoJb3J........llpIMI=</SessionToken>
// <Expiration>2022-09-07T00:22:51Z</Expiration>
// </Credentials>
// </GetSessionTokenResult>
// <ResponseMetadata>
// <RequestId>8bad22cc-1c55-4265-a010-45d139359404</RequestId>
// </ResponseMetadata>
// </GetSessionTokenResponse>
// Sample parse code:
_ = try xml.getAttrValue(alloc, "xmlns");
_ = try xml.getChildContent(alloc, "GetSessionTokenResult|Credentials|AccessKeyId");
_ = try xml.getChildContent(alloc, "GetSessionTokenResult|Credentials|SecretAccessKey");
_ = try xml.getChildContent(alloc, "GetSessionTokenResult|Credentials|SessionToken");
_ = try xml.getChildContent(alloc, "GetSessionTokenResult|Credentials|Expiration");
_ = try xml.getChildContent(alloc, "ResponseMetadata|RequestId");
// Save the session token XML to a file for use by another Chilkat example..
try xml.saveXml("qa_data/tokens/aws_session_token.xml");
}