tiny_google_oidc 0.6.0

Tiny library for Google's OpenID Connect
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
//! Defines structures and builders related to authentication configuration.  
//!
//! Provides a structured way to handle credentials
//! and endpoints required for authentication and token exchange.
//!
//! ## Structures
//! - `Config`: Stores all the necessary authentication information.
//! - `ConfigBuilder`: A builder for constructing a `Config` instance.
//!
//! # Example
//! ```rust
//! use tny_google_oidc::config::Config;
//!
//! let config = Config::builder()
//!     .auth_endpoint("https://accounts.google.com/o/oauth2/auth")
//!     .client_id("your-client-id")
//!     .client_secret("your-client-secret")
//!     .token_endpoint("https://oauth2.googleapis.com/token")
//!     .redirect_uri("https://your-app.com/callback")
//!     .build();
//! ```
//!
//! This ensures a structured and safe way to manage configuration details.

/// Holds all necessary authentication information required for Google's OpenID Connect flow.  
///
/// It is designed to be immutable once constructed.
///
/// # Fields
/// - `auth_endpoint`: The authorization endpoint URL.
/// - `client_id`: The client ID obtained from Google Cloud Console.
/// - `client_secret`: The client secret linked to the client ID.
/// - `token_endpoint`: The token exchange endpoint URL.
/// - `redirect_uri`: The redirect URI registered in Google Cloud Console.
///
/// This struct is primarily built using the `ConfigBuilder`.
///
/// # Example
/// ```rust
/// use tiny_google_oidc::config::Config;
///
/// let config = Config::builder()
///     .auth_endpoint("https://accounts.google.com/o/oauth2/auth")
///     .client_id("your-client-id")
///     .client_secret("your-client-secret")
///     .token_endpoint("https://oauth2.googleapis.com/token")
///     .redirect_uri("https://your-app.com/callback")
///     .build();
/// ```
#[derive(Debug, Clone)]
pub struct Config {
    pub(crate) auth_endpoint: AuthEndPoint,
    pub(crate) client_id: ClientID,
    pub(crate) client_secret: ClientSecret,
    pub(crate) token_endpoint: TokenEndPoint,
    pub(crate) redirect_uri: RedirectURI,
}

impl Config {
    /// Returns a new `ConfigBuilder` instance to create a `Config` object.  
    /// This method provides a convenient way to start building a `Config` instance.
    pub fn builder() -> ConfigBuilder {
        ConfigBuilder::default()
    }

    pub fn auth_endpoint(&self) -> &AuthEndPoint {
        &self.auth_endpoint
    }

    pub fn client_id(&self) -> &ClientID {
        &self.client_id
    }

    pub fn client_secret(&self) -> &ClientSecret {
        &self.client_secret
    }

    pub fn token_endpoint(&self) -> &TokenEndPoint {
        &self.token_endpoint
    }

    pub fn redirect_uri(&self) -> &RedirectURI {
        &self.redirect_uri
    }
}

/// Provides a convenient way to create a `Config` instance step by step.  
/// This ensures that all required fields are set before the `Config`
/// object is constructed.
///
/// # Example
/// ```rust
/// use tiny_google_oidc::config::ConfigBuilder;
///
/// let builder = ConfigBuilder::new()
///     .auth_endpoint("https://accounts.google.com/o/oauth2/auth")
///     .client_id("your-client-id")
///     .client_secret("your-client-secret")
///     .token_endpoint("https://oauth2.googleapis.com/token")
///     .redirect_uri("https://your-app.com/callback");
///
/// let config = builder.build();
/// ```
#[derive(Debug, Clone, Default)]
pub struct ConfigBuilder {
    auth_endpoint: AuthEndPoint,
    client_id: ClientID,
    client_secret: ClientSecret,
    token_endpoint: TokenEndPoint,
    redirect_uri: RedirectURI,
}

impl ConfigBuilder {
    /// Creates a new `ConfigBuilder` instance with default values.
    pub fn new() -> Self {
        ConfigBuilder::default()
    }

    /// Sets the authorization endpoint URL.
    pub fn auth_endpoint<T: Into<AuthEndPoint>>(mut self, auth_endpoint: T) -> ConfigBuilder {
        self.auth_endpoint = auth_endpoint.into();
        self
    }

    /// Sets the client ID obtained from Google Cloud Console.
    pub fn client_id<T: Into<ClientID>>(mut self, client_id: T) -> Self {
        self.client_id = client_id.into();
        self
    }

    /// Sets the client secret associated with the client ID.
    pub fn client_secret<T: Into<ClientSecret>>(mut self, client_secret: T) -> Self {
        self.client_secret = client_secret.into();
        self
    }

    /// Sets the token exchange endpoint URL.
    pub fn token_endpoint<T: Into<TokenEndPoint>>(mut self, token_endpoint: T) -> Self {
        self.token_endpoint = token_endpoint.into();
        self
    }

    /// Sets the redirect URI registered in Google Cloud Console.
    pub fn redirect_uri<T: Into<RedirectURI>>(mut self, redirect_uri: T) -> Self {
        self.redirect_uri = redirect_uri.into();
        self
    }

    /// Constructs a `Config` instance with the provided values.
    pub fn build(self) -> Config {
        Config {
            auth_endpoint: self.auth_endpoint,
            client_id: self.client_id,
            client_secret: self.client_secret,
            token_endpoint: self.token_endpoint,
            redirect_uri: self.redirect_uri,
        }
    }
}

/// Represents the authorization endpoint URL used in Google's OpenID Connect flow.
///
/// This tuple struct is a simple wrapper around a `String` and is used to specify
/// the URL where the authorization request is sent.
///
/// # Purpose
/// The `AuthEndPoint` struct is used as part of the `Config` to define the endpoint
/// for initiating the OpenID Connect authentication flow.
///
/// # Example
/// ```rust
/// use tiny_google_oidc::config::AuthEndPoint;
/// let endpoint_str = "https://accounts.google.com/o/oauth2/auth";
/// let auth_endpoint: AuthEndPoint = endpoint_str.into();
/// assert_eq!(auth_endpoint, AuthEndPoint(endpoint_str.to_string()))
/// ```
#[derive(Debug, Clone, Default, PartialEq)]
pub struct AuthEndPoint(pub(crate) String);

impl AuthEndPoint {
    pub fn new(endpoint: String) -> Self {
        AuthEndPoint(endpoint)
    }

    pub fn value(&self) -> &str {
        &self.0
    }
}

impl From<&str> for AuthEndPoint {
    fn from(value: &str) -> Self {
        AuthEndPoint(value.to_string())
    }
}

impl From<String> for AuthEndPoint {
    fn from(value: String) -> Self {
        AuthEndPoint(value)
    }
}

/// Represents the client ID used in Google's OpenID Connect flow.
///
/// This tuple struct is a simple wrapper around a `String` and is used to securely store
/// the client ID required for authentication and token exchange.
///
/// # Purpose
/// The `ClientID` struct is used as part of the `Config` to define the unique identifier
/// for your application. This ID is provided by Google Cloud Console when registering
/// your application.
///
/// # Example
/// ```rust
/// use tiny_google_oidc::config::ClientID;
///
/// let client_id_str = "your-client-id";
/// let client_id: ClientID = client_id_str.into();
/// assert_eq!(client_id, ClientID(client_id_str.to_string()));
/// ```
#[derive(Debug, Clone, Default, PartialEq)]
pub struct ClientID(pub(crate) String);

impl ClientID {
    pub fn new(id: String) -> Self {
        ClientID(id)
    }

    pub fn value(&self) -> &str {
        &self.0
    }
}

impl From<&str> for ClientID {
    fn from(value: &str) -> Self {
        ClientID(value.to_string())
    }
}

impl From<String> for ClientID {
    fn from(value: String) -> Self {
        ClientID(value)
    }
}

/// Represents the client secret associated with the client ID in Google's OpenID Connect flow.
///
/// This tuple struct is a simple wrapper around a `String` and is used to securely store
/// the client secret required for authentication and token exchange.
///
/// # Purpose
/// The `ClientSecret` struct is used as part of the `Config` to define the secret key
/// associated with the client ID. This secret is provided by Google Cloud Console
/// when registering your application.
///
/// # Example
/// ```rust
/// use tiny_google_oidc::config::ClientSecret;
///
/// let secret_str = "your-client-secret";
/// let client_secret: ClientSecret = secret_str.into();
/// assert_eq!(client_secret, ClientSecret(secret_str.to_string()));
/// ```
#[derive(Debug, Clone, Default, PartialEq)]
pub struct ClientSecret(pub(crate) String);

impl ClientSecret {
    pub fn new(secret: String) -> Self {
        ClientSecret(secret)
    }

    pub fn value(&self) -> &str {
        &self.0
    }
}

impl From<&str> for ClientSecret {
    fn from(value: &str) -> Self {
        ClientSecret(value.to_string())
    }
}

impl From<String> for ClientSecret {
    fn from(value: String) -> Self {
        ClientSecret(value)
    }
}

/// Represents the token endpoint URL used in Google's OpenID Connect flow.
///
/// This tuple struct is a simple wrapper around a `String` and is used to specify
/// the URL where the token exchange request is sent.
///
/// # Purpose
/// The `TokenEndPoint` struct is used as part of the `Config` to define the endpoint
/// for exchanging an authorization code for tokens (e.g., ID token, access token).
///
/// # Example
/// ```rust
/// use tiny_google_oidc::config::TokenEndPoint;
///
/// let token_endpoint_str = "https://oauth2.googleapis.com/token";
/// let token_endpoint: TokenEndPoint = token_endpoint_str.into();
/// assert_eq!(token_endpoint, TokenEndPoint(token_endpoint_str.to_string()));
/// ```
#[derive(Debug, Clone, Default, PartialEq)]
pub struct TokenEndPoint(pub(crate) String);

impl TokenEndPoint {
    pub fn new(endpoint: String) -> Self {
        TokenEndPoint(endpoint)
    }

    pub fn value(&self) -> &str {
        &self.0
    }
}

impl From<&str> for TokenEndPoint {
    fn from(value: &str) -> Self {
        TokenEndPoint(value.to_string())
    }
}

impl From<String> for TokenEndPoint {
    fn from(value: String) -> Self {
        TokenEndPoint(value)
    }
}

/// Represents the redirect URI registered in Google's OpenID Connect flow.
///
/// This tuple struct is a simple wrapper around a `String` and is used to specify
/// the URI where Google redirects the user after authentication.
///
/// # Purpose
/// The `RedirectURI` struct is used as part of the `Config` to define the URI
/// that is registered in the Google Cloud Console for your application. This URI
/// must match the one provided during the authentication request.
///
/// # Example
/// ```rust
/// use tiny_google_oidc::config::RedirectURI;
///
/// let redirect_uri_str = "https://your-app.com/callback";
/// let redirect_uri: RedirectURI = redirect_uri_str.into();
/// assert_eq!(redirect_uri, RedirectURI(redirect_uri_str.to_string()));
/// ```
#[derive(Debug, Clone, Default, PartialEq)]
pub struct RedirectURI(pub(crate) String);

impl RedirectURI {
    pub fn new(uri: String) -> Self {
        RedirectURI(uri)
    }

    pub fn value(&self) -> &str {
        &self.0
    }
}

impl From<&str> for RedirectURI {
    fn from(value: &str) -> Self {
        RedirectURI(value.to_string())
    }
}

impl From<String> for RedirectURI {
    fn from(value: String) -> Self {
        RedirectURI(value)
    }
}

#[cfg(test)]
mod tests {
    use crate::config::Config;

    use super::ConfigBuilder;

    #[test]
    fn test_config_builder() {
        let auth_endpoint = "https://auth.example.com/auth";
        let client_id = "my_client_id";
        let client_secret = "my_secret";
        let token_endpoint = "https://token.example.com";
        let redirect_uri = "https://redirect.example.com";

        let config = ConfigBuilder::new()
            .auth_endpoint(auth_endpoint)
            .client_id(client_id)
            .client_secret(client_secret)
            .token_endpoint(token_endpoint)
            .redirect_uri(redirect_uri)
            .build();

        assert_eq!(config.auth_endpoint.0, auth_endpoint);
        assert_eq!(config.client_id.0, client_id);
        assert_eq!(config.client_secret.0, client_secret);
        assert_eq!(config.token_endpoint.0, token_endpoint);
        assert_eq!(config.redirect_uri.0, redirect_uri);
    }

    #[test]
    fn test_config_builder_default() {
        let config_builder = ConfigBuilder::default();

        assert_eq!(config_builder.auth_endpoint.0, "");
        assert_eq!(config_builder.client_id.0, "");
        assert_eq!(config_builder.client_secret.0, "");
        assert_eq!(config_builder.token_endpoint.0, "");
        assert_eq!(config_builder.redirect_uri.0, "");
    }

    #[test]
    fn test_config_builder_method_chain() {
        let auth_endpoint = "https://auth.example.com/auth";
        let client_id = "my_client_id";
        let client_secret = "my_secret";
        let token_endpoint = "https://token.example.com";
        let redirect_uri = "https://redirect.example.com";

        let config = Config::builder()
            .auth_endpoint(auth_endpoint)
            .client_id(client_id)
            .client_secret(client_secret)
            .token_endpoint(token_endpoint)
            .redirect_uri(redirect_uri)
            .build();

        assert_eq!(config.auth_endpoint.0, auth_endpoint);
        assert_eq!(config.client_id.0, client_id);
        assert_eq!(config.client_secret.0, client_secret);
        assert_eq!(config.token_endpoint.0, token_endpoint);
        assert_eq!(config.redirect_uri.0, redirect_uri);
    }
}