Module oxide_auth::iron
[−]
[src]
Offers bindings for the code_grant module with iron servers.
Hello world
extern crate router; use oxide_auth::iron::prelude::*; use iron::prelude::*; use std::thread; use iron::modifier::Modifier; use router::Router; /// Example of a main function of a iron server supporting oauth. pub fn main() { let passphrase = "This is a super secret phrase"; // Create the main token instance, a code_granter with an iron frontend. let ohandler = IronGranter::new( // Stores clients in a simple in-memory hash map. ClientMap::new(), // Authorization tokens are 16 byte random keys to a memory hash map. Storage::new(RandomGenerator::new(16)), // Bearer tokens are signed (but not encrypted) using a passphrase. TokenSigner::new_from_passphrase(passphrase)); // Register a dummy client instance let client = Client::public("LocalClient", // Client id "http://localhost:8021/endpoint".parse().unwrap(), // Redirection url "default".parse().unwrap()); // Allowed client scope ohandler.registrar().unwrap().register_client(client); // Create a router and bind the relevant pages let mut router = Router::new(); router.get("/authorize", ohandler.authorize(handle_get), "authorize"); router.post("/authorize", ohandler.authorize(IronOwnerAuthorizer(handle_post)), "authorize"); router.post("/token", ohandler.token(), "token"); let mut protected = iron::Chain::new(|_: &mut Request| { Ok(Response::with((iron::status::Ok, "Hello World!"))) }); // Set up a protected resource, only accessible with a token with `default scope`. protected.link_before(ohandler.guard(vec!["default".parse::<Scope>().unwrap()])); // Instead of an error, show a warning and instructions protected.link_after(HelpfulAuthorizationError()); router.get("/", protected, "protected"); // Start the server let server = thread::spawn(|| iron::Iron::new(router).http("localhost:8020").unwrap()); server.join().expect("Failed to run"); } /// This should display a page to the user asking for his permission to proceed. /// You can use the Response in Ok to achieve this. fn handle_get(_: &mut Request, auth: &PreGrant) -> Result<(Authentication, Response), OAuthError> { unimplemented!(); } /// This shows the second style of authentication handler, a iron::Handler compatible form. /// Allows composition with other libraries or frameworks built around iron. fn handle_post(req: &mut Request) -> IronResult<Response> { unimplemented!(); } /// Show a message to unauthorized requests of the protected resource. 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); } let mut response = err.response; let text = "<html> This page is only accessible with an oauth token, scope <em>default</em>. </html>"; text.modify(&mut response); iron::modifiers::Header(iron::headers::ContentType::html()).modify(&mut response); Ok(response) } }
Reexports
pub use super::code_grant::frontend::Authentication; |
pub use super::code_grant::frontend::OAuthError; |
pub use super::code_grant::Scope; |
pub use super::code_grant::prelude::PreGrant; |
Modules
prelude |
Reexport most useful structs as well as the code_grant core library. |
Structs
IronAuthorizer |
Handles authorization requests from user-agents directed by clients. |
IronGranter |
Groups together all partial systems used in the code_grant process. |
IronGuard |
Protects a resource as an AroundMiddleware |
IronOwnerAuthorizer |
Wraps an |
IronTokenRequest |
Handles token requests from clients. |
Traits
GenericOwnerAuthorizer |
An owner authorizer for iron requests specifically. |