sync changes
working state
This commit is contained in:
@@ -58,6 +58,7 @@ func initiateLoginWithPKCE(c echo.Context, config *CognitoConfig, logger *slog.L
|
|||||||
logger.Error("Failed to generate state parameter", "error", err)
|
logger.Error("Failed to generate state parameter", "error", err)
|
||||||
return c.JSON(http.StatusInternalServerError, map[string]string{"error": "Authentication initialization failed"})
|
return c.JSON(http.StatusInternalServerError, map[string]string{"error": "Authentication initialization failed"})
|
||||||
}
|
}
|
||||||
|
fmt.Println("random state:", state)
|
||||||
|
|
||||||
// Generate code verifier (random string between 43-128 chars)
|
// Generate code verifier (random string between 43-128 chars)
|
||||||
codeVerifier, err := generateRandomString(64)
|
codeVerifier, err := generateRandomString(64)
|
||||||
|
|||||||
@@ -86,10 +86,10 @@ func GetJWKS(jwksURL string, logger *slog.Logger) (jwk.Set, error) {
|
|||||||
return keySet, nil
|
return keySet, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// TokenValidationMiddleware verifies JWT tokens efficiently
|
// PkceTokenValidationMiddleware verifies JWT tokens efficiently
|
||||||
// This is the primary middleware that handles both authentication and authorization
|
// This is the primary middleware that handles both authentication and authorization
|
||||||
// Applied to all routes to enforce security requirements
|
// Applied to all routes to enforce security requirements
|
||||||
func TokenValidationMiddleware(config *CognitoConfig, routePermissions map[string][]string, logger *slog.Logger) echo.MiddlewareFunc {
|
func PkceTokenValidationMiddleware(config *CognitoConfig, routePermissions map[string][]string, logger *slog.Logger) echo.MiddlewareFunc {
|
||||||
return func(next echo.HandlerFunc) echo.HandlerFunc {
|
return func(next echo.HandlerFunc) echo.HandlerFunc {
|
||||||
return func(c echo.Context) error {
|
return func(c echo.Context) error {
|
||||||
requestPath := c.Request().URL.Path
|
requestPath := c.Request().URL.Path
|
||||||
@@ -110,7 +110,7 @@ func TokenValidationMiddleware(config *CognitoConfig, routePermissions map[strin
|
|||||||
return handleOAuthCallback(c, config, routePermissions, logger)
|
return handleOAuthCallback(c, config, routePermissions, logger)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Initialize login flow if this is a login request
|
// Initialize login flow if this is a login request - first thing after we arrive at /login
|
||||||
if requestPath == "/login" {
|
if requestPath == "/login" {
|
||||||
return initiateLoginWithPKCE(c, config, logger)
|
return initiateLoginWithPKCE(c, config, logger)
|
||||||
}
|
}
|
||||||
@@ -409,6 +409,12 @@ func maskString(s string) string {
|
|||||||
return s[:4] + "..." + s[len(s)-4:]
|
return s[:4] + "..." + s[len(s)-4:]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func getJwksURL(region, userPoolID string) string {
|
||||||
|
// original logic
|
||||||
|
// : fmt.Sprintf("https://cognito-idp.%s.amazonaws.com/%s/.well-known/jwks.json", region, userPoolID),
|
||||||
|
return fmt.Sprintf("https://cognito-idp.%s.amazonaws.com/%s/.well-known/jwks.json", region, userPoolID)
|
||||||
|
}
|
||||||
|
|
||||||
// main is the entry point of the application
|
// main is the entry point of the application
|
||||||
// Sets up the Echo server, middleware, routes, and starts listening
|
// Sets up the Echo server, middleware, routes, and starts listening
|
||||||
func main() {
|
func main() {
|
||||||
@@ -451,6 +457,8 @@ func main() {
|
|||||||
domain = fmt.Sprintf("%s.auth.%s.amazoncognito.com", userPoolID, region)
|
domain = fmt.Sprintf("%s.auth.%s.amazoncognito.com", userPoolID, region)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fmt.Printf("Using domain %s\n", domain)
|
||||||
|
|
||||||
// Ensure domain format is correct (no protocol prefix)
|
// Ensure domain format is correct (no protocol prefix)
|
||||||
domain = strings.TrimPrefix(domain, "https://")
|
domain = strings.TrimPrefix(domain, "https://")
|
||||||
domain = strings.TrimPrefix(domain, "http://")
|
domain = strings.TrimPrefix(domain, "http://")
|
||||||
@@ -461,8 +469,9 @@ func main() {
|
|||||||
RedirectURI: "http://localhost:8080" + LOGIN_CALLBACK_PATH,
|
RedirectURI: "http://localhost:8080" + LOGIN_CALLBACK_PATH,
|
||||||
TokenURL: fmt.Sprintf("https://%s/oauth2/token", domain),
|
TokenURL: fmt.Sprintf("https://%s/oauth2/token", domain),
|
||||||
AuthURL: fmt.Sprintf("https://%s/oauth2/authorize", domain),
|
AuthURL: fmt.Sprintf("https://%s/oauth2/authorize", domain),
|
||||||
// actual https://cognito-idp.us-east-2.amazonaws.com/us-east-2_1y6po8rR8/.well-known/jwks.json
|
|
||||||
JwksURL: fmt.Sprintf("https://cognito-idp.%s.amazonaws.com/%s/.well-known/jwks.json", region, userPoolID),
|
// actual : https://cognito-idp.us-east-2.amazonaws.com/us-east-2_1y6po8rR8/.well-known/jwks.json
|
||||||
|
JwksURL: getJwksURL(region, userPoolID),
|
||||||
UserPoolID: userPoolID,
|
UserPoolID: userPoolID,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -486,11 +495,11 @@ func main() {
|
|||||||
|
|
||||||
// Then apply token validation middleware
|
// Then apply token validation middleware
|
||||||
// This will validate the token from the Authorization header
|
// This will validate the token from the Authorization header
|
||||||
e.Use(TokenValidationMiddleware(config, routePermissions, logger))
|
e.Use(PkceTokenValidationMiddleware(config, routePermissions, logger))
|
||||||
|
|
||||||
// Login route
|
// Login route entirely handled by middleware - nothing to do here.
|
||||||
e.GET("/login", func(c echo.Context) error {
|
e.GET("/login", func(c echo.Context) error {
|
||||||
// The TokenValidationMiddleware will handle this
|
// The PkceTokenValidationMiddleware will handle this
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user