ezidam: oauth: redirect: get and check code, get user info, mark code as used, display html template

This commit is contained in:
Philippe Loctaux 2023-03-18 00:40:11 +01:00
parent 719048e268
commit 827bba041a
15 changed files with 310 additions and 19 deletions

View file

@ -1,11 +1,11 @@
use crate::error::Error;
use crate::AuthorizationCodes;
use crate::AuthorizationCode;
use chrono::{Duration, Utc};
use database::sqlx::SqliteExecutor;
use database::AuthorizationCodes as DatabaseAuthorizationCodes;
use id::{AppID, UserID};
impl From<DatabaseAuthorizationCodes> for AuthorizationCodes {
impl From<DatabaseAuthorizationCodes> for AuthorizationCode {
fn from(db: DatabaseAuthorizationCodes) -> Self {
Self {
// Info
@ -21,7 +21,7 @@ impl From<DatabaseAuthorizationCodes> for AuthorizationCodes {
}
}
impl AuthorizationCodes {
impl AuthorizationCode {
pub async fn insert(
conn: impl SqliteExecutor<'_>,
code: &str,
@ -39,4 +39,15 @@ impl AuthorizationCodes {
)
.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?)
}
}

View file

@ -7,7 +7,7 @@ use id::{AppID, UserID};
pub use crate::error::Error;
#[derive(Debug)]
pub struct AuthorizationCodes {
pub struct AuthorizationCode {
// Info
code: String,
app: AppID,
@ -18,3 +18,12 @@ pub struct AuthorizationCodes {
expires_at: DateTime<Utc>,
used_at: Option<DateTime<Utc>>,
}
impl AuthorizationCode {
pub fn has_been_used(&self) -> bool {
self.used_at.is_some()
}
pub fn has_expired(&self) -> bool {
self.expires_at < Utc::now()
}
}