deviceflow

package
v0.2.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 9, 2025 License: MIT Imports: 15 Imported by: 0

Documentation

Overview

Package deviceflow handles GitHub App access token generation using OAuth device flow. It provides functionality to authenticate GitHub Apps and obtain access tokens.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AccessToken

type AccessToken struct {
	App            string    `json:"app"`
	AccessToken    string    `json:"access_token"`
	ExpirationDate time.Time `json:"expiration_date"`
}

AccessToken represents a GitHub App access token with its metadata. It includes the token value, associated app, and expiration date.

type AccessTokenResponse

type AccessTokenResponse struct {
	AccessToken string `json:"access_token"`
	ExpiresIn   int    `json:"expires_in"`

	Error string `json:"error"`
}

AccessTokenResponse represents the response from GitHub's access token endpoint. It contains either an access token or an error message.

type Browser

type Browser interface {
	Open(ctx context.Context, logger *slog.Logger, url string) error
}

Browser provides an interface for opening URLs in a web browser. This is used to open the GitHub verification URL during device flow authentication.

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client handles GitHub App authentication and access token generation using OAuth device flow. It manages the complete authentication flow including device code requests, user authorization, and access token polling.

func NewClient

func NewClient(input *Input) *Client

NewClient creates a new Client with the provided HTTP client. The client uses the provided HTTP client for all API requests.

func (*Client) Create

func (c *Client) Create(ctx context.Context, logger *slog.Logger, clientID string) (*AccessToken, error)

Create initiates the OAuth device flow and returns an access token. It displays the verification URL and user code, optionally opens a browser, and polls for the access token until the user completes authentication.

func (*Client) SetBrowser

func (c *Client) SetBrowser(b Browser)

SetBrowser updates the browser implementation used by the client. This allows customization of how verification URLs are opened in the browser.

func (*Client) SetDeviceCodeUI

func (c *Client) SetDeviceCodeUI(ui DeviceCodeUI)

SetDeviceCodeUI updates the device code UI implementation used by the client. This allows customization of how device flow information is presented to users.

func (*Client) SetLogger

func (c *Client) SetLogger(logger *log.Logger)

SetLogger updates the logger instance used by the client. This allows dynamic reconfiguration of logging behavior.

type DeviceCodeResponse

type DeviceCodeResponse struct {
	DeviceCode      string `json:"device_code"`
	UserCode        string `json:"user_code"`
	VerificationURI string `json:"verification_uri"`
	ExpiresIn       int    `json:"expires_in"`
	Interval        int    `json:"interval"`
}

DeviceCodeResponse represents the response from GitHub's device code endpoint. It contains the device code and user code needed for authentication.

type DeviceCodeUI

type DeviceCodeUI interface {
	Show(ctx context.Context, logger *slog.Logger, deviceCode *DeviceCodeResponse, expirationDate time.Time) error
}

DeviceCodeUI provides an interface for displaying device flow information to users. Implementations should show the device code, verification URL, and handle user interaction.

type Input

type Input struct {
	HTTPClient   *http.Client                       // HTTP client for API requests
	Now          func() time.Time                   // Function to get current time (for testing)
	Stderr       io.Writer                          // Writer for error output
	Browser      Browser                            // Interface for opening URLs in browser
	NewTicker    func(d time.Duration) *time.Ticker // Function to create tickers (for testing)
	Logger       *log.Logger                        // Logger for debugging and info messages
	DeviceCodeUI DeviceCodeUI                       // UI for displaying device flow information
}

Input contains all dependencies and configuration needed by the Client. It allows for dependency injection and makes testing easier by providing customizable implementations of external dependencies.

func NewInput

func NewInput() *Input

NewInput creates a new Input instance with default dependencies. This provides sensible defaults for production use, including the default HTTP client, system stderr, real browser integration, and standard time functions.

type SimpleDeviceCodeUI

type SimpleDeviceCodeUI struct {
	// contains filtered or unexported fields
}

SimpleDeviceCodeUI is a basic implementation of DeviceCodeUI that displays device flow information to stderr and waits for user input from stdin. It handles the GitHub device flow authentication process by showing the user code and verification URL, then waiting for the user to press Enter.

func NewDeviceCodeUI

func NewDeviceCodeUI(stdin io.Reader, stderr io.Writer, waiter Waiter) *SimpleDeviceCodeUI

NewDeviceCodeUI creates a new SimpleDeviceCodeUI instance. It takes stdin for user input and stderr for output messages.

func (*SimpleDeviceCodeUI) Show

func (d *SimpleDeviceCodeUI) Show(ctx context.Context, _ *slog.Logger, deviceCode *DeviceCodeResponse, expirationDate time.Time) error

Show displays the device flow information to the user and waits for Enter key press. It shows the user code, expiration time, and verification URL. The function returns when Enter is pressed or the context is cancelled. Note that it exits immediately without waiting input if stdin is not a terminal (pipe/redirect). In case of Git Credential Helper stdin is not a terminal, so it exits immediately.

type SimpleWaiter added in v0.2.1

type SimpleWaiter struct{}

func (*SimpleWaiter) Wait added in v0.2.1

func (*SimpleWaiter) Wait(ctx context.Context, duration time.Duration) error

type Waiter added in v0.2.1

type Waiter interface {
	Wait(ctx context.Context, duration time.Duration) error
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL