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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
use std::fmt;
use url::Url;
use graphql_client::{ GraphQLQuery, Response };
#[doc(hidden)]
pub enum Error {
Network(reqwest::Error),
Message(&'static str),
GraphQL(Vec<graphql_client::Error>),
}
impl fmt::Debug for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Error::Network(err) => write!(f, "{:?}", err),
Error::Message(s) => write!(f, "{}", s),
Error::GraphQL(err) => write!(f, "{:?}", err),
}
}
}
impl From<reqwest::Error> for Error {
fn from(err: reqwest::Error) -> Error {
Error::Network(err)
}
}
impl From<&'static str> for Error {
fn from(err: &'static str) -> Error {
Error::Message(err)
}
}
#[derive(GraphQLQuery)]
#[graphql(
schema_path = "schema.graphql",
query_path = "api.graphql",
response_derives = "Debug",
)]
struct UserSearch;
#[derive(GraphQLQuery)]
#[graphql(
schema_path = "schema.graphql",
query_path = "api.graphql",
response_derives = "Debug",
)]
struct UserGet;
#[derive(GraphQLQuery)]
#[graphql(
schema_path = "schema.graphql",
query_path = "api.graphql",
response_derives = "Debug",
)]
struct TagsGet;
#[derive(GraphQLQuery)]
#[graphql(
schema_path = "schema.graphql",
query_path = "api.graphql",
response_derives = "Debug",
)]
struct CheckInTag;
pub type CheckInReturn = (bool, check_in_tag::UserData, check_in_tag::TagData);
pub struct CheckinAPI {
base_url: Url,
client: reqwest::blocking::Client,
auth_cookie: String,
}
impl CheckinAPI {
pub fn login(username: &str, password: &str, url: &str) -> Result<Self, Error> {
let client = reqwest::blocking::Client::new();
let base_url = Url::parse(url).expect("Invalid base URL configured");
let params = [("username", username), ("password", password)];
let response = client.post(base_url.join("/api/user/login").unwrap())
.form(¶ms)
.send()?;
if !response.status().is_success() {
return Err("Invalid username or password".into());
}
let cookies = response.headers().get_all(reqwest::header::SET_COOKIE);
let mut auth_token: Option<String> = None;
let auth_regex = regex::Regex::new(r"^auth=(?P<token>[a-f0-9]+);").unwrap();
for cookie in cookies.iter() {
if let Ok(cookie) = cookie.to_str() {
if let Some(capture) = auth_regex.captures(cookie) {
auth_token = Some(capture["token"].to_owned());
}
}
}
match auth_token {
Some(mut token) => {
token.insert_str(0, "auth=");
Ok(Self {
base_url,
client,
auth_cookie: token,
})
},
None => Err("No auth token set by server".into())
}
}
pub fn from_token(mut auth_token: String, url: &str) -> Self {
let client = reqwest::blocking::Client::new();
let base_url = Url::parse(url).expect("Invalid base URL configured");
auth_token.insert_str(0, "auth=");
Self { base_url, client, auth_cookie: auth_token }
}
pub fn auth_token(&self) -> &str {
&self.auth_cookie[5..]
}
pub fn add_user(&self, username: &str, password: &str) -> Result<(), Error> {
let params = [("username", username), ("password", password)];
let response = self.client.put(self.base_url.join("/api/user/update").unwrap())
.header(reqwest::header::COOKIE, self.auth_cookie.as_str())
.form(¶ms)
.send()?;
if !response.status().is_success() {
Err("Account creation unsuccessful".into())
}
else {
Ok(())
}
}
pub fn delete_user(&self, username: &str) -> Result<(), Error> {
let params = [("username", username)];
let response = self.client.delete(self.base_url.join("/api/user/update").unwrap())
.header(reqwest::header::COOKIE, self.auth_cookie.as_str())
.form(¶ms)
.send()?;
if !response.status().is_success() {
Err("Account deletion unsuccessful".into())
}
else {
Ok(())
}
}
fn checkin_action(&self, check_in: bool, uuid: &str, tag: &str) -> Result<CheckInReturn, Error> {
let body = CheckInTag::build_query(check_in_tag::Variables {
id: uuid.to_string(),
tag: tag.to_string(),
checkin: check_in,
});
let response: Response<check_in_tag::ResponseData> = self.client.post(self.base_url.join("/graphql").unwrap())
.header(reqwest::header::COOKIE, self.auth_cookie.as_str())
.json(&body)
.send()?
.json()?;
if let Some(errors) = response.errors {
return Err(Error::GraphQL(errors));
}
let data = match response.data {
Some(data) => data,
None => return Err("Check in API returned no data".into()),
};
let check_in_data = match data.check_in {
Some(check_in_data) => check_in_data,
None => return Err("Invalid user ID on badge".into()),
};
let user = check_in_data.user.user_data;
if !user.accepted || !user.confirmed {
return Err("User not accepted and confirmed".into());
}
let tag_details = check_in_data.tags.into_iter()
.map(|item| item.tag_data)
.find(|item| item.tag.name == tag)
.unwrap();
Ok((
tag_details.checkin_success,
user,
tag_details
))
}
pub fn check_in(&self, uuid: &str, tag: &str) -> Result<CheckInReturn, Error> {
self.checkin_action(true, uuid, tag)
}
pub fn check_out(&self, uuid: &str, tag: &str) -> Result<CheckInReturn, Error> {
self.checkin_action(false, uuid, tag)
}
pub fn get_tags_names(&self, only_current: bool) -> Result<Vec<String>, Error> {
let body = TagsGet::build_query(tags_get::Variables {
only_current
});
let response: Response<tags_get::ResponseData> = self.client.post(self.base_url.join("/graphql").unwrap())
.header(reqwest::header::COOKIE, self.auth_cookie.as_str())
.json(&body)
.send()?
.json()?;
if let Some(errors) = response.errors {
return Err(Error::GraphQL(errors));
}
if response.data.is_none() {
return Err("Check in API returned no data".into());
}
Ok(
response.data.unwrap()
.tags.into_iter()
.map(|tag| tag.name)
.collect()
)
}
}
#[cfg(test)]
mod checkin_api_tests {
use super::CheckinAPI;
#[test]
fn login() {
let username = std::env::var("CHECKIN_USERNAME").unwrap();
let password = std::env::var("CHECKIN_PASSWORD").unwrap();
let instance = CheckinAPI::login(&username, &password).unwrap();
assert_eq!(instance.auth_token().len(), 64);
instance.check_in("7dd00021-89fd-49f1-9c17-bd0ba7dcf97e", "123").unwrap();
instance.get_tags_names(true).unwrap();
instance.add_user("test_user", "just testing").unwrap();
instance.delete_user("test_user").unwrap();
}
}