Skip to content

Commit cd3ce25

Browse files
committed
use Authorization header in AllowMe example
1 parent 10443e0 commit cd3ce25

File tree

8 files changed

+48
-28
lines changed

8 files changed

+48
-28
lines changed

README.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ cd myService && serverless deploy
3131

3232
## Tutorial: simple example to use S4
3333

34-
Let's use S4 to implement a file upload/download service with a trivial authorization mechanism. Every user that has the string "allowMeToUpload" in their name can upload. With "allowMeToDownload" they can download any previously uploaded file. It is the "allowMe" example in S4 repository.
34+
Let's use S4 to implement a file upload/download service with a trivial authorization mechanism. Every user that has the string "allowMeToUpload" in their token can upload. With "allowMeToDownload" they can download any previously uploaded file. It is the "allowMe" example in S4 repository.
3535

3636
### Quickly set up S4 and test "allowMe" example
3737

@@ -40,8 +40,8 @@ The allow me examples in the [examples](examples)
4040
### The getUploadUrlAuthorizer Lambda
4141

4242
This Api Gateway Lambda custom authorizer checks that the user is allowed to upload a file and invoke another Lambda to generate a signed upload URL.
43-
**What is necessary to implement?** The access control strategy in the `getUploadUrlAuthorizer` code. Currently it uses the queryStringParameters of the request but it could be any [Api Gateway Lambda custom authorizer](https://docs.aws.amazon.com/apigateway/latest/developerguide/apigateway-use-lambda-authorizer.html).
44-
**What does it do in the example?** The lambda is triggered by an API Gateway Get event with a query string parameter name that should containe "allowMeToUpload" to request and return the download url.
43+
**What is necessary to implement?** The access control strategy in the `getUploadUrlAuthorizer` code. Currently it uses the token in the Authorization header of the request but it could be any [Api Gateway Lambda custom authorizer](https://docs.aws.amazon.com/apigateway/latest/developerguide/apigateway-use-lambda-authorizer.html).
44+
**What does it do in the example?** The lambda is triggered by an API Gateway Get event with a token that should contain "allowMeToUpload" to request and return the download url.
4545

4646
[See getUploadUrlAuthorizer code](examples/allowMe/functions/getUploadUrlAuthorizer/handler.ts)
4747

@@ -56,8 +56,8 @@ This Lambda is triggered by the `FILE_UPLOADED` EventBridge event and receives t
5656
### The getDownloadUrlAuthorizer Lambda
5757

5858
This Api Gateway Lambda custom authorizer checks that the user is allowed to download the file, requested by its file prefix, and invoke another Lambda to generate a signed download URL.
59-
**What is necessary to implement?** The access control strategy in the `getDownloadUrlAuthorizer` code. Currently it uses the queryStringParameters of the request but it could be any [Api Gateway Lambda custom authorizer](https://docs.aws.amazon.com/apigateway/latest/developerguide/apigateway-use-lambda-authorizer.html).
60-
**What does it do in the example?** The lambda is triggered by an API Gateway Get event with a query string parameter name that should containe "allowMeToDownload" to request and return the download url.
59+
**What is necessary to implement?** The access control strategy in the `getDownloadUrlAuthorizer` code. Currently it uses the token in the Authorization header of the request but it could be any [Api Gateway Lambda custom authorizer](https://docs.aws.amazon.com/apigateway/latest/developerguide/apigateway-use-lambda-authorizer.html).
60+
**What does it do in the example?** The lambda is triggered by an API Gateway Get event with a token that should contain "allowMeToDownload" to request and return the download url.
6161

6262
[See getDownloadUrlAuthorizer code](examples/allowMe/functions/getDownloadUrlAuthorizer/handler.ts)
6363

@@ -75,7 +75,7 @@ Let's deploy S4, upload and download a pdf
7575
**Request:**
7676

7777
```bash
78-
curl 'https://{API_GATEWAY_ID}.execute-api.{REGION}.amazonaws.com/{STAGE}/api/signed-upload-url?name=allowMeToUpload&fileType=application/pdf'
78+
curl 'https://{API_GATEWAY_ID}.execute-api.{REGION}.amazonaws.com/{STAGE}/api/signed-upload-url?fileType=application/pdf' --header 'Authorization: Bearer allowMeToUpload'
7979
```
8080

8181
**Response:**
@@ -119,7 +119,7 @@ Let's deploy S4, upload and download a pdf
119119
**Request:**
120120
121121
```bash
122-
curl "https://{API_GATEWAY_ID}.execute-api.{REGION}.amazonaws.com/{STAGE}/api/download-url?filePrefix={FILE_PREFIX}&filename={FILENAME}&name=allowMeToDownload"
122+
curl "https://{API_GATEWAY_ID}.execute-api.{REGION}.amazonaws.com/{STAGE}/api/download-url?filePrefix={FILE_PREFIX}&filename={FILENAME}" --header "Authorization: Bearer allowMeToDownload"
123123
```
124124
125125
**Response:**
@@ -146,7 +146,7 @@ This Lambda queries uploaded files metadata to display a list of files available
146146
147147
- **A S3 bucket:** a S3 bucket to _store the files_ of end users.
148148
- **A Files metadata table:** a Dynamodb table to store _uploaded files metadat_. These metadata is used to to retrieve the files after their upload
149-
- **A getSignedUploadUrl http endpoint:** an endpoint on the route `/api/get-signed-upload-url?fileType=FILTE_TYPE&name=NAME` that verifies that the user is allowed to upload files,using the name query string parameter, and returns a presigned POST url to upload a file directly to the S3 bucket.
149+
- **A getSignedUploadUrl http endpoint:** an endpoint on the route `/api/get-signed-upload-url?fileType=FILTE_TYPE` that verifies that the user is allowed to upload files, using the token in the Authorization header, and returns a presigned POST url to upload a file directly to the S3 bucket.
150150
151151
- **A dispatchFileUploadedEvent handler and an event bridge:** a handler that dispatches a `FILE_UPLOADED` event in an event bridge. This event may be used to trigger any lambda. The payload of the event contains:
152152
@@ -162,7 +162,7 @@ This Lambda queries uploaded files metadata to display a list of files available
162162
}
163163
```
164164
165-
- **A getSignedDownloadUrl http endpoint:** an endpoint on the route `/api/get-signed-download-url?filePrefix=FILTE_PREFIX&fileName=FILE_NAME&name=NAME` that verifies that the user is allowed to download files, using the name query string parameter, and returns a presigned POST url to download a file directly to the S3 bucket.
165+
- **A getSignedDownloadUrl http endpoint:** an endpoint on the route `/api/get-signed-download-url?filePrefix=FILTE_PREFIX&fileName=FILE_NAME` that verifies that the user is allowed to download files, using the token in the Authorization header, and returns a presigned POST url to download a file directly to the S3 bucket.
166166
167167
### Flowchart
168168

examples/allowMe/S4-allowMe.postman_collection.json

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,20 @@
88
{
99
"name": "getSignedUploadUrl",
1010
"request": {
11+
"auth": {
12+
"type": "bearer",
13+
"bearer": [
14+
{
15+
"key": "token",
16+
"value": "allowMeToUpload",
17+
"type": "string"
18+
}
19+
]
20+
},
1121
"method": "GET",
1222
"header": [],
1323
"url": {
14-
"raw": "https://{{API_GATEWAY_ID}}.execute-api.{{REGION}}.amazonaws.com/dev/api/signed-upload-url?fileType=application/pdf&name=allowMeToUpload",
24+
"raw": "https://{{API_GATEWAY_ID}}.execute-api.{{REGION}}.amazonaws.com/dev/api/signed-upload-url?fileType=application/pdf",
1525
"protocol": "https",
1626
"host": [
1727
"{{API_GATEWAY_ID}}",
@@ -32,7 +42,8 @@
3242
},
3343
{
3444
"key": "name",
35-
"value": "allowMeToUpload"
45+
"value": "allowMeToUpload",
46+
"disabled": true
3647
}
3748
]
3849
}
@@ -42,10 +53,20 @@
4253
{
4354
"name": "getSignedDownloadUrl",
4455
"request": {
56+
"auth": {
57+
"type": "bearer",
58+
"bearer": [
59+
{
60+
"key": "token",
61+
"value": "allowMeToDownload",
62+
"type": "string"
63+
}
64+
]
65+
},
4566
"method": "GET",
4667
"header": [],
4768
"url": {
48-
"raw": "https://{{API_GATEWAY_ID}}.execute-api.{{REGION}}.amazonaws.com/dev/api/signed-download-url?name=allowMeToDownload&filePrefix={{FILE_PREFIX}}&fileName={{FILE_NAME}}",
69+
"raw": "https://{{API_GATEWAY_ID}}.execute-api.{{REGION}}.amazonaws.com/dev/api/signed-download-url?filePrefix={{FILE_PREFIX}}&fileName={{FILE_NAME}}",
4970
"protocol": "https",
5071
"host": [
5172
"{{API_GATEWAY_ID}}",
@@ -62,7 +83,8 @@
6283
"query": [
6384
{
6485
"key": "name",
65-
"value": "allowMeToDownload"
86+
"value": "allowMeToDownload",
87+
"disabled": true
6688
},
6789
{
6890
"key": "filePrefix",
@@ -166,15 +188,15 @@
166188
},
167189
{
168190
"key": "REGION",
169-
"value": ""
191+
"value": "eu-west-1"
170192
},
171193
{
172194
"key": "FILE_PREFIX",
173-
"value": ""
195+
"value": "ced50ff1-26eb-468a-92a8-373ad0535fbf"
174196
},
175197
{
176198
"key": "FILE_NAME",
177-
"value": "test.pdf"
199+
"value": "file.pdf"
178200
},
179201
{
180202
"key": "BUCKET_NAME",

examples/allowMe/functions/getDownloadUrlAuthorizer/handler.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ import { APIGatewayRequestAuthorizerHandler } from "aws-lambda";
33
import { generateInvokePolicyDocument } from "../../../../utils/authorizerUtils";
44

55
export const main: APIGatewayRequestAuthorizerHandler = async (event) => {
6-
const { name } = event.queryStringParameters;
6+
const { Authorization: token } = event.headers;
77

88
// Naive authentification strategy
9-
// all requests with a queryStringParameter "name" containing the "allowMeToDownload" string are accepted
10-
if (!name.includes("allowMeToDownload")) {
9+
// all requests with a "token" containing the "allowMeToDownload" string are accepted
10+
if (!token?.includes("allowMeToDownload")) {
1111
return generateInvokePolicyDocument(event.methodArn, "Deny");
1212
}
1313

examples/allowMe/functions/getUploadUrlAuthorizer/handler.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ import { APIGatewayRequestAuthorizerHandler } from "aws-lambda";
33
import { generateInvokePolicyDocument } from "../../../../utils/authorizerUtils";
44

55
export const main: APIGatewayRequestAuthorizerHandler = async (event) => {
6-
const { name } = event.queryStringParameters;
6+
const { Authorization: token } = event.headers;
77

88
// Naive authentification strategy
9-
// all requests with a queryStringParameter "name" containing the "allowMeToUpload" string are accepted
10-
if (!name || !name.includes("allowMeToUpload")) {
9+
// all requests with a "token" containing the "allowMeToUpload" string are accepted
10+
if (!token?.includes("allowMeToUpload")) {
1111
return generateInvokePolicyDocument(event.methodArn, "Deny");
1212
}
1313

functions/getSignedDownloadUrl/config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export const getSignedDownloadUrl = {
1212
authorizer: {
1313
name: "getDownloadUrlAuthorizer",
1414
type: "request",
15-
identitySource: "method.request.querystring.name",
15+
identitySource: "method.request.header.Authorization",
1616
},
1717
},
1818
},

functions/getSignedDownloadUrl/schema.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,8 @@ export default {
66
properties: {
77
filePrefix: { type: "string" },
88
fileName: { type: "string" },
9-
name: { type: "string" },
109
},
11-
required: ["filePrefix", "fileName", "name"],
10+
required: ["filePrefix", "fileName"],
1211
},
1312
},
1413
required: ["queryStringParameters"],

functions/getSignedUploadUrl/config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export const getSignedUploadUrl = {
1212
authorizer: {
1313
name: "getUploadUrlAuthorizer",
1414
type: "request",
15-
identitySource: "method.request.querystring.name",
15+
identitySource: "method.request.header.Authorization",
1616
},
1717
},
1818
},

functions/getSignedUploadUrl/schema.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@ export default {
55
type: "object",
66
properties: {
77
fileType: { type: "string" },
8-
name: { type: "string" },
98
},
10-
required: ["fileType", "name"],
9+
required: ["fileType"],
1110
},
1211
},
1312
required: ["queryStringParameters"],

0 commit comments

Comments
 (0)