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

Upgrade rocket to 0.5 #198

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Contributors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ asonix <[email protected]> (actix 1.0 update)
Geobert Quach (on behalf of Isode Ltd.)
robjtede <[email protected]> (actix-web 3.0 update)
Oleg Chirukhin <[email protected]>
Jordan Morris <[email protected]>
23 changes: 12 additions & 11 deletions examples/support/rocket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,24 @@ mod generic;

use self::generic::{Client, ClientConfig, ClientError};

use rocket::{Rocket, State};
use rocket::fairing::{Fairing, Info, Kind};
use rocket::{Rocket, State,Build};
use rocket::fairing::{self,Fairing, Info, Kind};
use rocket::http::Status;
use rocket::response::{Redirect, content::Html, status::Custom};
use rocket::response::{Redirect, content::RawHtml, status::Custom};

pub use self::generic::consent_page_html;
pub struct ClientFairing;

#[rocket::async_trait]
impl Fairing for ClientFairing {
fn info(&self) -> Info {
Info {
name: "Simple oauth client implementation",
kind: Kind::Attach,
kind: Kind::Ignite,
}
}

fn on_attach(&self, rocket: Rocket) -> Result<Rocket, Rocket> {
async fn on_ignite(&self, rocket: Rocket<Build>) -> fairing::Result {
let config = ClientConfig {
client_id: "LocalClient".into(),
protected_url: "http://localhost:8000/".into(),
Expand All @@ -37,7 +38,7 @@ impl Fairing for ClientFairing {
}

#[get("/endpoint?<code>&<error>")]
fn oauth_endpoint<'r>(code: Option<String>, error: Option<String>, state: State<Client>)
fn oauth_endpoint<'r>(code: Option<String>, error: Option<String>, state: &State<Client>)
-> Result<Redirect, Custom<String>>
{
if let Some(error) = error {
Expand All @@ -55,7 +56,7 @@ fn oauth_endpoint<'r>(code: Option<String>, error: Option<String>, state: State<
}

#[get("/")]
fn client_view(state: State<Client>) -> Result<Html<String>, Custom<String>> {
fn client_view(state: &State<Client>) -> Result<RawHtml<String>, Custom<String>> {
let protected_page = state
.retrieve_protected_page()
.map_err(internal_error)?;
Expand All @@ -74,19 +75,19 @@ fn client_view(state: State<Client>) -> Result<Html<String>, Custom<String>> {
<form action=\"/clientside/refresh\" method=\"post\"><button>Refresh token</button></form>
</main></html>", state.as_html(), protected_page);

Ok(Html(display_page))
Ok(RawHtml(display_page))
}

#[post("/refresh")]
fn refresh(state: State<Client>) -> Result<Redirect, Custom<String>> {
fn refresh(state: &State<Client>) -> Result<Redirect, Custom<String>> {
state.refresh()
.map_err(internal_error)
.map(|()| Redirect::found("/clientside"))
}

#[get("/debug")]
fn client_debug(state: State<Client>) -> Html<String> {
Html(state.as_html())
fn client_debug(state: &State<Client>) -> RawHtml<String> {
RawHtml(state.as_html())
}

fn internal_error(err: ClientError) -> Custom<String> {
Expand Down
3 changes: 2 additions & 1 deletion oxide-auth-rocket/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@ edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
rocket = "0.4.2"
rocket = "0.5.1"
oxide-auth = { version = "0.6.0", path = "../oxide-auth" }
serde_urlencoded = "0.7"

[dev-dependencies]
reqwest = { version = "0.11.10", features = ["blocking"] }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"

72 changes: 30 additions & 42 deletions oxide-auth-rocket/examples/rocket.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#![feature(proc_macro_hygiene, decl_macro)]

extern crate oxide_auth;
extern crate oxide_auth_rocket;
#[macro_use]
Expand All @@ -9,7 +7,6 @@ extern crate rocket;
#[path = "../../examples/support/rocket.rs"]
mod support;

use std::io;
use std::sync::Mutex;

use oxide_auth::endpoint::{OwnerConsent, Solicitation};
Expand All @@ -18,8 +15,7 @@ use oxide_auth::primitives::prelude::*;
use oxide_auth::primitives::registrar::RegisteredUrl;
use oxide_auth_rocket::{OAuthResponse, OAuthRequest, OAuthFailure};

use rocket::{Data, State, Response, http};
use rocket::http::ContentType;
use rocket::State;
use rocket::response::Responder;

struct MyState {
Expand All @@ -30,8 +26,8 @@ struct MyState {

#[get("/authorize")]
fn authorize<'r>(
oauth: OAuthRequest<'r>, state: State<MyState>,
) -> Result<OAuthResponse<'r>, OAuthFailure> {
oauth: OAuthRequest<'r>, state: &State<MyState>,
) -> Result<OAuthResponse, OAuthFailure> {
state
.endpoint()
.with_solicitor(FnSolicitor(consent_form))
Expand All @@ -42,8 +38,8 @@ fn authorize<'r>(

#[post("/authorize?<allow>")]
fn authorize_consent<'r>(
oauth: OAuthRequest<'r>, allow: Option<bool>, state: State<MyState>,
) -> Result<OAuthResponse<'r>, OAuthFailure> {
oauth: OAuthRequest<'r>, allow: Option<bool>, state: &State<MyState>,
) -> Result<OAuthResponse, OAuthFailure> {
let allowed = allow.unwrap_or(false);
state
.endpoint()
Expand All @@ -55,23 +51,21 @@ fn authorize_consent<'r>(
.map_err(|err| err.pack::<OAuthFailure>())
}

#[post("/token", data = "<body>")]
fn token<'r>(
mut oauth: OAuthRequest<'r>, body: Data, state: State<MyState>,
) -> Result<OAuthResponse<'r>, OAuthFailure> {
oauth.add_body(body);
#[post("/token", data = "<oauth>")]
async fn token<'r>(
oauth: OAuthRequest<'r> , state: &State<MyState>,
) -> Result<OAuthResponse, OAuthFailure> {
state
.endpoint()
.access_token_flow()
.execute(oauth)
.map_err(|err| err.pack::<OAuthFailure>())
}

#[post("/refresh", data = "<body>")]
fn refresh<'r>(
mut oauth: OAuthRequest<'r>, body: Data, state: State<MyState>,
) -> Result<OAuthResponse<'r>, OAuthFailure> {
oauth.add_body(body);
#[post("/refresh", data = "<oauth>")]
async fn refresh<'r>(
oauth: OAuthRequest<'r>, state: &State<MyState>,
) -> Result<OAuthResponse, OAuthFailure> {
state
.endpoint()
.refresh_flow()
Expand All @@ -80,7 +74,7 @@ fn refresh<'r>(
}

#[get("/")]
fn protected_resource<'r>(oauth: OAuthRequest<'r>, state: State<MyState>) -> impl Responder<'r> {
fn protected_resource<'r,'o:'r>(oauth: OAuthRequest<'r>, state: &State<MyState>) -> impl Responder<'r,'o> {
const DENY_TEXT: &str = "<html>
This page should be accessed via an oauth token from the client in the example. Click
<a href=\"/authorize?response_type=code&client_id=LocalClient\">
Expand All @@ -95,28 +89,26 @@ here</a> to begin the authorization process.
.execute(oauth);
match protect {
Ok(_grant) => Ok("Hello, world"),
Err(Ok(response)) => {
let error: OAuthResponse = Response::build_from(response.into())
.header(ContentType::HTML)
.sized_body(io::Cursor::new(DENY_TEXT))
.finalize()
.into();
Err(Ok(error))
Err(Ok(mut response)) => {
response.body_html(DENY_TEXT);
// let error= response.try_into().unwrap();
Err(Ok(response))
}
Err(Err(err)) => Err(Err(err.pack::<OAuthFailure>())),
}
}

fn main() {
rocket::ignite()
#[rocket::main]
async fn main() {
rocket::build()
.mount(
"/",
routes![authorize, authorize_consent, token, protected_resource, refresh,],
)
// We only attach the test client here because there can only be one rocket.
.attach(support::ClientFairing)
.manage(MyState::preconfigured())
.launch();
.ignite().await.unwrap()
.launch().await.unwrap();
}

impl MyState {
Expand Down Expand Up @@ -162,21 +154,17 @@ impl MyState {

fn consent_form<'r>(
_: &mut OAuthRequest<'r>, solicitation: Solicitation,
) -> OwnerConsent<OAuthResponse<'r>> {
) -> OwnerConsent<OAuthResponse> {
let output = support::consent_page_html(
"/authorize",
solicitation,
);
OwnerConsent::InProgress(
Response::build()
.status(http::Status::Ok)
.header(http::ContentType::HTML)
.sized_body(io::Cursor::new(support::consent_page_html(
"/authorize",
solicitation,
)))
.finalize()
.into(),
OAuthResponse::new().body_html(&output).to_owned()
)
}

fn consent_decision<'r>(allowed: bool, _: Solicitation) -> OwnerConsent<OAuthResponse<'r>> {
fn consent_decision<'r>(allowed: bool, _: Solicitation) -> OwnerConsent<OAuthResponse> {
if allowed {
OwnerConsent::Authorized("dummy user".into())
} else {
Expand Down
4 changes: 2 additions & 2 deletions oxide-auth-rocket/src/failure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ enum Kind {
OAuth(OAuthError),
}

impl<'r> Responder<'r> for OAuthFailure {
fn respond_to(self, _: &Request) -> Result<'r> {
impl<'r,'o:'r> Responder<'r,'o> for OAuthFailure {
fn respond_to(self, _: &Request) -> Result<'o> {
match self.inner {
Web(_) | OAuth(DenySilently) | OAuth(BadRequest) => Err(Status::BadRequest),
OAuth(PrimitiveError) => Err(Status::InternalServerError),
Expand Down
Loading