AWS API Gateway Lambda Authorizer Blueprints 教程

AWS API Gateway Lambda Authorizer Blueprints 教程

aws-apigateway-lambda-authorizer-blueprintsBlueprints and examples for Lambda-based custom Authorizers for use in API Gateway.项目地址:https://gitcode.com/gh_mirrors/aw/aws-apigateway-lambda-authorizer-blueprints

1. 项目的目录结构及介绍

aws-apigateway-lambda-authorizer-blueprints/
├── LICENSE
├── README.md
├── blueprints/
│   ├── basic-auth/
│   │   ├── index.js
│   │   └── package.json
│   ├── custom-auth/
│   │   ├── index.js
│   │   └── package.json
│   ├── token-auth/
│   │   ├── index.js
│   │   └── package.json
│   └── token-verifier/
│       ├── index.js
│       └── package.json
└── template.yaml
  • LICENSE: 项目许可证文件。
  • README.md: 项目说明文档。
  • blueprints/: 包含不同类型的 Lambda Authorizer 示例。
    • basic-auth/: 基本认证示例。
    • custom-auth/: 自定义认证示例。
    • token-auth/: 令牌认证示例。
    • token-verifier/: 令牌验证示例。
  • template.yaml: AWS SAM 模板文件,用于定义和部署 Lambda 函数。

2. 项目的启动文件介绍

每个 blueprints 目录下的子目录都包含一个 index.js 文件,这是 Lambda Authorizer 的入口文件。以下是 basic-auth 示例的 index.js 文件内容:

'use strict';

exports.handler = function(event, context, callback) {
    var token = event.authorizationToken;
    switch (token) {
        case 'allow':
            callback(null, generatePolicy('user', 'Allow', event.methodArn));
            break;
        case 'deny':
            callback(null, generatePolicy('user', 'Deny', event.methodArn));
            break;
        case 'unauthorized':
            callback('Unauthorized');   // Return a 401 Unauthorized response
            break;
        default:
            callback('Error: Invalid token'); // Return a 500 Invalid token response
    }
};

// Help function to generate an IAM policy
var generatePolicy = function(principalId, effect, resource) {
    var authResponse = {};
    
    authResponse.principalId = principalId;
    if (effect && resource) {
        var policyDocument = {};
        policyDocument.Version = '2012-10-17';
        policyDocument.Statement = [];
        var statementOne = {};
        statementOne.Action = 'execute-api:Invoke';
        statementOne.Effect = effect;
        statementOne.Resource = resource;
        policyDocument.Statement.push(statementOne);
        authResponse.policyDocument = policyDocument;
    }
    
    return authResponse;
};

3. 项目的配置文件介绍

template.yaml 文件是 AWS SAM 模板文件,用于定义和部署 Lambda 函数。以下是该文件的部分内容:

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Resources:
  BasicAuthFunction:
    Type: AWS::Serverless::Function
    Properties:
      Handler: index.handler
      Runtime: nodejs14.x
      CodeUri: blueprints/basic-auth/
      Description: Basic Auth Lambda Authorizer
      MemorySize: 128
      Timeout: 5
      Policies:
        - AWSLambdaBasicExecutionRole
  • AWSTemplateFormatVersion: 模板格式版本。
  • Transform: 指定使用 AWS SAM 转换。
  • Resources: 定义 AWS 资源,例如 Lambda 函数。
    • BasicAuthFunction: 定义一个名为 BasicAuthFunction 的 Lambda 函数。
      • Handler: 指定入口文件和处理函数。
      • Runtime: 指定运行时环境。
      • CodeUri: 指定代码目录。
      • Description: 函数描述。
      • MemorySize: 内存大小。
      • Timeout: 超时时间。
      • Policies: 附加的 IAM 策略。

aws-apigateway-lambda-authorizer-blueprintsBlueprints and examples for Lambda-based custom Authorizers for use in API Gateway.项目地址:https://gitcode.com/gh_mirrors/aw/aws-apigateway-lambda-authorizer-blueprints

  • 4
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

水珊习Gale

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值