Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support different authentication types for a REST API #438

Open
kstro21 opened this issue Mar 10, 2019 · 14 comments
Open

Support different authentication types for a REST API #438

kstro21 opened this issue Mar 10, 2019 · 14 comments
Labels

Comments

@kstro21
Copy link

kstro21 commented Mar 10, 2019

So, I have used amplify api add and now have GraphQL and REST API in my project, but the auth for my REST API is using Cognito. How can I change that? I would like to use API Key authentication for my REST API and keep using Cognito for the rest. I can not find any reference in the doc and by running amplify auth add again I get the message:

Auth has already been added to this project. To update run amplify update auth.

If this is currently not supported going through some amplify-cli command or by editing some CloudFormation template, then it can be a candidate for a feature request.

@UnleashedMind UnleashedMind added question Further information is requested auth labels Mar 11, 2019
@kaustavghosh06
Copy link
Contributor

@kstro21 The API Gateway service doesn't have API Keys as one of their authentication methods I beleive. As a part of the CLI we support Cognito for auth/unauth access which you could probably use.

@kstro21
Copy link
Author

kstro21 commented Mar 12, 2019

@kaustavghosh06 thanks for the response. I'm currently using Cognito for auth/unauth and it works OK, but I'm trying to switch to API Keys and Usage Plans as described here https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-api-usage-plans.html

Here is an example that describes the process using de AWS API Gateway console https://medium.com/@zeebaig/protect-aws-api-gateway-endpoints-using-api-keys-5d8c066c5a5d

If it can be configured using the console, it can be configured using CloudFormation too, right?

@kaustavghosh06 kaustavghosh06 added api-rest pending-triage and removed pending-response question Further information is requested labels Mar 14, 2019
@kaustavghosh06 kaustavghosh06 added feature-request New feature or request and removed pending-triage labels Apr 12, 2019
@javieraldape
Copy link

+1 for this feature! It would be really nice to have different authentication methods for a REST API

@kaustavghosh06 kaustavghosh06 removed their assignment Aug 16, 2019
@iamtekeste
Copy link

This is an awesome project and I was wondering as to why the CLI doesn't support adding API keys as auth type for REST APIs?

@kaustavghosh06 kaustavghosh06 changed the title How to use a different authentication type for a REST API? Support different authentication types for a REST API? Oct 11, 2019
@kaustavghosh06 kaustavghosh06 changed the title Support different authentication types for a REST API? Support different authentication types for a REST API Oct 11, 2019
@hisham
Copy link

hisham commented Mar 18, 2020

+1

@egreenmachine
Copy link

egreenmachine commented Mar 29, 2020

So I have found that you cannot do this via the amplify-cli. However for anyone searching for a way to do this and not have amplify blow out your api-key on every deploy I have this solution.

You need to modify the file amplify/backend/api/<your_api_name>/<your_api_name>-cloudformation-template.json.

In the section labeled "x-amazon-apigateway-any-method" you need to add the following snippet. If you have multiple resources you will need to add the section to each defined "x-amazon-apigateway-any-method" section.

 "security": [
     {
          "api_key": []
     }
],

Note if you are doing other auth methods in addition to api-key you will need to add the api_key to the security section that is already there.

"security": [
     {
         "api_key": []
     },
    {
        "sigv4": []
    }
],

Additionally, you need to add the "api_key" to the "securityDefinitions" section.

"api_key": {
    "type": "apiKey",
    "name": "x-api-key",
    "in": "header"
}

Larger Example:

"x-amazon-apigateway-any-method": {
  "consumes": [
    "application/json"
  ],
  "produces": [
    "application/json"
  ],
  "parameters": [
    {
      "in": "body",
      "name": "RequestSchema",
      "required": false,
      "schema": {
        "$ref": "#/definitions/RequestSchema"
      }
    }
  ],
  "responses": {
    "200": {
      "description": "200 response",
      "schema": {
        "$ref": "#/definitions/ResponseSchema"
      }
    }
  },
  "security": [
    {
      "api_key": []
    }
  ],

  "x-amazon-apigateway-integration": {
    "responses": {
      "default": {
        "statusCode": "200"
      }
    },
    "uri": {
        "Fn::Join": [
          "",
          [
            "arn:aws:apigateway:",
            {
              "Ref": "AWS::Region"
            },
            ":lambda:path/2015-03-31/functions/",

            {

                "Ref": "functionnumberlookupArn"
            },

            "/invocations"
          ]
        ]
      },
    "passthroughBehavior": "when_no_match",
    "httpMethod": "POST",
    "type": "aws_proxy"
  }
}
"securityDefinitions": {
  "sigv4": {
    "type": "apiKey",
    "name": "Authorization",
    "in": "header",
    "x-amazon-apigateway-authtype": "awsSigv4"
  },
  "api_key": {
    "type": "apiKey",
    "name": "x-api-key",
    "in": "header"
  }
},

I hope this helps some people.

@rafaelsorto
Copy link

@egreenmachine thanks for sharing. ^ This works!

@iOrcohen
Copy link

This solution is not working in the latest amplify version.

Since now there is only cli-inputs.json file, and the cloud formation are generating during the amplify push we can't make custom changes in the cloud-formation file.

Is there any way we can do it in amplify version after 7 ?

@j-schuma
Copy link

yea, I am wondering about the same thing as @iOrcohen. :/

@oddbytes
Copy link

Same here. Can anyone point us in the right direction? Thanks!

@josefaidt josefaidt added the p3 label Mar 23, 2022
@ecc7220
Copy link

ecc7220 commented Mar 26, 2022

I need this also...

@L0y3r
Copy link

L0y3r commented Mar 28, 2022

I just open aws-amplify/amplify-cli#10085, where I'm trying to override security for methods, something similar to @egreenmachine suggests

@alharris-at alharris-at transferred this issue from aws-amplify/amplify-cli May 17, 2022
@dmost714
Copy link

I am also trying to get an API Key and Usage Plan working. And I'd like the API key to be required as the rest apis are not otherwise authenticated.

@PritamDutt
Copy link

PritamDutt commented Mar 27, 2023

I have been able to configure usage of API Key Auth on specific endpoints, though still exploring how to configure UsagePlans.

You can add overrides.ts by using amplify override api command


"// This file is used to override the REST API resources configuration
import {AmplifyApiRestResourceStackTemplate} from '@aws-amplify/cli-extensibility-helper';

export function override(resources: AmplifyApiRestResourceStackTemplate) {
// 	Put /myAPI endpoint behind API_KEY
	resources.restApi.body.securityDefinitions['api_key'] = {
		"type": "apiKey",
		"name": "x-api-key",
		"in": "header",
	}
	resources.restApi.body.paths['/myAPI']['x-amazon-apigateway-any-method']['security'] = [{'api_key': []}]
}


Hope this helps

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests