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
use crate::repository;
use log::{error, info};
use minerva_data as lib_data;
use minerva_data::db::DBPool;
use minerva_rpc as lib_rpc;
use minerva_rpc::session::session_server::Session;
use minerva_rpc::{messages, metadata};
use std::collections::HashMap;
use tonic::{Request, Response, Status};
#[derive(Clone)]
pub struct SessionService {
pub pools: HashMap<String, (DBPool, mongodb::Client, redis::Client)>,
}
#[tonic::async_trait]
impl Session for SessionService {
async fn generate(
&self,
req: Request<messages::SessionCreationData>,
) -> Result<Response<messages::SessionToken>, Status> {
let tenant = metadata::get_value(req.metadata(), "tenant").ok_or_else(|| {
error!("Tenant not found on request metadata!");
Status::failed_precondition("Missing tenant on request metadata")
})?;
let requestor = metadata::get_value(req.metadata(), "requestor").ok_or_else(|| {
error!("Requestor not found on request metadata!");
Status::failed_precondition("Missing requestor on request metadata")
})?;
info!(
"{}",
lib_data::log::format(
lib_rpc::get_address(&req),
&requestor,
&tenant,
"generate user session",
)
);
let data = req.into_inner().into();
let token = {
let (postgres, mongodb, redis) =
self.pools.get(&tenant).expect("Unable to find tenant");
let mongo = mongodb.database(&tenant);
repository::create_session(&tenant, data, postgres.clone(), mongo, redis).await
}?;
Ok(Response::new(messages::SessionToken { token }))
}
async fn retrieve(
&self,
req: Request<messages::SessionToken>,
) -> Result<Response<messages::SessionData>, Status> {
let tenant = metadata::get_value(req.metadata(), "tenant").ok_or_else(|| {
error!("Tenant not found on request metadata!");
Status::failed_precondition("Missing tenant on request metadata")
})?;
let requestor = metadata::get_value(req.metadata(), "requestor").ok_or_else(|| {
error!("Requestor not found on request metadata!");
Status::failed_precondition("Missing requestor on request metadata")
})?;
info!(
"{}",
lib_data::log::format(
lib_rpc::get_address(&req),
&requestor,
&tenant,
"retrieve user session",
)
);
let token = req.into_inner().token;
let (_, mongodb, redis) = self.pools.get(&tenant).expect("Unable to find tenant");
let mongo = mongodb.database(&tenant);
let session = repository::recover_session(&tenant, token, mongo, redis).await?;
Ok(Response::new(session.into()))
}
async fn remove(&self, req: Request<messages::SessionToken>) -> Result<Response<()>, Status> {
let tenant = metadata::get_value(req.metadata(), "tenant").ok_or_else(|| {
error!("Tenant not found on request metadata!");
Status::failed_precondition("Missing tenant on request metadata")
})?;
let requestor = metadata::get_value(req.metadata(), "requestor").ok_or_else(|| {
error!("Requestor not found on request metadata!");
Status::failed_precondition("Missing requestor on request metadata")
})?;
info!(
"{}",
lib_data::log::format(
lib_rpc::get_address(&req),
&requestor,
&tenant,
"remove user session",
)
);
let token = req.into_inner().token;
let (postgres, mongodb, redis) = self.pools.get(&tenant).expect("Unable to find tenant");
let mongo = mongodb.database(&tenant);
let _ = repository::remove_session(&tenant, token, postgres.clone(), mongo, redis).await?;
Ok(Response::new(()))
}
}