use std::fmt;
use serde::{Deserialize, Serialize};
use url::Url;
pub const AUTH_CODE_RESPONSE_TYPE: &str = "code";
pub const AUTHORIZATION_CODE_GRANT: &str = "authorization_code";
pub const REFRESH_TOKEN_GRANT: &str = "refresh_token";
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct GenerateAuthCodeRequest {
pub redirect_uri: Url,
pub response_type: String,
pub state: String,
}
impl GenerateAuthCodeRequest {
pub fn new(redirect_uri: Url, state: impl Into<String>) -> Self {
Self {
redirect_uri,
response_type: AUTH_CODE_RESPONSE_TYPE.to_owned(),
state: state.into(),
}
}
}
#[derive(Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ValidateAuthCodeRequest {
pub grant_type: String,
#[serde(rename = "appIdHash")]
pub app_id_hash: String,
pub code: String,
}
impl ValidateAuthCodeRequest {
pub fn new(app_id_hash: impl Into<String>, code: impl Into<String>) -> Self {
Self {
grant_type: AUTHORIZATION_CODE_GRANT.to_owned(),
app_id_hash: app_id_hash.into(),
code: code.into(),
}
}
}
impl fmt::Debug for ValidateAuthCodeRequest {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("ValidateAuthCodeRequest")
.field("grant_type", &self.grant_type)
.field("app_id_hash", &"[redacted]")
.field("code", &"[redacted]")
.finish()
}
}
#[derive(Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct RefreshTokenRequest {
pub grant_type: String,
#[serde(rename = "appIdHash")]
pub app_id_hash: String,
pub refresh_token: String,
pub pin: String,
}
impl RefreshTokenRequest {
pub fn new(
app_id_hash: impl Into<String>,
refresh_token: impl Into<String>,
pin: impl Into<String>,
) -> Self {
Self {
grant_type: REFRESH_TOKEN_GRANT.to_owned(),
app_id_hash: app_id_hash.into(),
refresh_token: refresh_token.into(),
pin: pin.into(),
}
}
}
impl fmt::Debug for RefreshTokenRequest {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("RefreshTokenRequest")
.field("grant_type", &self.grant_type)
.field("app_id_hash", &"[redacted]")
.field("refresh_token", &"[redacted]")
.field("pin", &"[redacted]")
.finish()
}
}
#[derive(Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct AccessTokenResponse {
pub code: i64,
pub s: String,
pub message: String,
#[serde(default)]
pub access_token: Option<String>,
#[serde(default)]
pub refresh_token: Option<String>,
}
impl fmt::Debug for AccessTokenResponse {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("AccessTokenResponse")
.field("code", &self.code)
.field("s", &self.s)
.field("message", &self.message)
.field(
"access_token",
&self.access_token.as_ref().map(|_| "[redacted]"),
)
.field(
"refresh_token",
&self.refresh_token.as_ref().map(|_| "[redacted]"),
)
.finish()
}
}