3

I setup an API Gateway in AWS that uses custom authorizers to implement an OAuth2 flow. It works fine. When the user is not authorized they get a 401 Authorized response. That is correct as well, but I would like to add a header that gives the client the endpoint where it can get the token. Something like AuthorizeUrl: url

How can I add this header to my response?

Alex Lungu
  • 1,104
  • 2
  • 12
  • 28

3 Answers3

1

Unfortunately this isn't possible but it's on our backlog. I know it doesn't really make sense when the client gets a 401 but you can't tell them how to authorize.

I don't have an ETA to provide but I'll add a +1 to the feature request.

jackko
  • 6,998
  • 26
  • 38
1

AWS added this functionality last year. Refer to this

To do it manually:

  1. Go to 'Response Headers' in API Gateway Console.
  2. Choose Unauthorized (401)
  3. Below 'Response Headers' add AuthorizeUrl and url
  4. Save and deploy API to some stage.

To add this to Cloudformation, refer to this similar answer.

You can also add this to swagger, by adding this snippet(yaml):

x-amazon-apigateway-gateway-responses:
  UNAUTHORIZED:
    statusCode: 401
    responseParameters:
      gatewayresponse.header.AuthorizeUrl:"url"
    responseTemplates:
      application/json: "{\"message\":$context.error.messageString}"
amsh
  • 3,097
  • 2
  • 12
  • 26
-1

In the API Gateway console go to the "Method Response" interface. You can add HTTP Status 401. In "Response Headers" add your custom "AuthorizeUrl" header. Then in the "Integration Response" interface you can add the value you'd like for that header.

Dave Maple
  • 8,102
  • 4
  • 45
  • 64
  • I tried that too. But in my case it doesn't work because "Proxy integrations cannot be configured to transform responses" so the Integration Respose is not editable – Alex Lungu Jan 05 '17 at 09:26