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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
use async_graphql::{
dynamic::{Field, FieldFuture, InputValue, TypeRef},
Value,
};
use bson::{doc, Bson};
use log::{debug, error, trace};
use crate::{
configuration::subgraph::data_sources::sql::DialectEnum,
data_sources::{sql::PoolEnum, DataSource, DataSources},
graphql::schema::ServiceSchema,
};
impl ServiceSchema {
pub fn create_register_start(mut self) -> Self {
debug!("Creating register start");
let auth_config = match self.subgraph_config.service.auth.clone() {
Some(auth) => auth,
None => {
panic!("Auth config not found.");
}
};
let resolver = Field::new(
"register_start",
TypeRef::named_nn(TypeRef::STRING),
move |ctx| {
debug!("Resolving register start");
let auth_config = auth_config.clone();
FieldFuture::new(async move {
let identifier = match ctx.args.try_get("identifier") {
Ok(input) => input
.deserialize::<String>().map_err(|e| {
error!("Failed to get input: {:?}", e);
async_graphql::Error::new(format!("Failed to get input: {:?}", e))
})?,
Err(e) => {
return Err(async_graphql::Error::new(format!(
"Failed to get input: {:?}",
e
)))
}
};
//match name of data source to the auth_config.data_source string
let data_sources = ctx.data_unchecked::<DataSources>();
let data_source = DataSources::get_data_source_by_name(
&data_sources,
&auth_config.data_source,
);
// Check if user exists. If previous register, reject, else delete the user.
let user = ServiceSchema::get_user(&data_source, &identifier).await;
if !user.is_err() && user.clone().unwrap().clone().is_some() {
if user.clone().unwrap().unwrap().passkey.is_some() {
error!("User already exists: {:?}", &identifier);
return Err(async_graphql::Error::new(format!(
"User already exists: {:?}",
&identifier
)));
}else {
ServiceSchema::delete_user(&data_source, &identifier).await?;
}
}
trace!("Creating webauthn service");
let webauthn = ServiceSchema::build_webauthn(&auth_config).map_err(|e| {
error!("Failed to build webauthn: {:?}", e);
async_graphql::Error::new(format!("Failed to build webauthn: {:?}", e))
})?;
let user_uuid = uuid::Uuid::new_v4();
let (ccr, reg_state) = webauthn.start_passkey_registration(
user_uuid.clone(),
&identifier,
&identifier,
None,
).map_err(|e| {
error!("Failed to start passkey registration: {:?}", e);
async_graphql::Error::new(format!(
"Failed to start passkey registration: {:?}",
e
))
})?;
let reg_state = match serde_json::to_string(®_state) {
Ok(reg_state) => reg_state,
Err(e) => {
return Err(async_graphql::Error::new(format!(
"Failed to serialize registration state: {}",
e
)))
}
};
let user_uuid_string = user_uuid.to_string();
// Save registration state to database
match &data_source {
DataSource::Mongo(mongo_ds) => {
let user = doc! {
"uuid": user_uuid_string.to_string(),
"identifier": identifier.clone(),
"registration_state": ®_state,
"authentication_state": Bson::Null,
"passkey": Bson::Null,
};
mongo_ds
.db
.collection("subgraph_user")
.insert_one(user, None)
.await?;
}
DataSource::SQL(sql_ds) => {
match sql_ds.config.dialect {
DialectEnum::MYSQL => {
let query = sqlx::query("INSERT INTO subgraph_user (uuid, identifier, registration_state) VALUES (?, ?, ?);")
.bind(&user_uuid_string)
.bind(&identifier)
.bind(®_state);
match sql_ds.pool.clone() {
PoolEnum::MySql(pool) => {
query.execute(&pool).await?;
}
_ => unreachable!(),
};
}
DialectEnum::SQLITE => {
let query = sqlx::query("INSERT INTO subgraph_user (uuid, identifier, registration_state) VALUES (?, ?, ?);")
.bind(&user_uuid_string)
.bind(&identifier)
.bind(®_state);
match sql_ds.pool.clone() {
PoolEnum::SqLite(pool) => {
query.execute(&pool).await?;
}
_ => unreachable!(),
};
}
DialectEnum::POSTGRES => {
let query = sqlx::query("INSERT INTO subgraph_user (uuid, identifier, registration_state) VALUES ($1, $2, $3);")
.bind(&user_uuid)
.bind(&identifier)
.bind(®_state);
match sql_ds.pool.clone() {
PoolEnum::Postgres(pool) => {
query.execute(&pool).await?;
}
_ => unreachable!(),
};
}
};
}
_ => panic!("Data Source not supported."),
};
trace!("Challenge created: {:?}", ccr);
trace!("Registration state created: {:?}", reg_state);
let json = match serde_json::to_value(&ccr) {
Ok(json) => json,
Err(e) => {
return Err(async_graphql::Error::new(format!(
"Failed to serialize challenge: {}",
e
)))
}
};
let value = Value::from_json(json);
match value {
Ok(value) => Ok(Some(value)),
Err(_) => Err(async_graphql::Error::new("Failed to resolve challenge.")),
}
})
},
)
.argument(InputValue::new(
"identifier",
TypeRef::named_nn(TypeRef::STRING),
));
self.mutation = self.mutation.field(resolver);
self
}
}