145 lines
5.5 KiB
Rust
145 lines
5.5 KiB
Rust
use super::database::{File, options};
|
|
use dever_runtime::{
|
|
auth::{self, Claims, Error, Identity, Provider, Verified},
|
|
database::{Context, Database, Parameter, Sql},
|
|
task,
|
|
value::secret::Secret,
|
|
};
|
|
mod access;
|
|
mod capability;
|
|
mod tenant;
|
|
|
|
fn provider() -> Provider {
|
|
Provider::new("accounts".into(), Secret::from_input(vec![42; 32]), 60).unwrap()
|
|
}
|
|
fn claims(site: &str) -> Claims {
|
|
Claims {
|
|
subject: "subject".into(),
|
|
session: "session".into(),
|
|
tenant: None,
|
|
site: site.into(),
|
|
}
|
|
}
|
|
pub(super) async fn verified(site: &str, user: i64, tenant: Option<i64>) -> Verified {
|
|
let provider = provider();
|
|
let token = provider.issue(&claims(site), 100).unwrap();
|
|
auth::authenticate(&provider, &token, site, 101, async |_| {
|
|
Ok(Identity {
|
|
id: "user".into(),
|
|
user_id: Some(user),
|
|
tenant_id: tenant,
|
|
})
|
|
})
|
|
.await
|
|
.unwrap()
|
|
}
|
|
|
|
#[test]
|
|
fn token_encoding_matches_independent_hs256_vectors() {
|
|
let provider = provider();
|
|
// Python 标准库 json/base64/hmac 生成固定向量,防止签发和验签一起改错仍自证通过。
|
|
// 只比较测试 Token 的 HMAC,不公开 Secret 字节。
|
|
for (subject, session, tenant, expected) in [
|
|
(
|
|
"subject",
|
|
"session",
|
|
None,
|
|
[
|
|
175, 58, 78, 186, 13, 221, 100, 150, 175, 147, 188, 89, 169, 12, 116, 138, 251,
|
|
137, 214, 237, 67, 159, 53, 145, 1, 23, 40, 40, 65, 230, 166, 130,
|
|
],
|
|
),
|
|
(
|
|
"用户\"甲\n\\",
|
|
"会话\\01",
|
|
Some("租户/一"),
|
|
[
|
|
51, 78, 228, 224, 50, 86, 151, 37, 25, 23, 41, 10, 208, 165, 41, 40, 125, 149, 63,
|
|
180, 237, 28, 15, 28, 78, 234, 216, 108, 178, 9, 113, 199,
|
|
],
|
|
),
|
|
] {
|
|
let claims = Claims {
|
|
subject: subject.into(),
|
|
session: session.into(),
|
|
tenant: tenant.map(str::to_owned),
|
|
site: "admin".into(),
|
|
};
|
|
let token = provider.issue(&claims, 100).unwrap();
|
|
let proof = dever_runtime::crypto::hmac_sha256(&token, b"auth-token-contract");
|
|
assert_eq!(proof.as_ref(), expected);
|
|
assert_eq!(provider.verify_token(&token, "admin", 101).unwrap(), claims);
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn tokens_check_signature_provider_site_and_time_before_project_verify() {
|
|
task::execute(true, async {
|
|
let provider = provider();
|
|
let token = provider.issue(&claims("admin"), 100).unwrap();
|
|
assert_eq!(
|
|
provider.verify_token(&token, "admin", 101).unwrap(),
|
|
claims("admin")
|
|
);
|
|
for (site, now) in [("other", 101), ("admin", 99), ("admin", 160)] {
|
|
let result = auth::authenticate(
|
|
&provider,
|
|
&token,
|
|
site,
|
|
now,
|
|
async |_| -> Result<Identity, Error> {
|
|
panic!("invalid token cannot invoke project verifier")
|
|
},
|
|
)
|
|
.await;
|
|
assert!(matches!(result, Err(Error::Unauthorized)));
|
|
}
|
|
let wrong_key =
|
|
Provider::new("accounts".into(), Secret::from_input(vec![43; 32]), 60).unwrap();
|
|
assert!(matches!(
|
|
wrong_key.verify_token(&token, "admin", 101),
|
|
Err(Error::Unauthorized)
|
|
));
|
|
let wrong_provider =
|
|
Provider::new("other".into(), Secret::from_input(vec![42; 32]), 60).unwrap();
|
|
assert!(matches!(
|
|
wrong_provider.verify_token(&token, "admin", 101),
|
|
Err(Error::Unauthorized)
|
|
));
|
|
let invalid = auth::authenticate(&provider, &token, "admin", 101, async |_| {
|
|
Ok(Identity {
|
|
id: "user".into(),
|
|
user_id: Some(0),
|
|
tenant_id: None,
|
|
})
|
|
})
|
|
.await;
|
|
assert!(matches!(invalid, Err(Error::Unauthorized)));
|
|
})
|
|
.unwrap();
|
|
}
|
|
|
|
#[test]
|
|
fn a_valid_signature_does_not_bypass_live_session_revocation() {
|
|
let file = File::new("auth-session");
|
|
task::run(true, 16, async |scope| {
|
|
let database = Database::sqlite(&file.0,true,options(), None).await?;
|
|
let mut fixture = database.acquire().await?;
|
|
fixture.execute(scope, Sql::same("CREATE TABLE session(id TEXT PRIMARY KEY,user_id INTEGER NOT NULL,active BOOLEAN NOT NULL)"), vec![]).await?;
|
|
fixture.execute(scope, Sql::same("INSERT INTO session VALUES ('session',7,TRUE)"), vec![]).await?;
|
|
drop(fixture);
|
|
let provider = provider();
|
|
let token = provider.issue(&claims("admin"),100)?;
|
|
let verify = async |claims: &Claims| {
|
|
let rows = database.acquire().await?.query(scope,Sql::same("SELECT user_id FROM session WHERE id=$1 AND active=TRUE"),vec![Parameter::Text(claims.session.clone())],|row|row.get::<i64>(0)).await?;
|
|
let user_id = rows.into_iter().next().ok_or(Error::Unauthorized)?;
|
|
Ok(Identity { id: user_id.to_string(),user_id:Some(user_id),tenant_id:None })
|
|
};
|
|
let identity = auth::authenticate(&provider,&token,"admin",101,&verify).await?;
|
|
assert_eq!(identity.user(),Some(7));
|
|
database.acquire().await?.execute(scope,Sql::same("UPDATE session SET active=FALSE WHERE id=$1"),vec![Parameter::Text("session".into())]).await?;
|
|
assert!(matches!(auth::authenticate(&provider,&token,"admin",101,&verify).await,Err(Error::Unauthorized)));
|
|
Ok::<_,Error>(())
|
|
}).unwrap().unwrap();
|
|
}
|