use crate::errors::{AuthError, Result};
use crate::storage::core::AuthStorage;
use crate::storage::memory::InMemoryStorage;
use serde_json::{Value, json};
use std::sync::Arc;
pub use crate::client::{ClientConfig, ClientType};
const RECORD_PREFIX: &str = "oauth2_client:";
const LEGACY_RECORD_PREFIX: &str = "oauth_client:";
fn strings(record: &Value, field: &str) -> Option<Vec<String>> {
record.get(field).and_then(Value::as_array).map(|items| {
items
.iter()
.filter_map(|item| item.as_str().map(String::from))
.collect()
})
}
async fn config_to_record(config: &ClientConfig) -> Result<Value> {
let mut record = json!({
"client_id": config.client_id,
"client_type": match config.client_type {
ClientType::Confidential => "confidential",
ClientType::Public => "public",
},
"client_name": config.client_name,
"client_description": config.client_description,
"redirect_uris": config.redirect_uris.iter().collect::<Vec<_>>(),
"grant_types": config.authorized_grant_types.iter().collect::<Vec<_>>(),
"response_types": config.authorized_response_types.iter().collect::<Vec<_>>(),
"scope": config.authorized_scopes.iter().cloned().collect::<Vec<_>>().join(" "),
"metadata": config.metadata,
});
if let Some(secret) = &config.client_secret {
record["client_secret_hash"] =
Value::String(crate::client_secret::hash_secret(secret).await?);
}
Ok(record)
}
fn record_to_config(record: &Value) -> Result<ClientConfig> {
let client_id = record
.get("client_id")
.and_then(Value::as_str)
.ok_or_else(|| AuthError::internal("Stored client has no client_id"))?
.to_string();
let client_type = match record.get("client_type").and_then(Value::as_str) {
Some(kind) if kind.eq_ignore_ascii_case("confidential") => ClientType::Confidential,
Some(kind) if kind.eq_ignore_ascii_case("public") => ClientType::Public,
_ if crate::client_secret::is_confidential(record) => ClientType::Confidential,
_ => ClientType::Public,
};
let scopes = strings(record, "authorized_scopes").unwrap_or_else(|| {
record
.get("scope")
.and_then(Value::as_str)
.map(|scope| scope.split_whitespace().map(String::from).collect())
.unwrap_or_default()
});
Ok(ClientConfig {
client_id,
client_secret: None,
client_type,
redirect_uris: strings(record, "redirect_uris").unwrap_or_default().into(),
authorized_scopes: scopes.into(),
authorized_grant_types: strings(record, "authorized_grant_types")
.or_else(|| strings(record, "grant_types"))
.unwrap_or_else(|| vec!["authorization_code".to_string()])
.into(),
authorized_response_types: strings(record, "authorized_response_types")
.or_else(|| strings(record, "response_types"))
.unwrap_or_else(|| vec!["code".to_string()])
.into(),
client_name: record
.get("client_name")
.and_then(Value::as_str)
.map(String::from),
client_description: record
.get("client_description")
.and_then(Value::as_str)
.map(String::from),
metadata: record
.get("metadata")
.and_then(Value::as_object)
.map(|fields| fields.clone().into_iter().collect())
.unwrap_or_default(),
})
}
fn parse_record(data: &[u8]) -> Result<Value> {
let text = std::str::from_utf8(data)
.map_err(|e| AuthError::internal(format!("Invalid UTF-8 in client data: {}", e)))?;
serde_json::from_str(text)
.map_err(|e| AuthError::internal(format!("Failed to deserialize client: {}", e)))
}
const CLEANUP_MARKER: &str = "legacy_record_pending_removal";
async fn remove_legacy_record(storage: &dyn AuthStorage, legacy_key: &str) -> Result<()> {
let Err(delete_error) = storage.delete_kv(legacy_key).await else {
return Ok(());
};
if let Ok(Some(data)) = storage.get_kv(legacy_key).await
&& let Ok(mut old) = parse_record(&data)
&& let Some(fields) = old.as_object_mut()
{
fields.remove("client_secret");
if let Err(e) = storage
.store_kv(legacy_key, old.to_string().as_bytes(), None)
.await
{
tracing::error!("Could not remove the secret from the old client record: {e}");
}
}
Err(delete_error)
}
async fn finish_migration(storage: &dyn AuthStorage, client_id: &str, record: &mut Value) {
match remove_legacy_record(storage, &format!("{LEGACY_RECORD_PREFIX}{client_id}")).await {
Ok(()) => {
if let Some(fields) = record.as_object_mut() {
fields.remove(CLEANUP_MARKER);
}
if let Err(e) = storage
.store_kv(
&format!("{RECORD_PREFIX}{client_id}"),
record.to_string().as_bytes(),
None,
)
.await
{
tracing::warn!("Could not clear the migration marker of client {client_id}: {e}");
}
}
Err(e) => tracing::error!(
"Could not remove the old record of client {client_id}, it will be retried: {e}"
),
}
}
pub(crate) async fn load_record(
storage: &dyn AuthStorage,
client_id: &str,
) -> Result<Option<Value>> {
if let Some(data) = storage
.get_kv(&format!("{RECORD_PREFIX}{client_id}"))
.await?
{
let mut record = parse_record(&data)?;
if record.get(CLEANUP_MARKER).is_some() {
finish_migration(storage, client_id, &mut record).await;
}
return Ok(Some(record));
}
let legacy_key = format!("{LEGACY_RECORD_PREFIX}{client_id}");
let Some(data) = storage.get_kv(&legacy_key).await? else {
return Ok(None);
};
let legacy = parse_record(&data)?;
let mut config = record_to_config(&legacy)?;
config.client_secret = legacy
.get("client_secret")
.and_then(Value::as_str)
.filter(|secret| !secret.is_empty())
.map(String::from);
let mut record = config_to_record(&config).await?;
record[CLEANUP_MARKER] = Value::Bool(true);
storage
.store_kv(
&format!("{RECORD_PREFIX}{client_id}"),
record.to_string().as_bytes(),
None,
)
.await?;
finish_migration(storage, client_id, &mut record).await;
Ok(Some(record))
}
#[derive(Clone)]
pub struct ClientRegistry {
storage: Arc<dyn AuthStorage>,
}
impl ClientRegistry {
pub async fn new(storage: Arc<dyn AuthStorage>) -> Result<Self> {
Ok(Self { storage })
}
pub async fn register_client(&self, config: ClientConfig) -> Result<ClientConfig> {
self.validate_client_config(&config)?;
let record = config_to_record(&config).await?;
self.storage
.store_kv(
&format!("{RECORD_PREFIX}{}", config.client_id),
record.to_string().as_bytes(),
None,
)
.await?;
Ok(config)
}
pub async fn get_client(&self, client_id: &str) -> Result<Option<ClientConfig>> {
match load_record(self.storage.as_ref(), client_id).await? {
Some(record) => record_to_config(&record).map(Some),
None => Ok(None),
}
}
pub async fn update_client(&self, client_id: &str, config: ClientConfig) -> Result<()> {
if config.client_id != client_id {
return Err(AuthError::validation("Client ID mismatch"));
}
self.validate_client_config(&config)?;
let record = config_to_record(&config).await?;
self.storage
.store_kv(
&format!("{RECORD_PREFIX}{client_id}"),
record.to_string().as_bytes(),
None,
)
.await?;
Ok(())
}
pub async fn delete_client(&self, client_id: &str) -> Result<()> {
self.storage
.delete_kv(&format!("{RECORD_PREFIX}{client_id}"))
.await?;
self.storage
.delete_kv(&format!("{LEGACY_RECORD_PREFIX}{client_id}"))
.await?;
Ok(())
}
pub async fn validate_redirect_uri(&self, client_id: &str, redirect_uri: &str) -> Result<bool> {
if let Some(client) = self.get_client(client_id).await? {
Ok(client.redirect_uris.contains(redirect_uri))
} else {
Ok(false)
}
}
pub async fn validate_scope(&self, client_id: &str, scope: &str) -> Result<bool> {
if let Some(client) = self.get_client(client_id).await? {
Ok(client.authorized_scopes.contains(scope))
} else {
Ok(false)
}
}
pub async fn validate_grant_type(&self, client_id: &str, grant_type: &str) -> Result<bool> {
if let Some(client) = self.get_client(client_id).await? {
Ok(client.authorized_grant_types.contains(grant_type))
} else {
Ok(false)
}
}
pub async fn authenticate_client(&self, client_id: &str, client_secret: &str) -> Result<bool> {
let Some(record) = load_record(self.storage.as_ref(), client_id).await? else {
return Ok(false);
};
if crate::client_secret::is_confidential(&record) {
Ok(crate::client_secret::authenticate(
self.storage.as_ref(),
client_id,
&record,
client_secret,
)
.await)
} else {
Ok(true)
}
}
fn validate_client_config(&self, config: &ClientConfig) -> Result<()> {
if config.client_id.is_empty() {
return Err(AuthError::validation("Client ID cannot be empty"));
}
if config.client_type == ClientType::Confidential && config.client_secret.is_none() {
return Err(AuthError::validation(
"Confidential clients must have a client secret",
));
}
if config.client_type == ClientType::Public && config.client_secret.is_some() {
return Err(AuthError::validation(
"Public clients must not have a client secret",
));
}
if config.redirect_uris.is_empty() {
return Err(AuthError::validation(
"At least one redirect URI must be provided",
));
}
for uri in &config.redirect_uris {
if uri.is_empty() {
return Err(AuthError::validation("Redirect URI cannot be empty"));
}
if !uri.starts_with("https://") && !uri.starts_with("http://localhost") {
return Err(AuthError::validation(
"Redirect URIs must use HTTPS (except localhost)",
));
}
}
if config.authorized_scopes.is_empty() {
return Err(AuthError::validation(
"At least one authorized scope must be provided",
));
}
if config.authorized_grant_types.is_empty() {
return Err(AuthError::validation(
"At least one authorized grant type must be provided",
));
}
if config.authorized_response_types.is_empty() {
return Err(AuthError::validation(
"At least one authorized response type must be provided",
));
}
Ok(())
}
}
impl Default for ClientRegistry {
fn default() -> Self {
let storage =
if std::env::var("CLIENT_REGISTRY_STORAGE").unwrap_or_default() == "persistent" {
Arc::new(InMemoryStorage::new())
} else {
Arc::new(InMemoryStorage::new())
};
Self { storage }
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::storage::memory::InMemoryStorage;
#[tokio::test]
async fn test_client_registry_operations() {
let storage = Arc::new(InMemoryStorage::new());
let registry = ClientRegistry::new(storage).await.unwrap();
let client_config = ClientConfig {
client_id: "test_client".to_string(),
client_type: ClientType::Confidential,
client_secret: Some("test_secret".to_string()),
redirect_uris: vec!["https://example.com/callback".to_string()].into(),
..Default::default()
};
let registered_client = registry
.register_client(client_config.clone())
.await
.unwrap();
assert_eq!(registered_client.client_id, "test_client");
let retrieved_client = registry.get_client("test_client").await.unwrap().unwrap();
assert_eq!(retrieved_client.client_id, "test_client");
assert_eq!(retrieved_client.client_type, ClientType::Confidential);
let auth_result = registry
.authenticate_client("test_client", "test_secret")
.await
.unwrap();
assert!(auth_result);
let auth_fail = registry
.authenticate_client("test_client", "wrong_secret")
.await
.unwrap();
assert!(!auth_fail);
let valid_uri = registry
.validate_redirect_uri("test_client", "https://example.com/callback")
.await
.unwrap();
assert!(valid_uri);
let invalid_uri = registry
.validate_redirect_uri("test_client", "https://malicious.com/callback")
.await
.unwrap();
assert!(!invalid_uri);
registry.delete_client("test_client").await.unwrap();
let deleted_client = registry.get_client("test_client").await.unwrap();
assert!(deleted_client.is_none());
}
#[tokio::test]
async fn test_client_validation() {
let storage = Arc::new(InMemoryStorage::new());
let registry = ClientRegistry::new(storage).await.unwrap();
let invalid_config = ClientConfig {
client_id: "".to_string(),
..Default::default()
};
assert!(registry.register_client(invalid_config).await.is_err());
let invalid_config = ClientConfig {
client_type: ClientType::Confidential,
client_secret: None,
..Default::default()
};
assert!(registry.register_client(invalid_config).await.is_err());
let invalid_config = ClientConfig {
client_type: ClientType::Public,
client_secret: Some("secret".to_string()),
..Default::default()
};
assert!(registry.register_client(invalid_config).await.is_err());
let invalid_config = ClientConfig {
redirect_uris: vec![].into(),
..Default::default()
};
assert!(registry.register_client(invalid_config).await.is_err());
let invalid_config = ClientConfig {
redirect_uris: vec!["http://example.com/callback".to_string()].into(),
..Default::default()
};
assert!(registry.register_client(invalid_config).await.is_err());
}
async fn raw(storage: &Arc<InMemoryStorage>, key: &str) -> Option<String> {
storage
.get_kv(key)
.await
.unwrap()
.map(|data| String::from_utf8(data).unwrap())
}
#[tokio::test]
async fn new_clients_are_stored_under_the_unified_key_with_a_hashed_secret() {
let storage = Arc::new(InMemoryStorage::new());
let registry = ClientRegistry::new(storage.clone()).await.unwrap();
registry
.register_client(ClientConfig {
client_id: "unified".to_string(),
client_type: ClientType::Confidential,
client_secret: Some("plain-secret-value".to_string()),
redirect_uris: vec!["https://example.com/callback".to_string()].into(),
..Default::default()
})
.await
.unwrap();
let stored = raw(&storage, "oauth2_client:unified").await.unwrap();
assert!(stored.contains("client_secret_hash"), "{stored}");
assert!(!stored.contains("plain-secret-value"), "{stored}");
assert!(raw(&storage, "oauth_client:unified").await.is_none());
let client = registry.get_client("unified").await.unwrap().unwrap();
assert_eq!(client.client_type, ClientType::Confidential);
assert!(
client.client_secret.is_none(),
"the secret is not recoverable"
);
assert!(
registry
.authenticate_client("unified", "plain-secret-value")
.await
.unwrap()
);
assert!(
!registry
.authenticate_client("unified", "wrong")
.await
.unwrap()
);
}
#[tokio::test]
async fn a_record_under_the_old_key_is_still_readable_and_moves_on_first_read() {
let storage = Arc::new(InMemoryStorage::new());
let old = ClientConfig {
client_id: "legacy".to_string(),
client_type: ClientType::Confidential,
client_secret: Some("legacy-plain-secret".to_string()),
redirect_uris: vec!["https://example.com/callback".to_string()].into(),
client_name: Some("Legacy app".to_string()),
..Default::default()
};
storage
.store_kv(
"oauth_client:legacy",
serde_json::to_string(&old).unwrap().as_bytes(),
None,
)
.await
.unwrap();
let registry = ClientRegistry::new(storage.clone()).await.unwrap();
assert!(
raw(&storage, "oauth_client:legacy")
.await
.unwrap()
.contains("legacy-plain-secret")
);
let client = registry.get_client("legacy").await.unwrap().unwrap();
assert_eq!(client.client_name.as_deref(), Some("Legacy app"));
assert_eq!(client.client_type, ClientType::Confidential);
assert!(
client
.redirect_uris
.contains("https://example.com/callback")
);
assert!(
raw(&storage, "oauth_client:legacy").await.is_none(),
"old record removed"
);
let moved = raw(&storage, "oauth2_client:legacy").await.unwrap();
assert!(moved.contains("client_secret_hash") && !moved.contains("legacy-plain-secret"));
assert!(
registry
.authenticate_client("legacy", "legacy-plain-secret")
.await
.unwrap()
);
assert!(
!registry
.authenticate_client("legacy", "nope")
.await
.unwrap()
);
}
#[tokio::test]
async fn a_record_written_by_the_http_api_is_readable() {
let storage = Arc::new(InMemoryStorage::new());
let hash = crate::client_secret::hash_secret("http-secret")
.await
.unwrap();
storage
.store_kv(
"oauth2_client:http-app",
serde_json::json!({
"client_id": "http-app",
"client_secret_hash": hash,
"client_name": "HTTP app",
"redirect_uris": ["https://example.com/cb"],
"grant_types": ["authorization_code", "refresh_token"],
"response_types": ["code"],
"scope": "openid profile",
})
.to_string()
.as_bytes(),
None,
)
.await
.unwrap();
let registry = ClientRegistry::new(storage).await.unwrap();
let client = registry.get_client("http-app").await.unwrap().unwrap();
assert_eq!(client.client_type, ClientType::Confidential);
assert!(client.authorized_scopes.contains("profile"));
assert!(client.authorized_grant_types.contains("refresh_token"));
assert!(
registry
.authenticate_client("http-app", "http-secret")
.await
.unwrap()
);
assert!(
registry
.validate_redirect_uri("http-app", "https://example.com/cb")
.await
.unwrap()
);
}
#[tokio::test]
async fn deleting_a_client_removes_both_keys() {
let storage = Arc::new(InMemoryStorage::new());
for key in ["oauth2_client:gone", "oauth_client:gone"] {
storage
.store_kv(key, br#"{"client_id":"gone"}"#, None)
.await
.unwrap();
}
let registry = ClientRegistry::new(storage.clone()).await.unwrap();
registry.delete_client("gone").await.unwrap();
assert!(raw(&storage, "oauth2_client:gone").await.is_none());
assert!(raw(&storage, "oauth_client:gone").await.is_none());
}
}