Files
query-orchestration/internal/cognitoauth/auth.go
T
Jay Brown 2ff6e05ffc Merged in feature/permit-integration-1 (pull request #172)
Permit integration - part 1

* WIP permit integration

* slim down build

* Merge branch 'main' of bitbucket.org:aarete/query-orchestration into feature/permit-integration-1

* omit swagger from auth

* clean build

* comment

* part 1 completed

this is the initial permitio parts and tests without integration. Also testing.md since we have a new test target `task test:permitio`

* feature flag for no jwt validation

* permit integration

* fix ci unit tests

* ci fix

* build fix

* fix home handler

* fix redirect

* test fix

* update docs for auth
2025-07-11 19:27:14 +00:00

151 lines
4.4 KiB
Go

package cognitoauth
import (
"context"
"fmt"
"net/http"
"os"
"queryorchestration/internal/serviceconfig/auth"
"strings"
"github.com/labstack/echo/v4"
"github.com/lestrrat-go/jwx/v2/jwt"
)
// RegisterRoutes registers all authentication-related routes to the Echo engine.
// It configures login and callback endpoints, and applies JWT authentication middleware.
// If the DISABLE_AUTH environment variable is set to "true", authentication is bypassed.
//
// Parameters:
// - e: The Echo instance to register routes on
// - config: Configuration provider for authentication settings
func RegisterRoutes(e *echo.Echo, config auth.ConfigProvider) {
// check the auth feature flag
fmt.Println("Registering routes for Auth.")
disableAuth := os.Getenv("DISABLE_AUTH")
if strings.ToLower(disableAuth) == "true" {
fmt.Println("Auth is disabled. Skipping auth routes.")
return // Do nothing if auth is not enabled
}
// Register the login route - the middleware will handle this
e.GET(config.GetAuthLoginPath(), func(c echo.Context) error {
// This is handled by the middleware
return nil
})
// Register callback route - the middleware will handle this
e.GET(config.GetAuthCallbackPath(), func(c echo.Context) error {
// This handler will only be called for requests that don't have a code parameter
// (those are handled directly in the middleware)
// Get user info from context
userClaims, _ := c.Get("user_claims").(map[string]interface{})
userGroups, _ := c.Get("user_groups").([]string)
return c.JSON(http.StatusOK, map[string]interface{}{
"authenticated": true,
"username": userClaims["cognito:username"],
"email": userClaims["email"],
"groups": userGroups,
})
})
// Apply the JWT Auth middleware first
e.Use(JWTAuthMiddleware(config))
// Then apply the token validation middleware
e.Use(TokenValidationMiddleware(config))
}
// GetTokenFromRequest extracts the JWT token from the HTTP request.
// It first attempts to retrieve the token from the Authorization header
// in Bearer token format. If not found, it then tries to retrieve the token
// from the "auth_token" cookie. Returns an empty string if no token is found.
//
// Parameters:
// - c: The Echo context containing the HTTP request
//
// Returns:
// - string: The extracted JWT token, or an empty string if not found
func GetTokenFromRequest(c echo.Context) string {
// First try from Authorization header
authHeader := c.Request().Header.Get("Authorization")
if authHeader != "" && strings.HasPrefix(authHeader, "Bearer ") {
return authHeader[7:]
}
// Then try from cookie
tokenCookie, err := c.Cookie("auth_token")
if err == nil && tokenCookie.Value != "" {
return tokenCookie.Value
}
return ""
}
// GetUserInfo retrieves user information from the Echo context.
// It first attempts to retrieve user info directly from the context.
// If not found there, it tries to extract and parse the JWT token,
// then extract user information from the token claims.
//
// Parameters:
// - c: The Echo context containing the HTTP request and context values
//
// Returns:
// - UserInfo: Structure containing username, email, and groups
// - bool: True if user information was successfully retrieved, false otherwise
func GetUserInfo(c echo.Context) (UserInfo, bool) {
// Try to get user info from context
userInfo, ok := c.Get("user_info").(UserInfo)
if ok {
return userInfo, true
}
// Try to get token and extract user info
token := GetTokenFromRequest(c)
if token == "" {
return UserInfo{}, false
}
// Parse token to extract claims
parsedToken, err := jwt.Parse([]byte(token), jwt.WithVerify(false))
if err != nil {
return UserInfo{}, false
}
claims, err := parsedToken.AsMap(context.Background())
if err != nil {
return UserInfo{}, false
}
// Extract user info
userInfo = ExtractUserInfo(claims)
return userInfo, true
}
// GetUserSubject extracts the subject ID from JWT claims for Permit.io
// The subject ('sub') claim uniquely identifies the user in Cognito
//
// Parameters:
// - c: The Echo context containing the HTTP request and context values
//
// Returns:
// - string: The user's subject ID from the JWT token
// - bool: True if subject was successfully extracted, false otherwise
func GetUserSubject(c echo.Context) (string, bool) {
userClaims, ok := c.Get("user_claims").(map[string]interface{})
if !ok {
return "", false
}
subject, ok := userClaims["sub"].(string)
if !ok {
return "", false
}
return subject, true
}