serverless / serverless/examples

How to pass expire token error in custom authoriser for API gateway response

Open
#446 8 comments 5 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
JavaScript
Stars
11.5k
Forks
4.4k
Avg merge
2h 55m
Merged PRs (30d)
3

Description

Hi Team,
We are using following configuration.
Lambda :-

exports.handler =  async (event, context, callback) => {
    try {
        callback(null,policyData);
        return;
    } catch (err) {
      // Always accepts "Unauthorized".
        callback("Unauthorized");
        return;
    }
};

SAM template :-

ExpiredGatewayResponse:
    Type: 'AWS::ApiGateway::GatewayResponse'
    Properties:
      ResponseParameters:
        gatewayresponse.header.Access-Control-Allow-Origin: "'*'"
        gatewayresponse.header.Access-Control-Allow-Headers: "'*'"
      ResponseType: EXPIRED_TOKEN
      ResponseTemplates:
        application/json: |
          {
            "success":false,
            "message":"Token Expired"
          }
      RestApiId: !Ref ApiGatewayApi
      StatusCode: '401'
  AuthFailureGatewayResponse:
    Type: 'AWS::ApiGateway::GatewayResponse'
    Properties:
      ResponseParameters:
        gatewayresponse.header.Access-Control-Allow-Credentials: "'true'"
        gatewayresponse.header.Access-Control-Allow-Origin: !Ref CorsOriginUrl
        gatewayresponse.header.Access-Control-Allow-Headers: "'Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token'"
      ResponseType: UNAUTHORIZED
      RestApiId: !Ref ApiGatewayApi
      StatusCode: '401'

In above code its always return unauthorized error.
If we pass anything else other then "Unauthorized" in call back then its return the error (message:null).

Please suggest how can we pass any other error response type like expired token form custom authorizer and In SAM it will take the configuration "ExpiredGatewayResponse".

Thanks,
Dilip

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the custom authorizer behavior from the JavaScript handler and SAM template, comparing the Unauthorized callback with other callback errors and the configured EXPIRED_TOKEN GatewayResponse. Verify whether API Gateway can select that response type from a custom authorizer, then document the supported behavior or update the relevant example if a change is identified.

Written by the indexing model from the issue text.

Assessment

Tech stack
aws, javascript
Domain
api, cloud
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.