Implement authorization_uri
This commit is contained in:
parent
c05488dab2
commit
692e912d2b
|
@ -2,3 +2,6 @@
|
||||||
name = "inth-oauth2"
|
name = "inth-oauth2"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
authors = ["Curtis McEnroe <programble@gmail.com>"]
|
authors = ["Curtis McEnroe <programble@gmail.com>"]
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
url = "0.5.0"
|
||||||
|
|
36
src/lib.rs
36
src/lib.rs
|
@ -1,4 +1,36 @@
|
||||||
|
extern crate url;
|
||||||
|
|
||||||
|
use url::{Url, ParseResult};
|
||||||
|
|
||||||
|
/// OAuth2 client.
|
||||||
pub struct Client {
|
pub struct Client {
|
||||||
client_id: String,
|
pub authorization_uri: String,
|
||||||
client_secret: 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())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue