/
AuthDisplay.js
114 lines (94 loc) · 3.88 KB
/
AuthDisplay.js
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import React, { useState, useEffect } from 'react';
import {Auth} from 'aws-amplify';
import JSONTree from 'react-json-tree';
import AWS from 'aws-sdk';
import AWSConfiguration from './aws-iotcore-configuration.js';
function createAndAttachPolicy(id) {
var Iot = new AWS.Iot({region: AWSConfiguration.region, apiVersion: AWSConfiguration.apiVersion, endpoint: AWSConfiguration.endpoint});
var policyName = "amplifyIotReactApp";
var params = {policyName: policyName};
Iot.getPolicy(params , function(err, data) {
if (err) {
var policy = {"Version": "2012-10-17", "Statement": [{"Effect": "Allow", "Action": ["*"],"Resource": ["*"]}]};
var policyDoc = JSON.stringify(policy);
console.log("Creating policy: " + policyName + " with doc: " + policyDoc);
var params = {
policyName: policyName,
policyDocument: policyDoc
};
Iot.createPolicy(params , function(err, data) {
if (err) {
//console.error(err);
if (err.code !== 'ResourceAlreadyExistsException') {
console.log(err);
}
}
else {
console.log("CreatePolicy response=" + data);
attachPolicy(id, policyName);
}
});
}
else {
console.log("Policy " + policyName + " already exists..");
attachPolicy(id, policyName);
}
});
}
function attachPolicy(id, policyName) {
var Iot = new AWS.Iot({region: AWSConfiguration.region, apiVersion: AWSConfiguration.apiVersion, endpoint: AWSConfiguration.endpoint});
var params = {policyName: policyName, target: id};
console.log("Attach IoT Policy: " + policyName + " with cognito identity id: " + id);
Iot.attachPolicy(params, function(err, data) {
if (err) {
//console.error(err);
if (err.code !== 'ResourceAlreadyExistsException') {
console.log(err);
}
}
else {
console.log("Successfully attached policy with the identity", data);
}
});
}
function AuthDisplay(props) {
const [essentialCredentials, setEssentialCredentials] = useState({});
useEffect(() => {
console.log('useEffect for essentialCredentials triggered.');
Auth.currentCredentials()
.then(credentials => {
setEssentialCredentials(Auth.essentialCredentials(credentials));
});
},[]);
// Initialize the Amazon Cognito credentials provider
AWS.config.region = 'us-east-1';
AWS.config.credentials = essentialCredentials;
const [cognitoIdentityId, setCognitoIdentityId] = useState({});
useEffect(() => {
console.log('useEffect for cognitoIdentityId triggered.');
Auth.currentCredentials().then((info) => {
setCognitoIdentityId(info._identityId);
console.log("Calling Iot to create policy and attaching with cognito identity");
createAndAttachPolicy(info._identityId);
});
},[]);
return (
<div className="AuthDisplay">
Auth state: {props.authState}
<br/><br/>
Auth identity ID: {cognitoIdentityId.toString()}
<br/><br/>
Auth data:<br/>
<div className="AuthData">
<JSONTree data={props.authData} theme={"tomorrow"} invertTheme={true} />
</div>
<br/><br/>
Essential Credentials:<br/>
<div className="AuthData">
<JSONTree data={essentialCredentials} theme={"tomorrow"} invertTheme={true} />
</div>
<br/><br/>
</div>
);
}
export default AuthDisplay;