forked from wundergraph/cosmo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: improve code structure in graphqlmetrics service, allow api…
…-key override in cli (wundergraph#348)
- Loading branch information
Showing
12 changed files
with
244 additions
and
130 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package core | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"github.com/golang-jwt/jwt/v5" | ||
"go.uber.org/zap" | ||
"net/http" | ||
"strings" | ||
) | ||
|
||
type GraphAPITokenClaims struct { | ||
OrganizationID string `json:"organization_id"` | ||
FederatedGraphID string `json:"federated_graph_id"` | ||
jwt.RegisteredClaims | ||
} | ||
|
||
type claimsContextKey string | ||
|
||
const claimsKey claimsContextKey = "claims" | ||
|
||
func getClaims(ctx context.Context) (*GraphAPITokenClaims, error) { | ||
claims, ok := ctx.Value(claimsKey).(*GraphAPITokenClaims) | ||
if !ok { | ||
return nil, fmt.Errorf("could not get claims from context") | ||
} | ||
return claims, nil | ||
} | ||
|
||
func setClaims(ctx context.Context, claims *GraphAPITokenClaims) context.Context { | ||
return context.WithValue(ctx, claimsKey, claims) | ||
} | ||
|
||
func authenticate(jwtSecret []byte, logger *zap.Logger, next http.Handler) http.Handler { | ||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
|
||
parts := strings.Split(r.Header.Get("Authorization"), " ") | ||
if len(parts) != 2 { | ||
w.WriteHeader(http.StatusUnauthorized) | ||
return | ||
} | ||
|
||
token, err := jwt.ParseWithClaims(parts[1], &GraphAPITokenClaims{}, func(token *jwt.Token) (interface{}, error) { | ||
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok { | ||
return nil, fmt.Errorf("unexpected signing method: %v", token.Header["alg"]) | ||
} | ||
return jwtSecret, nil | ||
}) | ||
if err != nil { | ||
logger.Debug("Failed to parse token", zap.Error(err)) | ||
w.WriteHeader(http.StatusUnauthorized) | ||
return | ||
} | ||
|
||
if !token.Valid { | ||
logger.Debug("Token is invalid", zap.Bool("valid", token.Valid)) | ||
w.WriteHeader(http.StatusUnauthorized) | ||
return | ||
} | ||
|
||
claims, ok := token.Claims.(*GraphAPITokenClaims) | ||
if !ok { | ||
w.WriteHeader(http.StatusUnauthorized) | ||
return | ||
} | ||
|
||
r = r.WithContext(setClaims(r.Context(), claims)) | ||
|
||
next.ServeHTTP(w, r) | ||
}) | ||
} |
Oops, something went wrong.