atomic-lti-tool 2.2.0

LTI Tool related functionality
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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
use crate::errors::AtomicToolError;
use actix_web::HttpRequest;
use atomic_lti::stores::{
  jwt_store::JwtStore, key_store::KeyStore, oidc_state_store::OIDCStateStore,
  platform_store::PlatformStore,
};
use std::collections::HashMap;

/// Trait for providing dependencies to LTI handlers
///
/// Implement this trait to provide your application's specific store
/// implementations to the generic LTI handlers. This pattern enables:
/// - Dependency injection
/// - Easy testing with mock implementations
/// - Flexibility to use different storage backends
///
/// # Type Parameters
///
/// The trait defines four associated types, each corresponding to a different
/// store that the LTI handlers need:
///
/// - `OidcStateStore`: Manages OIDC authentication state
/// - `PlatformStore`: Manages LMS platform configurations
/// - `JwtStore`: Handles JWT creation and validation
/// - `KeyStore`: Manages RSA keys for JWT signing
///
/// # Example
///
/// ```ignore
/// use atomic_lti_tool::handlers::dependencies::LtiDependencies;
/// use atomic_lti::stores::{
///     jwt_store::JwtStore,
///     key_store::KeyStore,
///     oidc_state_store::OIDCStateStore,
///     platform_store::PlatformStore,
/// };
/// use std::collections::HashMap;
///
/// struct MyAppDeps {
///     // Your application state, e.g., database pool
///     // pool: PgPool,
///     jwk_passphrase: String,
///     assets: HashMap<String, String>,
/// }
///
/// impl LtiDependencies for MyAppDeps {
///     type OidcStateStore = MyOidcStateStore;
///     type PlatformStore = MyPlatformStore;
///     type JwtStore = MyJwtStore;
///     type KeyStore = MyKeyStore;
///
///     async fn create_oidc_state_store(
///         &self,
///     ) -> Result<Self::OidcStateStore, atomic_lti_tool::errors::AtomicToolError> {
///         // Create and return your OIDC state store
///         todo!()
///     }
///
///     // ... implement other methods
/// }
/// ```
///
/// # Method Overview
///
/// - `create_oidc_state_store`: Create a new OIDC state store for authentication flow
/// - `init_oidc_state_store`: Initialize OIDC state store from existing state string
/// - `create_platform_store`: Create platform store for specific issuer
/// - `create_jwt_store`: Create JWT store for token operations
/// - `key_store`: Get reference to key store
/// - `get_assets`: Get compiled frontend assets mapping
/// - `get_host`: Extract host from HTTP request
pub trait LtiDependencies: Send + Sync {
  /// Associated type for OIDC state storage
  type OidcStateStore: OIDCStateStore;

  /// Associated type for platform storage
  type PlatformStore: PlatformStore;

  /// Associated type for JWT operations
  type JwtStore: JwtStore;

  /// Associated type for key management
  type KeyStore: KeyStore;

  /// Create a new OIDC state store for a new authentication flow
  ///
  /// This method is called at the beginning of an LTI launch to create
  /// a new state value for the OIDC authentication flow.
  ///
  /// # Returns
  ///
  /// A new instance of the OIDC state store with a fresh state value.
  ///
  /// # Errors
  ///
  /// Returns an error if the state store cannot be created (e.g., database error).
  fn create_oidc_state_store(
    &self,
  ) -> impl std::future::Future<Output = Result<Self::OidcStateStore, AtomicToolError>> + Send;

  /// Initialize OIDC state store from existing state string
  ///
  /// This method is called when processing an OIDC callback to load
  /// the previously created state.
  ///
  /// # Arguments
  ///
  /// * `state` - The state string from the OIDC callback
  ///
  /// # Returns
  ///
  /// An instance of the OIDC state store loaded with the specified state.
  ///
  /// # Errors
  ///
  /// Returns an error if the state cannot be found or loaded.
  fn init_oidc_state_store(
    &self,
    state: &str,
  ) -> impl std::future::Future<Output = Result<Self::OidcStateStore, AtomicToolError>> + Send;

  /// Create platform store for specific issuer
  ///
  /// This method creates a store for accessing platform (LMS) configuration
  /// for a specific issuer.
  ///
  /// # Arguments
  ///
  /// * `iss` - The issuer URL of the LMS platform
  ///
  /// # Returns
  ///
  /// A platform store instance for the specified issuer.
  ///
  /// # Errors
  ///
  /// Returns an error if the platform store cannot be created.
  fn create_platform_store(
    &self,
    iss: &str,
  ) -> impl std::future::Future<Output = Result<Self::PlatformStore, AtomicToolError>> + Send;

  /// Create JWT store for token operations
  ///
  /// This method creates a store for creating and validating JWTs.
  ///
  /// # Returns
  ///
  /// A JWT store instance.
  ///
  /// # Errors
  ///
  /// Returns an error if the JWT store cannot be created.
  fn create_jwt_store(
    &self,
  ) -> impl std::future::Future<Output = Result<Self::JwtStore, AtomicToolError>> + Send;

  /// Get reference to key store
  ///
  /// This method returns a reference to the key store, which manages
  /// RSA keys for JWT signing and verification.
  ///
  /// # Returns
  ///
  /// A reference to the key store.
  fn key_store(&self) -> &Self::KeyStore;

  /// Get compiled frontend assets mapping
  ///
  /// This method returns a mapping of asset names to their hashed filenames,
  /// typically used for cache busting in frontend assets.
  ///
  /// # Returns
  ///
  /// A reference to the assets mapping.
  fn get_assets(&self) -> &HashMap<String, String>;

  /// Extract host from request
  ///
  /// This method extracts the host (domain name) from an HTTP request,
  /// typically used to construct callback URLs.
  ///
  /// # Arguments
  ///
  /// * `req` - The HTTP request
  ///
  /// # Returns
  ///
  /// The host string extracted from the request.
  fn get_host(&self, req: &HttpRequest) -> String;
}

#[cfg(test)]
mod tests {
  use super::*;
  use atomic_lti::errors::{OIDCError, PlatformError, SecureError};
  use atomic_lti::id_token::IdToken;
  use atomic_lti::stores::oidc_state_store::OIDCStateData;
  use atomic_lti::stores::platform_store::PlatformData;
  use async_trait::async_trait;
  use chrono::NaiveDateTime;
  use openssl::rsa::Rsa;
  use std::sync::Arc;

  // Mock implementations for testing

  struct MockOidcStateStore {
    state: String,
    nonce: String,
  }

  #[async_trait]
  impl OIDCStateStore for MockOidcStateStore {
    async fn get_state(&self) -> String {
      self.state.clone()
    }

    async fn get_nonce(&self) -> String {
      self.nonce.clone()
    }

    async fn get_created_at(&self) -> NaiveDateTime {
      use chrono::DateTime;
      DateTime::from_timestamp(0, 0).unwrap().naive_utc()
    }

    async fn destroy(&self) -> Result<usize, OIDCError> {
      Ok(1)
    }

    async fn create_with_issuer(
      &self,
      _state: &str,
      _nonce: &str,
      _issuer: &str,
    ) -> Result<(), OIDCError> {
      Ok(())
    }

    async fn find_by_state(&self, state: &str) -> Result<OIDCStateData, OIDCError> {
      Ok(OIDCStateData {
        state: state.to_string(),
        nonce: self.nonce.clone(),
        issuer: None,
      })
    }
  }

  struct MockPlatformStore {
    issuer: String,
  }

  #[async_trait]
  impl PlatformStore for MockPlatformStore {
    async fn get_oidc_url(&self) -> Result<String, PlatformError> {
      Ok(format!("https://{}/oidc", self.issuer))
    }

    async fn get_jwk_server_url(&self) -> Result<String, PlatformError> {
      Ok(format!("https://{}/jwks", self.issuer))
    }

    async fn get_token_url(&self) -> Result<String, PlatformError> {
      Ok(format!("https://{}/token", self.issuer))
    }

    async fn create(&self, _platform: PlatformData) -> Result<PlatformData, PlatformError> {
      Ok(PlatformData {
        issuer: self.issuer.clone(),
        name: Some("Mock Platform".to_string()),
        jwks_url: format!("https://{}/jwks", self.issuer),
        token_url: format!("https://{}/token", self.issuer),
        oidc_url: format!("https://{}/oidc", self.issuer),
      })
    }

    async fn find_by_iss(&self, _issuer: &str) -> Result<Option<PlatformData>, PlatformError> {
      Ok(None)
    }

    async fn update(
      &self,
      _issuer: &str,
      platform: PlatformData,
    ) -> Result<PlatformData, PlatformError> {
      Ok(platform)
    }

    async fn delete(&self, _issuer: &str) -> Result<(), PlatformError> {
      Ok(())
    }

    async fn list(&self) -> Result<Vec<PlatformData>, PlatformError> {
      Ok(vec![])
    }
  }

  struct MockJwtStore;

  #[async_trait]
  impl JwtStore for MockJwtStore {
    async fn build_jwt(&self, _id_token: &IdToken) -> Result<String, SecureError> {
      Ok("mock.jwt.token".to_string())
    }
  }

  struct MockKeyStore {
    keys: HashMap<String, Rsa<openssl::pkey::Private>>,
  }

  #[async_trait]
  impl KeyStore for MockKeyStore {
    async fn get_current_keys(
      &self,
      _limit: i64,
    ) -> Result<HashMap<String, Rsa<openssl::pkey::Private>>, SecureError> {
      Ok(self.keys.clone())
    }

    async fn get_current_key(&self) -> Result<(String, Rsa<openssl::pkey::Private>), SecureError> {
      self
        .keys
        .iter()
        .next()
        .map(|(k, v)| (k.clone(), v.clone()))
        .ok_or(SecureError::EmptyKeys)
    }

    async fn get_key(&self, kid: &str) -> Result<Rsa<openssl::pkey::Private>, SecureError> {
      self.keys.get(kid).cloned().ok_or(SecureError::InvalidKeyId)
    }
  }

  // Mock implementation of LtiDependencies for testing

  struct MockLtiDeps {
    key_store: Arc<MockKeyStore>,
    assets: HashMap<String, String>,
  }

  impl MockLtiDeps {
    fn new() -> Self {
      let mut keys = HashMap::new();
      let rsa_key = Rsa::generate(2048).unwrap();
      keys.insert("test_key_1".to_string(), rsa_key);

      let key_store = Arc::new(MockKeyStore { keys });

      let mut assets = HashMap::new();
      assets.insert("app.js".to_string(), "app.abc123.js".to_string());
      assets.insert("styles.css".to_string(), "styles.def456.css".to_string());

      Self { key_store, assets }
    }
  }

  impl LtiDependencies for MockLtiDeps {
    type OidcStateStore = MockOidcStateStore;
    type PlatformStore = MockPlatformStore;
    type JwtStore = MockJwtStore;
    type KeyStore = MockKeyStore;

    async fn create_oidc_state_store(&self) -> Result<Self::OidcStateStore, AtomicToolError> {
      Ok(MockOidcStateStore {
        state: "new-state-123".to_string(),
        nonce: "new-nonce-456".to_string(),
      })
    }

    async fn init_oidc_state_store(
      &self,
      state: &str,
    ) -> Result<Self::OidcStateStore, AtomicToolError> {
      Ok(MockOidcStateStore {
        state: state.to_string(),
        nonce: "loaded-nonce".to_string(),
      })
    }

    async fn create_platform_store(
      &self,
      iss: &str,
    ) -> Result<Self::PlatformStore, AtomicToolError> {
      Ok(MockPlatformStore {
        issuer: iss.to_string(),
      })
    }

    async fn create_jwt_store(&self) -> Result<Self::JwtStore, AtomicToolError> {
      Ok(MockJwtStore)
    }

    fn key_store(&self) -> &Self::KeyStore {
      &self.key_store
    }

    fn get_assets(&self) -> &HashMap<String, String> {
      &self.assets
    }

    fn get_host(&self, _req: &HttpRequest) -> String {
      "tool.example.com".to_string()
    }
  }

  #[tokio::test]
  async fn test_create_oidc_state_store() {
    let deps = MockLtiDeps::new();
    let store = deps.create_oidc_state_store().await.unwrap();
    assert_eq!(store.get_state().await, "new-state-123");
    assert_eq!(store.get_nonce().await, "new-nonce-456");
  }

  #[tokio::test]
  async fn test_init_oidc_state_store() {
    let deps = MockLtiDeps::new();
    let store = deps
      .init_oidc_state_store("existing-state-789")
      .await
      .unwrap();
    assert_eq!(store.get_state().await, "existing-state-789");
    assert_eq!(store.get_nonce().await, "loaded-nonce");
  }

  #[tokio::test]
  async fn test_create_platform_store() {
    let deps = MockLtiDeps::new();
    let store = deps
      .create_platform_store("https://lms.example.com")
      .await
      .unwrap();
    assert_eq!(
      store.get_oidc_url().await.unwrap(),
      "https://https://lms.example.com/oidc"
    );
  }

  #[tokio::test]
  async fn test_create_jwt_store() {
    let deps = MockLtiDeps::new();
    let store = deps.create_jwt_store().await.unwrap();
    let id_token = IdToken::default();
    let jwt = store.build_jwt(&id_token).await.unwrap();
    assert_eq!(jwt, "mock.jwt.token");
  }

  #[tokio::test]
  async fn test_key_store() {
    let deps = MockLtiDeps::new();
    let key_store = deps.key_store();
    let (kid, _key) = key_store.get_current_key().await.unwrap();
    assert_eq!(kid, "test_key_1");
  }

  #[tokio::test]
  async fn test_get_assets() {
    let deps = MockLtiDeps::new();
    let assets = deps.get_assets();
    assert_eq!(assets.get("app.js"), Some(&"app.abc123.js".to_string()));
    assert_eq!(
      assets.get("styles.css"),
      Some(&"styles.def456.css".to_string())
    );
  }

  #[tokio::test]
  async fn test_get_host() {
    let deps = MockLtiDeps::new();
    // Create a mock HttpRequest (this is simplified - in real tests you'd use actix-web test utilities)
    let req = actix_web::test::TestRequest::default().to_http_request();
    let host = deps.get_host(&req);
    assert_eq!(host, "tool.example.com");
  }

  #[tokio::test]
  async fn test_trait_is_send_sync() {
    // This test verifies that the trait and its implementations are Send + Sync
    fn assert_send_sync<T: Send + Sync>() {}
    assert_send_sync::<MockLtiDeps>();
  }
}