@@ -2,8 +2,6 @@ package lib
22
33import (
44 "context"
5- "encoding/base64"
6- "encoding/json"
75 "fmt"
86 "log/slog"
97 "net/http"
@@ -12,6 +10,7 @@ import (
1210 "strings"
1311 "time"
1412
13+ "github.com/dgrijalva/jwt-go"
1514 "github.com/mark3labs/mcp-go/client"
1615 "github.com/mark3labs/mcp-go/client/transport"
1716 "github.com/mark3labs/mcp-go/mcp"
@@ -343,27 +342,25 @@ func getAuthorizationHeaderFromBearer(auth string) string {
343342 }
344343
345344 token := strings .TrimPrefix (auth , "Bearer " )
346- var decodedToken map [string ]interface {}
347-
348- // The token will be several base64 pieces separated by '.'. Split in '.', use base64.StdEncoding.DecodeString(eachPart)
349- // This method is insecure, because it trusts the bearer token, rather than verifying it cryptographically or by
350- // passing it to an OIDC endpoint
351- tokenParts := strings .Split (token , "." )
352- if len (tokenParts ) == 3 {
353- // Only look at the middle part
354- tokenPart := tokenParts [1 ]
355- decodedPart , err := base64 .StdEncoding .DecodeString (tokenPart )
356- if err != nil {
357- fmt .Printf ("@@@ couldn't decode part: %v of %q\n " , err , tokenPart )
358- } else {
359- // fmt.Printf("part is %q\n", decodedPart)
360- err = json .Unmarshal (decodedPart , & decodedToken )
361- if err != nil {
362- fmt .Printf ("@@@ couldn't unmarshal part: %v\n " , err )
363- }
364- }
345+
346+ // For explanation see https://stackoverflow.com/questions/55698770/decode-jwt-without-validation-and-find-scope
347+ jwtToken , _ , err := new (jwt.Parser ).ParseUnverified (token , jwt.MapClaims {})
348+ if err != nil {
349+ fmt .Printf ("Bearer token couldn't be parsed: %v\n " , err )
350+ return ""
365351 }
366352
353+ // TODO check that jwtToken hasn't expired, is valid, etc.
354+
355+ claims , ok := jwtToken .Claims .(jwt.MapClaims )
356+ if ! ok {
357+ fmt .Printf ("Parsed token not MapClaims: %v\n " , err )
358+ return ""
359+ }
360+
361+ // fmt.Printf("Got claims %#v\n", claims)
362+ decodedToken := claims
363+
367364 /*
368365 x := map[string]interface {}{
369366 "acr":"1",
0 commit comments