Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Basic Auth for API and Owner API #1566

Merged
merged 25 commits into from
Sep 26, 2018

Conversation

quentinlesceller
Copy link
Member

@quentinlesceller quentinlesceller commented Sep 20, 2018

Fixes #1290.
@hashmap many thanks for the middleware implementation.
Grin wallet generates an api_secret and owner_api_secret that can be used to query both API with Basic Auth.
This basic authentication can be disabled/enabled in the grin-server.toml/grin-wallet.toml but is enabled by default.
Feedbacks are welcome.

I'll add a bit of documentation later today.

@quentinlesceller quentinlesceller changed the title Implement Basic Auth for API and Owner API [WIP] Implement Basic Auth for API and Owner API Sep 20, 2018
@quentinlesceller quentinlesceller changed the title [WIP] Implement Basic Auth for API and Owner API Implement Basic Auth for API and Owner API Sep 20, 2018
@yeastplume
Copy link
Member

👍 Looks like a good start at wallet auth (and as a bonus doesn't look like it conflicts with T4 wallet changes)

@quentinlesceller
Copy link
Member Author

Great, I'm waiting for @hashmap per router middleware instead of per route middleware improvement to reduce the number of line of codes.

@@ -878,7 +885,7 @@ pub fn build_router(
};

let mut router = Router::new();
// example how we can use midlleware

This comment was marked as spam.

@@ -117,7 +127,16 @@ pub fn unban_peer(config: &ServerConfig, peer_addr: &SocketAddr) {
pub fn list_connected_peers(config: &ServerConfig) {
let mut e = term::stdout().unwrap();
let url = format!("http://{}/v1/peers/connected", config.api_http_addr);
match api::client::get::<Vec<p2p::PeerInfo>>(url.as_str()).map_err(|e| Error::API(e)) {
let peers_info: Result<Vec<p2p::PeerInfo>, api::Error>;
if config.api_basic_auth.is_some() && config.api_basic_auth.unwrap() {

This comment was marked as spam.

This comment was marked as spam.

@@ -52,6 +57,8 @@ impl Default for WalletConfig {
chain_type: Some(ChainTypes::Testnet3),
api_listen_interface: "127.0.0.1".to_string(),
api_listen_port: 13415,
owner_api_basic_auth: Some(true),
owner_api_secret: thread_rng().sample_iter(&Alphanumeric).take(20).collect(),

This comment was marked as spam.

This comment was marked as spam.

Copy link
Contributor

@hashmap hashmap left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks simpler now, great! I left some suggestions, feel free to ignore them, could be refactored later one. What is important to decide now - do we need 2 config params or the key is enough?


pub type ClientResponseFuture<T> = Box<Future<Item = T, Error = Error> + Send>;

/// Helper function to easily issue a HTTP GET request against a given URL that
/// returns a JSON object. Handles request building, JSON deserialization and
/// response code checking.
pub fn get<'a, T>(url: &'a str) -> Result<T, Error>
pub fn get<'a, T>(url: &'a str, api_basic_auth: bool, api_secret: &'a str) -> Result<T, Error>

This comment was marked as spam.

This comment was marked as spam.

.map_err(|e| {
ErrorKind::RequestError(format!("Bad request {} {}: {}", method, url, e)).into()
})
if api_basic_auth {

This comment was marked as spam.

This comment was marked as spam.

@@ -113,6 +114,12 @@ pub struct ServerConfig {
/// Network address for the Rest API HTTP server.
pub api_http_addr: String,

/// Whether to enable basic access authentication on API
pub api_basic_auth: Option<bool>,

This comment was marked as spam.

This comment was marked as spam.

pub api_basic_auth: Option<bool>,

/// Secret for basic auth on Rest API HTTP server.
pub api_secret: String,

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

@hashmap
Copy link
Contributor

hashmap commented Sep 26, 2018

👍

@quentinlesceller
Copy link
Member Author

@hashmap Just adding @Kargakis idea on using another file should land in a few minutes here :)

@quentinlesceller
Copy link
Member Author

@hashmap @Kargakis can you take a look. Now grin will generate a .api_secret file under grin directory. You can comment the line to disable basic auth in server and wallet configs. Basic Auth is enabled by default. Both APIs use the same secret.

@0xmichalis
Copy link
Contributor

0xmichalis commented Sep 26, 2018 via email

@hashmap hashmap merged commit 62fd8f2 into mimblewimble:master Sep 26, 2018
@quentinlesceller quentinlesceller deleted the api2 branch September 26, 2018 20:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants