forked from caffeinehit/django-oauth2-provider
-
Notifications
You must be signed in to change notification settings - Fork 3
/
aws_identity_example.py
68 lines (55 loc) · 1.92 KB
/
aws_identity_example.py
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
import os
import sys
import json
from datetime import datetime
from urllib import request, error
import requests
import boto3
# aws-v4-signature==2.0
from awsv4sign import generate_http11_header
service = 'sts'
region = 'us-west-2'
session = boto3.Session()
creds = session.get_credentials()
access_key = creds.access_key
secret_key = creds.secret_key
session_token = creds.token
print(f"access_key: {access_key[:10]}<redacted...>")
print(f"secret_key: {secret_key[:10]}<redacted...>")
print(f"session_token: {session_token[:20]}<redacted...>")
print(f"profile: {os.environ.get('AWS_PROFILE')}")
url = 'https://sts.{region}.amazonaws.com/'.format(region=region)
httpMethod = 'post'
canonicalHeaders = {
'host': f'sts.{region}.amazonaws.com',
'x-amz-date': datetime.utcnow().strftime('%Y%m%dT%H%M%SZ'),
'content-type': 'application/x-www-form-urlencoded; charset=utf-8',
}
if session_token:
canonicalHeaders['x-amz-security-token'] = session_token
payload_str = "Action=GetCallerIdentity&Version=2011-06-15"
headers = generate_http11_header(
service, region, access_key, secret_key,
url, 'post', canonicalHeaders, {},
'', payload_str
)
token_request_args = {
"grant_type": "aws_identity",
"region": region,
"post_body": payload_str,
"headers_json": json.dumps(headers),
}
print(payload_str)
print(json.dumps(headers, indent=4))
req = request.Request("https://sts.us-west-2.amazonaws.com/", data=payload_str.encode('utf-8'), headers=headers, method='POST')
try:
response = request.urlopen(req)
print(f"Local request test result: {response.read()}")
except error.HTTPError as e:
print(f"HTTPError: {e}: {e.fp.read()}")
sys.exit(1)
print("Attempting access_token grant request with same signed request:\n")
token_response = requests.post("http://localhost:8000/oauth2/access_token",
data=token_request_args)
token_info = token_response.json()
print(token_info)