Implement authorization_uri

This commit is contained in:
Curtis McEnroe 2015-11-28 03:36:03 -05:00
parent c05488dab2
commit 692e912d2b
2 changed files with 37 additions and 2 deletions

View File

@ -2,3 +2,6 @@
name = "inth-oauth2"
version = "0.1.0"
authors = ["Curtis McEnroe <programble@gmail.com>"]
[dependencies]
url = "0.5.0"

View File

@ -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<String>,
}
impl Client {
pub fn authorization_uri(&self, scope: Option<&str>, state: Option<&str>) -> ParseResult<String> {
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())
}
}