steam_connect/
redirect.rs

1use url::Url;
2
3use crate::Error;
4
5#[cfg(feature = "actix")]
6use actix_web::{http, HttpResponse};
7
8#[derive(Serialize)]
9struct OpenIDRequest<'a> {
10    #[serde(rename = "openid.mode")]
11    mode: &'a str,
12    #[serde(rename = "openid.ns")]
13    ns: &'a str,
14    #[serde(rename = "openid.identity")]
15    identity: &'a str,
16    #[serde(rename = "openid.claimed_id")]
17    claimed_id: &'a str,
18    #[serde(rename = "openid.return_to")]
19    return_to: &'a str,
20    #[serde(rename = "openid.realm")]
21    realm: &'a str,
22}
23
24pub struct Redirect {
25    url: Url,
26}
27
28impl Redirect {
29    /// Create a link to the authorization page with the callback page
30    pub fn new(callback: &str) -> Result<Self, Error> {
31        let url = Url::parse(callback).map_err(Error::ParseUrl)?;
32
33        let request = OpenIDRequest {
34            mode: "checkid_setup",
35            ns: "http://specs.openid.net/auth/2.0",
36            identity: "http://specs.openid.net/auth/2.0/identifier_select",
37            claimed_id: "http://specs.openid.net/auth/2.0/identifier_select",
38            return_to: &url.to_string(),
39            realm: &url.origin().ascii_serialization(),
40        };
41
42        let params = serde_qs::to_string(&request).map_err(Error::ParseQuery)?;
43
44        let mut auth_url =
45            Url::parse("https://steamcommunity.com/openid/login").map_err(Error::ParseUrl)?;
46        auth_url.set_query(Some(&params));
47
48        Ok(Self { url: auth_url })
49    }
50
51    /// Ready HttpResponse with redirection to authorization page
52    #[cfg(feature = "actix")]
53    pub fn redirect(&self) -> HttpResponse {
54        HttpResponse::TemporaryRedirect()
55            .append_header((http::header::LOCATION, self.url.to_string()))
56            .finish()
57    }
58
59    pub fn url(&self) -> &Url {
60        &self.url
61    }
62}