MCPcopy Create free account
hub / github.com/modelcontextprotocol/go-sdk / TestAuthorize

Function TestAuthorize

auth/authorization_code_test.go:22–112  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

20)
21
22func TestAuthorize(t *testing.T) {
23 authServer := oauthtest.NewFakeAuthorizationServer(oauthtest.Config{
24 RegistrationConfig: &oauthtest.RegistrationConfig{
25 PreregisteredClients: map[string]oauthtest.ClientInfo{
26 "test_client_id": {
27 Secret: "test_client_secret",
28 RedirectURIs: []string{"http://localhost:12345/callback"},
29 },
30 },
31 },
32 })
33 authServer.Start(t)
34
35 resourceMux := http.NewServeMux()
36 resourceServer := httptest.NewServer(resourceMux)
37 t.Cleanup(resourceServer.Close)
38 resourceURL := resourceServer.URL + "/resource"
39
40 resourceMux.Handle("/.well-known/oauth-protected-resource/resource", ProtectedResourceMetadataHandler(&oauthex.ProtectedResourceMetadata{
41 Resource: resourceURL,
42 AuthorizationServers: []string{authServer.URL()},
43 }))
44
45 handler, err := NewAuthorizationCodeHandler(&AuthorizationCodeHandlerConfig{
46 RedirectURL: "http://localhost:12345/callback",
47 PreregisteredClient: &oauthex.ClientCredentials{
48 ClientID: "test_client_id",
49 ClientSecretAuth: &oauthex.ClientSecretAuth{
50 ClientSecret: "test_client_secret",
51 },
52 },
53 AuthorizationCodeFetcher: func(ctx context.Context, args *AuthorizationArgs) (*AuthorizationResult, error) {
54 // The fake authorization server will redirect to an URL with code and state.
55 client := &http.Client{
56 CheckRedirect: func(req *http.Request, via []*http.Request) error {
57 return http.ErrUseLastResponse
58 },
59 }
60 resp, err := client.Get(args.URL)
61 if err != nil {
62 return nil, fmt.Errorf("failed to visit auth URL: %v", err)
63 }
64 defer resp.Body.Close()
65 dump, err := httputil.DumpResponse(resp, true)
66 if err != nil {
67 t.Fatalf("failed to dump response: %v", err)
68 }
69 t.Log(string(dump))
70
71 location, err := resp.Location()
72 if err != nil {
73 return nil, fmt.Errorf("failed to get location header: %v", err)
74 }
75 return &AuthorizationResult{
76 Code: location.Query().Get("code"),
77 State: location.Query().Get("state"),
78 }, nil
79 },

Callers

nothing calls this directly

Calls 10

StartMethod · 0.95
URLMethod · 0.95
AuthorizeMethod · 0.95
TokenSourceMethod · 0.95
LogMethod · 0.80
HandleMethod · 0.65
CloseMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…