2020-09-22 14:53:52 +02:00
|
|
|
// Copyright 2015-2020 Parity Technologies (UK) Ltd.
|
|
|
|
// This file is part of OpenEthereum.
|
2016-05-27 13:03:00 +02:00
|
|
|
|
2020-09-22 14:53:52 +02:00
|
|
|
// OpenEthereum is free software: you can redistribute it and/or modify
|
2016-05-27 13:03:00 +02:00
|
|
|
// 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.
|
|
|
|
|
2020-09-22 14:53:52 +02:00
|
|
|
// OpenEthereum is distributed in the hope that it will be useful,
|
2016-05-27 13:03:00 +02:00
|
|
|
// 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
|
2020-09-22 14:53:52 +02:00
|
|
|
// along with OpenEthereum. If not, see <http://www.gnu.org/licenses/>.
|
2016-05-27 13:03:00 +02:00
|
|
|
|
2020-08-05 06:08:03 +02:00
|
|
|
use std::{
|
|
|
|
io,
|
|
|
|
path::{Path, PathBuf},
|
|
|
|
};
|
2017-02-04 22:18:19 +01:00
|
|
|
|
2021-03-12 14:55:49 +01:00
|
|
|
use crate::{path::restrict_permissions_owner, rpc, rpc_apis};
|
2017-07-28 19:07:38 +02:00
|
|
|
use ansi_term::Colour::White;
|
|
|
|
use ethcore_logger::Config as LogConfig;
|
2017-05-24 12:24:07 +02:00
|
|
|
use parity_rpc;
|
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 {
|
2020-08-05 06:08:03 +02:00
|
|
|
pub token: String,
|
|
|
|
pub message: String,
|
2016-11-16 09:37:48 +01:00
|
|
|
}
|
|
|
|
|
2020-08-05 06:08:03 +02:00
|
|
|
pub fn new_service(
|
|
|
|
ws_conf: &rpc::WsConfiguration,
|
|
|
|
logger_config: &LogConfig,
|
|
|
|
) -> rpc_apis::SignerService {
|
|
|
|
let logger_config_color = logger_config.color;
|
|
|
|
let signer_path = ws_conf.signer_path.clone();
|
|
|
|
let signer_enabled = ws_conf.support_token_api;
|
2017-02-14 22:45:43 +01:00
|
|
|
|
2020-08-05 06:08:03 +02:00
|
|
|
rpc_apis::SignerService::new(
|
|
|
|
move || {
|
|
|
|
generate_new_token(&signer_path, logger_config_color).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 {
|
2020-08-05 06:08:03 +02:00
|
|
|
let mut p = path.to_owned();
|
|
|
|
p.push(CODES_FILENAME);
|
|
|
|
let _ = restrict_permissions_owner(&p, true, false);
|
|
|
|
p
|
2016-06-07 17:21:19 +02:00
|
|
|
}
|
|
|
|
|
2018-06-06 10:05:52 +02:00
|
|
|
pub fn execute(ws_conf: rpc::WsConfiguration, logger_config: LogConfig) -> Result<String, String> {
|
2020-08-05 06:08:03 +02:00
|
|
|
Ok(generate_token_and_url(&ws_conf, &logger_config)?.message)
|
2016-11-02 19:42:21 +01:00
|
|
|
}
|
|
|
|
|
2020-08-05 06:08:03 +02:00
|
|
|
pub fn generate_token_and_url(
|
|
|
|
ws_conf: &rpc::WsConfiguration,
|
|
|
|
logger_config: &LogConfig,
|
|
|
|
) -> Result<NewToken, String> {
|
|
|
|
let code = generate_new_token(&ws_conf.signer_path, logger_config.color)
|
|
|
|
.map_err(|err| format!("Error generating token: {:?}", err))?;
|
|
|
|
let colored = |s: String| match logger_config.color {
|
|
|
|
true => format!("{}", White.bold().paint(s)),
|
|
|
|
false => s,
|
|
|
|
};
|
2018-03-20 18:57:37 +01:00
|
|
|
|
2020-08-05 06:08:03 +02:00
|
|
|
Ok(NewToken {
|
|
|
|
token: code.clone(),
|
|
|
|
message: format!(
|
|
|
|
r#"
|
2018-06-06 10:05:52 +02:00
|
|
|
Generated token:
|
|
|
|
{}
|
|
|
|
"#,
|
2020-08-05 06:08:03 +02:00
|
|
|
colored(code)
|
|
|
|
),
|
|
|
|
})
|
2016-07-25 16:09:47 +02:00
|
|
|
}
|
|
|
|
|
2017-07-28 19:07:38 +02:00
|
|
|
fn generate_new_token(path: &Path, logger_config_color: bool) -> io::Result<String> {
|
2020-08-05 06:08:03 +02:00
|
|
|
let path = codes_path(path);
|
|
|
|
let mut codes = parity_rpc::AuthCodes::from_file(&path)?;
|
|
|
|
codes.clear_garbage();
|
|
|
|
let code = codes.generate_new()?;
|
|
|
|
codes.to_file(&path)?;
|
|
|
|
trace!(
|
|
|
|
"New key code created: {}",
|
|
|
|
match logger_config_color {
|
|
|
|
true => format!("{}", White.bold().paint(&code[..])),
|
|
|
|
false => format!("{}", &code[..]),
|
|
|
|
}
|
|
|
|
);
|
|
|
|
Ok(code)
|
2016-06-07 17:21:19 +02:00
|
|
|
}
|