AWS SAM Authorizer: получение нулевых значений для параметров заголовка - PullRequest
0 голосов
/ 22 апреля 2019

Я создал SAM template, который включает API Gateway, содержащий Lambda Authorizer. В Authorizer я настроил 3 параметра заголовка, а именно - authorization, xXsrfToken и cookie. Когда я вызываю этот API со всеми необходимыми значениями, в конце лямбды принимается только значение xXsrfToken, а значения cookie и authorization приходят null. Ниже я поделился своим шаблоном sam и снимками консоли AWS.

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
  my-api

  Sample SAM Template for my-api

# More info about Globals: https://github.com/awslabs/serverless-application-model/blob/master/docs/globals.rst
Globals:
  Function:
    Timeout: 20



Resources:
  TestAuthSAM:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: target/JWTValidateToken-1.0-SNAPSHOT.jar
      Handler: com.test.LambdaFunctionHandler::handleRequest
      Runtime: java8


  MyApi:
      Type: AWS::Serverless::Api
      Properties:
        StageName: v1
        DefinitionBody: 
            swagger: "2.0"
            info:
              version: "2019-04-22T08:30:48Z"
              title: "TestAuth"
            host: "d2xas9f38a.execute-api.us-east-2.amazonaws.com"
            basePath: "/v1"
            schemes:
            - "https"
            paths:
              /testauth:
                post:
                  produces:
                  - "application/json"
                  responses:
                    "200":
                      description: "200 response"
                      schema:
                        $ref: "#/definitions/Empty"
                  security:
                  - HelloWorld: []
                  x-amazon-apigateway-integration:
                    uri: "http://someapi.com/getdata"
                    responses:
                      default:
                        statusCode: "200"
                    passthroughBehavior: "when_no_match"
                    httpMethod: "GET"
                    type: "http"


            definitions:
              Empty:
                type: "object"
                title: "Empty Schema"




        Auth: 
            DefaultAuthorizer: MyLambdaRequestAuthorizer
            Authorizers:
                MyLambdaRequestAuthorizer:
                  FunctionPayloadType: REQUEST
                  FunctionArn: !GetAtt TestAuthSAM.Arn
                  Identity:
                    Headers:
                      - cookie
                      - xXsrfToken
                      - authorization

Outputs:

  TestAuthSAM:
    Description: "Custom Cookie Auth Lambda Function ARN"
    Value: !GetAtt TestAuthSAM.Arn
  TestAuthSAMIamRole:
    Description: "Implicit IAM Role created for Custom Cookie Auth function"
    Value: !GetAtt TestAuthSAM.Arn

С этим шаблоном мой Авторизатор настроен так на консоли Amazon: enter image description here

Когда я вызываю этот API через Почтальон со всеми необходимыми значениями, только xXsrfToken принимается. Cookie и Authorization идут как null. Я попытался реализовать то же самое, вручную настроив все, и в этом случае он работает как положено.

Мой класс запросов для Lambda Authorizer приведен ниже:

public class RequestAuthorizerContext {

private String type;
private String methodArn;
private RequestHeader headers;
private String sourceIp;
private Identity identity;

public RequestAuthorizerContext(){}

public RequestAuthorizerContext(String type, String methodArn, RequestHeader headers) {
    this.type = type;
    this.methodArn = methodArn;
    this.headers = headers;
}

public String getSourceIp() {
    return sourceIp;
}

public void setSourceIp(String sourceIp) {
    this.sourceIp = sourceIp;
}

public String getType() {
    return type;
}

public void setType(String type) {
    this.type = type;
}

public String getMethodArn() {
    return methodArn;
}

public void setMethodArn(String methodArn) {
    this.methodArn = methodArn;
}

public RequestHeader getHeaders() {
    return headers;
}

public void setHeaders(RequestHeader headers) {
    this.headers = headers;
}

public Identity getIdentity() {
    return identity;
}

public void setIdentity(Identity identity) {
    this.identity = identity;
}

public static class RequestHeader {
    @JsonProperty("X-XSRF-TOKEN")
    private String xXsrfToken;
    private String cookie;
    private String authorization;

    public RequestHeader(){}

    public RequestHeader(String xXsrfToken, String cookie, String authorization) {
        this.xXsrfToken = xXsrfToken;
        this.cookie = cookie;
        this.authorization = authorization;
    }

    public String getxXsrfToken() { return xXsrfToken; }

    public void setxXsrfToken(String xXsrfToken) { this.xXsrfToken = xXsrfToken; }

    public String getCookie() { return cookie; }

    public void setCookie(String cookie) { this.cookie = cookie; }

    public String getAuthorization() { return authorization; }

    public void setAuthorization(String authorization) { this.authorization = authorization; }

    @Override
    public String toString() {
        return ToStringBuilder.reflectionToString(this);
    }
}

public static class Identity {
    private String sourceIp;

    public String getSourceIp() {
        return sourceIp;
    }

    public void setSourceIp(String sourceIp) {
        this.sourceIp = sourceIp;
    }

    @Override
    public String toString() {
        return ToStringBuilder.reflectionToString(this);
    }
}

@Override
public String toString() {
    return ToStringBuilder.reflectionToString(this);
}

}

...