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:
idis the key identifier placed in the JWTkidheaderhex_secretis 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
impl AdminApiKey
Sourcepub fn new(key: impl Into<String>) -> Result<Self>
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 idmust be non-emptyhex_secretmust 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());Sourcepub fn is_valid(&self) -> bool
pub fn is_valid(&self) -> bool
Returns true if the key is structurally valid.
Keys constructed via AdminApiKey::new always pass this check.
Sourcepub fn generate_jwt(&self) -> Result<String>
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
impl AsRef<str> for AdminApiKey
Source§impl Clone for AdminApiKey
impl Clone for AdminApiKey
Source§fn clone(&self) -> AdminApiKey
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)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl Debug for AdminApiKey
impl Debug for AdminApiKey
Source§impl Display for AdminApiKey
impl Display for AdminApiKey
Source§fn fmt(&self, f: &mut Formatter<'_>) -> Result
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"));impl Eq for AdminApiKey
Source§impl PartialEq for AdminApiKey
impl PartialEq for AdminApiKey
Source§fn eq(&self, other: &AdminApiKey) -> bool
fn eq(&self, other: &AdminApiKey) -> bool
Tests for
self and other values to be equal, and is used by ==.impl StructuralPartialEq for AdminApiKey
Auto Trait Implementations§
impl Freeze for AdminApiKey
impl RefUnwindSafe for AdminApiKey
impl Send for AdminApiKey
impl Sync for AdminApiKey
impl Unpin for AdminApiKey
impl UnsafeUnpin for AdminApiKey
impl UnwindSafe for AdminApiKey
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
Compare self to
key and return true if they are equal.