Skip to content

Latest commit

 

History

History

readme.md

AWS Lambda Function with Keycloak JWT Validation

← Back to Node.js | JavaScript | Main README

Overview

This project provides an AWS Lambda function implemented in Node.js to validate JWT tokens issued by Keycloak. The function uses the JWKS endpoint provided by Keycloak to fetch public keys dynamically for verifying RS256-signed tokens.


Prerequisites

Keycloak Setup

  1. Ensure you have a Keycloak server running.
  2. Create a realm and a client in Keycloak. Use confidential client for server-to-server communication.
  3. Obtain:
    • Client ID
    • Keycloak JWKS endpoint:
      https://<keycloak-domain>/realms/<realm-name>/protocol/openid-connect/certs

Project Structure

  1. File: index.js:
    Contains the main AWS Lambda code for validating JWT tokens.

  2. Dependencies:
    Install locally before deploying:
    npm install jsonwebtoken jwks-rsa


Steps for Deployment

Step 1: Install Dependencies

Install the required libraries locally:
npm install jsonwebtoken jwks-rsa

Step 2: Package the Function

  1. Include index.js and the node_modules folder.
  2. Zip the files:
    zip -r lambda-keycloak.zip index.js node_modules

Step 3: Deploy to AWS Lambda

  1. Log in to the AWS Management Console.
  2. Create a new Lambda function or select an existing one.
  3. Upload the lambda-keycloak.zip file under "Function Code".

Testing the Lambda

Example Input

Pass the following event object when testing the Lambda function:

{
  "headers": {
    "Authorization": "Bearer <your-jwt-token>"
  }
}

Replace <your-jwt-token> with a valid JWT generated by Keycloak.


Expected Responses

Valid Token

{
  "statusCode": 200,
  "body": "{\"message\":\"Token is valid\", \"user\":{\"sub\":\"1234abcd\",\"name\":\"John Doe\",\"email\":\"johndoe@example.com\"}}"
}

Invalid Token

{
  "statusCode": 401,
  "body": "{\"message\":\"Invalid or expired token\",\"error\":\"jwt malformed\"}"
}

Missing Authorization Header

{
  "statusCode": 401,
  "body": "{\"message\":\"Missing Authorization header\"}"
}

Keycloak Token Generation for Testing

To generate a token from Keycloak:

  1. Log into Keycloak Admin Console.
  2. Navigate to your realm, then to your client's settings (client ID).
  3. Use Keycloak’s token endpoint:
    • URL: https://<keycloak-domain>/realms/<realm-name>/protocol/openid-connect/token
    • Method: POST
    • Parameters:
      • grant_type: client_credentials
      • client_id: <your-client-id>
      • client_secret: <your-client-secret>

Use the generated token for testing.


Notes and Best Practices

Secure Configuration

  • Avoid hardcoding sensitive values like client_id and issuer in the code. Use AWS Secrets Manager or Parameter Store to securely retrieve these values at runtime.

Validating Additional Claims

Extend token validation to verify roles, expiration (exp), and audience (aud) based on your requirements.

API Gateway Pairing

Integrate Lambda with AWS API Gateway to secure endpoints and validate incoming requests.


References


File Organization

  • index.js: Lambda function code
  • README.md: Documentation
  • node_modules/: Installed dependencies
  • lambda-keycloak.zip: Deployment package created via zip command

Author's Note

This Lambda function securely integrates with Keycloak for validating JWT tokens and supports serverless architectures running on AWS. Let me know if further configurations or advanced setups (like multi-realm support) are required!