Added user apps and token resources.
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
use uuid::Uuid;
|
||||
|
||||
#[derive(Deserialize)]
|
||||
#[serde(rename_all = "snake_case", tag = "grant_type")]
|
||||
pub enum TokenRequest {
|
||||
ClientCredentials {
|
||||
client_id: Uuid,
|
||||
client_secret: String,
|
||||
},
|
||||
RefreshToken {
|
||||
refresh_token: String,
|
||||
},
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct TokenRevocationRequest {
|
||||
pub token: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize)]
|
||||
pub struct TokenResponse {
|
||||
pub access_token: String,
|
||||
pub token_type: TokenType,
|
||||
pub expires_in: i64,
|
||||
pub refresh_token: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize)]
|
||||
#[serde(rename_all = "snake_case")]
|
||||
pub enum TokenType {
|
||||
Bearer,
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn serializes_token_response() {
|
||||
let response = TokenResponse {
|
||||
access_token: "access-token".to_string(),
|
||||
token_type: TokenType::Bearer,
|
||||
expires_in: 3600,
|
||||
refresh_token: Some("refresh-token".to_string()),
|
||||
};
|
||||
let value = serde_json::to_value(response).unwrap();
|
||||
|
||||
assert_eq!(value["access_token"], "access-token");
|
||||
assert_eq!(value["token_type"], "bearer");
|
||||
assert_eq!(value["expires_in"], 3600);
|
||||
assert_eq!(value["refresh_token"], "refresh-token");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::domain;
|
||||
|
||||
#[derive(Debug, Clone, Deserialize)]
|
||||
pub struct UserAppRequest {}
|
||||
|
||||
impl UserAppRequest {
|
||||
pub fn into_domain(self, user_id: Uuid, id: Uuid) -> domain::UserApp {
|
||||
domain::UserApp { id, user_id }
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
pub struct UserAppResponse {
|
||||
pub id: Uuid,
|
||||
}
|
||||
|
||||
impl From<domain::UserApp> for UserAppResponse {
|
||||
fn from(app: domain::UserApp) -> Self {
|
||||
Self { id: app.id }
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn serializes_user_app_response() {
|
||||
let id = Uuid::new_v4();
|
||||
let response = UserAppResponse::from(domain::UserApp {
|
||||
id,
|
||||
user_id: Uuid::new_v4(),
|
||||
});
|
||||
|
||||
assert_eq!(
|
||||
serde_json::to_value(response).unwrap()["id"],
|
||||
id.to_string()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn accepts_empty_put_payload() {
|
||||
let request: UserAppRequest = serde_json::from_str("{}").unwrap();
|
||||
let user_id = Uuid::new_v4();
|
||||
let id = Uuid::new_v4();
|
||||
let app = request.into_domain(user_id, id);
|
||||
|
||||
assert_eq!(app, domain::UserApp { id, user_id });
|
||||
}
|
||||
}
|
||||
@@ -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