微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在AWS无服务器功能上设置Oauth范围?

如何解决如何在AWS无服务器功能上设置Oauth范围?

问题:使用Cloudformation和AWS-SAM的“方法请求设置”中的OAuth范围为空

显然,我可以在一些地方声明我的授权范围,如果我做对了,我应该在授权者处声明所有范围,而我想在我的功能模板中为每个函数指定范围。

这是我要修复的模板:

MyServerlessFn
 Type: AWS::Serverless::Function
    Properties:
      FunctionName: helloWorldFn
      Description: my test using cognito.
      Handler: src/handlers/helloWorld.handler
      Runtime: nodejs12.x
      Events:
        ApplicationPostAPI:
          Type: Api
          Properties:
            Auth:
              AuthorizationScopes:
                - https://foobar.acme.net/full-api
            Method: POST
            Path: /hello/world
            RestApiId: !Ref MyServerlessApi

使用sam cli可以验证,构建和部署此模板,但是当我使用AWS Web控制台对其进行检查时,oauthScopes为空。

以下是AWS文档: https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-property-function-apifunctionauth.html

解决方法

我还注意到您在函数级别定义的范围保持为空。 无论文档是错误的还是当前的 AWS SAM 错误,我都留在中间。 (见:https://github.com/aws/serverless-application-model/issues/1752

但是你可以通过在 api 级别指定授权方并在函数级别引用这个授权方来解决这个问题。

  ApiGw:
    Type: AWS::Serverless::Api
    Properties:
      Description: some description
      Auth:
        Authorizers:
          DefaultAuthorizer:
            UserPoolArn: somepool
            AuthType: "COGNITO_USER_POOLS"
          AuthorizerWithScopeABC:
            UserPoolArn: somepool
            AuthType: "COGNITO_USER_POOLS"
            AuthorizationScopes:
              - /ABC
          AuthorizerWithScopeXYZ:
            UserPoolArn: somepool
            AuthType: "COGNITO_USER_POOLS"
            AuthorizationScopes:
              - /XYZ
        DefaultAuthorizer: DefaultAuthorizer

  Function:
    Type: AWS::Serverless::Function
    Properties:
      Events:
        ApiEvent:
          Type: Api
          Properties:
            Path: /somepath
            Method: get
            RestApiId: !Ref ApiGw
            Auth:
              Authorizer: AuthorizerWithScopeABC

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。