Skip to content

Commit da77183

Browse files
author
Thomas Bahn
committed
Format code using rustfmt
1 parent 2d7aa24 commit da77183

File tree

5 files changed

+53
-58
lines changed

5 files changed

+53
-58
lines changed

src/client.rs

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ use std::borrow::Cow;
22
use std::io;
33

44
use base64;
5-
use rand::distributions::IndependentSample;
65
use rand::distributions::range::Range;
7-
use rand::{Rng, OsRng};
6+
use rand::distributions::IndependentSample;
7+
use rand::{OsRng, Rng};
88
use ring::digest::SHA256_OUTPUT_LEN;
99
use ring::hmac;
1010

11-
use utils::{hash_password, find_proofs};
12-
use error::{Error, Kind, Field};
11+
use error::{Error, Field, Kind};
12+
use utils::{find_proofs, hash_password};
1313
use NONCE_LENGTH;
1414

1515
#[deprecated(since = "0.2.0", note = "Please use `ScramClient` instead. (exported at crate root)")]
@@ -37,21 +37,20 @@ fn parse_server_first(data: &str) -> Result<(&str, Vec<u8>, u16), Error> {
3737
}
3838
};
3939
let salt = match parts.next() {
40-
Some(part) if &part.as_bytes()[..2] == b"s=" => {
41-
try!(base64::decode(part[2..].as_bytes()).map_err(|_| {
42-
Error::Protocol(Kind::InvalidField(Field::Salt))
43-
}))
44-
}
40+
Some(part) if &part.as_bytes()[..2] == b"s=" => try!(
41+
base64::decode(part[2..].as_bytes())
42+
.map_err(|_| Error::Protocol(Kind::InvalidField(Field::Salt)))
43+
),
4544
_ => {
4645
return Err(Error::Protocol(Kind::ExpectedField(Field::Salt)));
4746
}
4847
};
4948
let iterations = match parts.next() {
50-
Some(part) if &part.as_bytes()[..2] == b"i=" => {
51-
try!(part[2..].parse().map_err(|_| {
52-
Error::Protocol(Kind::InvalidField(Field::Iterations))
53-
}))
54-
}
49+
Some(part) if &part.as_bytes()[..2] == b"i=" => try!(
50+
part[2..]
51+
.parse()
52+
.map_err(|_| Error::Protocol(Kind::InvalidField(Field::Iterations)))
53+
),
5554
_ => {
5655
return Err(Error::Protocol(Kind::ExpectedField(Field::Iterations)));
5756
}
@@ -64,11 +63,8 @@ fn parse_server_final(data: &str) -> Result<Vec<u8>, Error> {
6463
return Err(Error::Protocol(Kind::ExpectedField(Field::VerifyOrError)));
6564
}
6665
match &data[..2] {
67-
"v=" => {
68-
base64::decode(&data.as_bytes()[2..]).map_err(|_| {
69-
Error::Protocol(Kind::InvalidField(Field::VerifyOrError))
70-
})
71-
}
66+
"v=" => base64::decode(&data.as_bytes()[2..])
67+
.map_err(|_| Error::Protocol(Kind::InvalidField(Field::VerifyOrError))),
7268
"e=" => Err(Error::Authentication(data[2..].to_string())),
7369
_ => Err(Error::Protocol(Kind::ExpectedField(Field::VerifyOrError))),
7470
}
@@ -128,7 +124,11 @@ impl<'a> ScramClient<'a> {
128124
let nonce: String = (0..NONCE_LENGTH)
129125
.map(move |_| {
130126
let x: u8 = range.ind_sample(&mut rng);
131-
if x > 43 { (x + 1) as char } else { x as char }
127+
if x > 43 {
128+
(x + 1) as char
129+
} else {
130+
x as char
131+
}
132132
})
133133
.collect();
134134

@@ -187,14 +187,11 @@ impl<'a> ServerFirst<'a> {
187187
/// * Error::Protocol
188188
/// * Error::UnsupportedExtension
189189
pub fn handle_server_first(self, server_first: &str) -> Result<ClientFinal, Error> {
190-
191190
let (nonce, salt, iterations) = try!(parse_server_first(server_first));
192191
if !nonce.starts_with(&self.client_nonce) {
193192
return Err(Error::Protocol(Kind::InvalidNonce));
194193
}
195-
196194
let salted_password = hash_password(self.password, iterations, &salt);
197-
198195
let (client_proof, server_signature): ([u8; SHA256_OUTPUT_LEN], hmac::Signature) =
199196
find_proofs(
200197
&self.gs2header,
@@ -233,7 +230,9 @@ impl ClientFinal {
233230
/// method to continue the SCRAM handshake.
234231
#[inline]
235232
pub fn client_final(self) -> (ServerFinal, String) {
236-
let server_final = ServerFinal { server_signature: self.server_signature };
233+
let server_final = ServerFinal {
234+
server_signature: self.server_signature,
235+
};
237236
(server_final, self.client_final)
238237
}
239238
}

src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,11 +136,11 @@ const NONCE_LENGTH: usize = 24;
136136

137137
#[macro_use]
138138
mod utils;
139-
mod error;
140139
pub mod client;
140+
mod error;
141141
pub mod server;
142142

143143
pub use client::ScramClient;
144-
pub use error::{Error, Kind, Field};
145-
pub use server::{ScramServer, AuthenticationProvider, PasswordInfo, AuthenticationStatus};
144+
pub use error::{Error, Field, Kind};
145+
pub use server::{AuthenticationProvider, AuthenticationStatus, PasswordInfo, ScramServer};
146146
pub use utils::hash_password;

src/server.rs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ use std::borrow::Cow;
22
use std::io;
33

44
use base64;
5-
use rand::distributions::IndependentSample;
65
use rand::distributions::range::Range;
7-
use rand::{Rng, OsRng};
6+
use rand::distributions::IndependentSample;
7+
use rand::{OsRng, Rng};
88
use ring::digest::SHA256_OUTPUT_LEN;
99
use ring::hmac;
1010

11-
use error::{Error, Kind, Field};
11+
use error::{Error, Field, Kind};
1212
use utils::find_proofs;
1313
use NONCE_LENGTH;
1414

@@ -187,7 +187,11 @@ impl<'a, P: AuthenticationProvider> ServerFirst<'a, P> {
187187
let server_nonce: String = (0..NONCE_LENGTH)
188188
.map(move |_| {
189189
let x: u8 = range.ind_sample(&mut *rng);
190-
if x > 43 { (x + 1) as char } else { x as char }
190+
if x > 43 {
191+
(x + 1) as char
192+
} else {
193+
x as char
194+
}
191195
})
192196
.collect();
193197
let mut nonce = self.client_nonce.to_string();
@@ -262,8 +266,7 @@ impl<'a, P: AuthenticationProvider> ClientFinal<'a, P> {
262266
status: AuthenticationStatus::NotAuthorized,
263267
signature: format!(
264268
"e=User '{}' not authorized to act as '{}'",
265-
self.authcid,
266-
authzid
269+
self.authcid, authzid
267270
),
268271
})
269272
}
@@ -334,8 +337,8 @@ impl ServerFinal {
334337

335338
#[cfg(test)]
336339
mod tests {
337-
use super::{parse_client_first, parse_client_final};
338-
use super::super::{Error, Kind, Field};
340+
use super::super::{Error, Field, Kind};
341+
use super::{parse_client_final, parse_client_first};
339342

340343
#[test]
341344
fn test_parse_client_first_success() {
@@ -344,8 +347,8 @@ mod tests {
344347
assert!(authzid.is_none());
345348
assert_eq!(nonce, "abcdefghijk");
346349

347-
let (authcid, authzid, nonce) = parse_client_first("y,a=other user,n=user,r=abcdef=hijk")
348-
.unwrap();
350+
let (authcid, authzid, nonce) =
351+
parse_client_first("y,a=other user,n=user,r=abcdef=hijk").unwrap();
349352
assert_eq!(authcid, "user");
350353
assert_eq!(authzid, Some("other user"));
351354
assert_eq!(nonce, "abcdef=hijk");

src/utils.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@ use std::borrow::Cow;
22

33
use base64;
44
use ring::digest::{digest, SHA256, SHA256_OUTPUT_LEN};
5-
use ring::hmac::{self, SigningKey, SigningContext};
5+
use ring::hmac::{self, SigningContext, SigningKey};
66
use ring::pbkdf2;
77

88
/// Parses a part of a SCRAM message, after it has been split on commas.
99
/// Checks to make sure there's a key, and then verifies its the right key.
1010
/// Returns everything after the first '='.
1111
/// Returns a `ExpectedField` error when one of the above conditions fails.
1212
macro_rules! parse_part {
13-
($iter: expr, $field: ident, $key: expr) => (
13+
($iter:expr, $field:ident, $key:expr) => {
1414
if let Some(part) = $iter.next() {
1515
if part.len() < 2 {
1616
return Err(Error::Protocol(Kind::ExpectedField(Field::$field)));
@@ -22,7 +22,7 @@ macro_rules! parse_part {
2222
} else {
2323
return Err(Error::Protocol(Kind::ExpectedField(Field::$field)));
2424
}
25-
);
25+
};
2626
}
2727

2828
/// Hashes a password with SHA-256 with the given salt and number of iterations. This should
@@ -66,8 +66,6 @@ pub fn find_proofs(
6666
client_final_without_proof.as_bytes(),
6767
];
6868

69-
70-
7169
let salted_password_signing_key = SigningKey::new(&SHA256, salted_password);
7270
let client_key = hmac::sign(&salted_password_signing_key, b"Client Key");
7371
let server_key = hmac::sign(&salted_password_signing_key, b"Server Key");

tests/client_server.rs

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
extern crate scram;
21
extern crate rand;
32
extern crate ring;
3+
extern crate scram;
44

55
use ring::digest::SHA256_OUTPUT_LEN;
66
use scram::*;
@@ -24,20 +24,16 @@ impl TestProvider {
2424
impl server::AuthenticationProvider for TestProvider {
2525
fn get_password_for(&self, username: &str) -> Option<server::PasswordInfo> {
2626
match username {
27-
"user" => {
28-
Some(server::PasswordInfo::new(
29-
self.user_password.to_vec(),
30-
4096,
31-
"salt".bytes().collect(),
32-
))
33-
}
34-
"admin" => {
35-
Some(server::PasswordInfo::new(
36-
self.admin_password.to_vec(),
37-
8192,
38-
"messy".bytes().collect(),
39-
))
40-
}
27+
"user" => Some(server::PasswordInfo::new(
28+
self.user_password.to_vec(),
29+
4096,
30+
"salt".bytes().collect(),
31+
)),
32+
"admin" => Some(server::PasswordInfo::new(
33+
self.admin_password.to_vec(),
34+
8192,
35+
"messy".bytes().collect(),
36+
)),
4137
_ => None,
4238
}
4339
}
@@ -47,7 +43,6 @@ impl server::AuthenticationProvider for TestProvider {
4743
}
4844
}
4945

50-
5146
#[test]
5247
fn test_simple_success() {
5348
let scram_client = ScramClient::new("user", "password", None).unwrap();

0 commit comments

Comments
 (0)