use serde::{Deserialize, Serialize};
#[non_exhaustive]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum OAuthGrantType {
AuthorizationCode,
RefreshToken,
}
impl OAuthGrantType {
#[must_use]
pub const fn as_str(self) -> &'static str {
match self {
OAuthGrantType::AuthorizationCode => "authorization_code",
OAuthGrantType::RefreshToken => "refresh_token",
}
}
}
#[non_exhaustive]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ResponseType {
Code,
}
impl ResponseType {
#[must_use]
pub const fn as_str(self) -> &'static str {
match self {
ResponseType::Code => "code",
}
}
}
#[non_exhaustive]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum CodeChallengeMethod {
S256,
}
#[non_exhaustive]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum TokenEndpointAuthMethod {
None,
ClientSecretBasic,
}
impl TokenEndpointAuthMethod {
#[must_use]
pub const fn as_str(self) -> &'static str {
match self {
TokenEndpointAuthMethod::None => "none",
TokenEndpointAuthMethod::ClientSecretBasic => "client_secret_basic",
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn grant_type_serializes_as_oauth_strings() {
assert_eq!(
serde_json::to_string(&OAuthGrantType::AuthorizationCode).unwrap(),
r#""authorization_code""#
);
assert_eq!(
serde_json::to_string(&OAuthGrantType::RefreshToken).unwrap(),
r#""refresh_token""#
);
}
#[test]
fn code_challenge_method_only_s256() {
let s = serde_json::to_string(&CodeChallengeMethod::S256).unwrap();
assert_eq!(s, r#""S256""#);
}
#[test]
fn response_type_as_str_matches_serde() {
assert_eq!(
serde_json::to_string(&ResponseType::Code).unwrap(),
r#""code""#
);
assert_eq!(ResponseType::Code.as_str(), "code");
}
#[test]
fn token_endpoint_auth_method_as_str_matches_serde() {
assert_eq!(TokenEndpointAuthMethod::None.as_str(), "none");
assert_eq!(
serde_json::to_string(&TokenEndpointAuthMethod::None).unwrap(),
r#""none""#
);
assert_eq!(
TokenEndpointAuthMethod::ClientSecretBasic.as_str(),
"client_secret_basic"
);
assert_eq!(
serde_json::to_string(&TokenEndpointAuthMethod::ClientSecretBasic).unwrap(),
r#""client_secret_basic""#
);
}
}