Skip to content

Commit ea067fe

Browse files
committed
Task 7: Add Authorization logic
1 parent 24d6dd5 commit ea067fe

27 files changed

Lines changed: 6796 additions & 6 deletions

File tree

README.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,3 +337,56 @@ https://github.com/rolling-scopes-school/aws/blob/main/aws-developer/06_async_mi
337337
# Additional Tasks
338338
- The `catalogBatchProcess` lambda is covered by unit tests.
339339
- Set a Filter Policy for the SNS `createProductTopic` in the AWS CDK Stack and created an additional email subscription to distribute messages to different emails depending on the filter for any product attribute.
340+
341+
# Task 7
342+
https://github.com/rolling-scopes-school/aws/blob/main/aws-developer/07_authorization/task.md
343+
344+
### Links
345+
- [Product Service API](https://dapdmi8g8h.execute-api.us-east-1.amazonaws.com/prod/products)
346+
- [Product Service API for id = 8f79f8e2-8841-4dc3-871e-2f7c861b130e](https://dapdmi8g8h.execute-api.us-east-1.amazonaws.com/prod/products/8f79f8e2-8841-4dc3-871e-2f7c861b130e)
347+
- [Frontend](https://d3oeh93tzbcw2m.cloudfront.net/)
348+
349+
### Settings:
350+
[Token Generator](https://www.debugbear.com/basic-auth-header-generator)
351+
userName = 'Tati-Moon'
352+
password =
353+
> add to console localStorage.setItem('authorization_token', "SOME_VALID_TOKEN");
354+
or
355+
> add to console localStorage.setItem('authorization_token', "SOME_INVALID_TOKEN");
356+
357+
### CVS:
358+
title,description,price,count
359+
some,some,666,666
360+
or
361+
title,description,price,count,photo
362+
Product x, Product x,111,9,https://d1yjjnpx0p53s8.cloudfront.net/styles/logo-thumbnail/s3/0025/1559/brand.gif?itok=vXujPldk
363+
364+
# Evaluation Criteria
365+
- [x] 100/100
366+
367+
- [x] ### Task 7.1
368+
1. Created a new service called `authorization-service`
369+
2. Created a lambda function called `basicAuthorizer` under the Authorization Service
370+
3. Lambda have environment variables: `GITHUB_LOGIN` and `GITHUB_PASSWORD`
371+
372+
![image](https://github.com/user-attachments/assets/fc8c9017-f4b2-45c8-b41e-ed771ed2c3a1)
373+
374+
- [x] ### Task 7.2
375+
1. Added Lambda authorization to the /import path of the Import Service API Gateway.
376+
2. Used basicAuthorizer lambda as the Lambda authorizer
377+
378+
- [x] ### Task 7.3
379+
1. The request from the client application to the /import path of the Import Service was required to have a Basic Authorization header:
380+
Authorization: Basic {authorization_token}
381+
382+
2. The {authorization_token} was a base64-encoded {your_github_account_login}
383+
example: Authorization: Basic sGLzdRxvZmw0ZXs0UGFzcw==
384+
385+
3. The client obtained the authorization_token value from the browser's localStorage:
386+
const authorization_token = localStorage.getItem('authorization_token')
387+
388+
![image](https://github.com/user-attachments/assets/cea5c371-9655-437f-bbc5-1c18b9b13361)
389+
390+
- [x] ### Additional
391+
392+
The client application displayed alerts for responses with 401 and 403 HTTP statuses. This behavior was added to the nodejs-aws-fe-main/src/index.tsx file.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Logs
2+
logs
3+
*.log
4+
npm-debug.log*
5+
yarn-debug.log*
6+
yarn-error.log*
7+
pnpm-debug.log*
8+
lerna-debug.log*
9+
10+
node_modules
11+
.serverless
12+
coverage
13+
dist
14+
dist-ssr
15+
*.local
16+
17+
# Editor directories and files
18+
.vscode/*
19+
!.vscode/extensions.json
20+
.idea
21+
.DS_Store
22+
*.suo
23+
*.ntvs*
24+
*.njsproj
25+
*.sln
26+
*.sw?
27+
/cdk_test/cdk.out.other
Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
# Import-Service
2+
3+
1. Install the latest version of AWS SDK
4+
5+
Run the following command to create a default package.json file:
6+
7+
```sh
8+
`npm init -y`
9+
```
10+
11+
Run the following command to install the Amazon S3 client package:
12+
13+
```sh
14+
`npm i @aws-sdk/client-s3`
15+
```
16+
17+
Add the following code to a file named index.js in the yours folder:
18+
19+
```js
20+
// This is used for getting user input.
21+
import { createInterface } from "readline/promises";
22+
23+
import {
24+
S3Client,
25+
PutObjectCommand,
26+
CreateBucketCommand,
27+
DeleteObjectCommand,
28+
DeleteBucketCommand,
29+
paginateListObjectsV2,
30+
GetObjectCommand,
31+
} from "@aws-sdk/client-s3";
32+
33+
export async function main() {
34+
// A region and credentials can be declared explicitly. For example
35+
// `new S3Client({ region: 'us-east-1', credentials: {...} })` would
36+
//initialize the client with those settings. However, the SDK will
37+
// use your local configuration and credentials if those properties
38+
// are not defined here.
39+
const s3Client = new S3Client({});
40+
41+
// Create an Amazon S3 bucket. The epoch timestamp is appended
42+
// to the name to make it unique.
43+
const bucketName = `test-bucket-${Date.now()}`;
44+
await s3Client.send(
45+
new CreateBucketCommand({
46+
Bucket: bucketName,
47+
})
48+
);
49+
50+
// Put an object into an Amazon S3 bucket.
51+
await s3Client.send(
52+
new PutObjectCommand({
53+
Bucket: bucketName,
54+
Key: "my-first-object.txt",
55+
Body: "Hello JavaScript SDK!",
56+
})
57+
);
58+
59+
// Read the object.
60+
const { Body } = await s3Client.send(
61+
new GetObjectCommand({
62+
Bucket: bucketName,
63+
Key: "my-first-object.txt",
64+
})
65+
);
66+
67+
console.log(await Body.transformToString());
68+
69+
// Confirm resource deletion.
70+
const prompt = createInterface({
71+
input: process.stdin,
72+
output: process.stdout,
73+
});
74+
75+
const result = await prompt.question("Empty and delete bucket? (y/n) ");
76+
prompt.close();
77+
78+
if (result === "y") {
79+
// Create an async iterator over lists of objects in a bucket.
80+
const paginator = paginateListObjectsV2(
81+
{ client: s3Client },
82+
{ Bucket: bucketName }
83+
);
84+
for await (const page of paginator) {
85+
const objects = page.Contents;
86+
if (objects) {
87+
// For every object in each page, delete it.
88+
for (const object of objects) {
89+
await s3Client.send(
90+
new DeleteObjectCommand({ Bucket: bucketName, Key: object.Key })
91+
);
92+
}
93+
}
94+
}
95+
96+
// Once all the objects are gone, the bucket can be deleted.
97+
await s3Client.send(new DeleteBucketCommand({ Bucket: bucketName }));
98+
}
99+
}
100+
101+
// Call a function if this file was run directly. This allows the file
102+
// to be runnable without running on import.
103+
import { fileURLToPath } from "url";
104+
if (process.argv[1] === fileURLToPath(import.meta.url)) {
105+
main();
106+
}
107+
```
108+
109+
2. Install the CSV parser package:
110+
111+
```sh
112+
`npm i csv-parser`
113+
```
114+
115+
# Task 7
116+
https://github.com/rolling-scopes-school/aws/blob/main/aws-developer/07_authorization/task.md
117+
118+
### Links
119+
- [Product Service API](https://dapdmi8g8h.execute-api.us-east-1.amazonaws.com/prod/products)
120+
- [Product Service API for id = 8f79f8e2-8841-4dc3-871e-2f7c861b130e](https://dapdmi8g8h.execute-api.us-east-1.amazonaws.com/prod/products/8f79f8e2-8841-4dc3-871e-2f7c861b130e)
121+
- [Frontend](https://d3oeh93tzbcw2m.cloudfront.net/)
122+
123+
### Settings:
124+
[Token Generator](https://www.debugbear.com/basic-auth-header-generator)
125+
userName = 'Tati-Moon'
126+
password =
127+
> add to console localStorage.setItem('authorization_token', "SOME_VALID_TOKEN");
128+
or
129+
> add to console localStorage.setItem('authorization_token', "SOME_INVALID_TOKEN");
130+
131+
### CVS:
132+
title,description,price,count
133+
some,some,666,666
134+
or
135+
title,description,price,count,photo
136+
Product x, Product x,111,9,https://d1yjjnpx0p53s8.cloudfront.net/styles/logo-thumbnail/s3/0025/1559/brand.gif?itok=vXujPldk
137+
138+
# Evaluation Criteria
139+
- [x] 100/100
140+
141+
- [x] ### Task 7.1
142+
1. Created a new service called `authorization-service`
143+
2. Created a lambda function called `basicAuthorizer` under the Authorization Service
144+
3. Lambda have environment variables: `GITHUB_LOGIN` and `GITHUB_PASSWORD`
145+
146+
![image](https://github.com/user-attachments/assets/fc8c9017-f4b2-45c8-b41e-ed771ed2c3a1)
147+
148+
- [x] ### Task 7.2
149+
1. Added Lambda authorization to the /import path of the Import Service API Gateway.
150+
2. Used basicAuthorizer lambda as the Lambda authorizer
151+
152+
- [x] ### Task 7.3
153+
1. The request from the client application to the /import path of the Import Service was required to have a Basic Authorization header:
154+
Authorization: Basic {authorization_token}
155+
156+
2. The {authorization_token} was a base64-encoded {your_github_account_login}
157+
example: Authorization: Basic sGLzdRxvZmw0ZXs0UGFzcw==
158+
159+
3. The client obtained the authorization_token value from the browser's localStorage:
160+
const authorization_token = localStorage.getItem('authorization_token')
161+
162+
![image](https://github.com/user-attachments/assets/cea5c371-9655-437f-bbc5-1c18b9b13361)
163+
164+
- [x] ### Additional
165+
166+
The client application displayed alerts for responses with 401 and 403 HTTP statuses. This behavior was added to the nodejs-aws-fe-main/src/index.tsx file.

0 commit comments

Comments
 (0)