Skip to main content

TokenStore

Trait TokenStore 

Source
pub trait TokenStore: WasmCompatSend + WasmCompatSync {
    // Required methods
    fn load<'a>(
        &'a self,
        key: &'a str,
    ) -> WasmBoxedFuture<'a, Result<Option<String>, BoxError>>;
    fn save<'a>(
        &'a self,
        key: &'a str,
        record: &'a str,
    ) -> WasmBoxedFuture<'a, Result<(), BoxError>>;
    fn delete<'a>(
        &'a self,
        key: &'a str,
    ) -> WasmBoxedFuture<'a, Result<(), BoxError>>;

    // Provided method
    fn lock<'a>(
        &'a self,
        _key: &'a str,
    ) -> WasmBoxedFuture<'a, Result<StoreLock, BoxError>> { ... }
}
Expand description

Keeps the credentials a sign-in writes and refreshes, each a JSON text under a key the provider names, such as chatgpt or copilot.

An authenticator reads its record before every request that needs one, and saves it after a sign-in, a refresh, or an invalidated token. It never touches storage except through its store. Clones of one authenticator share a refresh lock, and a store shared with other processes holds the record against them while a refresh or sign-in reads and saves it (lock), so one refresh runs at a time and each request reads what the last one saved. When a refresh token turns out to be spent, the authenticator reads the store again and takes a fresh credential another client or process saved there, before asking for a new sign-in.

use rig_core::providers::chatgpt::auth::{AuthSource, Authenticator, DeviceCodeHandler, MemoryTokenStore};
use std::sync::Arc;

let store = Arc::new(MemoryTokenStore::default());
let auth = Authenticator::new(AuthSource::OAuth, store, DeviceCodeHandler::default(), true);

Required Methods§

Source

fn load<'a>( &'a self, key: &'a str, ) -> WasmBoxedFuture<'a, Result<Option<String>, BoxError>>

The record kept under key, or None when there is none. An error means the store could not answer, and fails the request.

Source

fn save<'a>( &'a self, key: &'a str, record: &'a str, ) -> WasmBoxedFuture<'a, Result<(), BoxError>>

Keep record under key, replacing any record there.

Source

fn delete<'a>( &'a self, key: &'a str, ) -> WasmBoxedFuture<'a, Result<(), BoxError>>

Forget the record under key; forgetting a missing record succeeds.

Provided Methods§

Source

fn lock<'a>( &'a self, _key: &'a str, ) -> WasmBoxedFuture<'a, Result<StoreLock, BoxError>>

Hold the record under key until the returned StoreLock is dropped, against every other holder of it, in this process or another (such as a refresh another rig runs at the same moment). The default holds nothing: a store no other process reads needs no lock beyond the authenticator’s own.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementations on Foreign Types§

Source§

impl<T: TokenStore + ?Sized> TokenStore for Arc<T>

Source§

fn load<'a>( &'a self, key: &'a str, ) -> WasmBoxedFuture<'a, Result<Option<String>, BoxError>>

Source§

fn save<'a>( &'a self, key: &'a str, record: &'a str, ) -> WasmBoxedFuture<'a, Result<(), BoxError>>

Source§

fn delete<'a>( &'a self, key: &'a str, ) -> WasmBoxedFuture<'a, Result<(), BoxError>>

Source§

fn lock<'a>( &'a self, key: &'a str, ) -> WasmBoxedFuture<'a, Result<StoreLock, BoxError>>

Implementors§

Source§

impl TokenStore for FileTokenStore

Available on non-target_family=wasm only.
Source§

impl TokenStore for MemoryTokenStore