Documentation
¶
Index ¶
- func ExtractTokenFromSetCookie(resp *http.Response, cookieName string) string
- func SimulateUserAuthorization(serverURL, userCode string) error
- type FakeOAuthProvider
- type FakeUser
- type MeResponse
- type NativeAppClient
- func (c *NativeAppClient) AuthenticateViaDeviceCode(ctx context.Context, authorizeFunc func(userCode string) error) error
- func (c *NativeAppClient) BrowserAuthFlow(ctx context.Context) error
- func (c *NativeAppClient) GetMe(ctx context.Context) (*MeResponse, error)
- func (c *NativeAppClient) Request(ctx context.Context, method, path string, body io.Reader) (*http.Response, error)
- func (c *NativeAppClient) RequestWithCookie(ctx context.Context, method, path, cookieName string) (*http.Response, error)
- type TestServer
- type TestServerOption
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ExtractTokenFromSetCookie ¶
ExtractTokenFromSetCookie extracts a session token from Set-Cookie header.
func SimulateUserAuthorization ¶
SimulateUserAuthorization simulates a user authorizing a device code. This makes the necessary HTTP requests that a browser would make.
Types ¶
type FakeOAuthProvider ¶
type FakeOAuthProvider struct {
Server *httptest.Server
Config *oauth2.Config
AutoApprove bool // automatically approve authorization requests
// contains filtered or unexported fields
}
FakeOAuthProvider simulates an OAuth2 provider for testing. It provides minimal OAuth2 endpoints for authorization code flow.
func NewFakeOAuthProvider ¶
func NewFakeOAuthProvider(autoApprove bool) *FakeOAuthProvider
NewFakeOAuthProvider creates a new fake OAuth2 provider for testing. If autoApprove is true, authorization requests are immediately approved without user interaction, which is useful for automated testing.
func (*FakeOAuthProvider) Close ¶
func (p *FakeOAuthProvider) Close()
Close shuts down the fake OAuth server.
func (*FakeOAuthProvider) OAuthConfig ¶
func (p *FakeOAuthProvider) OAuthConfig() *oauth2.Config
OAuthConfig returns the oauth2.Config pointing to this fake provider.
func (*FakeOAuthProvider) Reset ¶
func (p *FakeOAuthProvider) Reset()
Reset clears all stored authorization codes and access tokens. This is useful for isolating tests.
type MeResponse ¶
type MeResponse struct {
UserID string `json:"user_id"`
SessionID string `json:"session_id"`
IdleDeadline time.Time `json:"idle_deadline"`
AbsoluteDeadline time.Time `json:"absolute_deadline"`
}
MeResponse represents the response from /api/me endpoint.
type NativeAppClient ¶ added in v0.9.2
type NativeAppClient struct {
BaseURL string
Token string // stored session token
HTTPClient *http.Client
}
NativeAppClient simulates a native application (desktop, mobile, CLI) authenticating with the server. It supports device code flow and browser OAuth flow.
func NewNativeAppClient ¶ added in v0.9.2
func NewNativeAppClient(baseURL string) *NativeAppClient
NewNativeAppClient creates a new native app client that talks to the given base URL.
func (*NativeAppClient) AuthenticateViaDeviceCode ¶ added in v0.9.2
func (c *NativeAppClient) AuthenticateViaDeviceCode(ctx context.Context, authorizeFunc func(userCode string) error) error
AuthenticateViaDeviceCode performs the device code OAuth flow. The authorizeFunc is called with the user code and should simulate the user authorizing the device via browser.
func (*NativeAppClient) BrowserAuthFlow ¶ added in v0.9.2
func (c *NativeAppClient) BrowserAuthFlow(ctx context.Context) error
BrowserAuthFlow simulates a browser OAuth flow and returns the session cookie. This is used for testing cookie-based authentication.
func (*NativeAppClient) GetMe ¶ added in v0.9.2
func (c *NativeAppClient) GetMe(ctx context.Context) (*MeResponse, error)
GetMe calls /api/me to verify authentication and get session info.
func (*NativeAppClient) Request ¶ added in v0.9.2
func (c *NativeAppClient) Request(ctx context.Context, method, path string, body io.Reader) (*http.Response, error)
Request makes an authenticated HTTP request using the stored token.
func (*NativeAppClient) RequestWithCookie ¶ added in v0.9.2
func (c *NativeAppClient) RequestWithCookie(ctx context.Context, method, path, cookieName string) (*http.Response, error)
RequestWithCookie makes an HTTP request with the token sent as a cookie instead of header. This simulates browser-based authentication.
type TestServer ¶
type TestServer struct {
Server *httptest.Server
Gosesh *gosesh.Gosesh
Store *gosesh.MemoryStore
DeviceStore *gosesh.MemoryDeviceCodeStore
OAuthServer *FakeOAuthProvider
OAuthConfig *oauth2.Config
}
TestServer wraps an HTTP test server with gosesh for e2e testing. It provides a complete working server with OAuth2, sessions, and authentication.
func NewTestServer ¶
func NewTestServer(opts ...TestServerOption) *TestServer
NewTestServer creates a new test server with gosesh configured for e2e testing. The server includes:
- FakeOAuthProvider for OAuth2 (auto-approves by default)
- MemoryStore for sessions
- MemoryDeviceCodeStore for device codes
- CompositeCredentialSource supporting both cookies and headers
- All authentication routes configured
func (*TestServer) Close ¶
func (ts *TestServer) Close()
Close shuts down the test server and fake OAuth provider.
func (*TestServer) Reset ¶
func (ts *TestServer) Reset()
Reset clears all session and device code state. Call this between tests to isolate state.
type TestServerOption ¶
type TestServerOption func(*TestServer)
TestServerOption configures a TestServer.