53 lines
1.5 KiB
Rust
53 lines
1.5 KiB
Rust
use crate::error::Error;
|
|
use crate::AuthorizationCode;
|
|
use chrono::{Duration, Utc};
|
|
use database::sqlx::SqliteExecutor;
|
|
use database::AuthorizationCodes as DatabaseAuthorizationCodes;
|
|
use id::{AppID, UserID};
|
|
|
|
impl From<DatabaseAuthorizationCodes> for AuthorizationCode {
|
|
fn from(db: DatabaseAuthorizationCodes) -> Self {
|
|
Self {
|
|
// Info
|
|
code: db.code,
|
|
app: AppID(db.app),
|
|
user: UserID(db.user),
|
|
|
|
// Timings
|
|
created_at: db.created_at,
|
|
expires_at: db.expires_at,
|
|
used_at: db.used_at,
|
|
}
|
|
}
|
|
}
|
|
|
|
impl AuthorizationCode {
|
|
pub async fn insert(
|
|
conn: impl SqliteExecutor<'_>,
|
|
code: &str,
|
|
app: &AppID,
|
|
user: &UserID,
|
|
) -> Result<Option<()>, Error> {
|
|
let expires_at = Utc::now() + Duration::minutes(10);
|
|
|
|
Ok(DatabaseAuthorizationCodes::insert(
|
|
conn,
|
|
code,
|
|
app.as_ref(),
|
|
user.as_ref(),
|
|
expires_at.timestamp(),
|
|
)
|
|
.await?)
|
|
}
|
|
|
|
pub async fn get_one(conn: impl SqliteExecutor<'_>, code: &str) -> Result<Option<Self>, Error> {
|
|
Ok(DatabaseAuthorizationCodes::get_one(conn, code)
|
|
.await?
|
|
.map(Self::from))
|
|
}
|
|
|
|
/// Consume AuthorizationCode, mark as used
|
|
pub async fn use_code(self, conn: impl SqliteExecutor<'_>) -> Result<Option<()>, Error> {
|
|
Ok(DatabaseAuthorizationCodes::use_code(conn, &self.code).await?)
|
|
}
|
|
}
|