clerk_rs/apis/
configuration.rs

1use crate::clerk::USER_AGENT;
2use reqwest::header::{HeaderMap, AUTHORIZATION, USER_AGENT as REQWEST_USER_AGENT};
3
4/*
5 * Clerk configuration for constructing authenticated requests to the clerk.dev api
6 *
7 * Please refer to the clerk.dev official documentation for more information: https://docs.clerk.dev
8 *
9 */
10#[derive(Debug, Clone)]
11pub struct ClerkConfiguration {
12	pub base_path: String,
13	pub user_agent: Option<String>,
14	pub client: reqwest::Client,
15	pub basic_auth: Option<BasicAuth>,
16	pub oauth_access_token: Option<String>,
17	pub bearer_access_token: Option<String>,
18	pub api_key: Option<ApiKey>,
19	// TODO: take an oauth2 token source, similar to the Go one
20}
21
22/// Merged auth allowing user to pass in a bearer token AND a api_key etc
23pub type BasicAuth = (String, Option<String>);
24
25#[derive(Debug, Clone)]
26pub struct ApiKey {
27	pub prefix: Option<String>,
28	pub key: String,
29}
30
31impl ClerkConfiguration {
32	// Creates a new client ClerkConfiguration object used to authenticate requests to the clerk.dev api
33	pub fn new(
34		basic_auth: Option<BasicAuth>,
35		oauth_access_token: Option<String>,
36		bearer_access_token: Option<String>,
37		api_key: Option<ApiKey>,
38	) -> Self {
39		// Generate our auth token
40		let construct_bearer_token = format!("Bearer {}", bearer_access_token.as_ref().unwrap_or(&String::from("")));
41		// Initialize our Clerk SDK with the default user_agent and auth headers
42		let mut headers = HeaderMap::new();
43		headers.insert(REQWEST_USER_AGENT, USER_AGENT.parse().unwrap());
44		headers.insert(
45			AUTHORIZATION,
46			construct_bearer_token
47				.parse()
48				.expect("Error: could not parse Bearer auth token into a valid request header."),
49		);
50
51		// Construct our http client (we should also support hyper client instead of reqwest in the future)
52		let client = reqwest::Client::builder()
53			.default_headers(headers)
54			.build()
55			.expect("Error: could not initialize Clerk SDK client. Please try again!");
56
57		Self {
58			base_path: "https://api.clerk.dev/v1".to_owned(),
59			user_agent: Some(USER_AGENT.to_owned()),
60			client,
61			basic_auth,
62			oauth_access_token,
63			bearer_access_token,
64			api_key,
65		}
66	}
67}
68
69impl Default for ClerkConfiguration {
70	fn default() -> Self {
71		// Initialize our Clerk SDK with the default user_agent and all stock settings (this will only give the user access to a select few clerk apis that are usable without full authorization)
72		let mut headers = HeaderMap::new();
73		headers.insert(REQWEST_USER_AGENT, USER_AGENT.parse().unwrap());
74		let client = reqwest::Client::builder()
75			.default_headers(headers)
76			.build()
77			.expect("Error: could not initialize Clerk SDK client. Please try again!");
78
79		Self {
80			base_path: "https://api.clerk.dev/v1".to_owned(),
81			user_agent: Some(USER_AGENT.to_owned()),
82			client,
83			basic_auth: None,
84			oauth_access_token: None,
85			bearer_access_token: None,
86			api_key: None,
87		}
88	}
89}