AWS API Dlang, hmac sha256 function.
holo via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Sat Oct 10 10:04:35 PDT 2015
On Friday, 9 October 2015 at 16:30:26 UTC, holo wrote:
> OK i find out error, in addRequestHeader i was using ":" after
> header name what casing problem. I removed it and now im
> getting "unauthorized". Here is how it looks right now:
>
> HTTP/1.1 401 Unauthorized\r\n
> [Expert Info (Chat/Sequence): HTTP/1.1 401
> Unauthorized\r\n]
> Request Version: HTTP/1.1
> Status Code: 401
> Response Phrase: Unauthorized
> Transfer-Encoding: chunked\r\n
> Date: Fri, 09 Oct 2015 16:22:47 GMT\r\n
> Server: AmazonEC2\r\n
> \r\n
> [HTTP response 1/2]
> [Next request in frame: 8371]
> HTTP chunked response
> Data chunk (254 octets)
> Chunk size: 254 octets
> Data (254 bytes)
>
> In data field i can read:
>
> "AWS was not able to validate provided access credentials" Is
> my signing process incorrect?
Maybe i will put my present code again:
#!/usr/bin/rdmd -L-lcurl
import std.stdio;
import std.string;
import std.file;
import std.datetime;
import std.process;
import std.digest.sha;
import std.net.curl;
import std.uri;
import sigv4;
auto zone = "us-east-1";
auto service = "ec2";
void main()
{
auto accKey = environment["AWS_ACCESS_KEY"];
auto secKey = environment["AWS_SECRET_KEY"];
auto currentClock = Clock.currTime;
auto currentDate = cast(Date)currentClock;
auto curDateStr = currentDate.toISOString;
auto currentTime = cast(TimeOfDay)currentClock;
auto curTimeStr = currentTime.toISOString;
auto xamztime = curDateStr ~ "T" ~ curTimeStr ~ "Z";
string[string] empty;
SignableRequest r;
r.dateString = curDateStr;
r.timeStringUTC = curTimeStr;
r.region = zone;
r.service = service;
r.canonicalRequest = CanonicalRequest(
"POST",
"/",
["action" : "DescribeInstances", "version" : "2013-10-15"],
// ["accept" : "*/*",
["content-type" : "application/x-www-form-urlencoded;
charset=utf-8",
"host" : service ~ ".amazonaws.com",
"x-amz-date" : xamztime],
cast(ubyte[])"");
//cast(ubyte[])"Action=DescribeInstances&Version=2013-10-15");
auto qParm =
canonicalQueryString(r.canonicalRequest.queryParameters);
auto sigHead = canonicalHeaders(r.canonicalRequest.headers);
auto sigStr = signableString(r);
auto sigKey = signingKey(secKey, curDateStr, zone, service);
auto signature = sign(sigKey,
cast(ubyte[])sigStr).toHexString().toLower();
writeln();
writeln(qParm);
writeln();
writeln(sigHead);
writeln();
writeln(sigStr);
writeln();
writeln(signature);
writeln();
auto client = HTTP();
// client.clearRequestHeaders;
client.addRequestHeader("content-type",
"application/x-www-form-urlencoded; charset=utf-8");
client.addRequestHeader("host", service ~ ".amazonaws.com");
client.addRequestHeader("x-amz-date", xamztime);
client.addRequestHeader("authorization", "AWS4-HMAC-SHA256" ~ "
" ~ "Credential=" ~ accKey ~ "/" ~ xamztime ~ "/" ~ zone ~ "/" ~
service ~ "/" ~ "aws4_request" ~ ", " ~ "SignedHeaders=" ~
"content-type;host;x-amz-date" ~ ", " ~ "Signature=" ~ signature);
auto url = "ec2.amazonaws.com/?" ~
"Action=DescribeInstances&Version=2013-10-15";
auto urlenc = encode(url);
writeln(url);
auto content = get(urlenc, client);
writeln(content);
}
Is my signing process correct?
More information about the Digitalmars-d-learn
mailing list