From a7710660c1328020fd9b783dc302d02c3795d14f Mon Sep 17 00:00:00 2001 From: Curtis McEnroe Date: Mon, 21 Dec 2015 22:50:41 -0500 Subject: [PATCH] Implement Client::auth_uri --- src/client.rs | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/src/client.rs b/src/client.rs index a532913..b8ebf7c 100644 --- a/src/client.rs +++ b/src/client.rs @@ -3,6 +3,7 @@ use std::marker::PhantomData; use hyper; +use url::{self, form_urlencoded, Url}; use provider::Provider; @@ -45,4 +46,30 @@ impl Client

{ provider: PhantomData, } } + + /// Returns an authorization endpoint URI to direct the user to. + /// + /// See [RFC 6749, section 3.1](http://tools.ietf.org/html/rfc6749#section-3.1). + pub fn auth_uri(&self, scope: Option<&str>, state: Option<&str>) -> Result + { + let mut uri = try!(Url::parse(P::auth_uri())); + + let mut query_pairs = vec![ + ("response_type", "code"), + ("client_id", &self.client_id), + ]; + if let Some(ref redirect_uri) = self.redirect_uri { + query_pairs.push(("redirect_uri", redirect_uri)); + } + if let Some(scope) = scope { + query_pairs.push(("scope", scope)); + } + if let Some(state) = state { + query_pairs.push(("state", state)); + } + + uri.set_query_from_pairs(query_pairs.iter()); + + Ok(uri.serialize()) + } }