API リファレンスはソースコード内の Go ドキュメントコメントから生成されており、英語で記述されています。
package authtoken
import "github.com/nsgo-dev/game/pkg/online/accounts/authtoken"概要
Package authtoken issues and verifies the short-lived player tokens of the accounts service.
A token is a compact JWT (RFC 7519) signed with Ed25519 ("alg": "EdDSA"). The account service holds the private key and issues tokens with a Signer; a game server holds only the public keys, as a KeySet loaded from a YAML file, and checks tokens with a Verifier. Verifying makes no network call: it decodes the token, checks the signature against a known key, then the issuer, audience and expiry.
The audience names the one consumer a token is for. A key set's audience is the game's servers, which a player sends a token to when joining a match; a service that acts on the player's behalf, such as cloud save or a leaderboard, verifies tokens for its own audience with NewVerifierFor, so a token handed to a game server cannot be replayed against it.
The package depends on the standard library and go.yaml.in/yaml/v3 only, so a game server that verifies tokens links none of the account service's password hashing or database drivers.
目次
- 定数
- 変数
- func GenerateKey(r io.Reader) (ed25519.PrivateKey, error)
- func KeyID(pub ed25519.PublicKey) string
- func MarshalPrivateKey(key ed25519.PrivateKey) ([]byte, error)
- func ParsePrivateKey(data []byte) (ed25519.PrivateKey, error)
- type Claims
- type KeySet
- type PublicKey
- type Signer
- func NewSigner(key ed25519.PrivateKey, issuer, audience string, ttl time.Duration) (*Signer, error)
- func (s *Signer) Issue(subject string) (string, Claims, error)
- func (s *Signer) IssueFor(subject, audience string) (string, Claims, error)
- func (s *Signer) KeyID() string
- func (s *Signer) KeySet() *KeySet
- func (s *Signer) TTL() time.Duration
- type Verifier
定数
const AccountServiceAudience = "accounts"AccountServiceAudience is the audience of tokens for the account service's own bearer endpoints (accounts.ServiceAudience). Another service must not accept it, or whoever runs that service could replay a player's token to read or link the player's account.
const KeySetVersion = 1KeySetVersion is the key set file schema version this package reads and writes.
const MaxLength = 4096MaxLength is the longest token Verify decodes; a longer string is refused before any work.
変数
var (
// ErrMalformed reports a token that is not a well-formed token of this package.
ErrMalformed = errors.New("authtoken: malformed")
// ErrUnknownKey reports a token signed with a key ID the verifier does not hold.
ErrUnknownKey = errors.New("authtoken: unknown signing key")
// ErrSignature reports a token whose signature does not match its contents.
ErrSignature = errors.New("authtoken: bad signature")
// ErrIssuer reports a token from another issuer.
ErrIssuer = errors.New("authtoken: wrong issuer")
// ErrAudience reports a token meant for another audience.
ErrAudience = errors.New("authtoken: wrong audience")
// ErrExpired reports a token past its expiry.
ErrExpired = errors.New("authtoken: expired")
// ErrNotYetValid reports a token issued after the verifier's current time.
ErrNotYetValid = errors.New("authtoken: not yet valid")
)関数
func GenerateKey(r io.Reader) (ed25519.PrivateKey, error)GenerateKey returns a new Ed25519 signing key read from r, or crypto/rand when r is nil.
func KeyID(pub ed25519.PublicKey) stringKeyID returns the ID of a public key: the first 8 bytes of its SHA-256 in hex. Signers and key sets derive IDs this way, so an ID always names one key.
func MarshalPrivateKey(key ed25519.PrivateKey) ([]byte, error)MarshalPrivateKey encodes key as a PKCS #8 "PRIVATE KEY" PEM block, the text form the account service keeps its signing key in.
func ParsePrivateKey(data []byte) (ed25519.PrivateKey, error)ParsePrivateKey decodes a PKCS #8 PEM block holding an Ed25519 private key. Errors never include the key material.
型
type Claims struct {
// Issuer is the account service that issued the token.
Issuer string
// Subject is the account ID the token stands for.
Subject string
// Audience is the consumer the token is meant for: the game's servers (the key set's audience) or a service such as cloud save.
Audience string
// IssuedAt is when the token was issued, to the second.
IssuedAt time.Time
// Expires is when the token stops being valid, to the second.
Expires time.Time
// ID is the token's unique random ID.
ID string
// KeyID is the ID of the key that signed the token.
KeyID string
}Claims are what a verified token says.
type KeySet struct {
// Version is the schema version; it must be KeySetVersion.
Version int `yaml:"version"`
// Issuer is the issuer the verifier accepts.
Issuer string `yaml:"issuer"`
// Audience is the audience the verifier accepts.
Audience string `yaml:"audience"`
// Keys are the public keys tokens may be signed with.
Keys []PublicKey `yaml:"keys"`
}KeySet is the public half of an account service's signing keys, kept in a YAML file a game server loads to verify tokens offline:
version: 1
issuer: https://accounts.example.com
audience: my-game
keys:
- id: 2317febd373fcbcd
key: hze+0cYruVRddgjkbHzgCeWWJbEsBkn6K8JNHRDf6Wg=
Several keys let a service rotate its signing key: publish the new key beside the old one, switch the service to it, and drop the old key once its last tokens have expired.
func ParseKeySet(data []byte) (*KeySet, error)ParseKeySet decodes and validates a YAML key set. Unknown fields and trailing documents are refused.
func (ks *KeySet) Marshal() ([]byte, error)Marshal validates ks and encodes it as deterministic 2-space YAML, keys in their order.
func (ks *KeySet) Merge(other *KeySet) (*KeySet, error)Merge returns a key set with the keys of ks followed by those of other it lacks. Both must have the same issuer and audience.
func (ks *KeySet) Validate() errorValidate checks that ks is a version 1 key set with an issuer, an audience and at least one well-formed key, each under its own KeyID once.
type PublicKey struct {
// ID is KeyID of the key; it must match the key.
ID string `yaml:"id"`
// Key is the 32-byte public key in standard base64.
Key string `yaml:"key"`
}PublicKey is one Ed25519 public key of a key set.
type Signer struct {
// Now is the clock tokens are issued by; nil uses time.Now.
Now func() time.Time
// Rand fills token IDs; nil uses crypto/rand. Issue reads it without a lock, so a signer used from several goroutines needs a reader that is safe for concurrent use.
Rand io.Reader
// contains filtered or unexported fields
}Signer issues tokens with one private key. Its fields other than Now are fixed by NewSigner.
func NewSigner(key ed25519.PrivateKey, issuer, audience string, ttl time.Duration) (*Signer, error)NewSigner returns a signer that issues tokens for audience from issuer, each valid for ttl (at least one second, in whole seconds).
func (s *Signer) Issue(subject string) (string, Claims, error)Issue returns a token for subject and the signer's audience, valid from now for the signer's TTL, and its claims.
func (s *Signer) IssueFor(subject, audience string) (string, Claims, error)IssueFor returns a token for subject meant for audience instead of the signer's own, valid from now for the signer's TTL, and its claims. Only a verifier built for that audience (NewVerifierFor) accepts it.
func (s *Signer) KeySet() *KeySetKeySet returns the public key set that verifies the signer's tokens.
func (s *Signer) TTL() time.DurationTTL is how long the signer's tokens stay valid.
type Verifier struct {
// Leeway tolerates clock skew between the account service and the verifier: a token stays valid until Leeway past its expiry and is accepted up to Leeway before its issue time.
Leeway time.Duration
// Now is the clock tokens are checked against; nil uses time.Now.
Now func() time.Time
// contains filtered or unexported fields
}Verifier checks tokens offline against a key set. Build one with NewVerifier; it is safe for concurrent use.
func NewVerifier(ks *KeySet) (*Verifier, error)NewVerifier returns a verifier for the keys, issuer and audience of ks: the one a game server uses.
func NewVerifierFor(ks *KeySet, audience string) (*Verifier, error)NewVerifierFor returns a verifier for the keys and issuer of ks that accepts only tokens for audience, such as the one a cloud save service verifies. It refuses tokens for the key set's own audience unless audience is that one.
func (v *Verifier) AcceptsGameServerTokens() boolAcceptsGameServerTokens reports whether the verifier accepts tokens for the audience of the key set it was built from: the tokens sign-in returns and a player sends every game server it joins. A service that acts on a player's own data, such as cloud save, must refuse such a verifier, however its audience was named.
func (v *Verifier) Audience() stringAudience is the audience the verifier accepts.
func (v *Verifier) Verify(tok string) (Claims, error)Verify checks tok and returns its claims. Errors wrap ErrMalformed, ErrUnknownKey, ErrSignature, ErrIssuer, ErrAudience, ErrExpired or ErrNotYetValid.