(t *testing.T)
| 20 | ) |
| 21 | |
| 22 | func 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 | }, |
nothing calls this directly
no test coverage detected
searching dependent graphs…