Files
query-orchestration/internal/cognitoauth/summary.md
T
2025-04-08 16:27:06 -07:00

1.7 KiB

Summary of the Cognito Auth Package

This package handles the complete PKCE flow with AWS Cognito and integrates cleanly with the Echo web framework.

Key Components:

Config Structure: Centralizes all configuration and can be initialized from environment variables.

Middleware:

JWTAuthMiddleware: Extracts tokens from cookies and adds them to Authorization headers

TokenValidationMiddleware: Handles token validation and authorization

Route Handlers:

Login initiation OAuth callback processing Logout functionality Home page with authentication status

Helper Functions:

Token verification User group extraction Permission checking

How to Use It:

Initialize the Config:

goconfig := cognitoauth.NewConfigFromEnv("http://localhost:8080", logger)

Set Route Permissions:
goroutePermissions := map[string][]string{
"/users": {"exporters", "uploaders"},
"/orders": {"exporters"},
}
config.SetRoutePermissions(routePermissions)

Register Routes and Middleware:

gocognitoauth.RegisterRoutes(e, config)

Define Your Protected Routes:

e.GET("/users", handleUsers)
e.GET("/orders", handleOrders)

Authentication Flow:

User navigates to /login User is redirected to Cognito login page After successful login, Cognito redirects to /login-callback The callback handler exchanges the authorization code for tokens Tokens are stored in cookies for subsequent requests Protected routes check token validity and user permissions

Customization:

The package is designed to be customizable:

Configure paths for login, callback, logout, and home Define your own route permissions Use RequireGroups middleware for fine-grained control Access user information in your handlers via GetUserInfo