deviceflow

package module
v0.0.0-...-666bb5a Latest Latest
Warning

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

Go to latest
Published: Oct 23, 2025 License: MIT Imports: 7 Imported by: 0

README

deviceflow

A simple, high-level Go package for implementing the OAuth 2.0 Device Authorization Grant (RFC 8628) with a Casdoor server.

This library is designed for applications that run on input-constrained devices, such as CLI tools, smart TVs, or IoT devices, allowing them to obtain access tokens without requiring an embedded browser.

See cmd/example for usage.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	CasdoorEndpoint string // Base URL of your Casdoor instance (e.g., "https://door.casdoor.com")
	ClientID        string // The Client ID of your Casdoor application
	Scope           string // The scopes you are requesting (e.g., "openid profile email")
	HttpClient      *http.Client

	// Optional endpoint paths. If not provided, default Casdoor paths will be used.
	DeviceAuthPath string // The path for the device authorization endpoint. Defaults to "/api/device-auth".
	TokenPath      string // The path for the token endpoint. Defaults to "/api/login/oauth/access_token".
}

Config holds the necessary configuration for the device flow.

type DeviceAuthResponse

type DeviceAuthResponse 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"`
}

DeviceAuthResponse is the response from the device authorization endpoint.

type TokenResponse

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

TokenResponse contains the tokens returned upon successful authorization.

func Authenticate

func Authenticate(config Config, displayFunc func(authInfo *DeviceAuthResponse)) (*TokenResponse, error)

Authenticate orchestrates the entire device flow. It initiates the flow, calls the provided `displayFunc` to instruct the user, and then polls for the token until it's granted, an error occurs, or it times out.

func RefreshToken

func RefreshToken(config Config, refreshToken string) (*TokenResponse, error)

RefreshToken uses a refresh token to obtain a new access and refresh token.

Directories

Path Synopsis
cmd
example command

Jump to

Keyboard shortcuts

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