use crate::std_shim::String;
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct AuthServerInfo {
#[serde(rename = "tokenURL")]
pub token_url: String,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ClientCredentialRequest {
pub grant_type: String,
pub client_id: String,
pub client_secret: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub scope: Option<String>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ClientCredentialResponse {
pub access_token: String,
pub token_type: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub expires_in: Option<u64>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub refresh_token: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub scope: Option<String>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum OAuthErrorKind {
InvalidRequest,
InvalidClient,
InvalidGrant,
InvalidScope,
UnauthorizedClient,
UnsupportedGrantType,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct OAuthError {
pub error: OAuthErrorKind,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub error_description: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub error_uri: Option<String>,
}
impl OAuthError {
pub fn new(error: OAuthErrorKind, description: impl Into<String>) -> Self {
Self {
error,
error_description: Some(description.into()),
error_uri: None,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn oauth_fields_stay_snake_case() {
let r = ClientCredentialResponse {
access_token: "t".into(),
token_type: "Bearer".into(),
expires_in: Some(3600),
refresh_token: None,
scope: None,
};
let json = serde_json::to_string(&r).unwrap();
assert!(json.contains("access_token"));
assert!(json.contains("expires_in"));
}
#[test]
fn error_kinds_use_the_rfc_spelling() {
let e = OAuthError::new(OAuthErrorKind::InvalidClient, "bad secret");
assert!(
serde_json::to_string(&e)
.unwrap()
.contains(r#""error":"invalid_client""#)
);
}
}