Skip to content
This repository has been archived by the owner on Apr 18, 2023. It is now read-only.

ivarprudnikov/auth0-micronaut-template

Repository files navigation

Auth0 Micronaut template

CI GitHub issues GitHub last commit

Live

App is deployed to AWS Lambda:

JAR with StreamLambdaHandler Graal VM native binary with custom runtime
Branch master aws-lambda-graalvm
URL https://te60oj36jd.execute-api.eu-west-1.amazonaws.com/Prod/ https://995oz2jt04.execute-api.eu-west-1.amazonaws.com/Prod/

Client application using this API:

Website URL Github repository
https://ivarprudnikov.github.io/react-auth0-template/ https://github.com/ivarprudnikov/react-auth0-template

About

Example should serve as a template when necessary to write a small service which uses JWT tokens signed by Auth0 server.

Blog post explaining implementation - ivarprudnikov.com/micronaut-kotlin-jwt-secured-api-aws-lambda

JWT config (by using JWKS) is visible in application.yml. Controller endpoint then uses @Secured annotation.

API endpoints

  • / - returns status UP in plain text
  • /me - returns user io.micronaut.security.authentication.Authentication in JSON response

Development

Running locally

Start Micronaut server

./gradlew run

Then to check status

curl http://localhost:8080
UP%

Running with SAM

This requires Docker to be running locally and sam CLI installed. https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-cli-command-reference-sam-local-start-api.html

sam local start-api --template template.yaml --port 8080

....

Mounting ExampleFunction at http://127.0.0.1:8080/ [DELETE, GET, HEAD, OPTIONS, PATCH, POST, PUT]
Mounting ExampleFunction at http://127.0.0.1:8080/{proxy+} [DELETE, GET, HEAD, OPTIONS, PATCH, POST, PUT]
You can now browse to the above endpoints to invoke your functions. You do not need to restart/reload SAM CLI while working on your functions, changes will be reflected instantly/automatically. You only need to restart SAM CLI if you update your AWS SAM template
2020-03-02 22:16:20  * Running on http://127.0.0.1:8080/ (Press CTRL+C to quit)

Invoking function with events

Instead of running the application and then trying to invoke API in the browser you could just invoke Lambda function with pre-built events:

sam local invoke --template template.yaml --event aws-test-events/options-me.json --skip-pull-image

Packaging and deployment

Utilizing AWS Lambda to make sure I'm not paying for idle time.

Example micronaut implementations:

Steps

  1. Build jar

    ./gradlew clean build --info
  2. Create S3 bucket where application version is going to be uploaded before deployed to Cloudformation:

    aws s3 mb s3://BUCKET_NAME
  3. Package Lambda (uploads to S3):

    sam package \
        --output-template-file packaged.yaml \
        --s3-bucket BUCKET_NAME
  4. Create Cloudformation Stack and deploy your SAM resources.

    sam deploy \
        --template-file packaged.yaml \
        --stack-name STACK_NAME \
        --capabilities CAPABILITY_IAM
  5. After deployment is complete you can run the following command to retrieve the API Gateway Endpoint URL:

    aws cloudformation describe-stacks \
        --stack-name STACK_NAME \
        --query 'Stacks[].Outputs[?OutputKey==`ApiUrl`]' \
        --output table

Simpler way

Below sam validate requires AWS Credentials to be set up.

sam validate && LAMBDA_APP_NAME=auth0-micronaut-template bash build-deploy.sh

Cloudformation cleanup

In order to delete our Serverless Application recently deployed you can use the following AWS CLI Command:

aws cloudformation delete-stack --stack-name STACK_NAME