-
Notifications
You must be signed in to change notification settings - Fork 3
/
WAFR-Cognito-Auth.user.js
202 lines (168 loc) · 7.06 KB
/
WAFR-Cognito-Auth.user.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
// ==UserScript==
// @name Amazon Web Services Well-Architected Framework Review Helper - Conformance Module
// @namespace http://console.aws.amazon.com/wellarchitected/
// @version 0.1.0
// @description Change the Cognito integration with latest version d36c02bd56, which only require username & password. ClientID & PoolID been removed from user input.
// @author [email protected] (github:juntinyeh)
// @match https://*.console.aws.amazon.com/wellarchitected/*
// @grant GM.xmlHttpRequest
// @include https://raw.githubusercontent.com/juntinyeh/aws-wafr-objective-helper/main/
// @run-at document-end
// ==/UserScript==
/*
set Log_Level = 'debug' if you want to try something new and use the debug(log_message) it will help you to dump the timestamp and message on browser console.
*/
var LOG_LEVEL = '';
var OH_AUTH_APIGW = '';
var OH_AUTH_DEFAULT_USERNAME = '';
var OH_AUTH_DEFAULT_PASSWORD = '';
var OH_AUTH_DEFAULT_POOLID = '';
var OH_AUTH_DEFAULT_CLIENTID = '';
/***************************************/
/*Cognito Auth*/
var oh_auth_container = document.createElement('div'); //Div Container
oh_auth_container.id = 'oh_auth_container';
oh_auth_container.style.display = 'block';
oh_auth_container.className = 'awsui-util-container-header';
var oh_auth_div_username = document.createElement('div');
oh_auth_div_username.id = 'oh_auth_div_username';
oh_auth_div_username.innerHTML = "username";
var oh_auth_input_username = document.createElement('input');
oh_auth_input_username.id = 'oh_auth_input_username';
oh_auth_input_username.type = 'text';
oh_auth_input_username.className = 'awsui-input';
oh_auth_input_username.value = OH_AUTH_DEFAULT_USERNAME;
oh_auth_div_username.appendChild(oh_auth_input_username);
var oh_auth_div_password = document.createElement('div');
oh_auth_div_password.id = 'oh_auth_div_password';
oh_auth_div_password.innerHTML = "password";
var oh_auth_input_password = document.createElement('input');
oh_auth_input_password.id = 'oh_auth_input_password';
oh_auth_input_password.type = 'password';
oh_auth_input_password.className = 'awsui-input awsui-input-type-password';
oh_auth_input_password.value = OH_AUTH_DEFAULT_PASSWORD;
oh_auth_div_password.appendChild(oh_auth_input_password);
var oh_auth_div_poolid = document.createElement('div');
oh_auth_div_poolid.id = 'oh_auth_div_poolid';
oh_auth_div_poolid.innerHTML = "poolID";
var oh_auth_input_poolid = document.createElement('input');
oh_auth_input_poolid.id = 'oh_auth_input_poolid';
oh_auth_input_poolid.type = 'password';
oh_auth_input_poolid.className = 'awsui-input awsui-input-type-password';
oh_auth_input_poolid.value = OH_AUTH_DEFAULT_POOLID;
oh_auth_div_poolid.appendChild(oh_auth_input_poolid);
var oh_auth_div_clientid = document.createElement('div');
oh_auth_div_clientid.id = 'oh_auth_div_clientid';
oh_auth_div_clientid.innerHTML = "clientID";
var oh_auth_input_clientid = document.createElement('input');
oh_auth_input_clientid.id = 'oh_auth_input_clientid';
oh_auth_input_clientid.type = 'password';
oh_auth_input_clientid.className = 'awsui-input awsui-input-type-password';
oh_auth_input_clientid.value = OH_AUTH_DEFAULT_CLIENTID;
oh_auth_div_clientid.appendChild(oh_auth_input_clientid);
var oh_auth_submit = document.createElement('button');
oh_auth_submit.id = 'oh_auth_submit';
oh_auth_submit.innerHTML = 'Cognito Authentication'
oh_auth_submit.addEventListener("click", function() {
OH_auth_post_to_cognito();
});
oh_auth_submit.className = "awsui-button awsui-button-variant-primary";
var oh_auth_revoke = document.createElement('a');
oh_auth_revoke.id = "oh_auth_revoke";
oh_auth_revoke.innerHTML = '<p>sign out</p>';
oh_auth_revoke.addEventListener("click", function(){
OH_Auth_Revoke_Token();
})
/***************************************/
/* Mandatory function : OH_<Help-Module-Name>_Append_Div()
function OH_<Help-Module-Name>_Append_Div(){
//Steps before the main helper Div will apend module div as ChildNode
//here is the only chance you will call the variable oh_div_helper, append your local DOM Element into parent Div.
}
*/
function OH_Auth_Append_Div(){
return oh_auth_container;
}
function OH_Auth_Show_Signin_form(){
oh_auth_container.innerHTML = '';
oh_auth_container.appendChild(oh_auth_div_username);
oh_auth_container.appendChild(oh_auth_div_password);
//oh_auth_container.appendChild(oh_auth_div_poolid);
//oh_auth_container.appendChild(oh_auth_div_clientid);
oh_auth_container.appendChild(document.createElement('br'));
oh_auth_container.appendChild(oh_auth_submit);
}
function OH_Auth_check_id_token(){
(async () => {
let id_token = await GM.getValue("id_token",-1);
let id_token_ts = await GM.getValue("id_token_ts", -1);
if(id_token == -1 || id_token_ts == -1){
OH_Auth_Show_Signin_form();
console.log("Token not exited");
}
else if( Math.floor(Date.now() / 1000) - id_token_ts >86400 )
{
console.log("Token expired");
OH_Auth_Show_Signin_form();
div_append_text('oh_auth_container','Token expired');
}
else
{
div_reset_innerHTML('oh_auth_container');
oh_auth_container.innerHTML = '<p>Cognito Authenticated</p>';
oh_auth_container.appendChild(oh_auth_revoke);
}
})();
}
function OH_auth_post_to_cognito(JSON_value, callback){
var data = {
"username": oh_auth_input_username.value,
"password": oh_auth_input_password.value/*,
"userpoolid": oh_auth_input_poolid.value,
"clientid": oh_auth_input_clientid.value*/
}
var GM_payload = {
method: 'POST',
url: OH_AUTH_APIGW,
data: JSON.stringify(data),
headers: {"Content-Type":"application/json"},
onload: function(response) {
var data = JSON.parse(response.responseText);
if(data.hasOwnProperty("id_token"))
{
console.log("id_token",data["id_token"]);
GM.setValue("id_token",data["id_token"]);
GM.setValue("id_token_ts",Math.floor(Date.now() / 1000)),
OH_Auth_check_id_token();
}
}
};
GM.xmlHttpRequest(GM_payload);
}
function OH_Auth_Revoke_Token()
{
(async () => {
let keys = await GM.deleteValue("id_token");
OH_Auth_check_id_token();
})();
}
/*
Mandatory function OH_<Help-Module-Name>_reload()
function OH_Context_Helper_reload() {
*/
function OH_Auth_reload()
{
OH_Auth_check_id_token();
console.log("Conformance Helper reload Here");
}
/*
Mandatory function OH_<Help-Module-Name>_init()
function OH_Context_Helper_init() {
// Main entry point for the scripts
// All the step which need to load once at initial time.
*/
function OH_Auth_init() {
/* Main entry point for the scripts */
OH_Auth_check_id_token();
console.log("Conformance Helper Init Here");
}