-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.htm
383 lines (331 loc) · 13.4 KB
/
index.htm
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
<!DOCTYPE html>
<html lang="en">
<head>
<title>Falc AWS App</title>
</head>
<body>
<script src="./script/jquery.min.js"></script>
<script src="./script/aws-sdk-2.487.0.min.js"></script>
<script src="./script/aws-cognito-sdk.min.js"></script>
<script src="./script/amazon-cognito-identity.min.js"></script>
<script src="./script/s3_upload.js"></script>
<style>
.loader {
border: 16px solid #f3f3f3; /* Light grey */
border-top: 16px solid #3498db; /* Blue */
border-radius: 50%;
width: 20px;
height: 20px;
animation: spin 2s linear infinite;
}
@keyframes spin {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
}
</style>
<input id="emailInput" type="text" placeholder="Email" style="display:none"/></br>
<input id="userNameInput" type="text" placeholder="Username"/></br>
<input id="passwordInput" type="password" placeholder="Password"/></br>
<input id="confirmPasswordInput" type="password" placeholder="Confirm Password" style="display:none"/></br>
<input id="verificationCodeInput" type="text" placeholder="Verification Code" style="display:none"/></br>
<input id="bucketNameInput" type="text" placeholder="S3 Bucket Name" style="display:none"/></br><br>
<input id="logInButton" type="Button" value="Log In" onclick="logIn()">
<input id="registerButton" type="button" value="Register" onclick="register()">
<input id="logOutButton" type="Button" value="Log Out" onclick="logOut()" style="display:none">
<input id="verifyCodeButton" type="button" value="Verify" onclick="verifyCode()" style="display:none">
<input id="listS3ObjectsButton" type="button" value="List S3 Objects" onclick="listS3Objects()" style="display:none">
<input id="uploadS3ObjectsButton" type="button" value="upload S3 Objects" onclick="jslinkRedirection()" style="display:none">
</br></br>
<input id="clearLogsButton" type="button" value="Clear Logs" onclick="clearLogs()">
</br></br>
<div id="loader" class="loader" style="display:none"></div>
</br></br>
<div id="log"></div>
<script>
//=============== AWS IDs ===============
var userPoolId = 'us-east-2_tTtestHHI';
var clientId = '1mhis8testingr9chd8tvdc2n4gs';
var region = 'us-east-2';
var identityPoolId = 'us-east-2:5d646adc-test-4193-aa09-86c5rt3c8';
var bucketName = 'testbucket';
var bucketRegion = 'us-east-2';
var IdentityPoolId = 'us-east-2:test-b1ef-4193-aa09-86c5e5551cc8';
//=============== AWS IDs ===============
var cognitoUser;
var idToken;
var userPool;
var poolData = {
UserPoolId : userPoolId,
ClientId : clientId
};
getCurrentLoggedInSession();
function switchToVerificationCodeView(){
$("#emailInput").hide();
$("#userNameInput").hide();
$("#passwordInput").hide();
$("#confirmPasswordInput").hide();
$("#logInButton").hide();
$("#registerButton").hide();
$("#bucketNameInput").hide();
$("#verificationCodeInput").show();
$("#verifyCodeButton").show();
$("#listS3ObjectsButton").hide();
$("#uploadS3ObjectsButton").hide();
$("#s3filesection").hide();
$("#logOutButton").hide();
}
function switchToRegisterView(){
$("#emailInput").show();
$("#userNameInput").show();
$("#passwordInput").show();
$("#confirmPasswordInput").show();
$("#logInButton").hide();
$("#registerButton").show();
$("#verificationCodeInput").hide();
$("#verifyCodeButton").hide();
$("#listS3ObjectsButton").hide();
$("#uploadS3ObjectsButton").hide();
$("#s3filesection").hide();
$("#bucketNameInput").hide();
$("#logOutButton").hide();
}
function switchToLogInView(){
$("#userNameInput").val('');
$("#passwordInput").val('');
$("#emailInput").hide();
$("#userNameInput").show();
$("#passwordInput").show();
$("#confirmPasswordInput").hide();
$("#logInButton").show();
$("#registerButton").show();
$("#verificationCodeInput").hide();
$("#verifyCodeButton").hide();
$("#listS3ObjectsButton").hide();
$("#uploadS3ObjectsButton").hide();
$("#s3filesection").hide();
$("#bucketNameInput").hide();
$("#logOutButton").hide();
}
function switchToLoggedInView(){
$("#emailInput").hide();
$("#userNameInput").hide();
$("#passwordInput").hide();
$("#confirmPasswordInput").hide();
$("#logInButton").hide();
$("#registerButton").hide();
$("#verificationCodeInput").hide();
$("#verifyCodeButton").hide();
$("#listS3ObjectsButton").show();
$("#uploadS3ObjectsButton").show();
$("#s3filesection").show();
$("#bucketNameInput").show();
$("#logOutButton").show();
}
function clearLogs(){
$('#log').empty();
}
/*
Starting point for user logout flow
*/
function logOut(){
if (cognitoUser != null) {
$("#loader").show();
cognitoUser.signOut();
switchToLogInView();
logMessage('Logged out!');
$("#loader").hide();
}
}
/*
Starting point for user login flow with input validation
*/
function logIn(){
if(!$('#userNameInput').val() || !$('#passwordInput').val()){
logMessage('Please enter Username and Password!');
}else{
var authenticationData = {
Username : $('#userNameInput').val(),
Password : $("#passwordInput").val(),
};
var authenticationDetails = new AmazonCognitoIdentity.AuthenticationDetails(authenticationData);
var userData = {
Username : $('#userNameInput').val(),
Pool : userPool
};
cognitoUser = new AmazonCognitoIdentity.CognitoUser(userData);
$("#loader").show();
cognitoUser.authenticateUser(authenticationDetails, {
onSuccess: function (result) {
logMessage('Logged in!');
switchToLoggedInView();
idToken = result.getIdToken().getJwtToken();
getCognitoIdentityCredentials();
},
onFailure: function(err) {
logMessage(err.message);
$("#loader").hide();
},
});
}
}
/*
Starting point for user registration flow with input validation
*/
function register(){
switchToRegisterView();
if( !$('#emailInput').val() || !$('#userNameInput').val() || !$('#passwordInput').val() || !$('#confirmPasswordInput').val() ) {
logMessage('Please fill all the fields!');
}else{
if($('#passwordInput').val() == $('#confirmPasswordInput').val()){
registerUser($('#emailInput').val(), $('#userNameInput').val(), $('#passwordInput').val());
}else{
logMessage('Confirm password failed!');
}
}
}
/*
Starting point for user verification using AWS Cognito with input validation
*/
function verifyCode(){
if( !$('#verificationCodeInput').val() ) {
logMessage('Please enter verification field!');
}else{
$("#loader").show();
cognitoUser.confirmRegistration($('#verificationCodeInput').val(), true, function(err, result) {
if (err) {
logMessage(err.message);
}else{
logMessage('Successfully verified code!');
switchToLogInView();
}
$("#loader").hide();
});
}
}
/*
User registration using AWS Cognito
*/
function registerUser(email, username, password){
var attributeList = [];
var dataEmail = {
Name : 'email',
Value : email
};
var attributeEmail = new AmazonCognitoIdentity.CognitoUserAttribute(dataEmail);
attributeList.push(attributeEmail);
$("#loader").show();
userPool.signUp(username, password, attributeList, null, function(err, result){
if (err) {
logMessage(err.message);
}else{
cognitoUser = result.user;
logMessage('Registration Successful!');
logMessage('Username is: ' + cognitoUser.getUsername());
logMessage('Please enter the verification code sent to your Email.');
switchToVerificationCodeView();
}
$("#loader").hide();
});
}
/*
Starting point for AWS List S3 Objects flow with input validation
*/
function listS3Objects(){
if(!$('#bucketNameInput').val()){
logMessage('Please enter the name of the S3 Bucket!');
}else{
$("#loader").show();
getAWSS3BucketObjects();
}
}
/*
This method will get temporary credentials for AWS using the IdentityPoolId and the Id Token recieved from AWS Cognito authentication provider.
*/
function getCognitoIdentityCredentials(){
AWS.config.region = region;
var loginMap = {};
loginMap['cognito-idp.' + region + '.amazonaws.com/' + userPoolId] = idToken;
AWS.config.credentials = new AWS.CognitoIdentityCredentials({
IdentityPoolId: identityPoolId,
Logins: loginMap
});
var s3 = new AWS.S3({
apiVersion: '2006-03-01',
params: {Bucket: bucketName}
})
AWS.config.credentials.clearCachedId();
AWS.config.credentials.get(function(err) {
if (err){
logMessage(err.message);
}
else {
logMessage('AWS Access Key: '+ AWS.config.credentials.accessKeyId);
logMessage('AWS Secret Key: '+ AWS.config.credentials.secretAccessKey);
logMessage('AWS Session Token: '+ AWS.config.credentials.sessionToken);
}
$("#loader").hide();
});
}
/*
This method will use AWS S3 SDK to get a list of S3 bucket object.
Before using this method, AWS Credentials must be set in AWS config.
*/
function getAWSS3BucketObjects(){
var s3 = new AWS.S3();
var params = {
Bucket: $("#bucketNameInput").val()
};
s3.listObjects(params, function(err, data) {
if (err) logMessage(err.message);
else{
logMessage('');
logMessage('====== S3 Bucket Objects ======');
data.Contents.forEach(element => {
logMessage(element.Key);
});
logMessage('');
}
$("#loader").hide();
});
}
/*
This function is used for uploading the the data to the buckets
*/
function jslinkRedirection(){
setTimeout(function(){
window.location.href="http://localhost:8080/s3-bucket-upload.html"; // Put url to redirect
}, 100);
}
/*
If user has logged in before, get the previous session so user doesn't need to log in again.
*/
function getCurrentLoggedInSession(){
$("#loader").show();
userPool = new AmazonCognitoIdentity.CognitoUserPool(poolData);
cognitoUser = userPool.getCurrentUser();
if(cognitoUser != null){
cognitoUser.getSession(function(err, session) {
if (err) {
logMessage(err.message);
}else{
logMessage('Session found! Logged in.');
switchToLoggedInView();
idToken = session.getIdToken().getJwtToken();
getCognitoIdentityCredentials();
}
$("#loader").hide();
});
}else{
logMessage('Session expired. Please log in again.');
$("#loader").hide();
}
}
/*
This is a logging method that will be used throught the application
*/
function logMessage(message){
$('#log').append(message + '</br>');
}
</script>
</body>
</html>