      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
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
    348
    349
    350
    351

    // Copyright 2015-2017 Parity Technologies (UK) Ltd.
    // This file is part of Parity.

    // Parity is free software: you can redistribute it and/or modify
    // it under the terms of the GNU General Public License as published by
    // the Free Software Foundation, either version 3 of the License, or
    // (at your option) any later version.

    // Parity is distributed in the hope that it will be useful,
    // but WITHOUT ANY WARRANTY; without even the implied warranty of
    // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    // GNU General Public License for more details.

    // You should have received a copy of the GNU General Public License
    // along with Parity.  If not, see <http://www.gnu.org/licenses/>.

    use rand::Rng;
    use rand::os::OsRng;
    use std::io::{self, Read, Write};
    use std::path::Path;
    use std::{fs, time, mem};
    use util::{H256, Hashable, Itertools};

    /// Providing current time in seconds
    pub trait TimeProvider {
    	/// Returns timestamp (in seconds since epoch)
    	fn now(&self) -> u64;
    }

    impl<F : Fn() -> u64> TimeProvider for F {
    	fn now(&self) -> u64 {
    		self()
    	}
    }

    /// Default implementation of `TimeProvider` using system time.
    #[derive(Default)]
    pub struct DefaultTimeProvider;

    impl TimeProvider for DefaultTimeProvider {
    	fn now(&self) -> u64 {
    		time::UNIX_EPOCH.elapsed().expect("Valid time has to be set in your system.").as_secs()
    	}
    }

    /// No of seconds the hash is valid
    const TIME_THRESHOLD: u64 = 7;
    /// minimal length of hash
    const TOKEN_LENGTH: usize = 16;
    /// special "initial" token used for authorization when there are no tokens yet.
    const INITIAL_TOKEN: &'static str = "initial";
    /// Separator between fields in serialized tokens file.
    const SEPARATOR: &'static str = ";";
    /// Number of seconds to keep unused tokens.
    const UNUSED_TOKEN_TIMEOUT: u64 = 3600 * 24; // a day

    struct Code {
    	code: String,
    	/// Duration since unix_epoch
    	created_at: time::Duration,
    	/// Duration since unix_epoch
    	last_used_at: Option<time::Duration>,
    }

    fn decode_time(val: &str) -> Option<time::Duration> {
    	let time = val.parse::<u64>().ok();
    	time.map(time::Duration::from_secs)
    }

    fn encode_time(time: time::Duration) -> String {
    	format!("{}", time.as_secs())
    }

    /// Manages authorization codes for `SignerUIs`
    pub struct AuthCodes<T: TimeProvider = DefaultTimeProvider> {
    	codes: Vec<Code>,
    	now: T,
    }

    impl AuthCodes<DefaultTimeProvider> {

    	/// Reads `AuthCodes` from file and creates new instance using `DefaultTimeProvider`.
    	#[cfg_attr(feature="dev", allow(single_char_pattern))]
    	pub fn from_file(file: &Path) -> io::Result<AuthCodes> {
    		let content = {
    			if let Ok(mut file) = fs::File::open(file) {
    				let mut s = String::new();
    				let _ = file.read_to_string(&mut s)?;
    				s
    			} else {
    				"".into()
    			}
    		};
    		let time_provider = DefaultTimeProvider::default();

    		let codes = content.lines()
    			.filter_map(|line| {
    				let mut parts = line.split(SEPARATOR);
    				let token = parts.next();
    				let created = parts.next();
    				let used = parts.next();

    				match token {
    					None => None,
    					Some(token) if token.len() < TOKEN_LENGTH => None,
    					Some(token) => {
    						Some(Code {
    							code: token.into(),
    							last_used_at: used.and_then(decode_time),
    							created_at: created.and_then(decode_time)
    											.unwrap_or_else(|| time::Duration::from_secs(time_provider.now())),
    						})
    					}
    				}
    			})
    			.collect();
    		Ok(AuthCodes {
    			codes: codes,
    			now: time_provider,
    		})
    	}

    }

    impl<T: TimeProvider> AuthCodes<T> {

    	/// Writes all `AuthCodes` to a disk.
    	pub fn to_file(&self, file: &Path) -> io::Result<()> {
    		let mut file = fs::File::create(file)?;
    		let content = self.codes.iter().map(|code| {
    			let mut data = vec![code.code.clone(), encode_time(code.created_at.clone())];
    			if let Some(used_at) = code.last_used_at {
    				data.push(encode_time(used_at));
    			}
    			data.join(SEPARATOR)
    		}).join("\n");
    		file.write_all(content.as_bytes())
    	}

    	/// Creates a new `AuthCodes` store with given `TimeProvider`.
    	pub fn new(codes: Vec<String>, now: T) -> Self {
    		AuthCodes {
    			codes: codes.into_iter().map(|code| Code {
    				code: code,
    				created_at: time::Duration::from_secs(now.now()),
    				last_used_at: None,
    			}).collect(),
    			now: now,
    		}
    	}

    	/// Checks if given hash is correct authcode of `SignerUI`
    	/// Updates this hash last used field in case it's valid.
    	#[cfg_attr(feature="dev", allow(wrong_self_convention))]
    	pub fn is_valid(&mut self, hash: &H256, time: u64) -> bool {
    		let now = self.now.now();
    		// check time
    		if time >= now + TIME_THRESHOLD || time <= now - TIME_THRESHOLD {
    			warn!(target: "signer", "Received old authentication request. ({} vs {})", now, time);
    			return false;
    		}

    		let as_token = |code| format!("{}:{}", code, time).sha3();

    		// Check if it's the initial token.
    		if self.is_empty() {
    			let initial = &as_token(INITIAL_TOKEN) == hash;
    			// Initial token can be used only once.
    			if initial {
    				let _ = self.generate_new();
    			}
    			return initial;
    		}

    		// look for code
    		for mut code in &mut self.codes {
    			if &as_token(&code.code) == hash {
    				code.last_used_at = Some(time::Duration::from_secs(now));
    				return true;
    			}
    		}

    		false
    	}

    	/// Generates and returns a new code that can be used by `SignerUIs`
    	pub fn generate_new(&mut self) -> io::Result<String> {
    		let mut rng = OsRng::new()?;
    		let code = rng.gen_ascii_chars().take(TOKEN_LENGTH).collect::<String>();
    		let readable_code = code.as_bytes()
    			.chunks(4)
    			.filter_map(|f| String::from_utf8(f.to_vec()).ok())
    			.collect::<Vec<String>>()
    			.join("-");
    		trace!(target: "signer", "New authentication token generated.");
    		self.codes.push(Code {
    			code: code,
    			created_at: time::Duration::from_secs(self.now.now()),
    			last_used_at: None,
    		});
    		Ok(readable_code)
    	}

    	/// Returns true if there are no tokens in this store
    	pub fn is_empty(&self) -> bool {
    		self.codes.is_empty()
    	}

    	/// Removes old tokens that have not been used since creation.
    	pub fn clear_garbage(&mut self) {
    		let now = self.now.now();
    		let threshold = time::Duration::from_secs(now.saturating_sub(UNUSED_TOKEN_TIMEOUT));

    		let codes = mem::replace(&mut self.codes, Vec::new());
    		for code in codes {
    			// Skip codes that are old and were never used.
    			if code.last_used_at.is_none() && code.created_at <= threshold {
    				continue;
    			}
    			self.codes.push(code);
    		}
    	}
    }

    #[cfg(test)]
    mod tests {

    	use devtools;
    	use std::io::{Read, Write};
    	use std::{time, fs};
    	use std::cell::Cell;

    	use util::{H256, Hashable};
    	use super::*;

    	fn generate_hash(val: &str, time: u64) -> H256 {
    		format!("{}:{}", val, time).sha3()
    	}

    	#[test]
    	fn should_return_true_if_code_is_initial_and_store_is_empty() {
    		// given
    		let code = "initial";
    		let time = 99;
    		let mut codes = AuthCodes::new(vec![], || 100);

    		// when
    		let res1 = codes.is_valid(&generate_hash(code, time), time);
    		let res2 = codes.is_valid(&generate_hash(code, time), time);

    		// then
    		assert_eq!(res1, true);
    		assert_eq!(res2, false);
    	}

    	#[test]
    	fn should_return_true_if_hash_is_valid() {
    		// given
    		let code = "23521352asdfasdfadf";
    		let time = 99;
    		let mut codes = AuthCodes::new(vec![code.into()], || 100);

    		// when
    		let res = codes.is_valid(&generate_hash(code, time), time);

    		// then
    		assert_eq!(res, true);
    	}

    	#[test]
    	fn should_return_false_if_code_is_unknown() {
    		// given
    		let code = "23521352asdfasdfadf";
    		let time = 99;
    		let mut codes = AuthCodes::new(vec!["1".into()], || 100);

    		// when
    		let res = codes.is_valid(&generate_hash(code, time), time);

    		// then
    		assert_eq!(res, false);
    	}

    	#[test]
    	fn should_return_false_if_hash_is_valid_but_time_is_invalid() {
    		// given
    		let code = "23521352asdfasdfadf";
    		let time = 107;
    		let time2 = 93;
    		let mut codes = AuthCodes::new(vec![code.into()], || 100);

    		// when
    		let res1 = codes.is_valid(&generate_hash(code, time), time);
    		let res2 = codes.is_valid(&generate_hash(code, time2), time2);

    		// then
    		assert_eq!(res1, false);
    		assert_eq!(res2, false);
    	}

    	#[test]
    	fn should_read_old_format_from_file() {
    		// given
    		let path = devtools::RandomTempPath::new();
    		let code = "23521352asdfasdfadf";
    		{
    			let mut file = fs::File::create(&path).unwrap();
    			file.write_all(b"a\n23521352asdfasdfadf\nb\n").unwrap();
    		}

    		// when
    		let mut authcodes = AuthCodes::from_file(&path).unwrap();
    		let time = time::UNIX_EPOCH.elapsed().unwrap().as_secs();

    		// then
    		assert!(authcodes.is_valid(&generate_hash(code, time), time), "Code should be read from file");
    	}

    	#[test]
    	fn should_remove_old_unused_tokens() {
    		// given
    		let path = devtools::RandomTempPath::new();
    		let code1 = "11111111asdfasdf111";
    		let code2 = "22222222asdfasdf222";
    		let code3 = "33333333asdfasdf333";

    		let time = Cell::new(100);
    		let mut codes = AuthCodes::new(vec![code1.into(), code2.into(), code3.into()], || time.get());
    		// `code2` should not be removed (we never remove tokens that were used)
    		codes.is_valid(&generate_hash(code2, time.get()), time.get());

    		// when
    		time.set(100 + 10_000_000);
    		// mark `code1` as used now
    		codes.is_valid(&generate_hash(code1, time.get()), time.get());

    		let new_code = codes.generate_new().unwrap().replace('-', "");
    		codes.clear_garbage();
    		codes.to_file(&path).unwrap();

    		// then
    		let mut content = String::new();
    		let mut file = fs::File::open(&path).unwrap();
    		file.read_to_string(&mut content).unwrap();

    		assert_eq!(content, format!("{};100;10000100\n{};100;100\n{};10000100", code1, code2, new_code));
    	}

    }

# Help

## Keyboard Shortcuts

?
  Show this help dialog
S
  Focus the search field
⇤
  Move up in search results
⇥
  Move down in search results
⏎
  Go to active search result
+
  Collapse/expand all sections

## Search Tricks

Prefix searches with a type followed by a colon (e.g. `fn:`) to restrict the search to a given type.

Accepted types are: `fn`, `mod`, `struct`, `enum`, `trait`, `type`, `macro`, and `const`.

Search functions by type signature (e.g. `vec -> usize` or `* -> vec`)