#amazon-web-services #api #aws-api-gateway #aws-sam
#amazon-веб-сервисы #API #aws-api-шлюз #aws-sam
Вопрос:
Я пытаюсь развернуть API с помощью AWS SAM в API Gateway, мне нужен авторизатор Cognito с потоком OAuth учетных данных клиента.
До сих пор у меня есть развертывание, которое работает…
Мой Стек Cognito:
Resources: CognitoUserPool: Type: AWS::Cognito::UserPool Properties: UserPoolName: !Sub ${pPipelineCode}-${pGITBranch} UserPoolTags: application: !Ref pPipelineCode UserPoolClient: Type: AWS::Cognito::UserPoolClient Properties: AllowedOAuthFlows: - client_credentials GenerateSecret: "true" ClientName: !Sub ${pPipelineCode}-${pGITBranch}-client RefreshTokenValidity: !Ref InputRefreshTokenValidity SupportedIdentityProviders: - COGNITO AllowedOAuthScopes: - !Sub ${UserPoolResourceServer}/read - !Sub ${UserPoolResourceServer}/write UserPoolId: !Ref CognitoUserPool UserPoolResourceServer: Type: AWS::Cognito::UserPoolResourceServer Properties: UserPoolId: !Ref CognitoUserPool Identifier: !Sub ${pPipelineCode}-${pGITBranch}-server Name: !Sub ${pPipelineCode}-${pGITBranch}-server Scopes: - ScopeName: read ScopeDescription: read operation - ScopeName: write ScopeDescription: write operation UserPoolDomainResource: Type: AWS::Cognito::UserPoolDomain Properties: Domain: !Sub ${pPipelineCode}-${pGITBranch} UserPoolId: !Ref CognitoUserPool
Мой стек API:
ServerlessAPILogGroup: Type: AWS::Logs::LogGroup Properties: RetentionInDays: 30 CurrentTimeFunction: Type: AWS::Serverless::Function Properties: CodeUri: i010/currenttime/ Handler: app.lambda_handler Runtime: python3.8 Events: currenttime: Type: Api Properties: Path: /i010/currenttime Method: GET RestApiId: !Ref ServerlessAPI ServerlessAPI: Type: AWS::Serverless::Api Properties: StageName: !Ref pStageName AccessLogSetting: DestinationArn: !GetAtt ServerlessAPILogGroup.Arn Format: gt;- $context.requestId, $context.identity.sourceIp, $context.identity.caller, $context.identity.user, $context.requestTime, $context.httpMethod, $context.resourcePath, $context.status, $context.protocol, $context.responseLength TracingEnabled: true Auth: Authorizers: CognitoAuth: AuthType: "COGNITO_USER_POOLS" AuthorizationScopes: - i010-develop-server/read - write UserPoolArn: Fn::ImportValue: !Join [":", [!Ref pGITBranch, "CognitoUserPoolArn"]] # Identity: # Header: AuthHeader # ValidationExpression: Bearer.* # ReauthorizeEvery: 30 DefaultAuthorizer: CognitoAuth # InvokeRole: CALLER_CREDENTIALS # AddDefaultAuthorizerToCorsPreflight: false # ApiKeyRequired: false # ResourcePolicy: # CustomStatements: # - Effect: Allow # Principal: "*" # Action: execute-api:Invoke # Resource: # - execute-api:/v1/GET/* # - Effect: Deny # Principal: "*" # Action: execute-api:Invoke # Resource: # - execute-api:/v1/GET/* # Condition: # StringNotEquals: # aws:SourceIp: "lt;MYIPgt;"
Закомментированный код-это все, что у меня есть для включения/выключения, чтобы попробовать это проверить.
Однако после нескольких часов настройки я не могу получить никакого ответа, кроме:
{"error":"invalid_grant"}
С помощью:
curl -X POST --user lt;clientIdgt;:lt;secretgt; 'https://lt;URLgt;.auth.eu-west-1.amazoncognito.com/oauth2/token?grant_type=client_credentialsamp;scope=i010-develop-server/read' -H 'Content-Type: application/x-www-form-urlencoded'
Ответ №1:
Добавление:
AllowedOAuthFlowsUserPoolClient: верно
Для AWS::Cognito::UserPoolClient решил проблему