mod support;
#[cfg(feature = "iron-backend")]
mod main {
extern crate oxide_auth;
extern crate iron;
extern crate router;
extern crate url;
extern crate urlencoded;
use self::iron::prelude::*;
use self::oxide_auth::frontends::iron::prelude::*;
use self::urlencoded::UrlEncodedQuery;
use support::iron::dummy_client;
use support::open_in_browser;
use std::collections::HashMap;
use std::thread;
pub fn example() {
let passphrase = "This is a super secret phrase";
let ohandler = IronGranter::new(
ClientMap::new(),
Storage::new(RandomGenerator::new(16)),
TokenSigner::new_from_passphrase(passphrase, None));
let client = Client::public("LocalClient", "http://localhost:8021/endpoint".parse().unwrap(), "default".parse().unwrap()); ohandler.registrar().unwrap().register_client(client);
let mut router = router::Router::new();
let mut protected = iron::Chain::new(|_: &mut Request| {
Ok(Response::with((iron::status::Ok, "Hello World!")))
});
router.get("/authorize", ohandler.authorize(handle_get), "authorize");
router.post("/authorize", ohandler.authorize(IronOwnerAuthorizer(handle_post)), "authorize");
router.post("/token", ohandler.token(), "token");
protected.link_before(ohandler.guard(vec!["default".parse().unwrap()]));
protected.link_after(HelpfulAuthorizationError());
router.get("/", protected, "protected");
let join = thread::spawn(|| iron::Iron::new(router).http(("localhost", 8020)).unwrap());
let client = thread::spawn(|| iron::Iron::new(dummy_client).http(("localhost", 8021)).unwrap());
open_in_browser();
join.join().expect("Failed to run");
client.join().expect("Failed to run client");
}
fn handle_get(_: &mut Request, grant: &PreGrant) -> Result<(Authentication, Response), OAuthError> {
let text = format!(
"<html>'{}' (at {}) is requesting permission for '{}'
<form action=\"authorize?response_type=code&client_id={}\" method=\"post\">
<input type=\"submit\" value=\"Accept\">
</form>
<form action=\"authorize?response_type=code&client_id={}&deny=1\" method=\"post\">
<input type=\"submit\" value=\"Deny\">
</form>
</html>", grant.client_id, grant.redirect_uri, grant.scope, grant.client_id, grant.client_id);
let response = Response::with((iron::status::Ok, iron::modifiers::Header(iron::headers::ContentType::html()), text));
Ok((Authentication::InProgress, response))
}
fn handle_post(req: &mut Request) -> IronResult<Response> {
if req.get::<UrlEncodedQuery>().unwrap_or(HashMap::new()).contains_key("deny") {
req.extensions.insert::<Authentication>(Authentication::Failed);
} else {
req.extensions.insert::<Authentication>(Authentication::Authenticated("dummy user".to_string()));
}
Ok(Response::with(iron::status::Ok))
}
struct HelpfulAuthorizationError();
impl iron::middleware::AfterMiddleware for HelpfulAuthorizationError {
fn catch(&self, _: &mut Request, err: iron::IronError) -> IronResult<Response> {
if !err.error.is::<OAuthError>() {
return Err(err);
}
use main::iron::modifier::Modifier;
let mut response = err.response;
let text =
"<html>
This page should be accessed via an oauth token from the client in the example. Click
<a href=\"http://localhost:8020/authorize?response_type=code&client_id=LocalClient\">
here</a> to begin the authorization process.
</html>";
text.modify(&mut response);
iron::modifiers::Header(iron::headers::ContentType::html()).modify(&mut response);
Ok(response)
}
}
}
#[cfg(not(feature = "iron-backend"))]
mod main { pub fn example() { } }
fn main() {
main::example();
}