Giter Site home page Giter Site logo

api-gateway-auth's Introduction

api-gateway-auth

Feature request | Detailed blog post

This sample application showcases how to set up and automate different types of authentication supported by Amazon API Gateway HTTP API via AWS SAM

This SAM app uses java as language runtime for the lambda functions and custom resources.

Setup

All authentications methods including mTLS:

The main SAM template-all-auth.yaml is used to set up HTTP API and different types of auth mentioned above. As a pre requisite step, in order to configure JWT authorizer, you will need to run template-cognito.yaml to setup Amazon Cognito as the JWT token provider. If you wish to have and HTTP API setup with only mTLS, follow section [Only mTLS with HTTP API setup](#Only mTLS with HTTP API setup). Lets begin.

Setup JWT Token provider

This will end up creating cognito user pool which we will use to set up our HTTP API with different auths. This is needed because we will use Amazon Cognito as the JWT token provider. You can skip this step if you are not going to configure JWT Authorizer for your HTTP API in template-all-auth.yaml

    api-gateway-auth$ sam build -t template-cognito.yaml
    api-gateway-auth$ sam deploy -t .aws-sam/build/template.yaml -g

    Deploying with following values
    ===============================
    Stack name                 : jwt-auth
    Region                     : eu-west-1
    Confirm changeset          : False
    Deployment s3 bucket       : aws-sam-cli-managed-default-samclisourcebucket-randomhash
    Capabilities               : ["CAPABILITY_IAM"]
    Parameter overrides        : {'AppName': 'jwt-auth', 'ClientDomains': 'http://localhost:8080', 'AdminEmail': '[email protected]', 'AddGroupsToScopes': 'true'}

Set up HTTP API

    api-gateway-auth$ sam build -t template-all-auth.yaml
    api-gateway-auth$ sam deploy -t .aws-sam/build/template.yaml

    Deploying with following values
    ===============================
    Stack name                 : http-api-authdemo
    Region                     : eu-west-1
    Confirm changeset          : False
    Deployment s3 bucket       : aws-sam-cli-managed-default-samclisourcebucket-randomhash
    Capabilities               : ["CAPABILITY_IAM"]
    Parameter overrides        : {'UserPoolId': 'from previous stack output', 'Audience': 'from previous stack output', 'HostedZoneId': 'Hosted zone id for custom domain', 'DomainName': 'domain name for the http api', 'TruststoreKey': 'truststore.pem'}

Only mTLS with HTTP API setup

Set up HTTP API

    api-gateway-auth$ sam build
    api-gateway-auth$ sam deploy -t

    Deploying with following values
    ===============================
    Stack name                 : http-api-authdemo
    Region                     : eu-west-1
    Confirm changeset          : False
    Deployment s3 bucket       : aws-sam-cli-managed-default-samclisourcebucket-randomhash
    Capabilities               : ["CAPABILITY_IAM"]
    Parameter overrides        : {'HostedZoneId': 'Hosted zone id for custom domain', 'DomainName': 'domain name for the http api', 'TruststoreKey': 'truststore.pem'}

Testing and validation

At this point, your stack should update successfully and you will have a HTTP API with Mutual TLS setup by default using AWS Certificate Manager Private Certificate Authority.

Stack will also generate one of the client certificates for you to validate the API and output its ARN as stack output

####Export certificate via Console:

  • Navigate to AWS Certificate Manager console. You will find a private certificate already generated with Name as ClientOneCert.

  • Select the cert and under action choose Export (Private certificates only). Enter passphrase on next screen which will be needed to decrypt the Certificate private key later.

  • Export certificate body to a file and Export certificate private key to a file

Export certificate via CLI:

aws acm export-certificate --certificate-arn <<Certificat ARN from stack output>> --passphrase $(echo -n 'your paraphrase' | base64) --no-cli-auto-prompt --region eu-west-1 | jq -r '"\(.Certificate)"' > client.pem
aws acm export-certificate --certificate-arn <<Certificat ARN from stack output>> --passphrase $(echo -n 'your paraphrase' | base64) --no-cli-auto-prompt --region eu-west-1 | jq -r '"\(.PrivateKey)"' > client.encrypted.key

Decrypt the private key

  • Decrypt private key downloaded using below command:
    openssl rsa -in <<Encrypted file>> -out client.decrypted.key

    Enter pass phrase for client.encrypted.txt:
    writing RSA key

Call the HTTP API to validate mTLS

  • Now you should be able to access the configured api with different paths and auth methods using mutual TLS.
    curl -v --cert client.pem  --key client.decrypted.key https://<<api-auth-demo.domain.com>>

Auth0 setup for REST and HTTP API

API gateway both REST and HTTP can be configured to work with Auth0. There is a sample template template-auth0.yaml which sets up sample REST and HTTP Api to work with Auth0.

Template expects two parameters:

  • IssuerUrl: The issuer of the token. Use https://YOUR_DOMAIN/. Be sure to include the trailing slash.
  • APIAudience: The identifier value of the API you created in the Auth0 API.

HTTP API will be set up using native JWT Authorizers while REST API will be set up using Token based Lambda Authorizers to integrate with Auth0.

Setup

    api-gateway-auth$ sam build -t template-auth0.yaml
    api-gateway-auth$ sam deploy -t .aws-sam/build/template.yaml

Credits

Setting up of JWT authorizer is inspired from example in sessions-with-aws-sam

Security

See CONTRIBUTING for more information.

License

This library is licensed under the MIT-0 License. See the LICENSE file.

api-gateway-auth's People

Contributors

amazon-auto avatar dependabot[bot] avatar pankajagrawal16 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

api-gateway-auth's Issues

Inconsistent use of GetAtt syntax

One place:

      CertificateChain: !GetAtt
        - PrivateCAActivation
        - CompleteCertificateChain

Another place:

      Certs:
        - !GetAtt MtlsCertificate.Certificate
        - !GetAtt PrivateCACertificate.Certificate

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.