token

package
v0.4.1 Latest Latest
Warning

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

Go to latest
Published: Dec 6, 2026 License: Apache-2.0 Imports: 5 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

token

Package token generates, parses, and hashes the opaque bearer tokens of sessions: a brand, a kind, an underscore, and 49 base62 characters that end in a checksum, for example tss_…. Only a token's SHA-256 digest is stored.

The package documentation is in token.go. To read it with the API, run go doc ./internal/token.

Contents

Name What it holds
token.go The package and its documentation

The tests are in the *_test.go files next to the code.

See also

Documentation

Overview

Package token generates, parses and hashes opaque bearer tokens.

A token looks like "tss_" followed by 49 base62 characters:

<brand><kind>_<43 random chars><6 checksum chars>

The 43 random characters carry about 256 bits of entropy from crypto/rand. The checksum is CRC32-IEEE over everything before it, encoded as 6 base62 characters. It lets Parse reject typos and garbage before any database lookup, and lets secret scanners match leaked tokens with almost no false positives. It is not a security control.

Only the SHA-256 digest of a token is ever stored.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrMalformed = errors.New("token: malformed")
	ErrBrand     = errors.New("token: invalid brand")
)

Errors returned by this package.

Functions

func Generate

func Generate(r io.Reader, brand string, kind Kind) (string, error)

Generate returns a new token of the given kind, reading randomness from r. The brand must already be valid (see ValidateBrand).

func Hash

func Hash(s string) [32]byte

Hash returns the SHA-256 digest of the full token string. This digest is the only form of a token that may be stored.

func Len

func Len(brand string) int

Len returns the total length of tokens with the given brand.

func ValidateBrand

func ValidateBrand(brand string) error

ValidateBrand reports whether brand is 2 to 6 lowercase ASCII letters.

Types

type Kind

type Kind byte

Kind identifies what a token authenticates. It is the single character between the brand and the underscore.

const (
	KindHumanSession Kind = 's'
	KindAgentSession Kind = 'a'
	// KindClaimCode ('k', for claim key) is a single-use code that lets a
	// human claim an agent that signed itself up. It authenticates no
	// session.
	KindClaimCode    Kind = 'k'
	KindPAT          Kind = 'p' // personal access token
	KindOAuthAccess  Kind = 'o' // OAuth access token
	KindOAuthRefresh Kind = 'r' // OAuth refresh token
	KindOAuthCode    Kind = 'c' // OAuth authorization code
	KindDeviceCode   Kind = 'd' // OAuth device code
	// KindPendingSignIn ('i', for in progress) is a sign-in waiting for
	// another factor. It authenticates no session.
	KindPendingSignIn Kind = 'i'
	// KindEmailCode ('e') is a single-use code sent by email, to confirm
	// an address or reset a password. It authenticates no session.
	KindEmailCode Kind = 'e'
	// KindKnownDevice ('n', for known) marks a browser that signed in to
	// an account with its password, so its attempts get their own limit.
	// It authenticates no session.
	KindKnownDevice Kind = 'n'
	// KindAppKey ('x') is a tensile-auth server's app key: it lets an
	// app's backend call the server's /v1 API. It authenticates no
	// session. 'k' was taken by claim codes.
	KindAppKey Kind = 'x'
)

Token kinds. The library fixes the kind; only the brand is configurable.

func Parse

func Parse(s, brand string) (Kind, error)

Parse checks the prefix, length, alphabet and checksum of s and returns its kind. It does not tell whether the token exists.

Jump to

Keyboard shortcuts

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