| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package oauthtest |
| 6 |
|
| 7 |
import ( |
| 8 |
"crypto/rand" |
| 9 |
"crypto/sha256" |
| 10 |
"encoding/base64" |
| 11 |
"encoding/json" |
| 12 |
"fmt" |
| 13 |
"maps" |
| 14 |
"net/http" |
| 15 |
"net/http/httptest" |
| 16 |
"slices" |
| 17 |
"testing" |
| 18 |
"time" |
| 19 |
|
| 20 |
"github.com/modelcontextprotocol/go-sdk/oauthex" |
| 21 |
) |
| 22 |
|
| 23 |
|
| 24 |
type TokenExchangeConfig struct { |
| 25 |
|
| 26 |
|
| 27 |
IDJAGToken string |
| 28 |
} |
| 29 |
|
| 30 |
|
| 31 |
type IdPConfig struct { |
| 32 |
|
| 33 |
PreregisteredClients map[string]ClientInfo |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
TokenExchangeConfig *TokenExchangeConfig |
| 38 |
} |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
ed |
| 46 |
type FakeIdPServer struct { |
| 47 |
server *httptest.Server |
| 48 |
mux *http.ServeMux |
| 49 |
config IdPConfig |
| 50 |
clients map[string]ClientInfo |
| 51 |
codes map[string]codeInfo |
| 52 |
} |
| 53 |
|
| 54 |
|
| 55 |
func NewFakeIdPServer(config IdPConfig) *FakeIdPServer { |
| 56 |
s := &FakeIdPServer{ |
| 57 |
mux: http.NewServeMux(), |
| 58 |
config: config, |
| 59 |
clients: make(map[string]ClientInfo), |
| 60 |
codes: make(map[string]codeInfo), |
| 61 |
} |
| 62 |
maps.Copy(s.clients, config.PreregisteredClients) |
| 63 |
|
| 64 |
s.mux.HandleFunc("/.well-known/openid-configuration", s.handleMetadata) |
| 65 |
s.mux.HandleFunc("/authorize", s.handleAuthorize) |
| 66 |
s.mux.HandleFunc("/token", s.handleToken) |
| 67 |
s.server = httptest.NewUnstartedServer(s.mux) |
| 68 |
|
| 69 |
return s |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
func (s *FakeIdPServer) Start(t testing.TB) { |
| 74 |
s.server.Start() |
| 75 |
t.Cleanup(s.server.Close) |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
func (s *FakeIdPServer) URL() string { |
| 80 |
return s.server.URL |
| 81 |
} |
| 82 |
|
| 83 |
func (s *FakeIdPServer) handleMetadata(w http.ResponseWriter, r *http.Request) { |
| 84 |
if r.Method != http.MethodGet { |
| 85 |
http.Error(w, "method not allowed", http.StatusMethodNotAllowed) |
| 86 |
return |
| 87 |
} |
| 88 |
grantTypes := []string{"authorization_code"} |
| 89 |
if s.config.TokenExchangeConfig != nil { |
| 90 |
grantTypes = append(grantTypes, oauthex.GrantTypeTokenExchange) |
| 91 |
} |
| 92 |
w.Header().Set("Content-Type", "application/json") |
| 93 |
json.NewEncoder(w).Encode(map[string]any{ |
| 94 |
"issuer": s.URL(), |
| 95 |
"authorization_endpoint": s.URL() + "/authorize", |
| 96 |
"token_endpoint": s.URL() + "/token", |
| 97 |
"jwks_uri": s.URL() + "/.well-known/jwks.json", |
| 98 |
"response_types_supported": []string{"code"}, |
| 99 |
"code_challenge_methods_supported": []string{"S256"}, |
| 100 |
"grant_types_supported": grantTypes, |
| 101 |
}) |
| 102 |
} |
| 103 |
|
| 104 |
func (s *FakeIdPServer) handleAuthorize(w http.ResponseWriter, r *http.Request) { |
| 105 |
if r.Method != http.MethodGet { |
| 106 |
http.Error(w, "method not allowed", http.StatusMethodNotAllowed) |
| 107 |
return |
| 108 |
} |
| 109 |
clientID := r.URL.Query().Get("client_id") |
| 110 |
clientInfo, ok := s.clients[clientID] |
| 111 |
if !ok { |
| 112 |
http.Error(w, "unknown client_id", http.StatusBadRequest) |
| 113 |
return |
| 114 |
} |
| 115 |
redirectURI := r.URL.Query().Get("redirect_uri") |
| 116 |
if redirectURI == "" { |
| 117 |
http.Error(w, "missing redirect_uri", http.StatusBadRequest) |
| 118 |
return |
| 119 |
} |
| 120 |
if !slices.Contains(clientInfo.RedirectURIs, redirectURI) { |
| 121 |
http.Error(w, "invalid redirect_uri", http.StatusBadRequest) |
| 122 |
return |
| 123 |
} |
| 124 |
codeChallenge := r.URL.Query().Get("code_challenge") |
| 125 |
if codeChallenge == "" { |
| 126 |
http.Error(w, "missing code_challenge", http.StatusBadRequest) |
| 127 |
return |
| 128 |
} |
| 129 |
code := rand.Text() |
| 130 |
s.codes[code] = codeInfo{CodeChallenge: codeChallenge} |
| 131 |
state := r.URL.Query().Get("state") |
| 132 |
redirectURL := fmt.Sprintf("%s?code=%s&state=%s", redirectURI, code, state) |
| 133 |
http.Redirect(w, r, redirectURL, http.StatusFound) |
| 134 |
} |
| 135 |
|
| 136 |
func (s *FakeIdPServer) handleToken(w http.ResponseWriter, r *http.Request) { |
| 137 |
if r.Method != http.MethodPost { |
| 138 |
http.Error(w, "method not allowed", http.StatusMethodNotAllowed) |
| 139 |
return |
| 140 |
} |
| 141 |
if err := r.ParseForm(); err != nil { |
| 142 |
http.Error(w, "failed to parse form", http.StatusBadRequest) |
| 143 |
return |
| 144 |
} |
| 145 |
|
| 146 |
if err := s.authenticateClient(r); err != nil { |
| 147 |
http.Error(w, err.Error(), http.StatusUnauthorized) |
| 148 |
return |
| 149 |
} |
| 150 |
|
| 151 |
grantType := r.Form.Get("grant_type") |
| 152 |
switch grantType { |
| 153 |
case "authorization_code": |
| 154 |
s.handleAuthorizationCodeGrant(w, r) |
| 155 |
case oauthex.GrantTypeTokenExchange: |
| 156 |
s.handleTokenExchangeGrant(w, r) |
| 157 |
default: |
| 158 |
http.Error(w, fmt.Sprintf("unsupported grant_type: %s", grantType), http.StatusBadRequest) |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
func (s *FakeIdPServer) handleAuthorizationCodeGrant(w http.ResponseWriter, r *http.Request) { |
| 163 |
code := r.Form.Get("code") |
| 164 |
if code == "" { |
| 165 |
http.Error(w, "missing code", http.StatusBadRequest) |
| 166 |
return |
| 167 |
} |
| 168 |
ci, ok := s.codes[code] |
| 169 |
if !ok { |
| 170 |
http.Error(w, "unknown authorization code", http.StatusBadRequest) |
| 171 |
return |
| 172 |
} |
| 173 |
verifier := r.Form.Get("code_verifier") |
| 174 |
if verifier == "" { |
| 175 |
http.Error(w, "missing code_verifier", http.StatusBadRequest) |
| 176 |
return |
| 177 |
} |
| 178 |
sha := sha256.Sum256([]byte(verifier)) |
| 179 |
expectedChallenge := base64.RawURLEncoding.EncodeToString(sha[:]) |
| 180 |
if expectedChallenge != ci.CodeChallenge { |
| 181 |
http.Error(w, "PKCE verification failed", http.StatusBadRequest) |
| 182 |
return |
| 183 |
} |
| 184 |
|
| 185 |
clientID := r.Form.Get("client_id") |
| 186 |
now := time.Now().Unix() |
| 187 |
idToken := fakeIDToken(s.URL(), clientID, now) |
| 188 |
|
| 189 |
w.Header().Set("Content-Type", "application/json") |
| 190 |
json.NewEncoder(w).Encode(map[string]any{ |
| 191 |
"access_token": "test_access_token", |
| 192 |
"token_type": "Bearer", |
| 193 |
"expires_in": 3600, |
| 194 |
"refresh_token": "test_refresh_token", |
| 195 |
"id_token": idToken, |
| 196 |
}) |
| 197 |
} |
| 198 |
|
| 199 |
func (s *FakeIdPServer) handleTokenExchangeGrant(w http.ResponseWriter, r *http.Request) { |
| 200 |
if s.config.TokenExchangeConfig == nil { |
| 201 |
http.Error(w, "token exchange not supported", http.StatusBadRequest) |
| 202 |
return |
| 203 |
} |
| 204 |
if r.Form.Get("requested_token_type") != oauthex.TokenTypeIDJAG { |
| 205 |
http.Error(w, "invalid requested_token_type", http.StatusBadRequest) |
| 206 |
return |
| 207 |
} |
| 208 |
if r.Form.Get("subject_token_type") != oauthex.TokenTypeIDToken { |
| 209 |
http.Error(w, "invalid subject_token_type", http.StatusBadRequest) |
| 210 |
return |
| 211 |
} |
| 212 |
if r.Form.Get("subject_token") == "" { |
| 213 |
http.Error(w, "missing subject_token", http.StatusBadRequest) |
| 214 |
return |
| 215 |
} |
| 216 |
|
| 217 |
idJAG := s.config.TokenExchangeConfig.IDJAGToken |
| 218 |
if idJAG == "" { |
| 219 |
idJAG = "test-id-jag-token" |
| 220 |
} |
| 221 |
|
| 222 |
w.Header().Set("Content-Type", "application/json") |
| 223 |
json.NewEncoder(w).Encode(map[string]any{ |
| 224 |
"access_token": idJAG, |
| 225 |
"issued_token_type": oauthex.TokenTypeIDJAG, |
| 226 |
"token_type": "N_A", |
| 227 |
"expires_in": 300, |
| 228 |
}) |
| 229 |
} |
| 230 |
|
| 231 |
func (s *FakeIdPServer) authenticateClient(r *http.Request) error { |
| 232 |
clientID := r.Form.Get("client_id") |
| 233 |
clientSecret := r.Form.Get("client_secret") |
| 234 |
clientInfo, ok := s.clients[clientID] |
| 235 |
if !ok { |
| 236 |
return fmt.Errorf("unknown client") |
| 237 |
} |
| 238 |
if clientInfo.Secret != clientSecret { |
| 239 |
return fmt.Errorf("invalid client credentials") |
| 240 |
} |
| 241 |
return nil |
| 242 |
} |
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
func fakeIDToken(issuer, audience string, now int64) string { |
| 247 |
header := base64.RawURLEncoding.EncodeToString([]byte(`{"alg":"RS256","typ":"JWT"}`)) |
| 248 |
claimsJSON, _ := json.Marshal(map[string]any{ |
| 249 |
"iss": issuer, |
| 250 |
"sub": "test-user", |
| 251 |
"aud": audience, |
| 252 |
"exp": now + 3600, |
| 253 |
"iat": now, |
| 254 |
"email": "[email protected]", |
| 255 |
}) |
| 256 |
claims := base64.RawURLEncoding.EncodeToString(claimsJSON) |
| 257 |
return header + "." + claims + ".mock-signature" |
| 258 |
} |