From 692e912d2b8d7589d28cc2fd5b9e595fbfd2074d Mon Sep 17 00:00:00 2001 From: Curtis McEnroe Date: Sat, 28 Nov 2015 03:36:03 -0500 Subject: [PATCH] Implement authorization_uri --- Cargo.toml | 3 +++ src/lib.rs | 36 ++++++++++++++++++++++++++++++++++-- 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index ada489d..7b574a3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,3 +2,6 @@ name = "inth-oauth2" version = "0.1.0" authors = ["Curtis McEnroe "] + +[dependencies] +url = "0.5.0" diff --git a/src/lib.rs b/src/lib.rs index 8a2e01d..501aabc 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,4 +1,36 @@ +extern crate url; + +use url::{Url, ParseResult}; + +/// OAuth2 client. pub struct Client { - client_id: String, - client_secret: String, + pub authorization_uri: String, + + pub client_id: String, + pub client_secret: String, + pub redirect_uri: Option, +} + +impl Client { + pub fn authorization_uri(&self, scope: Option<&str>, state: Option<&str>) -> ParseResult { + let mut uri = try!(Url::parse(&self.authorization_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()) + } }