zitadel 5.7.6

An implementation of ZITADEL API access and authentication in Rust.
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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
//! Module for various gRPC interceptors that can be used with the
//! gRPC service clients for ZITADEL. The primary use-case of these
//! interceptors is to authenticate the clients to ZITADEL with
//! provided credentials.

use std::ops::Deref;
use std::sync::{Arc, RwLock};
use std::thread;

use tokio::runtime::Builder;
use tonic::{service::Interceptor, Request, Status};

use crate::credentials::{AuthenticationOptions, ServiceAccount};

/// Simple gRPC `Interceptor` that attaches a given access token to any request
/// a client sends. The token is attached with the `Bearer` auth-scheme.
///
/// The access token may be any valid access token for ZITADEL. A token
/// can be fetched with [`ServiceAccount`][crate::credentials::ServiceAccount]
/// credentials or you may create a `Personal Access Token` for a service account
/// in the ZITADEL console. Also, you could also use access tokens that are
/// passed from users.
///
/// The interceptor does not insert the access token if the intercepted call
/// already has an `Authorization` header.
///
/// ### Example
///
/// The following example shows how to use the `AccessTokenInterceptor` with
/// a personal access token to fetch the user profile of the service account.
///
/// ```
/// # #[tokio::main]
/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// use zitadel::api::{
///     clients::ClientBuilder, zitadel::auth::v1::GetMyUserRequest,
/// };
/// # const PERSONAL_ACCESS_TOKEN: &str = "dEnGhIFs3VnqcQU5D2zRSeiarB1nwH6goIKY0J8MWZbsnWcTuu1C59lW9DgCq1y096GYdXA";
/// # const ZITADEL_URL: &str = "https://zitadel-libraries-l8boqa.zitadel.cloud";
/// let mut client = ClientBuilder::new(ZITADEL_URL).with_access_token(PERSONAL_ACCESS_TOKEN).build_auth_client().await?;
/// let user = client.get_my_user(GetMyUserRequest {}).await?.into_inner();
/// println!("{:#?}", user);
/// # Ok(())
/// # }
/// ```
#[derive(Clone)]
pub struct AccessTokenInterceptor {
    access_token: String,
}

impl AccessTokenInterceptor {
    /// Create a new [`AccessTokenInterceptor`].
    /// The provided token can be any valid access token for ZITADEL.
    /// This includes:
    /// - Personal Access Tokens for service accounts
    /// - Access tokens that are passed from users
    /// - Access tokens that are fetched with a [`ServiceAccount`][crate::credentials::ServiceAccount]
    ///   and the corresponding [`authenticate`][crate::credentials::ServiceAccount::authenticate] method
    pub fn new(token: &str) -> Self {
        AccessTokenInterceptor {
            access_token: token.to_string(),
        }
    }
}

impl Interceptor for AccessTokenInterceptor {
    fn call(&mut self, mut request: Request<()>) -> Result<Request<()>, Status> {
        let meta = request.metadata_mut();
        if !meta.contains_key("authorization") {
            meta.insert(
                "authorization",
                format!("Bearer {}", self.access_token).parse().unwrap(),
            );
        }
        Ok(request)
    }
}

/// gRPC `Interceptor` that authenticates the service client calls
/// with the given [`ServiceAccount`][crate::credentials::ServiceAccount].
///
/// When no access token is available, the interceptor will fetch a new
/// token from the given audience (sometimes also called issuer) with
/// the - optionally - provided [`AuthenticationOptions`]. If the options
/// are set to `None`, the default options will be used.
///
/// When a token was fetched, the interceptor will only fetch a new token
/// when the lifetime of the token has expired (default 60 minutes).
///
/// **Note on async**: The interceptor does work in sync and async contexts.
/// However, in both cases, the interceptor spawns a new thread which then
/// runs a tokio runtime. This is necessary because the interceptor trait
/// only manages sync calls.
///
/// ### Example
///
/// The following example shows how to use the `ServiceAccountInterceptor`
/// to fetch the user profile of a service account.
///
/// ```
/// # #[tokio::main]
/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// use zitadel::{
///     api::{clients::ClientBuilder, zitadel::auth::v1::GetMyUserRequest},
///     credentials::{AuthenticationOptions, ServiceAccount},
/// };
/// # const SERVICE_ACCOUNT: &str = r#"
/// # {
/// #     "type": "serviceaccount",
/// #     "keyId": "181828078849229057",
/// #     "key": "-----BEGIN RSA PRIVATE KEY-----\nMIIEpQIBAAKCAQEA9VIWALQqzx1ypi42t7MG4KSOMldD10brsEUjTcjqxhl6TJrP\nsjaNKWArnV/XH+6ZKRd55mUEFFx9VflqdwQtMVPjZKXpV4cFDiPwf1Z1h1DS6im4\nSo7eKR7OGb7TLBhwt7i2UPF4WnxBhTp/M6pG5kCJ1t8glIo5yRbrILXObRmvNWMz\nVIFAyw68NDZGYNhnR8AT43zjeJTFXG/suuEoXO/mMmMjsYY8kS0BbiQeq5t5hIrr\na/odswkDPn5Zd4P91iJHDnYlgfJuo3oRmgpOj/dDsl+vTol+vveeMO4TXPwZcl36\ngUNPok7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ucIBmAQHFsYeRGL5nz1hHGkneA87uDzlk3zZk\nOORktReRAoGAGUfU2UfaniAlqrZsSma3ZTlvJWs1x8cbVDyKTYMX5ShHhp+cA86H\nYjSSol6GI2wQPP+qIvZ1E8XyzD2miMJabl92/WY0tHejNNBEHwD8uBZKrtMoFWM7\nWJNl+Xneu/sT8s4pP2ng6QE7jpHXi2TUNmSlgQry9JN2AmA9TuSTW2Y=\n-----END RSA PRIVATE KEY-----\n",
/// #     "userId": "181828061098934529"
/// # }"#;
/// # const ZITADEL_URL: &str = "https://zitadel-libraries-l8boqa.zitadel.cloud";
/// let service_account = ServiceAccount::load_from_json(SERVICE_ACCOUNT)?;
/// let mut client = ClientBuilder::new(ZITADEL_URL)
///     .with_service_account(
///         &service_account,
///         Some(AuthenticationOptions {
///             api_access: true,
///             ..Default::default()
///         })
///     )
///     .build_auth_client()
///     .await?;
/// let user = client.get_my_user(GetMyUserRequest {}).await?.into_inner();
/// println!("{:#?}", user);
/// # Ok(())
/// # }
/// ```
#[derive(Clone)]
pub struct ServiceAccountInterceptor {
    inner: Arc<ServiceAccountInterceptorInner>,
}

struct ServiceAccountInterceptorInner {
    audience: String,
    service_account: ServiceAccount,
    auth_options: AuthenticationOptions,
    state: RwLock<Option<ServiceAccountInterceptorState>>,
}

struct ServiceAccountInterceptorState {
    token: String,
    token_expiry: time::OffsetDateTime,
}

impl ServiceAccountInterceptor {
    /// Create a new [`ServiceAccountInterceptor`].
    /// The interceptor uses the provided service account with
    /// the given authentication options to fetch an access token
    /// and attach it to the intercepted calls.
    pub fn new(
        audience: &str,
        service_account: &ServiceAccount,
        auth_options: Option<AuthenticationOptions>,
    ) -> Self {
        Self {
            inner: Arc::new(ServiceAccountInterceptorInner {
                audience: audience.to_string(),
                service_account: service_account.clone(),
                auth_options: auth_options.unwrap_or_default(),
                state: RwLock::new(None),
            }),
        }
    }
}

impl Interceptor for ServiceAccountInterceptor {
    fn call(&mut self, mut request: tonic::Request<()>) -> Result<tonic::Request<()>, Status> {
        let meta = request.metadata_mut();
        if !meta.contains_key("authorization") {
            // We unwrap the RWLock to propagate the error if any
            // thread panics and the lock is poisoned
            let state_read_guard = self.inner.state.read().unwrap();

            if let Some(ServiceAccountInterceptorState {
                token,
                token_expiry,
            }) = state_read_guard.deref()
            {
                if token_expiry > &time::OffsetDateTime::now_utc() {
                    meta.insert(
                        "authorization",
                        format!("Bearer {}", token).parse().unwrap(),
                    );

                    return Ok(request);
                }
            }
            drop(state_read_guard);

            let aud = self.inner.audience.clone();
            let auth = self.inner.auth_options.clone();
            let sa = self.inner.service_account.clone();

            let token = thread::spawn(move || {
                let rt = Builder::new_current_thread().enable_all().build().unwrap();
                rt.block_on(async {
                    sa.authenticate_with_options(&aud, &auth)
                        .await
                        .map_err(|e| Status::internal(e.to_string()))
                })
            });

            let token = token
                .join()
                .map_err(|_| Status::internal("could not fetch token"))??;

            // We unwrap the RWLock to propagate the error if any
            // thread panics and the lock is poisoned
            let mut state_write_guard = self.inner.state.write().unwrap();

            *state_write_guard = Some(ServiceAccountInterceptorState {
                token: token.clone(),
                token_expiry: time::OffsetDateTime::now_utc() + time::Duration::minutes(59),
            });

            meta.insert(
                "authorization",
                format!("Bearer {}", token).parse().unwrap(),
            );
        }

        Ok(request)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    const ZITADEL_URL: &str = "https://zitadel-libraries-l8boqa.zitadel.cloud";
    const SERVICE_ACCOUNT: &str = r#"
    {
        "type": "serviceaccount",
        "keyId": "181828078849229057",
        "key": "-----BEGIN RSA PRIVATE KEY-----\nMIIEpQIBAAKCAQEA9VIWALQqzx1ypi42t7MG4KSOMldD10brsEUjTcjqxhl6TJrP\nsjaNKWArnV/XH+6ZKRd55mUEFFx9VflqdwQtMVPjZKXpV4cFDiPwf1Z1h1DS6im4\nSo7eKR7OGb7TLBhwt7i2UPF4WnxBhTp/M6pG5kCJ1t8glIo5yRbrILXObRmvNWMz\nVIFAyw68NDZGYNhnR8AT43zjeJTFXG/suuEoXO/m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sJpmNOSj6oTm3wndTdhRCT4x+TxUxf6aaZ9ZuEz7xRq6m/ZF1ynqUi5ramyyj9kt\neYD5OSBNODVUhJoSGpLEDjQDg1iucIBmAQHFsYeRGL5nz1hHGkneA87uDzlk3zZk\nOORktReRAoGAGUfU2UfaniAlqrZsSma3ZTlvJWs1x8cbVDyKTYMX5ShHhp+cA86H\nYjSSol6GI2wQPP+qIvZ1E8XyzD2miMJabl92/WY0tHejNNBEHwD8uBZKrtMoFWM7\nWJNl+Xneu/sT8s4pP2ng6QE7jpHXi2TUNmSlgQry9JN2AmA9TuSTW2Y=\n-----END RSA PRIVATE KEY-----\n",
        "userId": "181828061098934529"
    }"#;

    #[test]
    fn token_interceptor_attach_auth_metadata() {
        let mut interceptor = AccessTokenInterceptor::new("token");
        let request = Request::new(());

        assert!(request.metadata().is_empty());

        let request = interceptor.call(request).unwrap();

        assert!(request.metadata().contains_key("authorization"));
        assert_eq!(
            request.metadata().get("authorization").unwrap(),
            "Bearer token"
        );
    }

    #[test]
    fn token_interceptor_ignore_existing_auth_metadata() {
        let mut interceptor = AccessTokenInterceptor::new("token");
        let mut request = Request::new(());
        request
            .metadata_mut()
            .insert("authorization", "Bearer existing".parse().unwrap());

        assert!(!request.metadata().is_empty());

        let request = interceptor.call(request).unwrap();

        assert!(request.metadata().contains_key("authorization"));
        assert_eq!(
            request.metadata().get("authorization").unwrap(),
            "Bearer existing"
        );
    }

    #[test]
    fn service_account_interceptor_attach_auth_metadata_sync_context() {
        let sa = ServiceAccount::load_from_json(SERVICE_ACCOUNT).unwrap();
        let mut interceptor = ServiceAccountInterceptor::new(ZITADEL_URL, &sa, None);
        let request = Request::new(());

        assert!(request.metadata().is_empty());

        let request = interceptor.call(request).unwrap();

        assert!(request.metadata().contains_key("authorization"));
        assert!(!request
            .metadata()
            .get("authorization")
            .unwrap()
            .to_str()
            .unwrap()
            .is_empty());
    }

    #[tokio::test]
    async fn service_account_interceptor_attach_auth_metadata_async_context() {
        let sa = ServiceAccount::load_from_json(SERVICE_ACCOUNT).unwrap();
        let mut interceptor = ServiceAccountInterceptor::new(ZITADEL_URL, &sa, None);
        let request = Request::new(());

        assert!(request.metadata().is_empty());

        let request = interceptor.call(request).unwrap();

        assert!(request.metadata().contains_key("authorization"));
        assert!(!request
            .metadata()
            .get("authorization")
            .unwrap()
            .to_str()
            .unwrap()
            .is_empty());
    }

    #[test]
    fn service_account_interceptor_can_be_cloned_and_shares_token_sync_context() {
        let sa = ServiceAccount::load_from_json(SERVICE_ACCOUNT).unwrap();
        let mut interceptor = ServiceAccountInterceptor::new(ZITADEL_URL, &sa, None);
        let mut second_interceptor = interceptor.clone();
        let request = Request::new(());
        let second_request = Request::new(());

        assert!(request.metadata().is_empty());
        assert!(second_request.metadata().is_empty());

        let request = interceptor.call(request).unwrap();
        let second_request = second_interceptor.call(second_request).unwrap();

        assert_eq!(
            request.metadata().get("authorization"),
            second_request.metadata().get("authorization")
        );
    }

    #[tokio::test]
    async fn service_account_interceptor_can_be_cloned_and_shares_token_async_context() {
        let sa = ServiceAccount::load_from_json(SERVICE_ACCOUNT).unwrap();
        let mut interceptor = ServiceAccountInterceptor::new(ZITADEL_URL, &sa, None);
        let mut second_interceptor = interceptor.clone();
        let request = Request::new(());
        let second_request = Request::new(());

        assert!(request.metadata().is_empty());
        assert!(second_request.metadata().is_empty());

        let request = interceptor.call(request).unwrap();
        let second_request = second_interceptor.call(second_request).unwrap();

        assert_eq!(
            request.metadata().get("authorization"),
            second_request.metadata().get("authorization")
        );
    }

    #[test]
    fn service_account_interceptor_ignore_existing_auth_metadata_sync_context() {
        let sa = ServiceAccount::load_from_json(SERVICE_ACCOUNT).unwrap();
        let mut interceptor = ServiceAccountInterceptor::new(ZITADEL_URL, &sa, None);
        let mut request = Request::new(());
        request
            .metadata_mut()
            .insert("authorization", "Bearer existing".parse().unwrap());

        assert!(!request.metadata().is_empty());

        let request = interceptor.call(request).unwrap();

        assert!(request.metadata().contains_key("authorization"));
        assert_eq!(
            request.metadata().get("authorization").unwrap(),
            "Bearer existing"
        );
    }

    #[tokio::test]
    async fn service_account_interceptor_ignore_existing_auth_metadata_async_context() {
        let sa = ServiceAccount::load_from_json(SERVICE_ACCOUNT).unwrap();
        let mut interceptor = ServiceAccountInterceptor::new(ZITADEL_URL, &sa, None);
        let mut request = Request::new(());
        request
            .metadata_mut()
            .insert("authorization", "Bearer existing".parse().unwrap());

        assert!(!request.metadata().is_empty());

        let request = interceptor.call(request).unwrap();

        assert!(request.metadata().contains_key("authorization"));
        assert_eq!(
            request.metadata().get("authorization").unwrap(),
            "Bearer existing"
        );
    }

    #[test]
    fn service_account_interceptor_should_respect_token_lifetime_sync() {
        let sa = ServiceAccount::load_from_json(SERVICE_ACCOUNT).unwrap();
        let mut interceptor = ServiceAccountInterceptor::new(ZITADEL_URL, &sa, None);
        interceptor.call(Request::new(())).unwrap();
        let token = interceptor
            .inner
            .state
            .read()
            .unwrap()
            .as_ref()
            .unwrap()
            .token
            .clone();
        interceptor.call(Request::new(())).unwrap();

        assert_eq!(
            token,
            interceptor
                .inner
                .state
                .read()
                .unwrap()
                .as_ref()
                .unwrap()
                .token
        );
    }

    #[tokio::test]
    async fn service_account_interceptor_should_respect_token_lifetime_async() {
        let sa = ServiceAccount::load_from_json(SERVICE_ACCOUNT).unwrap();
        let mut interceptor = ServiceAccountInterceptor::new(ZITADEL_URL, &sa, None);
        interceptor.call(Request::new(())).unwrap();
        let token = interceptor
            .inner
            .state
            .read()
            .unwrap()
            .as_ref()
            .unwrap()
            .token
            .clone();
        interceptor.call(Request::new(())).unwrap();

        assert_eq!(
            token,
            interceptor
                .inner
                .state
                .read()
                .unwrap()
                .as_ref()
                .unwrap()
                .token
        );
    }
}