auth

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Mar 29, 2026 License: MIT Imports: 17 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var AllScopes = []string{
	"mfc/accounting/offices.read",
	"mfc/accounting/accounts.read",
	"mfc/accounting/departments.read",
	"mfc/accounting/journal.read",
	"mfc/accounting/report.read",
	"mfc/accounting/taxes.read",
	"mfc/accounting/trade_partners.read",
	"mfc/accounting/connected_account.read",
	"mfc/accounting/journal.write",
	"mfc/accounting/voucher.write",
	"mfc/accounting/trade_partners.write",
	"mfc/accounting/transaction.write",
}

AllScopes contains all available scopes including write permissions.

View Source
var DefaultScopes = []string{
	"mfc/accounting/offices.read",
	"mfc/accounting/accounts.read",
	"mfc/accounting/departments.read",
	"mfc/accounting/journal.read",
	"mfc/accounting/report.read",
	"mfc/accounting/taxes.read",
	"mfc/accounting/trade_partners.read",
	"mfc/accounting/connected_account.read",
}

DefaultScopes contains read-only scopes for MoneyForward Accounting API.

Functions

func BuildAuthURL

func BuildAuthURL(cfg *config.Config, codeChallenge, state string, scopes []string) string

BuildAuthURL constructs the OAuth 2.0 authorization URL with PKCE parameters.

func GenerateCodeChallenge

func GenerateCodeChallenge(verifier string) string

GenerateCodeChallenge derives a PKCE code challenge from a verifier (SHA256 + base64url).

func GenerateCodeVerifier

func GenerateCodeVerifier() (string, error)

GenerateCodeVerifier generates a PKCE code verifier (32 random bytes, base64url encoded).

func GenerateState

func GenerateState() (string, error)

GenerateState generates a random state parameter (16 random bytes, hex encoded).

func OverrideTokenURL

func OverrideTokenURL(u string) func()

OverrideTokenURL replaces the token endpoint and returns a restore function. Intended for use in tests from external packages.

func SaveToken

func SaveToken(cfg *config.Config, token *Token) error

SaveToken writes a token to disk atomically with restricted permissions.

func StartCallbackServer

func StartCallbackServer(port int, state string) (string, error)

StartCallbackServer starts a local HTTP server that waits for the OAuth callback. It verifies the state parameter and returns the authorization code.

func StartCallbackServerAsync

func StartCallbackServerAsync(port int, state string) (<-chan CallbackResult, error)

StartCallbackServerAsync starts the listener and returns a channel that delivers the result. The server is started before returning, so the caller can open the browser after this call.

func TokenPath

func TokenPath(cfg *config.Config) string

TokenPath returns the file path for the persisted token.

Types

type CallbackResult

type CallbackResult struct {
	Code string
	Err  error
}

CallbackResult holds the result from the OAuth callback.

type Token

type Token struct {
	AccessToken  string    `json:"access_token"`
	RefreshToken string    `json:"refresh_token"`
	TokenType    string    `json:"token_type"`
	ExpiresIn    int       `json:"expires_in,omitempty"`
	Expiry       time.Time `json:"expiry"`
	Scopes       []string  `json:"scopes"`
}

Token represents an OAuth 2.0 token.

func ExchangeCode

func ExchangeCode(ctx context.Context, cfg *config.Config, code, codeVerifier string) (*Token, error)

ExchangeCode exchanges an authorization code for an access token.

func GetValidToken

func GetValidToken(cfg *config.Config) (*Token, error)

GetValidToken loads a token, refreshes it if expired, and returns a valid token. It uses file locking to coordinate concurrent processes.

func LoadToken

func LoadToken(cfg *config.Config) (*Token, error)

LoadToken reads a token from disk.

func RefreshAccessToken

func RefreshAccessToken(ctx context.Context, cfg *config.Config, token *Token) (*Token, error)

RefreshAccessToken exchanges a refresh token for a new access token.

func (*Token) IsExpired

func (t *Token) IsExpired() bool

IsExpired reports whether the token has expired (with a 30-second buffer).

func (*Token) SetExpiryFromExpiresIn

func (t *Token) SetExpiryFromExpiresIn()

SetExpiryFromExpiresIn computes the Expiry field from ExpiresIn (seconds).

Jump to

Keyboard shortcuts

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