use super::*;
impl Display for AuthError {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
match self {
Self::MissingCredentials => f.write_str(MSG_AUTH_MISSING),
Self::InvalidCredentials => f.write_str(MSG_AUTH_INVALID),
Self::TokenExpired => f.write_str(MSG_AUTH_EXPIRED),
Self::TokenNotYetValid => f.write_str(MSG_AUTH_NOT_YET_VALID),
Self::UnsupportedAlgorithm => f.write_str(MSG_AUTH_UNSUPPORTED_ALG),
Self::ClaimMismatch => f.write_str(MSG_AUTH_CLAIM_MISMATCH),
Self::Forbidden => f.write_str(MSG_AUTH_FORBIDDEN),
}
}
}
impl Error for AuthError {}
impl JwtClaims {
pub fn new<S>(subject: S, ttl_secs: u64) -> Self
where
S: Into<String>,
{
let now: u64 = auth_now_secs();
Self {
sub: subject.into(),
iss: None,
aud: None,
exp: Some(now.saturating_add(ttl_secs)),
nbf: Some(now),
iat: Some(now),
jti: None,
roles: Vec::new(),
extra: HashMap::new(),
}
}
pub fn with_roles<I, S>(mut self, roles: I) -> Self
where
I: IntoIterator<Item = S>,
S: Into<String>,
{
self.set_roles(roles.into_iter().map(Into::into).collect());
self
}
pub fn with_issuer<S>(mut self, issuer: S) -> Self
where
S: Into<String>,
{
self.set_iss(Some(issuer.into()));
self
}
pub fn with_audience<S>(mut self, audience: S) -> Self
where
S: Into<String>,
{
self.set_aud(Some(audience.into()));
self
}
}
impl JwtCodec {
pub fn new<S>(secret: S) -> Self
where
S: Into<Vec<u8>>,
{
Self {
default_secret: secret.into(),
default_kid: None,
keyring: hash_map_xx_hash3_64(),
leeway_secs: DEFAULT_JWT_LEEWAY_SECS,
required_issuer: None,
required_audience: None,
}
}
pub fn with_key<K, S>(mut self, kid: K, secret: S) -> Self
where
K: Into<String>,
S: Into<Vec<u8>>,
{
let Self { keyring, .. } = &mut self;
keyring.insert(kid.into(), secret.into());
self
}
pub fn with_default_kid<K>(mut self, kid: K) -> Self
where
K: Into<String>,
{
let Self {
default_kid,
default_secret,
keyring,
..
} = &mut self;
let kid_str: String = kid.into();
keyring
.entry(kid_str.clone())
.or_insert_with(|| default_secret.clone());
*default_kid = Some(kid_str);
self
}
pub fn with_leeway_secs(mut self, leeway_secs: u64) -> Self {
let Self {
leeway_secs: slot, ..
} = &mut self;
*slot = leeway_secs;
self
}
pub fn with_required_issuer<S>(mut self, issuer: S) -> Self
where
S: Into<String>,
{
let Self {
required_issuer, ..
} = &mut self;
*required_issuer = Some(issuer.into());
self
}
pub fn with_required_audience<S>(mut self, audience: S) -> Self
where
S: Into<String>,
{
let Self {
required_audience, ..
} = &mut self;
*required_audience = Some(audience.into());
self
}
pub fn encode(&self, claims: &JwtClaims) -> Result<String, AuthError> {
let Self {
default_secret,
default_kid,
keyring,
..
} = self;
let header: JwtHeader = JwtHeader {
alg: JWT_ALG_HS256.to_owned(),
typ: Some(JWT_TYP_JWT.to_owned()),
kid: default_kid.clone(),
};
let header_json: Vec<u8> = to_vec(&header).map_err(|_| AuthError::InvalidCredentials)?;
let claims_json: Vec<u8> = to_vec(claims).map_err(|_| AuthError::InvalidCredentials)?;
let header_b64: String = base64url_encode(&header_json);
let claims_b64: String = base64url_encode(&claims_json);
let mut signing_input: String =
String::with_capacity(header_b64.len() + claims_b64.len() + 1);
signing_input.push_str(&header_b64);
signing_input.push('.');
signing_input.push_str(&claims_b64);
let secret: &[u8] = match default_kid {
Some(kid) => keyring
.get(kid)
.map(Vec::as_slice)
.unwrap_or(default_secret.as_slice()),
None => default_secret.as_slice(),
};
let mac: [u8; 32] = hmac_sha256(secret, signing_input.as_bytes());
let sig_b64: String = base64url_encode(&mac);
let mut token: String = String::with_capacity(signing_input.len() + 1 + sig_b64.len());
token.push_str(&signing_input);
token.push('.');
token.push_str(&sig_b64);
Ok(token)
}
pub fn decode_at(&self, token: &str, now_secs: u64) -> Result<JwtClaims, AuthError> {
let Self {
default_secret,
keyring,
leeway_secs,
required_issuer,
required_audience,
..
} = self;
let mut parts: Split<'_, char> = token.split('.');
let header_b64: &str = parts.next().ok_or(AuthError::InvalidCredentials)?;
let payload_b64: &str = parts.next().ok_or(AuthError::InvalidCredentials)?;
let sig_b64: &str = parts.next().ok_or(AuthError::InvalidCredentials)?;
if parts.next().is_some()
|| header_b64.is_empty()
|| payload_b64.is_empty()
|| sig_b64.is_empty()
{
return Err(AuthError::InvalidCredentials);
}
let header_bytes: Vec<u8> =
base64url_decode(header_b64).ok_or(AuthError::InvalidCredentials)?;
let header: JwtHeader =
from_slice(&header_bytes).map_err(|_| AuthError::InvalidCredentials)?;
if header.get_alg() != JWT_ALG_HS256 {
return Err(AuthError::UnsupportedAlgorithm);
}
let secret: &[u8] = match header.get_kid() {
Some(kid) => keyring
.get(&kid)
.map(Vec::as_slice)
.ok_or(AuthError::InvalidCredentials)?,
None => default_secret.as_slice(),
};
let provided_sig: Vec<u8> =
base64url_decode(sig_b64).ok_or(AuthError::InvalidCredentials)?;
let signed_len: usize = header_b64.len() + 1 + payload_b64.len();
let expected_mac: [u8; 32] = hmac_sha256(secret, &token.as_bytes()[..signed_len]);
if !constant_time_eq(&provided_sig, &expected_mac) {
return Err(AuthError::InvalidCredentials);
}
let payload_bytes: Vec<u8> =
base64url_decode(payload_b64).ok_or(AuthError::InvalidCredentials)?;
let claims: JwtClaims =
from_slice(&payload_bytes).map_err(|_| AuthError::InvalidCredentials)?;
if let Some(exp) = claims.get_exp()
&& now_secs > exp.saturating_add(*leeway_secs)
{
return Err(AuthError::TokenExpired);
}
if let Some(nbf) = claims.get_nbf()
&& now_secs.saturating_add(*leeway_secs) < nbf
{
return Err(AuthError::TokenNotYetValid);
}
if let Some(expected_iss) = required_issuer
&& claims.get_iss().as_deref() != Some(expected_iss.as_str())
{
return Err(AuthError::ClaimMismatch);
}
if let Some(expected_aud) = required_audience
&& claims.get_aud().as_deref() != Some(expected_aud.as_str())
{
return Err(AuthError::ClaimMismatch);
}
Ok(claims)
}
pub fn decode(&self, token: &str) -> Result<JwtClaims, AuthError> {
self.decode_at(token, auth_now_secs())
}
}
impl AuthPrincipal {
pub fn has_role(&self, role: &str) -> bool {
self.get_roles().iter().any(|item: &String| item == role)
}
}
impl JwtAuthenticator {
pub fn new(codec: JwtCodec) -> Self {
Self { codec }
}
}
impl Authenticator for JwtAuthenticator {
fn authenticate<'a>(&'a self, ctx: &'a Context) -> AuthFuture<'a> {
let Self { codec } = self;
let result: Result<AuthPrincipal, AuthError> =
match ctx.get_request().try_get_header_back(AUTHORIZATION) {
None => Err(AuthError::MissingCredentials),
Some(header) => match strip_auth_scheme(&header, PREFIX_BEARER) {
None => Err(AuthError::MissingCredentials),
Some(token) => codec.decode(token).map(|claims: JwtClaims| AuthPrincipal {
subject: claims.get_sub().clone(),
roles: claims.get_roles().clone(),
scheme: AuthScheme::BearerJwt,
claims: Some(claims),
}),
},
};
Box::pin(async move { result })
}
}
impl Default for ApiKeyAuthenticator {
fn default() -> Self {
Self::new(X_API_KEY)
}
}
impl ApiKeyAuthenticator {
pub fn new<H>(header_name: H) -> Self
where
H: Into<String>,
{
Self {
header_name: header_name.into(),
entries: Vec::new(),
}
}
pub fn with_key<K, S, I, R>(mut self, api_key: K, subject: S, roles: I) -> Self
where
K: Into<String>,
S: Into<String>,
I: IntoIterator<Item = R>,
R: Into<String>,
{
let Self { entries, .. } = &mut self;
entries.push((
api_key.into().into_bytes(),
subject.into(),
roles.into_iter().map(Into::into).collect(),
));
self
}
}
impl Authenticator for ApiKeyAuthenticator {
fn authenticate<'a>(&'a self, ctx: &'a Context) -> AuthFuture<'a> {
let Self {
header_name,
entries,
} = self;
let result: Result<AuthPrincipal, AuthError> =
match ctx.get_request().try_get_header_back(header_name) {
None => Err(AuthError::MissingCredentials),
Some(candidate) => {
let candidate_bytes: &[u8] = candidate.trim().as_bytes();
let mut matched: Option<AuthPrincipal> = None;
for (secret, subject, roles) in entries {
if constant_time_eq(candidate_bytes, secret) {
matched = Some(AuthPrincipal {
subject: subject.clone(),
roles: roles.clone(),
scheme: AuthScheme::ApiKey,
claims: None,
});
}
}
matched.ok_or(AuthError::InvalidCredentials)
}
};
Box::pin(async move { result })
}
}
impl Default for BasicAuthenticator {
fn default() -> Self {
Self::new()
}
}
impl BasicAuthenticator {
pub fn new() -> Self {
Self {
users: hash_map_xx_hash3_64(),
}
}
pub fn with_user<U, P, I, R>(mut self, username: U, password: P, roles: I) -> Self
where
U: Into<String>,
P: Into<String>,
I: IntoIterator<Item = R>,
R: Into<String>,
{
let Self { users } = &mut self;
users.insert(
username.into(),
(
password.into().into_bytes(),
roles.into_iter().map(Into::into).collect(),
),
);
self
}
}
impl Authenticator for BasicAuthenticator {
fn authenticate<'a>(&'a self, ctx: &'a Context) -> AuthFuture<'a> {
let Self { users } = self;
let result: Result<AuthPrincipal, AuthError> =
match ctx.get_request().try_get_header_back(AUTHORIZATION) {
None => Err(AuthError::MissingCredentials),
Some(header) => match strip_auth_scheme(&header, PREFIX_BASIC) {
None => Err(AuthError::MissingCredentials),
Some(encoded) => match base64_decode(encoded)
.and_then(|raw: Vec<u8>| String::from_utf8(raw).ok())
{
None => Err(AuthError::InvalidCredentials),
Some(decoded) => match decoded.split_once(':') {
None => Err(AuthError::InvalidCredentials),
Some((user, pass)) => match users.get(user) {
Some((expected_pass, roles))
if constant_time_eq(pass.as_bytes(), expected_pass) =>
{
Ok(AuthPrincipal {
subject: user.to_owned(),
roles: roles.clone(),
scheme: AuthScheme::Basic,
claims: None,
})
}
_ => Err(AuthError::InvalidCredentials),
},
},
},
},
};
Box::pin(async move { result })
}
}
impl CompositeAuthenticator {
pub fn new(chain: Vec<SharedAuthenticator>) -> Self {
Self { chain }
}
}
impl Authenticator for CompositeAuthenticator {
fn authenticate<'a>(&'a self, ctx: &'a Context) -> AuthFuture<'a> {
let Self { chain } = self;
Box::pin(async move {
let mut last_err: AuthError = AuthError::MissingCredentials;
for auth in chain {
match auth.authenticate(ctx).await {
Ok(principal) => return Ok(principal),
Err(AuthError::MissingCredentials) => {}
Err(err) => last_err = err,
}
}
Err(last_err)
})
}
}
impl Default for AuthConfig {
fn default() -> Self {
Self {
exempt_paths: Vec::new(),
required_roles: Vec::new(),
require_all_roles: false,
www_authenticate: DEFAULT_WWW_AUTHENTICATE.to_owned(),
}
}
}
impl AuthConfig {
pub fn with_exempt_paths<I, S>(mut self, paths: I) -> Self
where
I: IntoIterator<Item = S>,
S: Into<String>,
{
self.set_exempt_paths(paths.into_iter().map(Into::into).collect());
self
}
pub fn with_required_roles<I, S>(mut self, roles: I) -> Self
where
I: IntoIterator<Item = S>,
S: Into<String>,
{
self.set_required_roles(roles.into_iter().map(Into::into).collect());
self
}
}
impl AuthPlugin {
pub fn init(config: AuthConfig, authenticator: SharedAuthenticator) -> &'static Self {
AUTH_PLUGIN.get_or_init(|| Self {
config,
authenticator,
})
}
pub fn get() -> Option<&'static Self> {
AUTH_PLUGIN.get()
}
pub fn get_config(&self) -> &AuthConfig {
&self.config
}
pub fn get_authenticator(&self) -> &SharedAuthenticator {
&self.authenticator
}
pub fn principal(ctx: &Context) -> Option<AuthPrincipal> {
ctx.try_get_attribute(ATTR_AUTH_PRINCIPAL)
}
pub fn is_authorized(&self, principal: &AuthPrincipal) -> bool {
let required: &Vec<String> = self.get_config().get_required_roles();
if required.is_empty() {
return true;
}
if self.get_config().get_require_all_roles() {
required
.iter()
.all(|role: &String| principal.has_role(role))
} else {
required
.iter()
.any(|role: &String| principal.has_role(role))
}
}
}
impl ServerHook for AuthHook {
async fn new(_: &mut Stream, _: &mut Context) -> Self {
Self
}
async fn handle(self, stream: &mut Stream, ctx: &mut Context) -> Status {
let Some(plugin) = AuthPlugin::get() else {
return Status::Continue;
};
let path: &str = ctx.get_request().get_path();
if plugin
.get_config()
.get_exempt_paths()
.iter()
.any(|exempt: &String| exempt == path)
{
return Status::Continue;
}
match plugin.get_authenticator().authenticate(ctx).await {
Ok(principal) => {
if !plugin.is_authorized(&principal) {
ctx.get_mut_response()
.set_status_code(HttpStatus::Forbidden.code())
.set_body(Vec::new());
let wire: ResponseData = ctx.get_mut_response().build();
let _: Result<(), ResponseError> = stream.try_send(&wire).await;
return Status::Reject;
}
ctx.set_attribute(ATTR_AUTH_PRINCIPAL, principal);
Status::Continue
}
Err(AuthError::Forbidden) => {
ctx.get_mut_response()
.set_status_code(HttpStatus::Forbidden.code())
.set_body(Vec::new());
let wire: ResponseData = ctx.get_mut_response().build();
let _: Result<(), ResponseError> = stream.try_send(&wire).await;
Status::Reject
}
Err(_) => {
ctx.get_mut_response()
.set_status_code(HttpStatus::Unauthorized.code())
.set_header(WWW_AUTHENTICATE, plugin.get_config().get_www_authenticate())
.set_body(Vec::new());
let wire: ResponseData = ctx.get_mut_response().build();
let _: Result<(), ResponseError> = stream.try_send(&wire).await;
Status::Reject
}
}
}
}