-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: decleaver <85503726+decleaver@users.noreply.github.com>
- Loading branch information
Showing
36 changed files
with
470 additions
and
368 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Binary file not shown.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// Copyright 2024 Defense Unicorns | ||
// SPDX-License-Identifier: AGPL-3.0-or-later OR LicenseRef-Defense-Unicorns-Commercial | ||
|
||
package cluster | ||
|
||
import ( | ||
"net/http" | ||
"strings" | ||
|
||
"github.com/golang-jwt/jwt/v5" | ||
) | ||
|
||
var allowedGroups = []string{ | ||
"/UDS Core/Admin", | ||
"/UDS Core/Auditor", | ||
} | ||
|
||
// ValidateJWT checks if the request has a valid JWT token with the required groups. | ||
func ValidateJWT(w http.ResponseWriter, r *http.Request) bool { | ||
authHeader := r.Header.Get("Authorization") | ||
|
||
if authHeader == "" { | ||
http.Error(w, "Missing Authorization header", http.StatusUnauthorized) | ||
return false | ||
} | ||
|
||
tokenString := strings.TrimPrefix(authHeader, "Bearer ") | ||
|
||
// parse the JWT token without validation (authservice will validate it, we only need the groups here) | ||
token, _, err := jwt.NewParser(jwt.WithoutClaimsValidation()).ParseUnverified(tokenString, jwt.Claims(jwt.MapClaims{})) | ||
if err != nil { | ||
http.Error(w, "Invalid token", http.StatusUnauthorized) | ||
return false | ||
} | ||
|
||
// Check if the token contains a "groups" claim | ||
if groups, ok := token.Claims.(jwt.MapClaims)["groups"].([]interface{}); ok { | ||
// Check if any of the token's groups match the allowed groups | ||
for _, group := range groups { | ||
for _, allowedGroup := range allowedGroups { | ||
if group == allowedGroup { | ||
// Group is allowed | ||
return true | ||
} | ||
} | ||
} | ||
// If we reach here, no matching group was found | ||
http.Error(w, "Insufficient permissions", http.StatusForbidden) | ||
return false | ||
} | ||
|
||
http.Error(w, "Invalid token claims", http.StatusUnauthorized) | ||
return false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.