oauthtoken

package
v1.34.0 Latest Latest
Warning

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

Go to latest
Published: Feb 23, 2026 License: MIT Imports: 10 Imported by: 0

README

Token Flows

App API Key & Customer Token Flow

Organizations support the concept of an app, a container for a set of predefined API Keys for use with organization websites, servers, and other applications. The reason that Apps exist is that each App can be managed and tracked individually. Resetting an App can expire all keys just for that App.

API Keys are used for accessing all data within an particular Organization and for logging in Customers for that organization. Only a subset of endpoints are available to API Keys and Permissions are respected by each endpoint. Logging a customer in involves issuing an Customer Token based on the Customer's login and is exclusively used for accessing a Customer's. Customer tokens do not work on endpoints that API Keys work and vice versa.

Customer Tokens are good for 1 hour. API Keys have no limit.

Organization Reference Token & Access Token Flow

Organizations maintain Reference tokens with the scopes, teams, and permissions for each user with access to the Organization. Reference Tokens are used to generate both the OAuth Access and Refresh Tokens.

Access and Refresh Tokens are generated either by a User supplying their Login and Organizational credentials. Access Tokens can also be generated by supplying a previously generated Refresh Token.

Access Tokens are good for 1 hour. Refresh Tokens are good forever.

Documentation

Index

Constants

View Source
const (
	// Internal Claims used to store Access Token claims
	Reference Type = "reference"

	// Special Username/Password OAuth grant key
	Api = "api"

	// Claims generated by Reference Claims
	Access = "access"

	// Customer is a Special Access Token generated from API Claim
	Customer = "customer"

	// Token issued using the Reference Claims, used only to request new Acess Claims
	Refresh = "refresh"
)
View Source
const (
	Algorithm = "HS256"
)

Variables

View Source
var (
	InvalidTokenType      = errors.New("Invalid token type")
	TokenOwnershipInvalid = errors.New("Token does not belong to this user")
	TokenRevoked          = errors.New("Token is revoked")
	TokenIsExpired        = jwt.TokenIsExpired
	TokenIsNotValidYet    = jwt.TokenIsNotValidYet
)

Functions

func IsAccess

func IsAccess(claims Claims) bool

func IsApi

func IsApi(claims Claims) bool

API Based Tokens

func IsCustomer

func IsCustomer(claims Claims) bool

func IsReference

func IsReference(claims Claims) bool

Reference Based Tokens

func IsRefresh

func IsRefresh(claims Claims) bool

func Query

Types

type AccessClaims

type AccessClaims struct {
	// Admin Dashboard Claims (for display)
	FirstName string `json:"firstName,omitempty"`
	LastName  string `json:"lastName,omitempty"`
}

type Claims

type Claims struct {
	jwt.Claims
	AccessClaims

	AppId            string   `json:"app,omitempty"`
	UserId           string   `json:"usr,omitempty"`
	OrganizationName string   `json:"org,omitempty"`
	Type             Type     `json:"typ,omitempty"`
	Scopes           []string `json:"scopes,omitempty"`
	Test             bool     `json:"tst,omitempty"`

	Permissions bit.Field `json:"bit,omitempty"`
}

Used for Refresh and Site Tokens

func (Claims) Clone

func (c Claims) Clone() jwt.Claimable

func (Claims) HasPermission

func (c Claims) HasPermission(mask bit.Mask) bool

func (Claims) JSON

func (c Claims) JSON() string

type Token

type Token struct {
	mixin.Model

	Claims Claims `json:"claims"`

	Name string `json:"name"`
	// In Hours
	AccessPeriod int64 `json:"accessPeriod"`
	Revoked      bool  `json:"revoked"`

	String string `json:"-" datastore:",noindex"`
}

func New

func New(db *datastore.Datastore) *Token

func (*Token) Decode

func (t *Token) Decode(str string, secret []byte) error

func (*Token) Defaults

func (t *Token) Defaults()

func (*Token) Encode

func (t *Token) Encode(secret []byte) (string, error)

func (*Token) Init

func (t *Token) Init(db *datastore.Datastore)

func (*Token) IsValid

func (t *Token) IsValid(nowUnix int64) error

func (*Token) IssueAccessToken

func (t *Token) IssueAccessToken(usrId string, secret []byte) (string, error)

Issues short term expiration token for site/cli/dashboard

func (*Token) IssueRefreshToken

func (t *Token) IssueRefreshToken(usrId string, secret []byte) (string, error)

func (Token) Kind

func (t Token) Kind() string

func (*Token) Load

func (t *Token) Load(properties []datastore.Property) error

func (*Token) Revoke

func (t *Token) Revoke()

func (*Token) Save

func (t *Token) Save() ([]datastore.Property, error)

type Type

type Type string

Jump to

Keyboard shortcuts

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