https://github.com/heiwa4126/sam-auth1
Lambda オーソライザーのサンプル
https://github.com/heiwa4126/sam-auth1
authorizer aws-lambda example
Last synced: 4 months ago
JSON representation
Lambda オーソライザーのサンプル
- Host: GitHub
- URL: https://github.com/heiwa4126/sam-auth1
- Owner: heiwa4126
- Created: 2021-12-27T08:40:40.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2021-12-28T08:46:41.000Z (about 4 years ago)
- Last Synced: 2025-01-04T16:18:59.417Z (about 1 year ago)
- Topics: authorizer, aws-lambda, example
- Language: JavaScript
- Homepage:
- Size: 34.2 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# sam-auth1
Lambda オーソライザーのサンプル。
- [sam-auth1](#sam-auth1)
- [デプロイ](#デプロイ)
- [テスト](#テスト)
- [HTTPヘッダの中身で認証(Tokenオーソライザー)](#httpヘッダの中身で認証tokenオーソライザー)
- [デフォルトオーソライザーの無効化](#デフォルトオーソライザーの無効化)
- [TokenオーソライザーでBASIC認証](#tokenオーソライザーでbasic認証)
- [REQUESTオーソライザー](#requestオーソライザー)
- [削除](#削除)
- [参考](#参考)
- [BASIC認証のカスタムエラーページ](#basic認証のカスタムエラーページ)
- [その他](#その他)
- [そのほか参考](#そのほか参考)
# デプロイ
SAMなので
```sh
sam build
sam deploy --guided # --guidedは最初の1回
```
# テスト
OutputのHelloApiのURLに対してcurlでアクセスする。
## HTTPヘッダの中身で認証(Tokenオーソライザー)
/hello/は Auth:ヘッダに `allow` で認証される。
```sh
curl -H "Auth: allow" https://xxxxxxxxxx.execute-api.ap-northeast-1.amazonaws.com/Prod/hello/
```
テストのためにToken Sourceをデフォルトの"Authorization"から"Auth"に変えてある。(スペルもめんどくさいし)
## デフォルトオーソライザーの無効化
/goodbye/は認証不要
```sh
curl https://xxxxxxxxxx.execute-api.ap-northeast-1.amazonaws.com/Prod/goodbye/
```
`Authorizer: NONE`
で
デフォルトオーソライザーを無効化するテスト。
## TokenオーソライザーでBASIC認証
/basic/はBASIC認証
```sh
curl -u admin:password https://xxxxxxxxxx.execute-api.ap-northeast-1.amazonaws.com/Prod/basic/
```
通常だと WWW-Authenticate: ヘッダが返せないのでブラウザで使えないが
Gateway Responsesをカスタマイズして、
`WWW-Authenticate: Basic realm=xxxxx`
を返すようにしたのでブラウザで試してみてください。
ただし副作用として/hello/まで
`WWW-Authenticate: Basic`
が帰ってるので、ごめんなさい。
## REQUESTオーソライザー
クエリauthにallowを渡す
```sh
curl https://xxxxxxxxxx.execute-api.ap-northeast-1.amazonaws.com/Prod/hi?auth=allow
# or
curl -G --data-urlencode "auth=allow" https://xxxxxxxxxx.execute-api.ap-northeast-1.amazonaws.com/Prod/hi
```
# 削除
```sh
sam delete
```
# 参考
* [LambdaTokenAuthorizer - AWS Serverless Application Model](https://docs.aws.amazon.com/ja_jp/serverless-application-model/latest/developerguide/sam-property-api-lambdatokenauthorizer.html)
* [LambdaTokenAuthorizationIdentity - AWS Serverless Application Model](https://docs.aws.amazon.com/ja_jp/serverless-application-model/latest/developerguide/sam-property-api-lambdatokenauthorizationidentity.html)
* [API Gateway Lambda オーソライザーを使用する - Amazon API Gateway](https://docs.aws.amazon.com/ja_jp/apigateway/latest/developerguide/apigateway-use-lambda-authorizer.html)
LambdaTokenAuthorizationIdentityの
Header: に対するドキュメントが無い
(けど動くし、設定できてコンソールからも見える)。
同様に
Token Validationに指定できるのは正規表現らしいけど、
ドキュメントが見つからない。
## BASIC認証のカスタムエラーページ
* [カスタマイズされたレスポンスの例 - AWS Serverless Application Model](https://docs.aws.amazon.com/ja_jp/serverless-application-model/latest/developerguide/serverless-controlling-access-to-apis-customize-response.html)
* [Using Basic Authentication with AWS API Gateway and Lambda - Cloudmailin](https://www.cloudmailin.com/blog/basic_auth_with_aws_lambda)
Token Validationを有効にすると、
Custom Gateway Responsesが効かなくなる...
# その他
Lambdaオーソライザーは昔は「カスタムオーソライザー」と呼ばれていたらしい。
authのlambdaは非同期ハンドラ(non-async handler)で書かないといけないものなの?
non-async handlerのcallbackは
```javascript
callback(response_error, response_success)
```
らしいのだけど、ドキュメントが見つからない。
auth関数でprincipalIdには何を設定するべき?
> principalId 値には、マッピングテンプレートで $context.authorizer.principalId 変数を使ってアクセスできます。これはバックエンドに値を渡す場合に便利です。
引用元: [Amazon API Gateway Lambda オーソライザーからの出力 - Amazon API Gateway](https://docs.aws.amazon.com/ja_jp/apigateway/latest/developerguide/
api-gateway-lambda-authorizer-output.html)
後段のlambdaで使えるよう渡すだけ。
同様に、policyのcontextで設定した値は
後段のlambdaで
event.requestContext.authorizer
から取れる。
## そのほか参考
* [dougalb/lambda-authorizer-basic-auth: A Serverless Application that creates Lambda function to use as an authorizer in Amazon API Gateway for HTTP Basic Auth and a DynamoDB tables for users.](https://github.com/dougalb/lambda-authorizer-basic-auth)