2017-01-25 18:51:41 +01:00
|
|
|
// Copyright 2015-2017 Parity Technologies (UK) Ltd.
|
2016-05-27 13:03:00 +02:00
|
|
|
// 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/>.
|
|
|
|
|
2016-06-07 17:21:19 +02:00
|
|
|
use std::io;
|
2017-05-24 12:24:07 +02:00
|
|
|
use std::path::{Path, PathBuf};
|
2017-02-04 22:18:19 +01:00
|
|
|
|
2016-07-11 17:11:49 +02:00
|
|
|
use ansi_term::Colour;
|
2017-05-24 12:24:07 +02:00
|
|
|
use rpc;
|
2017-02-04 22:18:19 +01:00
|
|
|
use rpc_apis;
|
2017-05-24 12:24:07 +02:00
|
|
|
use parity_rpc;
|
2017-03-22 06:23:40 +01:00
|
|
|
use path::restrict_permissions_owner;
|
2016-05-27 13:03:00 +02:00
|
|
|
|
|
|
|
|
2017-05-24 12:24:07 +02:00
|
|
|
pub const CODES_FILENAME: &'static str = "authcodes";
|
2016-05-27 13:03:00 +02:00
|
|
|
|
2016-11-16 09:37:48 +01:00
|
|
|
pub struct NewToken {
|
|
|
|
pub token: String,
|
|
|
|
pub url: String,
|
|
|
|
pub message: String,
|
|
|
|
}
|
|
|
|
|
2017-05-24 12:24:07 +02:00
|
|
|
pub fn new_service(ws_conf: &rpc::WsConfiguration, ui_conf: &rpc::UiConfiguration) -> rpc_apis::SignerService {
|
|
|
|
let signer_path = ws_conf.signer_path.clone();
|
|
|
|
let signer_enabled = ui_conf.enabled;
|
2017-02-14 22:45:43 +01:00
|
|
|
|
2017-05-24 12:24:07 +02:00
|
|
|
rpc_apis::SignerService::new(move || {
|
|
|
|
generate_new_token(&signer_path).map_err(|e| format!("{:?}", e))
|
|
|
|
}, signer_enabled)
|
2016-05-27 17:46:15 +02:00
|
|
|
}
|
2016-05-27 13:03:00 +02:00
|
|
|
|
2017-05-24 12:24:07 +02:00
|
|
|
pub fn codes_path(path: &Path) -> PathBuf {
|
|
|
|
let mut p = path.to_owned();
|
2016-06-07 17:21:19 +02:00
|
|
|
p.push(CODES_FILENAME);
|
2016-12-16 18:17:15 +01:00
|
|
|
let _ = restrict_permissions_owner(&p, true, false);
|
2016-06-07 17:21:19 +02:00
|
|
|
p
|
|
|
|
}
|
|
|
|
|
2017-05-24 12:24:07 +02:00
|
|
|
pub fn execute(ws_conf: rpc::WsConfiguration, ui_conf: rpc::UiConfiguration) -> Result<String, String> {
|
|
|
|
Ok(generate_token_and_url(&ws_conf, &ui_conf)?.message)
|
2016-11-02 19:42:21 +01:00
|
|
|
}
|
|
|
|
|
2017-05-24 12:24:07 +02:00
|
|
|
pub fn generate_token_and_url(ws_conf: &rpc::WsConfiguration, ui_conf: &rpc::UiConfiguration) -> Result<NewToken, String> {
|
|
|
|
let code = generate_new_token(&ws_conf.signer_path).map_err(|err| format!("Error generating token: {:?}", err))?;
|
|
|
|
let auth_url = format!("http://{}:{}/#/auth?token={}", ui_conf.interface, ui_conf.port, code);
|
2016-11-11 17:56:52 +01:00
|
|
|
// And print in to the console
|
2016-11-16 09:37:48 +01:00
|
|
|
Ok(NewToken {
|
|
|
|
token: code.clone(),
|
|
|
|
url: auth_url.clone(),
|
|
|
|
message: format!(
|
|
|
|
r#"
|
2016-11-11 17:38:45 +01:00
|
|
|
Open: {}
|
|
|
|
to authorize your browser.
|
2016-11-16 09:37:48 +01:00
|
|
|
Or use the generated token:
|
2016-11-11 18:02:36 +01:00
|
|
|
{}"#,
|
2016-11-16 09:37:48 +01:00
|
|
|
Colour::White.bold().paint(auth_url),
|
|
|
|
code
|
|
|
|
)
|
|
|
|
})
|
2016-07-25 16:09:47 +02:00
|
|
|
}
|
|
|
|
|
2017-05-24 12:24:07 +02:00
|
|
|
fn generate_new_token(path: &Path) -> io::Result<String> {
|
2016-06-07 17:21:19 +02:00
|
|
|
let path = codes_path(path);
|
2017-05-24 12:24:07 +02:00
|
|
|
let mut codes = parity_rpc::AuthCodes::from_file(&path)?;
|
2016-11-14 11:56:01 +01:00
|
|
|
codes.clear_garbage();
|
2016-12-27 12:53:56 +01:00
|
|
|
let code = codes.generate_new()?;
|
|
|
|
codes.to_file(&path)?;
|
2016-07-17 23:00:57 +02:00
|
|
|
trace!("New key code created: {}", Colour::White.bold().paint(&code[..]));
|
|
|
|
Ok(code)
|
2016-06-07 17:21:19 +02:00
|
|
|
}
|