Added user apps and token resources.
This commit is contained in:
@@ -0,0 +1,80 @@
|
||||
use chrono::{DateTime, FixedOffset, Utc};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::domain;
|
||||
|
||||
#[derive(Debug, Clone, Deserialize)]
|
||||
pub struct UserAppSecretRequest {
|
||||
pub expires_at: DateTime<FixedOffset>,
|
||||
}
|
||||
|
||||
impl UserAppSecretRequest {
|
||||
pub fn expires_at_utc(&self) -> DateTime<Utc> {
|
||||
self.expires_at.with_timezone(&Utc)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
pub struct UserAppSecretResponse {
|
||||
pub id: Uuid,
|
||||
pub app_id: Uuid,
|
||||
pub expires_at: DateTime<Utc>,
|
||||
pub created_at: DateTime<Utc>,
|
||||
}
|
||||
|
||||
impl From<domain::UserAppSecret> for UserAppSecretResponse {
|
||||
fn from(secret: domain::UserAppSecret) -> Self {
|
||||
Self {
|
||||
id: secret.id,
|
||||
app_id: secret.app_id,
|
||||
expires_at: secret.expires_at,
|
||||
created_at: secret.created_at,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
pub struct CreatedUserAppSecretResponse {
|
||||
#[serde(flatten)]
|
||||
pub credential: UserAppSecretResponse,
|
||||
pub secret: String,
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn parses_timezone_aware_expiration() {
|
||||
let request: UserAppSecretRequest =
|
||||
serde_json::from_str(r#"{"expires_at":"2030-01-01T12:00:00+02:00"}"#).unwrap();
|
||||
|
||||
assert_eq!(
|
||||
request.expires_at_utc().to_rfc3339(),
|
||||
"2030-01-01T10:00:00+00:00"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rejects_expiration_without_timezone() {
|
||||
assert!(
|
||||
serde_json::from_str::<UserAppSecretRequest>(r#"{"expires_at":"2030-01-01T12:00:00"}"#)
|
||||
.is_err()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn read_response_does_not_expose_secret_hash() {
|
||||
let response = UserAppSecretResponse {
|
||||
id: Uuid::new_v4(),
|
||||
app_id: Uuid::new_v4(),
|
||||
expires_at: Utc::now(),
|
||||
created_at: Utc::now(),
|
||||
};
|
||||
let value = serde_json::to_value(response).unwrap();
|
||||
|
||||
assert!(value.get("secret").is_none());
|
||||
assert!(value.get("secret_hash").is_none());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user