Skip to main content

CredentialsToken

Struct CredentialsToken 

Source
pub struct CredentialsToken(/* private fields */);
Available on crate feature transport only.
Expand description

A credentials token: the shared secret two platforms authenticate each other with.

token: The credentials token for the other party to authenticate in your system. It should only contain printable non-whitespace ASCII characters, that is, characters with Unicode code points from the range of U+0021 up to and including U+007E.

This type exists so that a credentials token is hard to leak:

  • Debug and Display print Token(****ab12), never the secret. A token that ends up in a tracing span, a panic message or a serialised error is therefore not a disclosure.
  • PartialEq compares in constant time, so a server that looks a token up by comparing against known tokens does not leak its contents through timing.
  • The buffer is zeroised when the token is dropped.
  • There is no Serialize: a token reaches the wire only through CredentialsToken::to_header_value, or as the Credentials.token field of a credentials object, which is the one place the protocol puts it in a body.
use ocpi_kit::transport::CredentialsToken;

let token = CredentialsToken::new("example-token").unwrap();
assert_eq!(token.to_header_value(), "Token ZXhhbXBsZS10b2tlbg==");
assert_eq!(format!("{token:?}"), "Token(****oken)");

Spec: 2.3.0 §transport_and_format_authorization_header, §credentials_credentials_object

Implementations§

Source§

impl CredentialsToken

Source

pub const MAX_LEN: usize = 64

The maximum length the spec gives: string(64).

Source

pub fn new(value: impl Into<String>) -> Result<Self, InvalidToken>

Creates a token, enforcing the character set and length the spec gives.

§Errors

Returns InvalidToken if the value is empty, longer than 64 characters, or contains a character outside U+0021..=U+007E — which notably excludes the space.

Source

pub fn new_lenient(value: impl Into<String>) -> Self

Creates a token without enforcing anything, for values read off the wire.

Source

pub fn generate() -> Self

Generates a fresh random token.

Produces a hyphenated UUID v4, which is what the specification’s own examples use and what the vast majority of implementations do.

Source

pub fn expose_secret(&self) -> &str

The token in cleartext.

Named to be conspicuous at a call site: everything else about this type is designed to stop the secret escaping by accident.

Source

pub fn to_credentials_field(&self) -> OcpiString<64>

The token as the string(64) that goes into a Credentials object body.

Source

pub fn to_header_value(&self) -> String

The full Authorization header value, Base64-encoded as the spec requires.

After the literal ‘Token’, there SHALL be one space, followed by the ‘encoded token’. The encoded token is obtained by encoding the credentials token to an octet sequence with UTF-8 and then encoding that octet sequence with Base64 according to RFC 4648.

Source

pub fn to_header_value_unencoded(&self) -> String

The Authorization header value without Base64, for pre-2.2-d2 peers.

NOTE: Many OCPI 2.1.1 and 2.2 implementations do not Base64 encode the credentials token when including it in the ‘Authorization’ header. … Implementations that wish to be compatible with non-encoding 2.1.1 and 2.2 implementations have to choose the right way to parse and write authorization headers by either trial and error or configuration flags.

This crate chooses configuration flags: see Quirks::send_unencoded_token.

Source

pub fn parse_header( value: &str, accept_unencoded: bool, ) -> Result<Self, InvalidToken>

Parses an Authorization header value.

Both encodings are accepted: the value is Base64-decoded when that yields a valid token, and otherwise taken literally. accept_unencoded gates the fallback — leave it off for a peer that is known to encode properly, so that a mangled header is an error rather than a token nobody recognises.

§Errors

Returns InvalidToken if the value does not start with Token , or if what follows is neither valid Base64 of a token nor (when allowed) a bare token.

Source

pub fn is_conformant(&self) -> bool

Whether this value satisfies the character set and length the spec gives.

Source

pub fn hint(&self) -> String

A stable, non-reversible fingerprint, for logging and correlating without disclosure.

This is the last four characters of the token, which is what the redacted Debug shows. It is a debugging aid, not a secret-safe identifier for a short token.

Trait Implementations§

Source§

impl Clone for CredentialsToken

Source§

fn clone(&self) -> CredentialsToken

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for CredentialsToken

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Display for CredentialsToken

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Drop for CredentialsToken

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more
Source§

impl Eq for CredentialsToken

Source§

impl FromStr for CredentialsToken

Source§

type Err = InvalidToken

The associated error which can be returned from parsing.
Source§

fn from_str(s: &str) -> Result<Self, Self::Err>

Parses a string s to return a value of this type. Read more
Source§

impl PartialEq for CredentialsToken

Source§

fn eq(&self, other: &Self) -> bool

Constant-time comparison: a server resolving a token must not leak it through timing.

1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl Validate for CredentialsToken

Source§

fn validate_in(&self, v: &mut Validator)

Appends this value’s violations to v, relative to v’s current position.
Source§

fn validate(&self) -> Result<(), Violations>

Validates this value as the root of an object graph. Read more
Source§

impl Zeroize for CredentialsToken

Source§

fn zeroize(&mut self)

Zero out this object from memory using Rust intrinsics which ensure the zeroization operation is not “optimized away” by the compiler.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DynClone for T
where T: Clone,

Source§

fn __clone_box(&self, _: Private) -> *mut ()

Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> FromRef<T> for T
where T: Clone,

Source§

fn from_ref(input: &T) -> T

Converts to this type from a reference to the input type.
Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more