1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
|
use async_session::{Result, Session, SessionStore};
use async_trait::async_trait;
use shared_svc::cache::{CacheKey, RedisManager, redis::AsyncCommands};
use sqlx::{Executor, PgPool, Postgres};
use tracing::{debug, instrument};
use crate::Provider;
#[async_trait]
pub trait AccountMgr {
async fn get_apid_by_email(&self, email: &str) -> Result<Option<String>>;
async fn create_account<'c>(
&self,
provider: Provider,
provider_user_id: &str,
ap_id: &str,
email: &str,
transaction: Option<impl Executor<'c, Database = Postgres>>,
) -> Result;
async fn persist_session(&self);
}
#[derive(Debug, Clone)]
pub struct AuthService {
cache: RedisManager,
database: PgPool,
}
#[async_trait]
impl AccountMgr for AuthService {
#[instrument(skip(self))]
async fn get_apid_by_email(&self, email: &str) -> Result<Option<String>> {
todo!()
}
#[instrument(skip(transaction))]
async fn create_account<'c>(
&self,
provider: Provider,
provider_user_id: &str,
ap_id: &str,
email: &str,
transaction: Option<impl Executor<'c, Database = Postgres>>,
) -> Result {
let query = sqlx::query!(
"insert into account
(provider_id, provider_user_id, email, ap_id)
values
($1, $2, $3, $4)
on conflict (provider_id, provider_user_id)
do nothing
",
provider.to_string(),
provider_user_id,
email,
ap_id
);
if let Some(con) = transaction {
query.execute(con).await?;
} else {
query.execute(&self.database).await?;
};
todo!()
}
#[instrument(skip(self))]
async fn persist_session(&self) {}
}
impl AuthService {
pub fn new(cache: &RedisManager, database: &PgPool) -> Self {
Self {
cache: cache.clone(),
database: database.clone(),
}
}
}
#[async_trait]
impl SessionStore for AuthService {
#[doc = " Get a session from the storage backend."]
#[doc = ""]
#[doc = " The input is expected to be the value of an identifying"]
#[doc = " cookie. This will then be parsed by the session middleware"]
#[doc = " into a session if possible"]
#[instrument(skip(self, cookie_value))]
async fn load_session(&self, cookie_value: String) -> Result<Option<Session>> {
debug!("getting session");
let id = Session::id_from_cookie_value(&cookie_value)?;
let mut client = self.cache.get().await?;
let session = client
.get::<_, Option<Vec<u8>>>(CacheKey::Session(&id))
.await?;
match session {
Some(value) => Ok(Some(serde_json::from_slice(&value)?)),
None => Ok(None),
}
}
#[doc = " Store a session on the storage backend."]
#[doc = ""]
#[doc = " The return value is the value of the cookie to store for the"]
#[doc = " user that represents this session"]
#[instrument(err(Debug), skip(self, session), fields(id = session.id()))]
async fn store_session(&self, session: Session) -> Result<Option<String>> {
debug!("storing session");
let mut client = self.cache.get().await?;
let bytes = serde_json::to_vec(&session)?;
client
.set_ex::<_, _, ()>(CacheKey::Session(session.id()), bytes, 3600)
.await?;
Ok(session.into_cookie_value())
}
#[doc = " Remove a session from the session store"]
#[instrument(err(Debug), skip(self, session), fields(id = session.id()))]
async fn destroy_session(&self, session: Session) -> Result {
debug!("destroying session");
let mut client = self.cache.get().await?;
client.del::<_, ()>(CacheKey::Session(session.id())).await?;
Ok(())
}
#[doc = " Empties the entire store, destroying all sessions"]
#[instrument(skip(self))]
async fn clear_store(&self) -> Result {
debug!("clearing store");
let mut client = self.cache.get().await?;
client.del::<_, ()>(CacheKey::Session("").key()).await?;
Ok(())
}
}
async fn create_account_step<'c, E>(
provider: Provider,
provider_user_id: &str,
ap_id: &str,
email: &str,
transaction: E,
) -> Result
where
E: Executor<'c, Database = Postgres>,
{
sqlx::query!(
"insert into account
(provider_id, provider_user_id, email, ap_id)
values
($1, $2, $3, $4)
on conflict (provider_id, provider_user_id)
do nothing
",
provider.to_string(),
provider_user_id,
email,
ap_id
)
.execute(transaction)
.await?;
todo!()
}
|