Skip to content

Commit a1bd673

Browse files
committed
Use github.com/dgrijalva/jwt-go instead of custom logic to decode OAuth token
Signed-off-by: Ed Snible <snible@us.ibm.com>
1 parent 0e6f1b2 commit a1bd673

4 files changed

Lines changed: 24 additions & 23 deletions

File tree

mcp/github_tool/README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,4 +178,3 @@ oc --context kind-agent-platform apply -f /tmp/expose-github-tool.yaml
178178

179179
At this point, you can do the MCP curls above if you define `MCP=http://github-tool.127-0-0-1.sslip.io:8888/mcp`
180180

181-

mcp/github_tool/go.mod

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ module github.com/kagenti/mcp-mitm
22

33
go 1.24.9
44

5-
require github.com/mark3labs/mcp-go v0.41.1
5+
require (
6+
github.com/dgrijalva/jwt-go v3.2.0+incompatible
7+
github.com/mark3labs/mcp-go v0.41.1
8+
)
69

710
require (
811
github.com/bahlo/generic-list-go v0.2.0 // indirect

mcp/github_tool/go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ github.com/buger/jsonparser v1.1.1 h1:2PnMjfWD7wBILjqQbt530v576A/cAbQvEW9gGIpYMU
44
github.com/buger/jsonparser v1.1.1/go.mod h1:6RYKKt7H4d4+iWqouImQ9R2FZql3VbhNgx27UK13J/0=
55
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
66
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
7+
github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM=
8+
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
79
github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHkI4W8=
810
github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0=
911
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=

mcp/github_tool/gtlib/upstream.go

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ package lib
22

33
import (
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

Comments
 (0)