doiget-core 0.8.13

Core library: Source/Store traits, CapabilityProfile, safekey, provenance log
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
//! Elsevier ScienceDirect TDM source — DOI metadata via the
//! `/content/article/doi/<DOI>` endpoint (Phase 5c / Tier 3).
//!
//! Spec: `docs/SOURCES.md` §1 Tier 3 row + §4 "TDM sources (Phase 5)",
//! `docs/CAPABILITY.md` §2, ADR-0002 (per-publisher Cargo features),
//! ADR-0019 (eight safeguards).
//!
//! Whole module gated by `#[cfg(feature = "tdm-elsevier")]` so
//! default release binaries never include the host pattern or the
//! env-var read path (ADR-0002).
//!
//! ## Three-gate activation
//!
//! Per `docs/CAPABILITY.md` §2 a fetch only succeeds when ALL THREE
//! gates pass:
//!
//! 1. The binary was built with `--features tdm-elsevier`.
//! 2. The user set `DOIGET_KEY_ELSEVIER=<api-key>`.
//! 3. The user set `DOIGET_AGREE_TDM_ELSEVIER=1`.
//!
//! Gates 2 + 3 are checked at startup by
//! [`CapabilityProfile::from_env`] and surface as
//! `profile.tdm_elsevier = Some(TdmGrant)`. This source mirrors that
//! check in [`can_serve`](TdmElsevierSource::can_serve) and again in
//! [`fetch`](TdmElsevierSource::fetch) (defensive). Elsevier expects
//! the key in the `X-ELS-APIKey` header. The key is read once at
//! startup and carried in [`TdmGrant::api_key`](crate::TdmGrant)
//! (issue #153); the source consumes it from the grant and never
//! re-reads `DOIGET_KEY_ELSEVIER` at fetch time.
//!
//! ## Metadata-only contract (Phase 5c)
//!
//! `FetchResult.pdf_bytes` is always `None`. Elsevier's full-text
//! retrieval endpoint can stream PDF bytes, but Phase 5c
//! deliberately stays metadata-only — fetching PDFs requires the
//! eight ADR-0019 safeguards wired through the orchestrator, which
//! lands later in Phase 5.

#![cfg(feature = "tdm-elsevier")]

use async_trait::async_trait;
use secrecy::ExposeSecret;
use url::Url;

use crate::provenance::{Capability, LogEvent, LogResult, RowInput};
use crate::source::{FetchContext, FetchError, FetchResult, Source};
use crate::{CapabilityProfile, Ref};

/// Production Elsevier ScienceDirect API base URL.
const DEFAULT_BASE: &str = "https://api.elsevier.com";

/// DOI registrant prefixes Elsevier BV actually owns.
///
/// #442: without this the source answered `can_serve` for ANY DOI, so
/// wiring it into the chain would have sent every lookup — including
/// DOIs belonging to other publishers — to Elsevier BV. That is a
/// politeness problem (`docs/SOURCES.md` §6) and a privacy one
/// (`docs/PRIVACY.md`): it would disclose the user's whole reading list
/// to a publisher who has no part in it.
///
/// Scoped to the DOIs this publisher registered, the disclosure is
/// nil — resolving such a DOI goes through them anyway.
///
/// Verified against the Crossref registrant registry
/// (`api.crossref.org/prefixes/<prefix>`) on 2026-08-23.
/// `10.1016` is the bulk of ScienceDirect; `10.1006` and `10.1053` are legacy Academic Press / Mosby imprints Elsevier absorbed.
///
/// Deliberately conservative: a publisher may own prefixes not listed
/// here. A miss is not silent — it is recorded in the attempt trace as
/// `not consulted (DOI prefix ... is not ...)`, so it is diagnosable
/// from the error message rather than looking like a lookup failure.
pub(crate) const PUBLISHER_PREFIXES: &[&str] = &["10.1016", "10.1006", "10.1053"];

/// Elsevier ScienceDirect [`Source`] impl — DOI →
/// `/content/article/doi/<DOI>?httpAccept=application/json` JSON
/// record.
#[derive(Clone, Debug)]
pub struct TdmElsevierSource {
    /// API base URL. Production pins `https://api.elsevier.com`;
    /// [`with_base`](Self::with_base) lets wiremock substitute an
    /// `http://127.0.0.1:N` origin.
    base: Url,
}

impl TdmElsevierSource {
    /// Production constructor.
    #[must_use]
    pub fn new() -> Self {
        Self {
            #[allow(clippy::expect_used)]
            base: Url::parse(DEFAULT_BASE).expect("hard-coded base URL is valid"),
        }
    }

    /// Test-only constructor accepting an arbitrary base URL.
    pub fn with_base(base: Url) -> Self {
        Self { base }
    }

    /// Build the `/content/article/doi/<doi>?httpAccept=application/json`
    /// URL.
    ///
    /// Elsevier encodes the DOI directly in the path; the `/`
    /// separator in the DOI suffix must be percent-encoded. The
    /// `httpAccept` query parameter is the documented way to ask
    /// for the JSON variant of the full-text-retrieval response (a
    /// belt-and-braces companion to the `Accept` header).
    fn request_url(&self, doi: &crate::Doi) -> Result<Url, FetchError> {
        let path = format!(
            "/content/article/doi/{}",
            percent_encode_path_segment(doi.as_str())
        );
        let mut url = self
            .base
            .join(&path)
            .map_err(|e| FetchError::SourceSchema {
                hint: format!("tdm-elsevier URL construction failed: {e}"),
            })?;
        url.query_pairs_mut()
            .append_pair("httpAccept", "application/json");
        Ok(url)
    }
}

impl Default for TdmElsevierSource {
    fn default() -> Self {
        Self::new()
    }
}

#[async_trait]
impl Source for TdmElsevierSource {
    fn name(&self) -> &str {
        "tdm-elsevier"
    }

    fn can_serve(&self, profile: &CapabilityProfile, ref_: &Ref) -> bool {
        let Ref::Doi(doi) = ref_ else {
            return false;
        };
        // Both halves matter: the grant is the user's opt-in, the prefix
        // keeps that opt-in from leaking unrelated DOIs to the publisher
        // (#442). The orchestrator checks the same two conditions so it
        // can tell them apart in the trace; this is the defensive mirror.
        profile.tdm_elsevier.is_some() && PUBLISHER_PREFIXES.contains(&doi.prefix())
    }

    async fn fetch(
        &self,
        ref_: &Ref,
        profile: &CapabilityProfile,
        ctx: &FetchContext,
    ) -> Result<FetchResult, FetchError> {
        let doi = match ref_ {
            Ref::Doi(d) => d,
            Ref::Arxiv(_) => {
                return Err(FetchError::NotEligible {
                    source_key: "tdm-elsevier".into(),
                });
            }
        };

        // Defensive gate (1/3 + 2/3): the runtime grant must be
        // populated and now carries the key validated at startup
        // (issue #153). `CapabilityProfile` is immutable for the
        // process lifetime (`docs/CAPABILITY.md` §6), so the startup
        // grant is the single source of truth — no env re-read.
        let grant = profile
            .tdm_elsevier
            .as_ref()
            .ok_or_else(|| FetchError::NotEligible {
                source_key: "tdm-elsevier".into(),
            })?;
        let api_key = grant.api_key.expose_secret();
        if api_key.is_empty() {
            return Err(FetchError::NotEligible {
                source_key: "tdm-elsevier".into(),
            });
        }

        let _permit = ctx.rate_limiter.acquire(self.name()).await;

        let url = self.request_url(doi)?;
        // Elsevier authenticates via the `X-ELS-APIKey` request
        // header (Slice 20 wiring). The header value is sent on
        // the wire only; it is never logged or echoed back.
        let (body, final_url) = ctx
            .http
            .fetch_bytes_with_headers(self.name(), url, &[("X-ELS-APIKey", api_key)])
            .await?;

        let envelope: serde_json::Value =
            serde_json::from_slice(&body).map_err(|e| FetchError::SourceSchema {
                hint: format!("tdm-elsevier returned non-JSON: {e}"),
            })?;

        // Elsevier full-text-retrieval JSON envelope is shaped as:
        //   { "full-text-retrieval-response": { "coredata": {...}, ... } }
        // Surface a SourceSchema error if the top-level wrapper is
        // missing — the orchestrator falls through to the next
        // source.
        let response = envelope
            .get("full-text-retrieval-response")
            .ok_or_else(|| FetchError::SourceSchema {
                hint: format!(
                    "tdm-elsevier response missing `full-text-retrieval-response` (got: {})",
                    truncate_for_hint(&body)
                ),
            })?;
        if response.get("coredata").is_none() {
            return Err(FetchError::SourceSchema {
                hint: "tdm-elsevier response missing `coredata`".to_string(),
            });
        }

        let canonical = ref_.promote(self.name(), None).digest_hex();
        ctx.log.append(RowInput {
            event: LogEvent::Fetch,
            result: LogResult::Ok,
            capability: Capability::TdmElsevier,
            ref_: Some(doi.as_str()),
            source: Some(self.name()),
            error_code: None,
            size_bytes: Some(body.len() as u64),
            license: None,
            store_path: None,
            canonical_digest: Some(&canonical),
        })?;

        Ok(FetchResult {
            source: self.name().to_string(),
            license: "unknown".into(),
            pdf_bytes: None,
            final_url: Some(final_url),
            metadata_json: Some(response.clone()),
        })
    }
}

/// Percent-encode a path segment, preserving the RFC 3986 unreserved
/// set. `/` and other reserved characters are percent-encoded.
fn percent_encode_path_segment(segment: &str) -> String {
    let mut out = String::with_capacity(segment.len());
    for b in segment.bytes() {
        if b.is_ascii_alphanumeric() || matches!(b, b'-' | b'.' | b'_' | b'~') {
            out.push(b as char);
        } else {
            out.push_str(&format!("%{:02X}", b));
        }
    }
    out
}

fn truncate_for_hint(body: &[u8]) -> String {
    const MAX: usize = 200;
    let s = String::from_utf8_lossy(body);
    if s.len() <= MAX {
        s.into_owned()
    } else {
        format!("{}", &s[..MAX])
    }
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
#[allow(clippy::expect_used, clippy::unwrap_used, clippy::panic)]
mod tests {
    use super::*;

    use std::sync::Arc;

    use camino::Utf8PathBuf;
    use tempfile::TempDir;
    use wiremock::matchers::{header, method, path, query_param};
    use wiremock::{Mock, MockServer, ResponseTemplate};

    use crate::http::HttpClient;
    use crate::provenance::ProvenanceLog;
    use crate::rate_limiter::RateLimiter;
    use crate::{CapabilityProfile, Doi, RateLimits, Ref, TdmGrant};

    const SAMPLE_ARTICLE_HIT: &str = r#"{
        "full-text-retrieval-response": {
            "coredata": {
                "prism:doi": "10.1016/j.example.2024.001",
                "dc:title": "Example Elsevier Article",
                "prism:publicationName": "Example Journal"
            }
        }
    }"#;

    const SAMPLE_MISSING_WRAPPER: &str = r#"{"error": "not authorized"}"#;

    fn build_test_context(wiremock_host: &str) -> (TempDir, FetchContext) {
        let td = TempDir::new().expect("tempdir");
        let log_dir =
            Utf8PathBuf::try_from(td.path().to_path_buf()).expect("temp dir path must be UTF-8");
        let log_path = log_dir.join("test.jsonl");

        let http = Arc::new(HttpClient::new_for_tests_allow_http(
            "tdm-elsevier",
            wiremock_host,
        ));
        let rate_limiter = Arc::new(RateLimiter::new(RateLimits::HARD_CODED));
        let session_id = "01J0000000000000000000TEST".to_string();
        let log = Arc::new(
            ProvenanceLog::open(log_path, session_id.clone()).expect("provenance log opens"),
        );
        let ctx = FetchContext {
            http,
            rate_limiter,
            log,
            session_id,
            cache_root: None,
        };
        (td, ctx)
    }

    const TEST_KEY: &str = "els-test-key-xyz";

    fn profile_with_elsevier_grant() -> CapabilityProfile {
        let mut p = CapabilityProfile::for_tests();
        p.tdm_elsevier = Some(TdmGrant {
            // Issue #153: key flows through the grant, not the env var.
            api_key: secrecy::SecretString::from(TEST_KEY.to_string()),
            agree_env_var: "DOIGET_AGREE_TDM_ELSEVIER".to_string(),
            ..Default::default()
        });
        p
    }

    #[tokio::test]
    #[serial_test::serial]
    async fn fetch_doi_returns_full_text_retrieval_response() {
        let server = MockServer::start().await;
        Mock::given(method("GET"))
            // Elsevier path with percent-encoded DOI (`/` → `%2F`).
            .and(path("/content/article/doi/10.1016%2Fj.example.2024.001"))
            .and(query_param("httpAccept", "application/json"))
            // Slice 20: X-ELS-APIKey header MUST be present on the wire.
            .and(header("x-els-apikey", TEST_KEY))
            .respond_with(ResponseTemplate::new(200).set_body_string(SAMPLE_ARTICLE_HIT))
            .mount(&server)
            .await;

        let (_td, ctx) = build_test_context(&server.uri());
        let src =
            TdmElsevierSource::with_base(Url::parse(&server.uri()).expect("wiremock URI parses"));
        let profile = profile_with_elsevier_grant();
        let ref_ = Ref::Doi(Doi::parse("10.1016/j.example.2024.001").expect("DOI parses"));

        let result = src.fetch(&ref_, &profile, &ctx).await.expect("fetch ok");

        assert_eq!(result.source, "tdm-elsevier");
        assert!(result.pdf_bytes.is_none(), "metadata-only contract");
        let meta = result.metadata_json.expect("metadata_json present");
        assert_eq!(meta["coredata"]["dc:title"], "Example Elsevier Article");
        assert_eq!(meta["coredata"]["prism:doi"], "10.1016/j.example.2024.001");
    }

    #[tokio::test]
    #[serial_test::serial]
    async fn fetch_without_grant_is_not_eligible() {
        let (_td, ctx) = build_test_context("http://127.0.0.1:1");
        let src = TdmElsevierSource::with_base(Url::parse("http://127.0.0.1:1").expect("parses"));
        let profile = CapabilityProfile::for_tests();
        let ref_ = Ref::Doi(Doi::parse("10.1016/j.example.2024.001").expect("DOI parses"));

        assert!(
            !src.can_serve(&profile, &ref_),
            "can_serve must be false without TdmGrant"
        );
        let err = src
            .fetch(&ref_, &profile, &ctx)
            .await
            .expect_err("fetch must reject when grant is absent");
        assert!(matches!(err, FetchError::NotEligible { .. }));
    }

    #[tokio::test]
    #[serial_test::serial]
    async fn fetch_missing_wrapper_returns_source_schema() {
        let server = MockServer::start().await;
        Mock::given(method("GET"))
            .and(path("/content/article/doi/10.1016%2Fj.example.2024.001"))
            .respond_with(ResponseTemplate::new(200).set_body_string(SAMPLE_MISSING_WRAPPER))
            .mount(&server)
            .await;

        let (_td, ctx) = build_test_context(&server.uri());
        let src =
            TdmElsevierSource::with_base(Url::parse(&server.uri()).expect("wiremock URI parses"));
        let profile = profile_with_elsevier_grant();
        let ref_ = Ref::Doi(Doi::parse("10.1016/j.example.2024.001").expect("DOI parses"));

        let result = src.fetch(&ref_, &profile, &ctx).await;

        let err = result.expect_err("missing wrapper must surface as SourceSchema");
        assert!(matches!(err, FetchError::SourceSchema { .. }));
    }
    /// #442: the grant is the user's opt-in; the prefix keeps that opt-in
    /// from disclosing unrelated DOIs to Elsevier BV. Asserted with the grant
    /// PRESENT so the prefix is the only thing under test — otherwise a
    /// false `can_serve` would prove nothing about scoping.
    #[test]
    fn can_serve_is_scoped_to_this_publishers_prefixes() {
        let src = TdmElsevierSource::new();
        let profile = profile_with_elsevier_grant();

        let own = Ref::Doi(Doi::parse("10.1016/j.example.2024.001").expect("DOI parses"));
        assert!(
            src.can_serve(&profile, &own),
            "must serve its own publisher's DOI when the grant is present"
        );

        let foreign = Ref::Doi(Doi::parse("10.1103/PhysRevX.10.011001").expect("DOI parses"));
        assert!(
            !src.can_serve(&profile, &foreign),
            "must NOT serve a APS DOI even with a valid grant -- that would disclose an \
                unrelated lookup to Elsevier BV"
        );

        for p in PUBLISHER_PREFIXES {
            let doi = Doi::parse(&format!("{p}/probe")).expect("DOI parses");
            assert!(
                src.can_serve(&profile, &Ref::Doi(doi)),
                "declared prefix {p} must be served"
            );
        }
    }
}