Skip to content

Commit a8640c2

Browse files
committed
formatted backend with nightly rustfmt
1 parent 769aea6 commit a8640c2

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

80 files changed

+2197
-2266
lines changed

backend/auth/src/banned_set.rs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::{
22
collections::HashSet,
3-
sync::RwLock
3+
sync::RwLock,
44
};
55

66
use identifiers::user::UserUuid;
@@ -22,21 +22,15 @@ impl BannedSet {
2222
}
2323

2424
pub fn ban_user(&self, user_uuid: UserUuid) {
25-
self.0.write().unwrap().insert(
26-
user_uuid,
27-
);
25+
self.0.write().unwrap().insert(user_uuid);
2826
}
2927
/// True indicates that the user was unbanned.
3028
/// False indicates that the user was not in the banned set to begin with.
3129
pub fn unban_user(&self, user_uuid: &UserUuid) -> bool {
32-
self.0.write().unwrap().remove(
33-
user_uuid,
34-
)
30+
self.0.write().unwrap().remove(user_uuid)
3531
}
3632

3733
pub fn is_user_banned(&self, user_uuid: &UserUuid) -> bool {
38-
self.0.read().unwrap().contains(
39-
user_uuid,
40-
)
34+
self.0.read().unwrap().contains(user_uuid)
4135
}
4236
}

backend/auth/src/jwt.rs

Lines changed: 42 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1+
use error::JwtError;
12
use frank_jwt::{
2-
Algorithm,
3+
decode,
34
encode,
4-
decode
5+
Algorithm,
56
};
67
use serde_json;
7-
use Secret;
8-
use error::JwtError;
98
use wire::user::Jwt;
9+
use Secret;
1010

1111
#[cfg(feature = "rocket_support")]
1212
pub use self::rocket_support::user_authorization;
@@ -49,18 +49,26 @@ impl ServerJwt {
4949
}
5050
}
5151

52-
5352
#[cfg(feature = "rocket_support")]
5453
pub mod rocket_support {
5554
use super::*;
56-
use rocket::State;
57-
use rocket::http::Status;
58-
use rocket::request::{self, Request, FromRequest};
59-
use rocket::Outcome;
55+
use rocket::{
56+
http::Status,
57+
request::{
58+
self,
59+
FromRequest,
60+
Request,
61+
},
62+
Outcome,
63+
State,
64+
};
6065

61-
use identifiers::user::UserUuid;
62-
use wire::user::{UserRole, BEARER};
6366
use error::Error;
67+
use identifiers::user::UserUuid;
68+
use wire::user::{
69+
UserRole,
70+
BEARER,
71+
};
6472

6573
use chrono::Utc;
6674

@@ -79,13 +87,9 @@ pub mod rocket_support {
7987
}
8088
}
8189

82-
8390
/// Given a request, extract the JWT struct from the headers in the request.
8491
fn extract_jwt_from_request(request: &Request) -> request::Outcome<ServerJwt, Error> {
85-
let keys: Vec<_> = request
86-
.headers()
87-
.get("Authorization")
88-
.collect();
92+
let keys: Vec<_> = request.headers().get("Authorization").collect();
8993
if keys.len() != 1 {
9094
return Outcome::Failure((Status::Unauthorized, Error::MissingToken));
9195
};
@@ -101,10 +105,7 @@ pub mod rocket_support {
101105
}
102106
};
103107

104-
let authorization_words: Vec<String> = key.to_string()
105-
.split_whitespace()
106-
.map(String::from)
107-
.collect();
108+
let authorization_words: Vec<String> = key.to_string().split_whitespace().map(String::from).collect();
108109

109110
if authorization_words.len() != 2 {
110111
return Outcome::Failure((Status::Unauthorized, Error::MalformedToken));
@@ -132,15 +133,13 @@ pub mod rocket_support {
132133
Outcome::Success(jwt)
133134
}
134135

135-
136-
137136
pub mod user_authorization {
138137
use super::*;
139138

140139
trait FromJwt {
141140
fn from_jwt(jwt: &Jwt) -> Result<Self, RoleError>
142-
where
143-
Self: Sized;
141+
where
142+
Self: Sized;
144143
fn get_uuid(&self) -> UserUuid;
145144
}
146145

@@ -154,12 +153,9 @@ pub mod rocket_support {
154153

155154
impl FromJwt for NormalUser {
156155
fn from_jwt(jwt: &Jwt) -> Result<NormalUser, RoleError> {
157-
if jwt.user_roles.contains(
158-
&UserRole::Unprivileged,
159-
)
160-
{
161-
Ok(NormalUser { user_uuid: jwt.sub })
162-
} else {
156+
if jwt.user_roles.contains(&UserRole::Unprivileged) {
157+
Ok(NormalUser { user_uuid: jwt.sub })
158+
} else {
163159
Err(RoleError::InsufficientRights)
164160
}
165161
}
@@ -182,12 +178,9 @@ pub mod rocket_support {
182178

183179
impl FromJwt for AdminUser {
184180
fn from_jwt(jwt: &Jwt) -> Result<AdminUser, RoleError> {
185-
if jwt.user_roles.contains(
186-
&UserRole::Admin,
187-
)
188-
{
189-
Ok(AdminUser { user_uuid: jwt.sub })
190-
} else {
181+
if jwt.user_roles.contains(&UserRole::Admin) {
182+
Ok(AdminUser { user_uuid: jwt.sub })
183+
} else {
191184
Err(RoleError::InsufficientRights)
192185
}
193186
}
@@ -210,12 +203,9 @@ pub mod rocket_support {
210203

211204
impl FromJwt for ModeratorUser {
212205
fn from_jwt(jwt: &Jwt) -> Result<ModeratorUser, RoleError> {
213-
if jwt.user_roles.contains(
214-
&UserRole::Moderator,
215-
)
216-
{
217-
Ok(ModeratorUser { user_uuid: jwt.sub })
218-
} else {
206+
if jwt.user_roles.contains(&UserRole::Moderator) {
207+
Ok(ModeratorUser { user_uuid: jwt.sub })
208+
} else {
219209
Err(RoleError::InsufficientRights)
220210
}
221211
}
@@ -225,7 +215,6 @@ pub mod rocket_support {
225215
}
226216
}
227217

228-
229218
impl<'a, 'r> FromRequest<'a, 'r> for ModeratorUser {
230219
type Error = Error;
231220

@@ -234,10 +223,9 @@ pub mod rocket_support {
234223
}
235224
}
236225

237-
238226
fn extract_role_from_request<T>(request: &Request) -> request::Outcome<T, Error>
239-
where
240-
T: FromJwt,
227+
where
228+
T: FromJwt,
241229
{
242230
// Get the jwt from the request's header
243231
let jwt: ServerJwt = extract_jwt_from_request(request)?;
@@ -246,7 +234,14 @@ pub mod rocket_support {
246234

247235
let user = match T::from_jwt(&jwt.0) {
248236
Ok(user) => user,
249-
Err(_) => return Outcome::Failure((Status::Forbidden, Error::NotAuthorized { reason: "User does not have that role." })),
237+
Err(_) => {
238+
return Outcome::Failure((
239+
Status::Forbidden,
240+
Error::NotAuthorized {
241+
reason: "User does not have that role.",
242+
},
243+
))
244+
}
250245
};
251246

252247
// Check for stateful banned status

backend/auth/src/lib.rs

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,43 +7,39 @@
77
//! They will only succeed in creating themselves if the JWT contains the role the user type corresponds to.
88
//! By specifying one of these user types on a routable method, rocket will not route the request to it unless it can resolve the role in the JWT in the request header.
99
10-
11-
1210
#[cfg(feature = "rocket_support")]
1311
extern crate rocket;
1412
#[cfg(feature = "rocket_support")]
1513
extern crate rocket_contrib;
1614

17-
extern crate wire;
15+
extern crate chrono;
1816
extern crate crypto;
1917
extern crate frank_jwt;
20-
extern crate chrono;
18+
extern crate wire;
2119
#[macro_use]
2220
extern crate serde_json;
2321
#[macro_use]
2422
extern crate log;
25-
extern crate simplelog;
26-
extern crate identifiers;
2723
extern crate error;
24+
extern crate identifiers;
2825
extern crate rand;
26+
extern crate simplelog;
2927

28+
mod banned_set;
3029
mod jwt;
3130
mod password;
32-
mod banned_set;
3331
mod secret;
3432

35-
33+
pub use banned_set::BannedSet;
3634
pub use jwt::{
3735
user_authorization,
38-
ServerJwt
36+
ServerJwt,
3937
};
4038
pub use password::{
4139
hash_password,
42-
verify_hash
40+
verify_hash,
4341
};
44-
pub use banned_set::BannedSet;
4542
pub use secret::Secret;
4643

47-
4844
#[cfg(test)]
4945
mod test;

backend/auth/src/password.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
use std::io;
21
use crypto::scrypt;
2+
use std::io;
33

44
/// Hashes the password to a String that should be stored in the database.
55
pub fn hash_password(password: &str) -> io::Result<String> {

backend/auth/src/secret.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use rand::{
22
self,
3-
Rng
3+
Rng,
44
};
55

66
/// The secret contains a random string that is generated at startup.
@@ -12,20 +12,19 @@ pub struct Secret(pub String);
1212

1313
impl Secret {
1414
pub fn generate() -> Secret {
15-
let key = rand::thread_rng()
16-
.gen_ascii_chars()
17-
.take(256)
18-
.collect::<String>();
15+
let key = rand::thread_rng().gen_ascii_chars().take(256).collect::<String>();
1916
Secret(key)
2017
}
2118

2219
pub fn from_user_supplied_string(key: &str) -> Secret {
2320
if key.len() <= 128 {
2421
panic!("The secret key must be equal to or greater than 128 characters.")
2522
} else if key.len() < 256 {
26-
warn!("The secret key should be longer than 256 characters. It is {} characters long", key.len());
23+
warn!(
24+
"The secret key should be longer than 256 characters. It is {} characters long",
25+
key.len()
26+
);
2727
}
2828
Secret(key.to_string())
2929
}
3030
}
31-

backend/auth/src/test.rs

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
use super::*;
2-
use wire::{
3-
user::{
4-
UserRole,
5-
Jwt
6-
}
7-
};
82
use chrono::Utc;
3+
use wire::user::{
4+
Jwt,
5+
UserRole,
6+
};
97

108
//use log::{info, warn};
119
use identifiers::user::UserUuid;
1210

13-
1411
#[test]
1512
fn password_hash_and_verify() {
1613
let plaintext: &str = "12345";
@@ -32,7 +29,8 @@ fn jwt() {
3229
let jwt = ServerJwt(jwt);
3330

3431
let jwt_string: String = jwt.encode_jwt_string(&secret).unwrap();
35-
let decoded_jwt: ServerJwt = ServerJwt::decode_jwt_string(&jwt_string, &secret).expect("JWT should be decoded from the provided string");
32+
let decoded_jwt: ServerJwt =
33+
ServerJwt::decode_jwt_string(&jwt_string, &secret).expect("JWT should be decoded from the provided string");
3634
assert_eq!(jwt, decoded_jwt);
3735
}
3836

@@ -53,9 +51,7 @@ fn jwt_tampering_detected() {
5351
// alter the username of a copy of the accepted jwt
5452
let mut altered_jwt: ServerJwt = jwt.clone();
5553
altered_jwt.0.user_roles = vec![UserRole::Admin];
56-
let altered_jwt_string = altered_jwt
57-
.encode_jwt_string(&secret)
58-
.unwrap();
54+
let altered_jwt_string = altered_jwt.encode_jwt_string(&secret).unwrap();
5955
// split the JWTs
6056
let split_jwt: Vec<&str> = jwt_string.split(".").collect();
6157
let split_altered_jwt: Vec<&str> = altered_jwt_string.split(".").collect();
@@ -69,4 +65,3 @@ fn jwt_tampering_detected() {
6965
panic!("Should not be able to decode this modified jwt.");
7066
}
7167
}
72-

backend/db/src/calls/answer.rs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
use crate::{
2-
schema::answers,
3-
user::User,
4-
question::Question,
52
calls::prelude::*,
6-
schema
3+
question::Question,
4+
schema::{
5+
self,
6+
answers,
7+
},
8+
user::User,
79
};
810
//use error::JoeResult;
9-
use uuid::Uuid;
1011
use diesel::pg::PgConnection;
1112
use error::BackendResult;
1213
use identifiers::answer::AnswerUuid;
14+
use uuid::Uuid;
1315

1416
#[derive(Debug, Clone, Identifiable, Queryable, Associations, TypeName)]
1517
#[primary_key(uuid)]
@@ -39,13 +41,13 @@ pub struct AnswerData {
3941
}
4042

4143
impl Answer {
42-
pub fn get_answer(uuid: AnswerUuid,conn: &PgConnection) -> BackendResult<Answer> {
43-
get_row::<Answer,_>(schema::answers::table, uuid.0, conn)
44+
pub fn get_answer(uuid: AnswerUuid, conn: &PgConnection) -> BackendResult<Answer> {
45+
get_row::<Answer, _>(schema::answers::table, uuid.0, conn)
4446
}
4547
pub fn delete_answer(uuid: AnswerUuid, conn: &PgConnection) -> BackendResult<Answer> {
46-
delete_row::<Answer,_>(schema::answers::table, uuid.0, conn)
48+
delete_row::<Answer, _>(schema::answers::table, uuid.0, conn)
4749
}
4850
pub fn create_answer(new: NewAnswer, conn: &PgConnection) -> BackendResult<Answer> {
49-
create_row::<Answer, NewAnswer,_>(schema::answers::table, new, conn)
51+
create_row::<Answer, NewAnswer, _>(schema::answers::table, new, conn)
5052
}
5153
}

0 commit comments

Comments
 (0)