dpop

package
v1.0.101-beta.0 Latest Latest
Warning

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

Go to latest
Published: Dec 2, 2026 License: MIT Imports: 29 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.

Documentation

Overview

Package dpop implements the DPoP protocol primitives used by OAuth token requests and Lark OpenAPI requests.

Index

Constants

View Source
const (
	ProofHeader            = "DPoP"
	Scheme                 = "DPoP"
	TokenType              = "DPoP"
	ClockSkewErrorCode     = 20153
	InvalidProofOAuthError = "invalid_dpop_proof"
)
View Source
const (
	LocalKeyProviderName   = "local-key"
	DeviceCertProviderName = "device-cert"
	AuthHelperProviderName = "auth-helper"
)
View Source
const (

	// KeyStoreUnavailableHint is safe to use before or after a Token Endpoint
	// exchange because it makes no claim about whether a request was sent.
	KeyStoreUnavailableHint = "" /* 253-byte string literal not displayed */
	// KeyStorePreExchangeUnavailableHint adds the stronger guarantee available
	// only when key storage fails before the Token Endpoint request.
	KeyStorePreExchangeUnavailableHint = "no OAuth token request was sent; " + KeyStoreUnavailableHint
	// KeyAccessUnavailableHint covers an already-bound token whose key cannot be
	// loaded or used to sign the protected resource request.
	KeyAccessUnavailableHint = "" /* 284-byte string literal not displayed */
)
View Source
const HeartbeatPath = "/accounts/status/heartbeat"

Variables

View Source
var ErrBindingMismatch = errors.New("DPoP key binding does not match stored token metadata")
View Source
var ErrInvalidProofResponse = errors.New("token endpoint returned " + InvalidProofOAuthError)

ErrInvalidProofResponse is a safe cause for typed errors derived from an InvalidProofOAuthError Token Endpoint response. It intentionally excludes the response description, token, proof, and key material.

View Source
var ErrKeyNotFound = errors.New("DPoP key not found")
View Source
var ErrProofUnavailable = errors.New("could not obtain the requested proof")

ErrProofUnavailable marks a failure before the token request was sent.

View Source
var ErrRepeatedInvalidProof = errors.New("token endpoint rejected three consecutive DPoP proofs")

ErrRepeatedInvalidProof marks three consecutive explicit proof rejections during new token issuance. Callers own the preferred-mode fallback and key rollback.

View Source
var ErrUnknownProofProvider = errors.New("unknown DPoP proof provider")

Functions

func IsClockRecoverySignal

func IsClockRecoverySignal(code int, oauthError string) bool

IsClockRecoverySignal reports whether a Token Endpoint response carries the complete server signal that permits a one-time DPoP clock correction. The OAuth error comparison is deliberately exact.

func IsEnterpriseProvider

func IsEnterpriseProvider(name string) bool

func PrepareTokenRequest

func PrepareTokenRequest(req *http.Request, key *Key, operation TokenOperation, body AssertionBody) (*http.Request, error)

PrepareTokenRequest signs even when the HTTP client has no DPoP transport. A DPoP transport re-signs each network attempt using the same provider.

func SynchronizeClock

func SynchronizeClock(ctx context.Context, httpClient *http.Client, brand core.LarkBrand, key *Key) error

SynchronizeClock obtains the Accounts server time before a DPoP token request. The calibrated clock is kept on the key; callers decide when the corresponding key metadata becomes part of their token transaction.

func WithBinding

func WithBinding(ctx context.Context, binding *Binding) context.Context

func WithTokenEndpointKey

func WithTokenEndpointKey(ctx context.Context, key *Key) context.Context

WithTokenEndpointKey marks an OAuth Token Endpoint request for proof generation at the network boundary. It carries signing capability only in memory and never serializes the private key.

Types

type AdjustableClock

type AdjustableClock struct {
	// contains filtered or unexported fields
}

AdjustableClock is safe for concurrent proof generation and clock recovery.

func NewClock

func NewClock(base Clock) *AdjustableClock

func NewClockWithState

func NewClockWithState(base Clock, state ClockState) *AdjustableClock

func (*AdjustableClock) Now

func (c *AdjustableClock) Now() time.Time

func (*AdjustableClock) RestoreState

func (c *AdjustableClock) RestoreState(state ClockState)

RestoreState replaces the persisted calibration without reading the system clock. Token records can use this to remain the atomic source of truth for a UAT and its proof clock.

func (*AdjustableClock) SetServerTime

func (c *AdjustableClock) SetServerTime(serverTime, localReceiveTime time.Time)

SetServerTime records serverTime-localReceiveTime as the shared DPoP clock offset used by proofs and DPoP-bound token lifetime calculations.

func (*AdjustableClock) State

func (c *AdjustableClock) State() ClockState

type AssertionBody

type AssertionBody func(assertion string) ([]byte, error)

AssertionBody encodes a grant with the ASI returned alongside its DPoP proof.

type Binding

type Binding struct {
	Token               string
	KeyID               string
	JKT                 string
	KeyStoreSecureLevel keysigner.SecurityLevel
	// RecoverToken replaces an unavailable Helper grant and returns the context for its request.
	RecoverToken func(ctx context.Context, cause error) (token string, binding *Binding, requestCtx context.Context, err error) `json:"-"`
	// contains filtered or unexported fields
}

Binding associates a token with its proof provider.

func BindingFromContext

func BindingFromContext(ctx context.Context) *Binding

func NewBinding

func NewBinding(token string, key *Key) (*Binding, error)

func RestoreBinding

func RestoreBinding(token, keyID, expectedJKT string, key *Key) (*Binding, error)

func (*Binding) Key

func (b *Binding) Key() *Key

func (*Binding) SignProof

func (b *Binding) SignProof(ctx context.Context, method, rawURL string) (string, error)

type Clock

type Clock interface {
	Now() time.Time
}

Clock supplies timestamps for DPoP proofs and DPoP-bound token lifetimes.

type ClockState

type ClockState struct {
	OffsetMillis   int64
	SyncedAtMillis int64
}

ClockState is the non-secret persistent state needed to restore a calibrated clock with its platform key.

type DPoPProofProvider

type DPoPProofProvider interface {
	GenerateDPoPKey(ctx context.Context, reusableID string) (*Key, bool, error)
	RestoreDPoPKey(ctx context.Context, id string) (*Key, error)
	DeleteDPoPKey(ctx context.Context, id string) error
	ProbeAndRecoverForReauthorization(context.Context) (bool, error)
	Name() string
}

DPoPProofProvider manages signing keys for one proof scheme. GenerateDPoPKey reports whether the caller must delete a new key if issuance fails.

func NewAuthHelperProvider

func NewAuthHelperProvider(client authhelper.IPCClient, identity authhelper.Identity) DPoPProofProvider

func NewDeviceCertProvider

func NewDeviceCertProvider(selector keysigner.DeviceCertSelector) DPoPProofProvider

NewDeviceCertProvider selects system identities using the Terminal policy.

func NewLocalKeyProvider

func NewLocalKeyProvider(store *KeyStore) DPoPProofProvider

type Key

type Key struct {
	// contains filtered or unexported fields
}

Key carries the signing capability and clock for DPoP requests.

func (*Key) AuthHelperEndpoint

func (k *Key) AuthHelperEndpoint() string

AuthHelperEndpoint returns the endpoint selected for this key.

func (*Key) Clock

func (k *Key) Clock() *AdjustableClock

func (*Key) Delete

func (k *Key) Delete(ctx context.Context) error

Delete removes a CLI-managed key and its metadata.

func (*Key) ID

func (k *Key) ID() string

func (*Key) MatchesKeyID

func (k *Key) MatchesKeyID(id string) bool

Device certificate signing uses Terminal policy instead of a stored key handle.

func (*Key) ProbeWritable

func (k *Key) ProbeWritable(ctx context.Context) error

ProbeWritable checks the signing identity used by this key before token refresh.

func (*Key) ProofProviderName

func (k *Key) ProofProviderName() string

ProofProviderName identifies the binding scheme independently of its native backend.

func (*Key) Provider

func (k *Key) Provider() string

Provider returns the signing backend name used in key metadata.

func (*Key) PublicJWK

func (k *Key) PublicJWK() (keysigner.PublicJWK, error)

func (*Key) Save

func (k *Key) Save(ctx context.Context) error

Save commits the key metadata, including its current clock state.

func (*Key) SecurityLevel

func (k *Key) SecurityLevel() keysigner.SecurityLevel

func (*Key) Thumbprint

func (k *Key) Thumbprint() (string, error)

Thumbprint computes the RFC 7638 SHA-256 JWK thumbprint.

func (*Key) UsesAuthHelper

func (k *Key) UsesAuthHelper(endpoint string, identity authhelper.Identity) bool

UsesAuthHelper reports whether the key and assertion can share one IPC request.

type KeyStore

type KeyStore struct {
	// contains filtered or unexported fields
}

KeyStore coordinates ordered signing backends with the CLI secret store. Metadata records contain no private keys. L3 keeps its file unlock secret in a separate keychain entry; the signer owns its encrypted private-key files.

func NewKeyStore

func NewKeyStore(kc keychain.KeychainAccess, signers ...keysigner.Signer) *KeyStore

NewKeyStore uses the supplied signers in order. With no arguments after kc, it tries platform signers first, followed by the software signer.

func (*KeyStore) Delete

func (s *KeyStore) Delete(ctx context.Context, id string) error

func (*KeyStore) Ensure

func (s *KeyStore) Ensure(ctx context.Context, id string) (*Key, error)

Ensure opens or creates the key for a stable non-secret identifier.

func (*KeyStore) Load

func (s *KeyStore) Load(ctx context.Context, id string) (*Key, error)

func (*KeyStore) ProbeAndRecoverForReauthorization

func (s *KeyStore) ProbeAndRecoverForReauthorization(ctx context.Context) (bool, error)

ProbeAndRecoverForReauthorization probes the store before explicit reauthorization and reports whether it deleted keys whose unlock secret was lost.

func (*KeyStore) ProbeWritable

func (s *KeyStore) ProbeWritable(ctx context.Context) error

ProbeWritable verifies both public-metadata persistence and the exact generate/sign/delete capability needed by a DPoP flow. It must run before a Token Endpoint request when DPoP is required.

type TokenOperation

type TokenOperation string
const (
	TokenAcquire TokenOperation = "acquire"
	TokenRefresh TokenOperation = "refresh"
)

type Transport

type Transport struct {
	Base http.RoundTripper
}

Transport signs access-token requests at the final request boundary. Place it inside retry transports so each attempt receives a fresh proof. Callers own destination policy and must clear DPoP headers and context whenever they remove Authorization; this transport does not restrict request origins.

func (*Transport) BaseRoundTripper

func (t *Transport) BaseRoundTripper() http.RoundTripper

func (*Transport) RoundTrip

func (t *Transport) RoundTrip(req *http.Request) (*http.Response, error)

func (*Transport) WithBaseRoundTripper

func (t *Transport) WithBaseRoundTripper(base http.RoundTripper) http.RoundTripper

Jump to

Keyboard shortcuts

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