74 lines
2.0 KiB
Go
74 lines
2.0 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"queryorchestration/internal/rbac" // Replace with your actual module name
|
|
)
|
|
|
|
func main() {
|
|
// Example with local key provider for testing
|
|
localProvider := rbac.NewLocalKeyProvider("private_key.pem", "public_key.pem")
|
|
|
|
// Generate a mock JWT with multiple Cognito groups
|
|
groups := []string{"admins", "developers", "querybuilders", "uploaders", "exporters"}
|
|
tokenString, err := rbac.GenerateMockJWT(localProvider, "test-user-id", "test@example.com", groups)
|
|
if err != nil {
|
|
fmt.Println("Error generating token:", err)
|
|
return
|
|
}
|
|
|
|
fmt.Printf("raw token: %s\n", tokenString)
|
|
|
|
token, claims, err := rbac.ValidateJWT(tokenString, localProvider)
|
|
if err != nil {
|
|
fmt.Println("Token validation failed:", err)
|
|
return
|
|
}
|
|
|
|
// Print token details for debugging
|
|
rbac.PrintTokenDetails(token)
|
|
|
|
// Example with Cognito key provider for production
|
|
/*
|
|
cognitoProvider := rbac.NewCognitoKeyProvider("us-east-1", "us-east-1_XXXXXXXXX")
|
|
token, claims, err := rbac.ValidateJWT(tokenString, cognitoProvider)
|
|
*/
|
|
|
|
if token.Valid {
|
|
// Extract the token details
|
|
details := rbac.ExtractTokenDetails(token, claims)
|
|
|
|
fmt.Println("Token is valid.")
|
|
fmt.Println("User ID:", details.UserID)
|
|
fmt.Println("Email:", details.Email)
|
|
fmt.Println("Expires:", details.Expiration)
|
|
|
|
if len(details.Groups) > 0 {
|
|
fmt.Println("User Groups:")
|
|
for _, group := range details.Groups {
|
|
fmt.Println("-", group)
|
|
}
|
|
} else {
|
|
fmt.Println("No groups found in token.")
|
|
}
|
|
|
|
// Check if user is in a specific group
|
|
if rbac.HasGroup(claims, "admins") {
|
|
fmt.Println("User is an admin.")
|
|
}
|
|
|
|
// Check if user is in any of these groups
|
|
if rbac.HasAnyGroup(claims, []string{"developers", "querybuilders"}) {
|
|
fmt.Println("User can modify or query data.")
|
|
}
|
|
|
|
// Check if user is in all of these groups
|
|
if rbac.HasAllGroups(claims, []string{"uploaders", "exporters"}) {
|
|
fmt.Println("User can both upload and export data.")
|
|
}
|
|
} else {
|
|
fmt.Println("Invalid token")
|
|
}
|
|
}
|