oauth

package
v0.0.0-...-0bc535e Latest Latest
Warning

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

Go to latest
Published: Mar 9, 2026 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Overview

Package oauthimplements the OAuth 2.0 Device Authorization Grant (RFC 8628) for authenticating with Sourcegraph instances.

Index

Constants

View Source
const (
	// DefaultClientID is a predefined Client ID built into Sourcegraph
	DefaultClientID = "sgo_cid_sourcegraph-cli"

	GrantTypeDeviceCode string = "urn:ietf:params:oauth:grant-type:device_code"

	ScopeOpenID        string = "openid"
	ScopeProfile       string = "profile"
	ScopeEmail         string = "email"
	ScopeOfflineAccess string = "offline_access"
	ScopeUserAll       string = "user:all"
)

Variables

This section is empty.

Functions

func IsOAuthTransport

func IsOAuthTransport(trp http.RoundTripper) bool

IsOAuthTransport checks wether the underlying type of the given RoundTripper is a OAuthTransport

func StoreToken

func StoreToken(ctx context.Context, token *Token) error

Types

type Client

type Client interface {
	ClientID() string
	Discover(ctx context.Context, endpoint string) (*OIDCConfiguration, error)
	Start(ctx context.Context, endpoint string, scopes []string) (*DeviceAuthResponse, error)
	Poll(ctx context.Context, endpoint, deviceCode string, interval time.Duration, expiresIn int) (*TokenResponse, error)
	Refresh(ctx context.Context, token *Token) (*TokenResponse, error)
}

func NewClient

func NewClient(clientID string) Client

func NewClientWithHTTPClient

func NewClientWithHTTPClient(clientID string, c *http.Client) Client

type DeviceAuthResponse

type DeviceAuthResponse struct {
	DeviceCode              string `json:"device_code"`
	UserCode                string `json:"user_code"`
	VerificationURI         string `json:"verification_uri"`
	VerificationURIComplete string `json:"verification_uri_complete,omitempty"`
	ExpiresIn               int    `json:"expires_in"`
	Interval                int    `json:"interval"`
}

type ErrorResponse

type ErrorResponse struct {
	Error            string `json:"error"`
	ErrorDescription string `json:"error_description,omitempty"`
}

type OIDCConfiguration

type OIDCConfiguration struct {
	Issuer                      string `json:"issuer,omitempty"`
	TokenEndpoint               string `json:"token_endpoint,omitempty"`
	DeviceAuthorizationEndpoint string `json:"device_authorization_endpoint,omitempty"`
}

OIDCConfiguration represents the relevant fields from the OpenID Connect Discovery document at /.well-known/openid-configuration

type PollError

type PollError struct {
	Code        string
	Description string
}

func (*PollError) Error

func (e *PollError) Error() string

type Token

type Token struct {
	Endpoint     string    `json:"endpoint"`
	ClientID     string    `json:"client_id,omitempty"`
	AccessToken  string    `json:"access_token"`
	RefreshToken string    `json:"refresh_token,omitempty"`
	ExpiresAt    time.Time `json:"expires_at"`
}

func LoadToken

func LoadToken(ctx context.Context, endpoint string) (*Token, error)

func (*Token) ExpiringIn

func (t *Token) ExpiringIn(d time.Duration) bool

func (*Token) HasExpired

func (t *Token) HasExpired() bool

type TokenResponse

type TokenResponse struct {
	AccessToken  string `json:"access_token"`
	RefreshToken string `json:"refresh_token,omitempty"`
	ExpiresIn    int    `json:"expires_in,omitempty"`
	TokenType    string `json:"token_type"`
	Scope        string `json:"scope,omitempty"`
}

func (*TokenResponse) Token

func (t *TokenResponse) Token(endpoint string) *Token

type Transport

type Transport struct {
	Base  http.RoundTripper
	Token *Token
	// contains filtered or unexported fields
}

func (*Transport) RoundTrip

func (t *Transport) RoundTrip(req *http.Request) (*http.Response, error)

RoundTrip implements http.RoundTripper.

Jump to

Keyboard shortcuts

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