use std::collections::BTreeMap;
use std::time::Duration;
use argon2::Argon2;
use argon2::password_hash::{PasswordHash, PasswordHasher, PasswordVerifier, SaltString};
use async_trait::async_trait;
use jsonwebtoken::{Algorithm, DecodingKey, EncodingKey, Header, Validation};
use serde::{Deserialize, Serialize};
use super::{AuthError, Authenticator, Principal, Scopes, TokenError};
use crate::model::{
ClientCredentialRequest, ClientCredentialResponse, ClientId, OAuthError, OAuthErrorKind,
};
fn default_verifier_permits() -> usize {
std::thread::available_parallelism()
.map(|n| n.get())
.unwrap_or(2)
.clamp(1, 8)
}
const DECOY_HASH: &str = "$argon2id$v=19$m=19456,t=2,p=1$c29tZXNhbHRzb21lc2FsdA$\
8VYq7cA8+9zdChZk3ZKGZBTHDVKa0MEZGpTKMNL0d6E";
#[derive(Debug, Clone)]
struct Registered {
secret_hash: String,
scopes: Scopes,
}
#[derive(Debug, Serialize, Deserialize)]
struct Claims {
iss: String,
sub: String,
aud: String,
exp: i64,
iat: i64,
#[serde(default, skip_serializing_if = "String::is_empty")]
scope: String,
}
pub struct InternalAuth {
clients: BTreeMap<String, Registered>,
encoding: EncodingKey,
decoding: DecodingKey,
issuer: String,
token_url: String,
ttl: Duration,
allow_anonymous: bool,
clock: std::sync::Arc<dyn crate::core::Clock>,
verifiers: std::sync::Arc<tokio::sync::Semaphore>,
}
impl std::fmt::Debug for InternalAuth {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("InternalAuth")
.field("issuer", &self.issuer)
.field("clients", &self.clients.len())
.field("ttl", &self.ttl)
.finish_non_exhaustive()
}
}
pub struct InternalAuthBuilder {
clients: BTreeMap<String, Registered>,
signing_key: Option<Vec<u8>>,
issuer: String,
token_url: String,
ttl: Duration,
allow_anonymous: bool,
clock: std::sync::Arc<dyn crate::core::Clock>,
max_concurrent_verifications: usize,
}
impl std::fmt::Debug for InternalAuthBuilder {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("InternalAuthBuilder")
.field("issuer", &self.issuer)
.field("clients", &self.clients.len())
.finish_non_exhaustive()
}
}
impl InternalAuth {
pub fn builder(token_url: impl Into<String>) -> InternalAuthBuilder {
let token_url = token_url.into();
InternalAuthBuilder {
clients: BTreeMap::new(),
signing_key: None,
issuer: token_url.clone(),
token_url,
ttl: Duration::from_secs(3600),
allow_anonymous: false,
clock: std::sync::Arc::new(crate::core::SystemClock),
max_concurrent_verifications: default_verifier_permits(),
}
}
pub fn hash_secret(secret: &str) -> Result<String, AuthError> {
use rand::Rng as _;
let raw: [u8; 16] = rand::rng().random();
let salt =
SaltString::encode_b64(&raw).map_err(|e| AuthError::Unavailable(e.to_string()))?;
Argon2::default()
.hash_password(secret.as_bytes(), &salt)
.map(|h| h.to_string())
.map_err(|e| AuthError::Unavailable(e.to_string()))
}
fn mint(
&self,
client_id: &str,
scopes: &Scopes,
) -> Result<ClientCredentialResponse, TokenError> {
let now = self.clock.now().as_second();
let claims = Claims {
iss: self.issuer.clone(),
sub: client_id.to_string(),
aud: self.issuer.clone(),
iat: now,
exp: now + self.ttl.as_secs() as i64,
scope: scopes.to_scope_string(),
};
let token = jsonwebtoken::encode(&Header::new(Algorithm::HS256), &claims, &self.encoding)
.map_err(|e| {
TokenError::Refused(OAuthError::new(
OAuthErrorKind::InvalidRequest,
format!("could not mint a token: {e}"),
))
})?;
Ok(ClientCredentialResponse {
access_token: token,
token_type: "Bearer".into(),
expires_in: Some(self.ttl.as_secs()),
refresh_token: None,
scope: Some(claims.scope),
})
}
}
impl InternalAuthBuilder {
pub fn client(
self,
client_id: impl Into<String>,
secret: &str,
scopes: Scopes,
) -> Result<Self, AuthError> {
let hash = InternalAuth::hash_secret(secret)?;
Ok(self.client_hashed(client_id, hash, scopes))
}
pub fn client_hashed(
mut self,
client_id: impl Into<String>,
secret_hash: impl Into<String>,
scopes: Scopes,
) -> Self {
self.clients.insert(
client_id.into(),
Registered {
secret_hash: secret_hash.into(),
scopes,
},
);
self
}
pub fn signing_key(mut self, key: impl Into<Vec<u8>>) -> Self {
self.signing_key = Some(key.into());
self
}
pub fn issuer(mut self, issuer: impl Into<String>) -> Self {
self.issuer = issuer.into();
self
}
pub fn ttl(mut self, ttl: Duration) -> Self {
self.ttl = ttl;
self
}
pub fn allowing_anonymous(mut self) -> Self {
self.allow_anonymous = true;
self
}
pub fn clock(mut self, clock: std::sync::Arc<dyn crate::core::Clock>) -> Self {
self.clock = clock;
self
}
pub fn max_concurrent_verifications(mut self, permits: usize) -> Self {
self.max_concurrent_verifications = permits.max(1);
self
}
pub fn build(self) -> InternalAuth {
let key = self.signing_key.unwrap_or_else(|| {
use rand::Rng as _;
let bytes: [u8; 32] = rand::rng().random();
bytes.to_vec()
});
InternalAuth {
clients: self.clients,
encoding: EncodingKey::from_secret(&key),
decoding: DecodingKey::from_secret(&key),
issuer: self.issuer,
token_url: self.token_url,
ttl: self.ttl,
allow_anonymous: self.allow_anonymous,
clock: self.clock,
verifiers: std::sync::Arc::new(tokio::sync::Semaphore::new(
self.max_concurrent_verifications,
)),
}
}
}
#[async_trait]
impl Authenticator for InternalAuth {
async fn authenticate(&self, bearer: Option<&str>) -> Result<Principal, AuthError> {
let Some(token) = bearer else {
return if self.allow_anonymous {
Ok(Principal::anonymous())
} else {
Err(AuthError::Missing)
};
};
let mut validation = Validation::new(Algorithm::HS256);
validation.set_issuer(&[&self.issuer]);
validation.set_audience(&[&self.issuer]);
validation
.required_spec_claims
.extend(["iss".to_string(), "aud".to_string()]);
let data = jsonwebtoken::decode::<Claims>(token, &self.decoding, &validation).map_err(
|e| match e.kind() {
jsonwebtoken::errors::ErrorKind::ExpiredSignature => AuthError::Expired,
_ => AuthError::Invalid,
},
)?;
Ok(Principal {
client_id: ClientId::new(data.claims.sub).ok(),
scopes: Scopes::parse(&data.claims.scope),
})
}
fn token_url(&self) -> String {
self.token_url.clone()
}
fn allows_anonymous(&self) -> bool {
self.allow_anonymous
}
fn issues_tokens(&self) -> bool {
true
}
async fn issue_token(
&self,
request: &ClientCredentialRequest,
) -> Result<ClientCredentialResponse, TokenError> {
if request.grant_type != "client_credentials" {
return Err(TokenError::Refused(OAuthError::new(
OAuthErrorKind::UnsupportedGrantType,
"only the client_credentials grant is supported",
)));
}
let found = self.clients.get(&request.client_id);
let hash = found
.map_or(DECOY_HASH, |c| c.secret_hash.as_str())
.to_string();
let permit = self
.verifiers
.clone()
.acquire_owned()
.await
.map_err(|_| TokenError::Unavailable("the VTN is shutting down".into()))?;
let secret = request.client_secret.clone();
let verified = tokio::task::spawn_blocking(move || {
let _permit = permit;
PasswordHash::new(&hash)
.map(|parsed| {
Argon2::default()
.verify_password(secret.as_bytes(), &parsed)
.is_ok()
})
.unwrap_or(false)
})
.await
.unwrap_or(false);
let Some(client) = found.filter(|_| verified) else {
return Err(TokenError::Refused(OAuthError::new(
OAuthErrorKind::InvalidClient,
"unknown client, or the secret does not match",
)));
};
let granted = match &request.scope {
None => client.scopes.clone(),
Some(asked) => {
let asked = Scopes::parse(asked);
let granted = Scopes::new(
asked
.as_slice()
.iter()
.copied()
.filter(|s| client.scopes.contains(*s)),
);
if granted.is_empty() {
return Err(TokenError::Refused(OAuthError::new(
OAuthErrorKind::InvalidScope,
"none of the requested scopes are granted to this client",
)));
}
granted
}
};
self.mint(&request.client_id, &granted)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::vtn::auth::Scope;
fn request(client_id: &str, secret: &str, scope: Option<&str>) -> ClientCredentialRequest {
ClientCredentialRequest {
grant_type: "client_credentials".into(),
client_id: client_id.into(),
client_secret: secret.into(),
scope: scope.map(str::to_string),
}
}
fn auth() -> InternalAuth {
InternalAuth::builder("https://vtn.test/auth/token")
.client("ven-7", "hunter2", Scopes::new(Scope::VEN))
.unwrap()
.build()
}
#[tokio::test]
async fn a_minted_token_authenticates_back() {
let auth = auth();
let issued = auth
.issue_token(&request("ven-7", "hunter2", None))
.await
.unwrap();
assert_eq!(issued.token_type, "Bearer");
assert_eq!(issued.expires_in, Some(3600));
let principal = auth.authenticate(Some(&issued.access_token)).await.unwrap();
assert_eq!(principal.client_id.as_ref().unwrap().as_str(), "ven-7");
assert!(principal.scopes.contains(Scope::ReadTargets));
assert!(!principal.is_business_logic());
}
#[tokio::test]
async fn a_wrong_secret_and_an_unknown_client_are_the_same_answer() {
let auth = auth();
for req in [
request("ven-7", "wrong", None),
request("nobody", "hunter2", None),
] {
match auth.issue_token(&req).await {
Err(TokenError::Refused(e)) => assert_eq!(e.error, OAuthErrorKind::InvalidClient),
other => panic!("expected invalid_client, got {other:?}"),
}
}
}
#[tokio::test]
async fn a_client_cannot_ask_for_more_than_it_holds() {
let auth = auth();
let issued = auth
.issue_token(&request(
"ven-7",
"hunter2",
Some("read_targets write_events"),
))
.await
.unwrap();
let principal = auth.authenticate(Some(&issued.access_token)).await.unwrap();
assert!(principal.scopes.contains(Scope::ReadTargets));
assert!(
!principal.scopes.contains(Scope::WriteEvents),
"asking for a scope must not grant it"
);
match auth
.issue_token(&request("ven-7", "hunter2", Some("write_events")))
.await
{
Err(TokenError::Refused(e)) => assert_eq!(e.error, OAuthErrorKind::InvalidScope),
other => panic!("expected invalid_scope, got {other:?}"),
}
}
#[tokio::test]
async fn only_the_client_credentials_grant_is_offered() {
let mut req = request("ven-7", "hunter2", None);
req.grant_type = "password".into();
match auth().issue_token(&req).await {
Err(TokenError::Refused(e)) => {
assert_eq!(e.error, OAuthErrorKind::UnsupportedGrantType)
}
other => panic!("expected unsupported_grant_type, got {other:?}"),
}
}
#[tokio::test(flavor = "current_thread")]
async fn a_token_request_yields_the_runtime_rather_than_blocking_it() {
let ran = std::sync::Arc::new(std::sync::atomic::AtomicBool::new(false));
let flag = ran.clone();
let queued = tokio::spawn(async move {
flag.store(true, std::sync::atomic::Ordering::SeqCst);
});
let auth = auth();
assert!(
auth.issue_token(&request("ghost", "wrong", None))
.await
.is_err()
);
assert!(
ran.load(std::sync::atomic::Ordering::SeqCst),
"the verification never yielded: it hashed on the runtime thread, so a handful of \
token requests would stall every other task the VTN has"
);
queued.await.unwrap();
}
#[tokio::test]
async fn verification_concurrency_is_bounded() {
let auth = InternalAuth::builder("https://vtn.test/auth/token")
.max_concurrent_verifications(0) .build();
assert_eq!(auth.verifiers.available_permits(), 1);
}
#[tokio::test]
async fn a_token_from_another_issuer_is_not_accepted() {
let issued = auth()
.issue_token(&request("ven-7", "hunter2", None))
.await
.unwrap();
let other = InternalAuth::builder("https://other.test/auth/token").build();
assert_eq!(
other
.authenticate(Some(&issued.access_token))
.await
.unwrap_err(),
AuthError::Invalid
);
}
#[tokio::test]
async fn an_expired_token_says_so_rather_than_merely_failing() {
use crate::core::FixedClock;
let past: crate::model::Timestamp = "2020-01-01T00:00:00Z".parse().unwrap();
let auth = InternalAuth::builder("https://vtn.test/auth/token")
.client("ven-7", "hunter2", Scopes::new(Scope::VEN))
.unwrap()
.clock(std::sync::Arc::new(FixedClock::new(past)))
.build();
let issued = auth
.issue_token(&request("ven-7", "hunter2", None))
.await
.unwrap();
assert_eq!(
auth.authenticate(Some(&issued.access_token))
.await
.unwrap_err(),
AuthError::Expired
);
}
#[test]
fn a_stored_hash_is_not_the_secret() {
let hash = InternalAuth::hash_secret("hunter2").unwrap();
assert!(hash.starts_with("$argon2id$"));
assert!(!hash.contains("hunter2"));
assert_ne!(
hash,
InternalAuth::hash_secret("hunter2").unwrap(),
"salted"
);
}
}