Skip to main content

AdminApiKey

Struct AdminApiKey 

Source
pub struct AdminApiKey { /* private fields */ }
Expand description

Admin API key for JWT-based authentication.

Ghost Admin API keys have the format {id}:{hex_secret} where:

  • id is the key identifier placed in the JWT kid header
  • hex_secret is a hex-encoded byte string used as the HMAC-SHA256 signing key

§Security

Admin API keys grant write access to your Ghost installation. Never expose them in client-side code or public repositories.

§Example

use ghost_io_api::auth::admin::AdminApiKey;

let raw = "6748592f4b9b7700010f6564:b1b5b9c1d4e5f6a7b8c9d0e1f2a3b4c5d6e7f8a9b0c1d2e3f4a5b6c7d8e9f0a1";
let key = AdminApiKey::new(raw).unwrap();
assert_eq!(key.key_id(), "6748592f4b9b7700010f6564");

Implementations§

Source§

impl AdminApiKey

Source

pub fn new(key: impl Into<String>) -> Result<Self>

Creates a new Admin API key from the Ghost {id}:{hex_secret} format.

§Validation
  • Must contain exactly one : separator
  • id must be non-empty
  • hex_secret must be non-empty, contain only hex digits, and have an even length
§Errors

Returns GhostError::Auth if the key is malformed.

§Example
use ghost_io_api::auth::admin::AdminApiKey;

let key = AdminApiKey::new(
    "6748592f4b9b7700010f6564:b1b5b9c1d4e5f6a7b8c9d0e1f2a3b4c5d6e7f8a9b0c1d2e3f4a5b6c7d8e9f0a1"
).unwrap();
assert!(key.is_valid());

// Missing separator
assert!(AdminApiKey::new("noseparator").is_err());

// Non-hex secret
assert!(AdminApiKey::new("kid:xyz").is_err());
Source

pub fn key_id(&self) -> &str

Returns the key ID used as the JWT kid header value.

Source

pub fn is_valid(&self) -> bool

Returns true if the key is structurally valid.

Keys constructed via AdminApiKey::new always pass this check.

Source

pub fn generate_jwt(&self) -> Result<String>

Generates a signed HS256 JWT for use with the Ghost Admin API.

The token includes:

  • Header: {"alg":"HS256","kid":"<id>","typ":"JWT"}
  • Payload: {"exp":<now+300>,"iat":<now>}
  • Signature: HMAC-SHA256 over <header_b64>.<payload_b64>, keyed with the hex-decoded secret
§Errors

Returns GhostError::Auth if the system clock is before the Unix epoch or if the secret bytes are rejected by the HMAC implementation.

§Example
use ghost_io_api::auth::admin::AdminApiKey;

let key = AdminApiKey::new(
    "6748592f4b9b7700010f6564:b1b5b9c1d4e5f6a7b8c9d0e1f2a3b4c5d6e7f8a9b0c1d2e3f4a5b6c7d8e9f0a1"
).unwrap();
let token = key.generate_jwt().unwrap();

// JWT has three dot-separated parts
assert_eq!(token.split('.').count(), 3);

Trait Implementations§

Source§

impl AsRef<str> for AdminApiKey

Source§

fn as_ref(&self) -> &str

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl Clone for AdminApiKey

Source§

fn clone(&self) -> AdminApiKey

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 AdminApiKey

Source§

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

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

impl Display for AdminApiKey

Source§

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

Formats the key for display, masking the secret entirely.

§Example
use ghost_io_api::auth::admin::AdminApiKey;

let key = AdminApiKey::new(
    "6748592f4b9b7700010f6564:b1b5b9c1d4e5f6a7b8c9d0e1f2a3b4c5d6e7f8a9b0c1d2e3f4a5b6c7d8e9f0a1"
).unwrap();
let display = format!("{key}");
assert!(display.contains("6748592f4b9b7700010f6564"));
assert!(!display.contains("b1b5b9c1"));
Source§

impl Eq for AdminApiKey

Source§

impl PartialEq for AdminApiKey

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl StructuralPartialEq for AdminApiKey

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<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> 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> Same for T

Source§

type Output = T

Should always be Self
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 = Infallible

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