backbone-integrations 0.6.1

Integration registry: connectors, integration accounts and an idempotent inbound event lane, with one OAuth flow (HMAC-bound state, PKCE)
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
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
//! Shared test helpers: a live pool, a fake target (records maps / ignores / rejects by event_type), a
//! REAL backbone-payment target (a settled notification drives create_payment), a capturing sink, and
//! the OAuth fakes — a credential store recording every verb call, and (as the transport port lands)
//! an outbound transport recording every URL it is handed.

#![allow(dead_code)]

use std::sync::{Arc, Mutex};

use backbone_integrations::application::service::integrations_events::{IntegrationEvent, IntegrationEventSink};
// The OAuth credential port's public shapes, re-exported so probe binaries can `use common::{..}`.
// Not every binary that includes this module consumes every name — same spirit as the
// `#![allow(dead_code)]` above.
#[allow(unused_imports)]
pub use backbone_integrations::application::service::integrations_oauth_ports::{
    OAuthCredentialFailure, OAuthCredentialStore, PURPOSE_OAUTH_TOKEN, TokenBundle,
};
use backbone_integrations::application::service::integrations_ports::*;
use backbone_integrations::IntegrationsModule;
use chrono::{DateTime, Utc};
use sqlx::PgPool;
use uuid::Uuid;

pub fn dburl() -> String {
    std::env::var("DATABASE_URL")
        .unwrap_or_else(|_| "postgres://postgres:postgres@localhost:5433/backbone_integrations".into())
}
pub async fn pool() -> PgPool {
    PgPool::connect(&dburl()).await.expect("connect")
}

/// Build the module through its public builder — the surface a composing service uses. Routing the write
/// service through `IntegrationsModule::builder().build()` makes the module wiring (struct field + builder)
/// a tested surface, so a regen that drops the field fails a test, not just a compile.
pub async fn module(pool: PgPool) -> IntegrationsModule {
    IntegrationsModule::builder()
        .with_database(pool)
        .build()
        .expect("module build")
}

/// A fake target: maps `payment_settled` to a synthetic ref, IGNORES `payment_pending`, rejects a key in
/// `reject`. Records each mapped op.
#[derive(Clone, Default)]
pub struct FakeTarget {
    pub maps: Arc<Mutex<Vec<MapRequest>>>,
    pub reject: Arc<Mutex<Option<(String, String)>>>,
}
impl FakeTarget {
    pub fn new() -> Self {
        Self::default()
    }
    pub fn rejecting(code: &str, message: &str) -> Self {
        let f = Self::default();
        *f.reject.lock().unwrap() = Some((code.into(), message.into()));
        f
    }
    pub fn map_count(&self) -> usize {
        self.maps.lock().unwrap().len()
    }
}
#[async_trait::async_trait]
impl TargetPort for FakeTarget {
    async fn map(&self, req: &MapRequest) -> Result<MapOutcome, MapRejected> {
        self.maps.lock().unwrap().push(req.clone());
        if let Some((code, message)) = self.reject.lock().unwrap().clone() {
            return Err(MapRejected { code, message });
        }
        if req.event_type == "payment_pending" {
            return Ok(MapOutcome::Ignored("payment not yet settled".into()));
        }
        Ok(MapOutcome::Mapped(MappedRef { internal_ref_type: "payment".into(), internal_ref_id: Uuid::new_v4() }))
    }
}

/// The ACL over the REAL backbone-payment module: a settled-payment notification becomes a customer receipt.
pub struct RealPaymentTarget {
    pub payment: backbone_payment::application::service::payment_write_service::PaymentWriteService,
}
impl RealPaymentTarget {
    pub fn new(pool: PgPool) -> Self {
        Self { payment: backbone_payment::application::service::payment_write_service::PaymentWriteService::new(pool) }
    }
}
#[async_trait::async_trait]
impl TargetPort for RealPaymentTarget {
    async fn map(&self, req: &MapRequest) -> Result<MapOutcome, MapRejected> {
        use backbone_payment::application::service::payment_write_service::NewPayment;
        use rust_decimal::Decimal;
        if req.event_type == "payment_pending" {
            return Ok(MapOutcome::Ignored("payment not yet settled".into()));
        }
        let p = &req.payload;
        let amount: Decimal = p.get("amount").and_then(|v| v.as_str()).and_then(|s| s.parse().ok())
            .ok_or(MapRejected { code: "bad_payload".into(), message: "missing amount".into() })?;
        let customer: Uuid = p.get("customer_id").and_then(|v| v.as_str()).and_then(|s| s.parse().ok())
            .ok_or(MapRejected { code: "bad_payload".into(), message: "missing customer_id".into() })?;
        let id = self.payment.create_payment(NewPayment {
            payment_number: format!("MID-{}", req.external_id),
            branch_id: None,
            payment_type: "receive".into(), party_type: Some("customer".into()), party_id: Some(customer),
            posting_date: chrono::Utc::now().date_naive(), currency: None, mode_of_payment_id: None,
            bank_account_id: Uuid::new_v4(), party_account_id: Uuid::new_v4(),
            paid_amount: amount, reference_no: Some(req.external_id.clone()), allocations: vec![],
            method: None, provider_txn_id: None,
            withholding_amount: Decimal::ZERO, withholding_account_id: None, withholding_tax_type: "none".into(),
        }).await.map_err(|e| MapRejected { code: "payment_rejected".into(), message: e.to_string() })?;
        Ok(MapOutcome::Mapped(MappedRef { internal_ref_type: "payment".into(), internal_ref_id: id }))
    }
}

#[derive(Clone, Default)]
pub struct CapturingSink {
    pub events: Arc<Mutex<Vec<IntegrationEvent>>>,
}
impl CapturingSink {
    pub fn new() -> Self {
        Self::default()
    }
    pub fn mapped(&self) -> usize {
        self.events.lock().unwrap().iter().filter(|e| matches!(e, IntegrationEvent::IntegrationEventMapped(_))).count()
    }
    pub fn ignored(&self) -> usize {
        self.events.lock().unwrap().iter().filter(|e| matches!(e, IntegrationEvent::IntegrationEventIgnored { .. })).count()
    }
    pub fn failed(&self) -> usize {
        self.events.lock().unwrap().iter().filter(|e| matches!(e, IntegrationEvent::IntegrationEventFailed { .. })).count()
    }
}
impl IntegrationEventSink for CapturingSink {
    fn publish(&self, event: &IntegrationEvent) {
        self.events.lock().unwrap().push(event.clone());
    }
}

pub struct DroppingSink;
impl IntegrationEventSink for DroppingSink {
    fn publish(&self, _e: &IntegrationEvent) {}
}

// ─────────────────────────────────────────────────────────────────────────────
// OAuth credential store fake
// ─────────────────────────────────────────────────────────────────────────────

/// Metadata-only record of one port call — mirrors the real store's
/// metadata-only response discipline, so assertions on the log never hold
/// token material either. `expires_at` is recorded on issue/rotate because
/// the honest-expiry contract (the stored credential carries now + expires_in)
/// is proven from this log.
#[derive(Debug, Clone, PartialEq)]
pub enum StoreCall {
    Issued { company_id: Uuid, provider: String, account_ref: String, purpose: String, expires_at: DateTime<Utc> },
    Read { company_id: Uuid, provider: String, account_ref: String },
    Rotated { company_id: Uuid, provider: String, account_ref: String, expires_at: DateTime<Utc> },
    Revoked { company_id: Uuid, provider: String, account_ref: String },
}

/// One stored credential row (the fake's internal shape — the plain strings
/// exist ONLY here, in test memory, so `read_token` can rebuild a bundle).
struct StoredFakeCredential {
    company_id: Uuid,
    provider: String,
    account_ref: String,
    access_token: String,
    refresh_token: Option<String>,
    scope: Option<String>,
    expires_at: DateTime<Utc>,
    revoked: bool,
}

impl StoredFakeCredential {
    fn matches(&self, company_id: Uuid, provider: &str, account_ref: &str) -> bool {
        self.company_id == company_id && self.provider == provider && self.account_ref == account_ref
    }
}

/// An in-memory [`OAuthCredentialStore`] that mirrors the real store's verb
/// semantics: duplicate `issue` on a live scope is refused (rotation is the
/// only replacement), `rotate` supersedes the active credential (lineage kept
/// as the ordered row list), `read_token` refuses not-found / terminal /
/// expired honestly with lazy expiry at read time, `revoke` is idempotent
/// once a scope has had a credential.
///
/// Every call is recorded in a metadata-only log ([`StoreCall`]); an
/// injectable failure short-circuits before the log to simulate a store
/// outage (the retryable transport code).
#[derive(Clone, Default)]
pub struct FakeStore {
    calls: Arc<Mutex<Vec<StoreCall>>>,
    rows: Arc<Mutex<Vec<StoredFakeCredential>>>,
    fail_with: Arc<Mutex<Option<OAuthCredentialFailure>>>,
}

impl FakeStore {
    pub fn new() -> Self {
        Self::default()
    }

    /// Make every subsequent verb fail with this error (short-circuits before
    /// the call log — an unreachable store observes nothing).
    pub fn fail_with(&self, failure: OAuthCredentialFailure) {
        *self.fail_with.lock().unwrap() = Some(failure);
    }

    pub fn clear_failure(&self) {
        *self.fail_with.lock().unwrap() = None;
    }

    /// The metadata-only call log, in order.
    pub fn calls(&self) -> Vec<StoreCall> {
        self.calls.lock().unwrap().clone()
    }

    pub fn issue_count(&self) -> usize {
        self.calls.lock().unwrap().iter().filter(|c| matches!(c, StoreCall::Issued { .. })).count()
    }

    pub fn rotate_count(&self) -> usize {
        self.calls.lock().unwrap().iter().filter(|c| matches!(c, StoreCall::Rotated { .. })).count()
    }

    pub fn revoke_count(&self) -> usize {
        self.calls.lock().unwrap().iter().filter(|c| matches!(c, StoreCall::Revoked { .. })).count()
    }

    pub fn read_count(&self) -> usize {
        self.calls.lock().unwrap().iter().filter(|c| matches!(c, StoreCall::Read { .. })).count()
    }

    fn gate(&self) -> Result<(), OAuthCredentialFailure> {
        match self.fail_with.lock().unwrap().clone() {
            Some(f) => Err(f),
            None => Ok(()),
        }
    }

    fn active_row_index(
        rows: &[StoredFakeCredential],
        company_id: Uuid,
        provider: &str,
        account_ref: &str,
    ) -> Option<usize> {
        // Latest non-revoked row for the scope — the successor of a rotation
        // shadows its predecessor, like the store's active-by-lineage read.
        rows.iter()
            .rposition(|r| !r.revoked && r.matches(company_id, provider, account_ref))
    }
}

#[async_trait::async_trait]
impl OAuthCredentialStore for FakeStore {
    async fn issue(
        &self,
        company_id: Uuid,
        provider: &str,
        account_ref: &str,
        purpose: &str,
        bundle: TokenBundle,
        expires_at: DateTime<Utc>,
    ) -> Result<Uuid, OAuthCredentialFailure> {
        self.gate()?;
        let mut rows = self.rows.lock().unwrap();
        if Self::active_row_index(&rows, company_id, provider, account_ref).is_some() {
            return Err(OAuthCredentialFailure::duplicate_active());
        }
        let id = Uuid::new_v4();
        rows.push(StoredFakeCredential {
            company_id,
            provider: provider.to_string(),
            account_ref: account_ref.to_string(),
            access_token: bundle.access_token().to_string(),
            refresh_token: bundle.refresh_token().map(str::to_string),
            scope: bundle.scope().map(str::to_string),
            expires_at,
            revoked: false,
        });
        drop(rows);
        self.calls.lock().unwrap().push(StoreCall::Issued {
            company_id,
            provider: provider.to_string(),
            account_ref: account_ref.to_string(),
            purpose: purpose.to_string(),
            expires_at,
        });
        Ok(id)
    }

    async fn read_token(
        &self,
        company_id: Uuid,
        provider: &str,
        account_ref: &str,
    ) -> Result<TokenBundle, OAuthCredentialFailure> {
        self.gate()?;
        self.calls.lock().unwrap().push(StoreCall::Read {
            company_id,
            provider: provider.to_string(),
            account_ref: account_ref.to_string(),
        });
        let rows = self.rows.lock().unwrap();
        let row = match Self::active_row_index(&rows, company_id, provider, account_ref) {
            Some(i) => &rows[i],
            None => {
                // Honest three-way refusal, matching the store's read: never
                // issued → NotFound; issued but terminal → NotActive.
                return match rows.iter().rev().find(|r| r.matches(company_id, provider, account_ref)) {
                    Some(_) => Err(OAuthCredentialFailure::not_active("revoked")),
                    None => Err(OAuthCredentialFailure::not_found()),
                };
            }
        };
        if row.expires_at <= Utc::now() {
            // Lazy expiry at read time — the observable refusal, exactly as
            // the store's read-time CAS makes an expired row unreadable.
            return Err(OAuthCredentialFailure::expired());
        }
        Ok(TokenBundle::new(
            row.access_token.clone(),
            row.refresh_token.clone(),
            row.expires_at,
            row.scope.clone(),
        ))
    }

    async fn rotate(
        &self,
        company_id: Uuid,
        provider: &str,
        account_ref: &str,
        bundle: TokenBundle,
        expires_at: DateTime<Utc>,
    ) -> Result<Uuid, OAuthCredentialFailure> {
        self.gate()?;
        let mut rows = self.rows.lock().unwrap();
        let predecessor = match Self::active_row_index(&rows, company_id, provider, account_ref) {
            Some(i) => i,
            None => return Err(OAuthCredentialFailure::not_found()),
        };
        // An expired-but-still-active row is exactly what rotation repairs —
        // the successor shadows it regardless of its expiry.
        rows[predecessor].revoked = true;
        rows.push(StoredFakeCredential {
            company_id,
            provider: provider.to_string(),
            account_ref: account_ref.to_string(),
            access_token: bundle.access_token().to_string(),
            refresh_token: bundle.refresh_token().map(str::to_string),
            scope: bundle.scope().map(str::to_string),
            expires_at,
            revoked: false,
        });
        drop(rows);
        self.calls.lock().unwrap().push(StoreCall::Rotated {
            company_id,
            provider: provider.to_string(),
            account_ref: account_ref.to_string(),
            expires_at,
        });
        Ok(Uuid::new_v4())
    }

    async fn revoke(
        &self,
        company_id: Uuid,
        provider: &str,
        account_ref: &str,
    ) -> Result<(), OAuthCredentialFailure> {
        self.gate()?;
        let mut rows = self.rows.lock().unwrap();
        let any = rows.iter().any(|r| r.matches(company_id, provider, account_ref));
        if !any {
            return Err(OAuthCredentialFailure::not_found());
        }
        // Idempotent: revoking an already-revoked scope stays a success.
        for row in rows.iter_mut() {
            if row.matches(company_id, provider, account_ref) {
                row.revoked = true;
            }
        }
        drop(rows);
        self.calls.lock().unwrap().push(StoreCall::Revoked {
            company_id,
            provider: provider.to_string(),
            account_ref: account_ref.to_string(),
        });
        Ok(())
    }
}

// ─────────────────────────────────────────────────────────────────────────────
// OAuth transport fake
// ─────────────────────────────────────────────────────────────────────────────

#[allow(unused_imports)]
use backbone_integrations::infrastructure::http::{
    IdentityClaims, OAuthTransport, TokenResponse, TransportFailure,
};

/// Metadata-only record of one outbound call the flow attempted — the URL and
/// host it was handed (a guarded endpoint by construction: the trait accepts
/// nothing else) and which verb ran. Proves "zero calls on rejection paths"
/// by absence in this log.
#[derive(Debug, Clone, PartialEq)]
pub struct TransportCall {
    pub verb: &'static str,
    pub url: String,
    pub host: String,
}

/// A scripted [`OAuthTransport`]: records every endpoint it is handed and
/// answers from configurable state — the token response, the identity claims,
/// an injectable failure. No network, no real HTTP client; the probe suite
/// drives provider behavior (audience mismatch, nonce mismatch, missing
/// expiry, invalid_grant, ...) by scripting this transport and asserting on
/// its call log.
#[derive(Clone)]
pub struct FakeTransport {
    calls: Arc<Mutex<Vec<TransportCall>>>,
    token_response: Arc<Mutex<TokenResponse>>,
    identity: Arc<Mutex<IdentityClaims>>,
    failure: Arc<Mutex<Option<TransportFailure>>>,
}

impl Default for FakeTransport {
    fn default() -> Self {
        Self {
            calls: Arc::new(Mutex::new(Vec::new())),
            token_response: Arc::new(Mutex::new(TokenResponse {
                access_token: String::new(),
                refresh_token: None,
                expires_in: None,
                scope: None,
                id_token: None,
                token_type: None,
            })),
            identity: Arc::new(Mutex::new(IdentityClaims::default())),
            failure: Arc::new(Mutex::new(None)),
        }
    }
}

impl FakeTransport {
    pub fn new() -> Self {
        Self::default()
    }

    /// The well-behaved provider: a 24h-class access token with refresh, and
    /// an identity matching `account_ref` with the expected audience/nonce.
    pub fn happy(email: &str, audience: &str, nonce: &str) -> Self {
        let t = Self::default();
        *t.token_response.lock().unwrap() = TokenResponse {
            access_token: "FAKE-ACCESS-TOKEN".into(),
            refresh_token: Some("FAKE-REFRESH-TOKEN".into()),
            expires_in: Some(86_400),
            scope: Some("https://mail.google.com/".into()),
            id_token: Some("FAKE-ID-TOKEN".into()),
            token_type: Some("Bearer".into()),
        };
        *t.identity.lock().unwrap() = IdentityClaims {
            sub: Some(format!("sub-{email}")),
            email: Some(email.into()),
            email_verified: Some(true),
            audience: Some(audience.into()),
            nonce: Some(nonce.into()),
        };
        t
    }

    /// A provider shaped like the real Google/Microsoft endpoints: the
    /// userinfo response carries only profile claims (sub/email — NO
    /// audience, NO nonce), and the audience + nonce live in the token
    /// response's id_token, which the server decodes. The id_token is
    /// minted here as an unsigned JWT-shaped triple whose payload carries
    /// `aud` + `nonce` — the echo of what the authorize request sent.
    pub fn realistic_idtoken_echo(email: &str, audience: &str, nonce: &str) -> Self {
        use base64::Engine;
        let t = Self::default();
        let claims = serde_json::json!({ "aud": audience, "nonce": nonce });
        let payload_b64 = base64::engine::general_purpose::URL_SAFE_NO_PAD
            .encode(claims.to_string().as_bytes());
        *t.token_response.lock().unwrap() = TokenResponse {
            access_token: "FAKE-ACCESS-TOKEN".into(),
            refresh_token: Some("FAKE-REFRESH-TOKEN".into()),
            expires_in: Some(86_400),
            scope: Some("https://mail.google.com/".into()),
            id_token: Some(format!("e30.{payload_b64}.")),
            token_type: Some("Bearer".into()),
        };
        *t.identity.lock().unwrap() = IdentityClaims {
            sub: Some(format!("sub-{email}")),
            email: Some(email.into()),
            email_verified: Some(true),
            audience: None,
            nonce: None,
        };
        t
    }

    pub fn set_token_response(&self, r: TokenResponse) {
        *self.token_response.lock().unwrap() = r;
    }

    pub fn set_identity(&self, c: IdentityClaims) {
        *self.identity.lock().unwrap() = c;
    }

    pub fn fail_with(&self, f: TransportFailure) {
        *self.failure.lock().unwrap() = Some(f);
    }

    pub fn clear_failure(&self) {
        *self.failure.lock().unwrap() = None;
    }

    pub fn calls(&self) -> Vec<TransportCall> {
        self.calls.lock().unwrap().clone()
    }

    pub fn call_count(&self) -> usize {
        self.calls.lock().unwrap().len()
    }

    pub fn urls(&self) -> Vec<String> {
        self.calls.lock().unwrap().iter().map(|c| c.url.clone()).collect()
    }
}

#[async_trait::async_trait]
impl OAuthTransport for FakeTransport {
    async fn exchange(
        &self,
        endpoint: &backbone_integrations::infrastructure::http::ValidatedEndpoint,
        _form: &backbone_integrations::infrastructure::http::TokenRequestForm,
    ) -> Result<TokenResponse, TransportFailure> {
        self.calls.lock().unwrap().push(TransportCall {
            verb: "exchange",
            url: endpoint.as_str().to_string(),
            host: endpoint.host().to_string(),
        });
        if let Some(f) = self.failure.lock().unwrap().clone() {
            return Err(f);
        }
        Ok(self.token_response.lock().unwrap().clone())
    }

    async fn fetch_identity(
        &self,
        endpoint: &backbone_integrations::infrastructure::http::ValidatedEndpoint,
        _access_token: &str,
    ) -> Result<IdentityClaims, TransportFailure> {
        self.calls.lock().unwrap().push(TransportCall {
            verb: "identity",
            url: endpoint.as_str().to_string(),
            host: endpoint.host().to_string(),
        });
        if let Some(f) = self.failure.lock().unwrap().clone() {
            return Err(f);
        }
        Ok(self.identity.lock().unwrap().clone())
    }
}