Added user apps and token resources.
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
use chrono::{DateTime, Utc};
|
||||
|
||||
use super::RepositoryError;
|
||||
use crate::{
|
||||
database::{dtos::TokenRow, tokens as db},
|
||||
domain::Token,
|
||||
};
|
||||
|
||||
pub async fn issue<'e, E>(executor: E, token: Token) -> Result<(), RepositoryError>
|
||||
where
|
||||
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||
{
|
||||
Ok(db::issue(executor, encode(token)).await?)
|
||||
}
|
||||
|
||||
pub async fn rotate_refresh_token<'e, E>(
|
||||
executor: E,
|
||||
old_refresh_hash: &[u8],
|
||||
access_token_hash: &[u8],
|
||||
refresh_token_hash: &[u8],
|
||||
access_expires_at: DateTime<Utc>,
|
||||
refresh_expires_at: DateTime<Utc>,
|
||||
) -> Result<bool, RepositoryError>
|
||||
where
|
||||
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||
{
|
||||
Ok(db::rotate_refresh_token(
|
||||
executor,
|
||||
old_refresh_hash,
|
||||
access_token_hash,
|
||||
refresh_token_hash,
|
||||
access_expires_at,
|
||||
refresh_expires_at,
|
||||
)
|
||||
.await?)
|
||||
}
|
||||
|
||||
pub async fn revoke<'e, E>(executor: E, token_hash: &[u8]) -> Result<(), RepositoryError>
|
||||
where
|
||||
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||
{
|
||||
Ok(db::revoke(executor, token_hash).await?)
|
||||
}
|
||||
|
||||
fn encode(token: Token) -> TokenRow {
|
||||
TokenRow {
|
||||
id: token.id,
|
||||
app_id: token.app_id,
|
||||
access_token_hash: token.access_token_hash,
|
||||
refresh_token_hash: token.refresh_token_hash,
|
||||
access_expires_at: token.access_expires_at,
|
||||
refresh_expires_at: token.refresh_expires_at,
|
||||
revoked_at: token.revoked_at,
|
||||
created_at: token.created_at,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,194 @@
|
||||
use uuid::Uuid;
|
||||
|
||||
use super::{
|
||||
QueryConversionError, ReferenceRelation, ReferenceViolationKind, RepositoryError, dtos::Upsert,
|
||||
};
|
||||
use crate::{
|
||||
database::{Constraint, DatabaseError, dtos::UserAppSecretRow, user_app_secrets as db},
|
||||
domain::UserAppSecret,
|
||||
};
|
||||
|
||||
pub struct Query {
|
||||
pub app_id: Uuid,
|
||||
pub limit: Option<u64>,
|
||||
pub offset: Option<u64>,
|
||||
}
|
||||
|
||||
impl Default for Query {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
app_id: Uuid::nil(),
|
||||
limit: None,
|
||||
offset: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<Query> for db::Query {
|
||||
type Error = QueryConversionError;
|
||||
|
||||
fn try_from(query: Query) -> Result<Self, Self::Error> {
|
||||
Ok(db::Query {
|
||||
app_id: query.app_id,
|
||||
limit: query
|
||||
.limit
|
||||
.map(|limit| i64::try_from(limit).map_err(|_| QueryConversionError::Limit(limit)))
|
||||
.transpose()?,
|
||||
offset: query
|
||||
.offset
|
||||
.map(|offset| {
|
||||
i64::try_from(offset).map_err(|_| QueryConversionError::Offset(offset))
|
||||
})
|
||||
.transpose()?,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn list<'e, E>(executor: E, query: Query) -> Result<Vec<UserAppSecret>, RepositoryError>
|
||||
where
|
||||
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||
{
|
||||
Ok(db::query(executor, query.try_into()?)
|
||||
.await?
|
||||
.into_iter()
|
||||
.map(decode)
|
||||
.collect())
|
||||
}
|
||||
|
||||
pub async fn get<'e, E>(
|
||||
executor: E,
|
||||
app_id: Uuid,
|
||||
id: Uuid,
|
||||
) -> Result<Option<UserAppSecret>, RepositoryError>
|
||||
where
|
||||
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||
{
|
||||
Ok(db::get_by_id(executor, app_id, id).await?.map(decode))
|
||||
}
|
||||
|
||||
pub async fn upsert<'e, E>(
|
||||
executor: E,
|
||||
secret: UserAppSecret,
|
||||
) -> Result<Upsert<UserAppSecret>, RepositoryError>
|
||||
where
|
||||
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||
{
|
||||
let upsert = db::upsert(executor, encode(secret))
|
||||
.await
|
||||
.map_err(classify_upsert_error)?;
|
||||
|
||||
Ok(match upsert {
|
||||
crate::database::dtos::Upsert::Created(row) => Upsert::Created(decode(row)),
|
||||
crate::database::dtos::Upsert::Updated(row) => Upsert::Updated(decode(row)),
|
||||
})
|
||||
}
|
||||
|
||||
fn classify_upsert_error(error: DatabaseError) -> RepositoryError {
|
||||
match &error {
|
||||
DatabaseError::ForeignKeyViolation {
|
||||
constraint: Constraint::UserAppSecretApp,
|
||||
..
|
||||
} => RepositoryError::ReferenceViolation {
|
||||
relation: ReferenceRelation::UserApp,
|
||||
kind: ReferenceViolationKind::Missing,
|
||||
source: error,
|
||||
},
|
||||
_ => RepositoryError::Database(error),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn verify<'e, E>(
|
||||
executor: E,
|
||||
app_id: Uuid,
|
||||
secret_hash: &[u8],
|
||||
) -> Result<bool, RepositoryError>
|
||||
where
|
||||
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||
{
|
||||
Ok(db::verify(executor, app_id, secret_hash).await?)
|
||||
}
|
||||
|
||||
pub async fn delete<'e, E>(
|
||||
executor: E,
|
||||
app_id: Uuid,
|
||||
id: Uuid,
|
||||
) -> Result<super::dtos::Delete, RepositoryError>
|
||||
where
|
||||
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||
{
|
||||
Ok(db::delete_by_id(executor, app_id, id).await?.into())
|
||||
}
|
||||
|
||||
fn decode(row: UserAppSecretRow) -> UserAppSecret {
|
||||
UserAppSecret {
|
||||
id: row.id,
|
||||
app_id: row.app_id,
|
||||
secret_hash: row.secret_hash,
|
||||
expires_at: row.expires_at,
|
||||
created_at: row.created_at,
|
||||
}
|
||||
}
|
||||
|
||||
fn encode(secret: UserAppSecret) -> UserAppSecretRow {
|
||||
UserAppSecretRow {
|
||||
id: secret.id,
|
||||
app_id: secret.app_id,
|
||||
secret_hash: secret.secret_hash,
|
||||
expires_at: secret.expires_at,
|
||||
created_at: secret.created_at,
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use chrono::Utc;
|
||||
|
||||
use super::*;
|
||||
|
||||
fn foreign_key_violation(constraint: Constraint) -> DatabaseError {
|
||||
DatabaseError::ForeignKeyViolation {
|
||||
constraint,
|
||||
source: sqlx::Error::Protocol("test database cause".to_owned()),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn classifies_user_app_secret_upsert_foreign_key_violation() {
|
||||
let error = classify_upsert_error(foreign_key_violation(Constraint::UserAppSecretApp));
|
||||
assert!(matches!(
|
||||
error,
|
||||
RepositoryError::ReferenceViolation {
|
||||
relation: ReferenceRelation::UserApp,
|
||||
kind: ReferenceViolationKind::Missing,
|
||||
..
|
||||
}
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn keeps_unknown_upsert_foreign_keys_generic() {
|
||||
let error = classify_upsert_error(foreign_key_violation(Constraint::Unnamed));
|
||||
assert!(matches!(error, RepositoryError::Database(_)));
|
||||
}
|
||||
|
||||
fn row() -> UserAppSecretRow {
|
||||
UserAppSecretRow {
|
||||
id: Uuid::new_v4(),
|
||||
app_id: Uuid::new_v4(),
|
||||
secret_hash: vec![7; 32],
|
||||
expires_at: Utc::now(),
|
||||
created_at: Utc::now(),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn round_trips_through_rows() {
|
||||
let secret = decode(row());
|
||||
let round_trip = decode(encode(secret.clone()));
|
||||
|
||||
assert_eq!(round_trip.id, secret.id);
|
||||
assert_eq!(round_trip.app_id, secret.app_id);
|
||||
assert_eq!(round_trip.secret_hash, secret.secret_hash);
|
||||
assert_eq!(round_trip.expires_at, secret.expires_at);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,172 @@
|
||||
use uuid::Uuid;
|
||||
|
||||
use super::{QueryConversionError, RepositoryError, dtos::Upsert};
|
||||
use crate::{
|
||||
database::{Constraint, DatabaseError, dtos::UserAppRow, user_apps as db},
|
||||
domain::UserApp,
|
||||
};
|
||||
|
||||
pub struct Query {
|
||||
pub user_id: Uuid,
|
||||
pub limit: Option<u64>,
|
||||
pub offset: Option<u64>,
|
||||
}
|
||||
|
||||
impl Default for Query {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
user_id: Uuid::nil(),
|
||||
limit: None,
|
||||
offset: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<Query> for db::Query {
|
||||
type Error = QueryConversionError;
|
||||
|
||||
fn try_from(query: Query) -> Result<Self, Self::Error> {
|
||||
Ok(db::Query {
|
||||
user_id: query.user_id,
|
||||
limit: query
|
||||
.limit
|
||||
.map(|limit| i64::try_from(limit).map_err(|_| QueryConversionError::Limit(limit)))
|
||||
.transpose()?,
|
||||
offset: query
|
||||
.offset
|
||||
.map(|offset| {
|
||||
i64::try_from(offset).map_err(|_| QueryConversionError::Offset(offset))
|
||||
})
|
||||
.transpose()?,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn list<'e, E>(executor: E, query: Query) -> Result<Vec<UserApp>, RepositoryError>
|
||||
where
|
||||
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||
{
|
||||
Ok(db::query(executor, query.try_into()?)
|
||||
.await?
|
||||
.into_iter()
|
||||
.map(decode)
|
||||
.collect())
|
||||
}
|
||||
|
||||
pub async fn get<'e, E>(
|
||||
executor: E,
|
||||
user_id: Uuid,
|
||||
id: Uuid,
|
||||
) -> Result<Option<UserApp>, RepositoryError>
|
||||
where
|
||||
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||
{
|
||||
Ok(db::get_by_id(executor, user_id, id).await?.map(decode))
|
||||
}
|
||||
|
||||
pub async fn upsert<'e, E>(executor: E, app: UserApp) -> Result<Upsert<UserApp>, RepositoryError>
|
||||
where
|
||||
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||
{
|
||||
let upsert = db::upsert(executor, encode(app))
|
||||
.await
|
||||
.map_err(classify_upsert_error)?;
|
||||
|
||||
Ok(match upsert {
|
||||
crate::database::dtos::Upsert::Created(row) => Upsert::Created(decode(row)),
|
||||
crate::database::dtos::Upsert::Updated(row) => Upsert::Updated(decode(row)),
|
||||
})
|
||||
}
|
||||
|
||||
fn classify_upsert_error(error: DatabaseError) -> RepositoryError {
|
||||
match &error {
|
||||
DatabaseError::ForeignKeyViolation {
|
||||
constraint: Constraint::UserAppUser,
|
||||
..
|
||||
} => RepositoryError::ReferenceViolation {
|
||||
relation: super::ReferenceRelation::User,
|
||||
kind: super::ReferenceViolationKind::Missing,
|
||||
source: error,
|
||||
},
|
||||
_ => RepositoryError::Database(error),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn delete<'e, E>(
|
||||
executor: E,
|
||||
user_id: Uuid,
|
||||
id: Uuid,
|
||||
) -> Result<super::dtos::Delete, RepositoryError>
|
||||
where
|
||||
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||
{
|
||||
Ok(db::delete_by_id(executor, user_id, id).await?.into())
|
||||
}
|
||||
|
||||
fn decode(row: UserAppRow) -> UserApp {
|
||||
UserApp {
|
||||
id: row.id,
|
||||
user_id: row.user_id,
|
||||
}
|
||||
}
|
||||
|
||||
fn encode(app: UserApp) -> UserAppRow {
|
||||
UserAppRow {
|
||||
id: app.id,
|
||||
user_id: app.user_id,
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn foreign_key_violation(constraint: Constraint) -> DatabaseError {
|
||||
DatabaseError::ForeignKeyViolation {
|
||||
constraint,
|
||||
source: sqlx::Error::Protocol("test database cause".to_owned()),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn classifies_user_app_upsert_foreign_key_violation() {
|
||||
let error = classify_upsert_error(foreign_key_violation(Constraint::UserAppUser));
|
||||
assert!(matches!(
|
||||
error,
|
||||
RepositoryError::ReferenceViolation {
|
||||
relation: super::super::ReferenceRelation::User,
|
||||
kind: super::super::ReferenceViolationKind::Missing,
|
||||
..
|
||||
}
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn keeps_unknown_upsert_foreign_keys_generic() {
|
||||
let error = classify_upsert_error(foreign_key_violation(Constraint::Unnamed));
|
||||
assert!(matches!(error, RepositoryError::Database(_)));
|
||||
}
|
||||
|
||||
fn row() -> UserAppRow {
|
||||
UserAppRow {
|
||||
id: Uuid::new_v4(),
|
||||
user_id: Uuid::new_v4(),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn decodes_valid_rows() {
|
||||
let row = row();
|
||||
let (id, user_id) = (row.id, row.user_id);
|
||||
let app = decode(row);
|
||||
|
||||
assert_eq!(app.id, id);
|
||||
assert_eq!(app.user_id, user_id);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn round_trips_through_rows() {
|
||||
let app = decode(row());
|
||||
assert_eq!(decode(encode(app.clone())), app);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user