2016-04-08 15:25:20 +02:00
|
|
|
// Copyright 2015, 2016 Ethcore (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/>.
|
|
|
|
|
|
|
|
//! HTTP Authorization implementations
|
|
|
|
|
|
|
|
use std::collections::HashMap;
|
2016-07-06 11:24:29 +02:00
|
|
|
use hyper::{server, net, header, status};
|
|
|
|
use endpoint::Handler;
|
|
|
|
use handlers::{AuthRequiredHandler, ContentHandler};
|
2016-04-08 15:25:20 +02:00
|
|
|
|
|
|
|
/// Authorization result
|
2016-04-14 20:38:48 +02:00
|
|
|
pub enum Authorized {
|
|
|
|
/// Authorization was successful.
|
|
|
|
Yes,
|
|
|
|
/// Unsuccessful authorization. Handler for further work is returned.
|
2016-07-06 11:24:29 +02:00
|
|
|
No(Box<Handler>),
|
2016-04-08 15:25:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Authorization interface
|
2016-04-08 16:11:58 +02:00
|
|
|
pub trait Authorization : Send + Sync {
|
2016-04-14 20:38:48 +02:00
|
|
|
/// Checks if authorization is valid.
|
2016-07-06 11:24:29 +02:00
|
|
|
fn is_authorized(&self, req: &server::Request<net::HttpStream>)-> Authorized;
|
2016-04-08 15:25:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// HTTP Basic Authorization handler
|
|
|
|
pub struct HttpBasicAuth {
|
|
|
|
users: HashMap<String, String>,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// No-authorization implementation (authorization disabled)
|
|
|
|
pub struct NoAuth;
|
|
|
|
|
|
|
|
impl Authorization for NoAuth {
|
2016-07-06 11:24:29 +02:00
|
|
|
fn is_authorized(&self, _req: &server::Request<net::HttpStream>)-> Authorized {
|
2016-04-14 20:38:48 +02:00
|
|
|
Authorized::Yes
|
2016-04-08 15:25:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Authorization for HttpBasicAuth {
|
2016-07-06 11:24:29 +02:00
|
|
|
fn is_authorized(&self, req: &server::Request<net::HttpStream>) -> Authorized {
|
2016-04-08 15:25:20 +02:00
|
|
|
let auth = self.check_auth(&req);
|
|
|
|
|
|
|
|
match auth {
|
|
|
|
Access::Denied => {
|
2016-08-31 16:53:22 +02:00
|
|
|
Authorized::No(Box::new(ContentHandler::error(
|
2016-07-06 11:24:29 +02:00
|
|
|
status::StatusCode::Unauthorized,
|
2016-08-31 16:53:22 +02:00
|
|
|
"Unauthorized", "You need to provide valid credentials to access this page.", None
|
2016-07-06 11:24:29 +02:00
|
|
|
)))
|
2016-04-08 15:25:20 +02:00
|
|
|
},
|
|
|
|
Access::AuthRequired => {
|
2016-04-14 20:38:48 +02:00
|
|
|
Authorized::No(Box::new(AuthRequiredHandler))
|
2016-04-08 15:25:20 +02:00
|
|
|
},
|
|
|
|
Access::Granted => {
|
2016-04-14 20:38:48 +02:00
|
|
|
Authorized::Yes
|
2016-04-08 15:25:20 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-28 11:50:45 +02:00
|
|
|
#[derive(Debug)]
|
2016-04-08 15:25:20 +02:00
|
|
|
enum Access {
|
|
|
|
Granted,
|
|
|
|
Denied,
|
|
|
|
AuthRequired,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl HttpBasicAuth {
|
|
|
|
/// Creates `HttpBasicAuth` instance with only one user.
|
|
|
|
pub fn single_user(username: &str, password: &str) -> Self {
|
|
|
|
let mut users = HashMap::new();
|
|
|
|
users.insert(username.to_owned(), password.to_owned());
|
|
|
|
HttpBasicAuth {
|
|
|
|
users: users
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn is_authorized(&self, username: &str, password: &str) -> bool {
|
|
|
|
self.users.get(&username.to_owned()).map_or(false, |pass| pass == password)
|
|
|
|
}
|
|
|
|
|
2016-07-06 11:24:29 +02:00
|
|
|
fn check_auth(&self, req: &server::Request<net::HttpStream>) -> Access {
|
2016-04-14 20:38:48 +02:00
|
|
|
match req.headers().get::<header::Authorization<header::Basic>>() {
|
2016-04-09 02:51:20 +02:00
|
|
|
Some(&header::Authorization(
|
|
|
|
header::Basic { ref username, password: Some(ref password) }
|
|
|
|
)) if self.is_authorized(username, password) => Access::Granted,
|
|
|
|
Some(_) => Access::Denied,
|
|
|
|
None => Access::AuthRequired,
|
2016-04-08 15:25:20 +02:00
|
|
|
}
|
|
|
|
}
|
2016-04-14 20:38:48 +02:00
|
|
|
}
|