| use std::collections::HashMap; |
|
|
| use derive_more::{Deref, From}; |
| use url::Url; |
|
|
| use super::{ |
| ApiKey, AuthorizationCode, DeviceCode, OAuthConfig, PkceVerifier, State, URLParam, |
| URLParamSpec, URLParamValue, UserCode, |
| }; |
|
|
| #[derive(Debug, Clone, PartialEq, Deref, From)] |
| pub struct URLParameters(HashMap<URLParam, URLParamValue>); |
|
|
| |
|
|
| |
| #[derive(Debug, Clone)] |
| pub struct ApiKeyRequest { |
| pub required_params: Vec<URLParamSpec>, |
| pub existing_params: Option<URLParameters>, |
| pub api_key: Option<ApiKey>, |
| } |
|
|
| |
| #[derive(Debug, Clone)] |
| pub struct ApiKeyResponse { |
| pub api_key: ApiKey, |
| pub url_params: HashMap<URLParam, URLParamValue>, |
| } |
|
|
| |
|
|
| |
| #[derive(Debug, Clone)] |
| pub struct CodeAuthFlow; |
|
|
| |
| #[derive(Debug, Clone)] |
| pub struct CodeRequest { |
| pub authorization_url: Url, |
| pub state: State, |
| pub pkce_verifier: Option<PkceVerifier>, |
| pub oauth_config: OAuthConfig, |
| } |
|
|
| |
| #[derive(Debug, Clone)] |
| pub struct CodeResponse { |
| pub code: AuthorizationCode, |
| } |
|
|
| |
|
|
| |
| #[derive(Debug, Clone)] |
| pub struct DeviceCodeAuthFlow; |
|
|
| |
| #[derive(Debug, Clone)] |
| pub struct DeviceCodeRequest { |
| pub user_code: UserCode, |
| pub device_code: DeviceCode, |
| pub verification_uri: Url, |
| pub verification_uri_complete: Option<Url>, |
| pub expires_in: u64, |
| pub interval: u64, |
| pub oauth_config: OAuthConfig, |
| } |
|
|
| |
| #[derive(Debug, Clone)] |
| pub struct DeviceCodeResponse; |
|
|
| |
| #[derive(Debug, Clone)] |
| pub struct AuthContext<Request, Response> { |
| pub request: Request, |
| pub response: Response, |
| } |
|
|
| |
| #[derive(Debug, Clone)] |
| pub enum AuthContextRequest { |
| ApiKey(ApiKeyRequest), |
| DeviceCode(DeviceCodeRequest), |
| Code(CodeRequest), |
| } |
|
|
| |
| #[derive(Debug, Clone)] |
| pub enum AuthContextResponse { |
| ApiKey(AuthContext<ApiKeyRequest, ApiKeyResponse>), |
| DeviceCode(AuthContext<DeviceCodeRequest, DeviceCodeResponse>), |
| Code(AuthContext<CodeRequest, CodeResponse>), |
| } |
|
|
| impl AuthContextResponse { |
| |
| pub fn api_key( |
| request: ApiKeyRequest, |
| api_key: impl ToString, |
| url_params: HashMap<String, String>, |
| ) -> Self { |
| Self::ApiKey(AuthContext { |
| request, |
| response: ApiKeyResponse { |
| api_key: api_key.to_string().into(), |
| url_params: url_params |
| .into_iter() |
| .map(|(k, v)| (k.into(), v.into())) |
| .collect(), |
| }, |
| }) |
| } |
|
|
| |
| pub fn device_code(request: DeviceCodeRequest) -> Self { |
| Self::DeviceCode(AuthContext { request, response: DeviceCodeResponse }) |
| } |
|
|
| |
| pub fn code(request: CodeRequest, code: impl ToString) -> Self { |
| Self::Code(AuthContext { |
| request, |
| response: CodeResponse { code: code.to_string().into() }, |
| }) |
| } |
| } |
|
|