forked from polandj/ESPAWSClient
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ESPAWSClient.h
72 lines (66 loc) · 2.52 KB
/
ESPAWSClient.h
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
/*
* ESPAWSClient
* Copyright 2018 - Jonathan Poland
*
* A simple client for AWS v4 request signing via the ESP8266.
*
* It implements the workflow described at:
* https://docs.aws.amazon.com/apigateway/api-reference/signing-requests/
*
* It depends on:
* https://github.com/PaulStoffregen/Time
* https://github.com/jjssoftware/Cryptosuite
*
* The ESP device must have a valid time set (e.g. NTP) for this to work.
*
*/
#ifndef ESPAWSCLIENT_H_
#define ESPAWSCLIENT_H_
#include <ESP8266WiFi.h>
typedef enum : uint8_t {
CAPTURE_HEADERS = 1,
CAPTURE_BODY = 2,
CAPTURE_BODY_ON_ERROR = 4
} AWSResponseFieldMask;
class AWSResponse {
public:
AWSResponse() {
status = 0;
contentLength = 0;
}
uint16_t status;
String contentType;
int contentLength;
String headers;
String body;
};
class ESPAWSClient : public WiFiClientSecure {
public:
ESPAWSClient(String service, String key, String secret, String host,
String region="us-east-1", String TLD="amazonaws.com");
void setCustomFQDN(String fqdn);
void setResponseFields(AWSResponseFieldMask fields);
String createRequest(String method, String uri, String payload="",
String contentType="application/json", String queryString="");
AWSResponse doGet(String uri, String queryString="");
AWSResponse doPost(String uri, String payload, String contentType="application/json", String queryString="");
AWSResponse send(const String request);
protected:
String _awsHost;
String _awsRegion;
String _awsTLD;
String _awsService;
String _awsKey;
String _awsSecret;
String _customFQDN;
String _signedHeaders = "content-type;host;x-amz-content-sha256;x-amz-date";
AWSResponseFieldMask _responseFields = CAPTURE_BODY_ON_ERROR;
String FQDN();
String hexHash(uint8_t *hash);
String createCanonicalHeaders(String contentType, String date, String time, String payloadHash);
String createRequestHeaders(String contentType, String date, String time, String payload, String payloadHash, String signature);
String createStringToSign(String canonical_request, String date, String time);
String createCanonicalRequest(String method, String uri, String date, String time, String payloadHash, String queryString="", String contentType="application/json");
String createSignature(String toSign, String date);
};
#endif