gamlastan 0.4.1

SAML 2.0 library - types, XML, crypto, metadata, bindings, security, profiles
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
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
// SAML 2.0 Identity Provider Discovery
//
// Two mechanisms are implemented:
// - SAML Profiles Section 4.3 (Common Domain Cookie): a `_saml_idp` cookie
//   in a common domain stores the list of IdPs that authenticated the user.
// - The Identity Provider Discovery Service Protocol and Profile
//   (sstc-saml-idp-discovery): an SP redirects the browser to a discovery
//   service with `entityID`/`return`/`returnIDParam`/`policy`/`isPassive`
//   query parameters; the DS redirects back with the chosen IdP entity ID.
//   The DS MUST validate the `return` URL against the SP's registered
//   `idpdisc:DiscoveryResponse` metadata endpoints (phishing protection).

use crate::bindings::encoding::{parse_query_string_raw, url_decode};
use crate::profiles::error::ProfileError;

/// The standard cookie name for SAML IdP Discovery.
pub const COMMON_DOMAIN_COOKIE_NAME: &str = "_saml_idp";

/// Namespace of the `idpdisc:DiscoveryResponse` metadata extension, also used
/// as the Binding value on DiscoveryResponse endpoints.
pub const IDPDISC_NS: &str = "urn:oasis:names:tc:SAML:profiles:SSO:idp-discovery-protocol";

/// The single-IdP discovery protocol policy URI (the default policy).
pub const DISCOVERY_POLICY_SINGLE: &str =
    "urn:oasis:names:tc:SAML:profiles:SSO:idp-discovery-protocol:single";

/// Default name of the query parameter carrying the chosen IdP entity ID.
pub const DEFAULT_RETURN_ID_PARAM: &str = "entityID";

/// Encode an IdP entity ID for inclusion in the Common Domain Cookie.
///
/// The value is base64url-encoded (no padding) per the SAML profiles spec.
pub fn encode_idp_entity_id(entity_id: &str) -> String {
    use base64::engine::general_purpose::URL_SAFE_NO_PAD;
    use base64::Engine;
    URL_SAFE_NO_PAD.encode(entity_id.as_bytes())
}

/// Decode an IdP entity ID from the Common Domain Cookie.
pub fn decode_idp_entity_id(encoded: &str) -> Result<String, ProfileError> {
    use base64::engine::general_purpose::URL_SAFE_NO_PAD;
    use base64::Engine;
    let bytes = URL_SAFE_NO_PAD
        .decode(encoded)
        .map_err(|_| ProfileError::InvalidCommonDomainCookie)?;
    String::from_utf8(bytes).map_err(|_| ProfileError::InvalidCommonDomainCookie)
}

/// Build the cookie value from a list of IdP entity IDs.
///
/// Each entity ID is base64url-encoded and separated by spaces.
pub fn build_cookie_value(idp_entity_ids: &[&str]) -> String {
    idp_entity_ids
        .iter()
        .map(|id| encode_idp_entity_id(id))
        .collect::<Vec<_>>()
        .join(" ")
}

/// Parse the cookie value to extract the list of IdP entity IDs.
pub fn parse_cookie_value(cookie_value: &str) -> Result<Vec<String>, ProfileError> {
    if cookie_value.is_empty() {
        return Ok(vec![]);
    }
    cookie_value
        .split(' ')
        .filter(|s| !s.is_empty())
        .map(decode_idp_entity_id)
        .collect()
}

/// Get the most recently used IdP entity ID from the cookie.
///
/// The most recent IdP is the last entry in the cookie.
pub fn most_recent_idp(cookie_value: &str) -> Result<Option<String>, ProfileError> {
    let idps = parse_cookie_value(cookie_value)?;
    Ok(idps.into_iter().last())
}

/// Add an IdP to the cookie value. If the IdP is already present,
/// move it to the end (most recent position).
pub fn add_idp_to_cookie(
    current_cookie: Option<&str>,
    idp_entity_id: &str,
) -> Result<String, ProfileError> {
    let mut idps = match current_cookie {
        Some(v) if !v.is_empty() => parse_cookie_value(v)?,
        _ => vec![],
    };

    // Remove if already present (to re-add at end)
    idps.retain(|id| id != idp_entity_id);
    idps.push(idp_entity_id.to_string());

    let refs: Vec<&str> = idps.iter().map(|s| s.as_str()).collect();
    Ok(build_cookie_value(&refs))
}

/// Build the discovery service return URL.
///
/// The discovery service redirects back to the SP with the selected IdP
/// entity ID as a query parameter.
pub fn build_return_url(sp_return_url: &str, idp_entity_id: &str, return_param: &str) -> String {
    let separator = if sp_return_url.contains('?') {
        "&"
    } else {
        "?"
    };
    format!(
        "{sp_return_url}{separator}{return_param}={}",
        url_encode(idp_entity_id)
    )
}

// ── Discovery Service protocol (sstc-saml-idp-discovery) ──────────────────

/// A parsed discovery service request (the SP -> DS redirect).
#[derive(Debug, Clone)]
pub struct DiscoveryServiceRequest {
    /// The requesting SP's entity ID (`entityID`, required).
    pub entity_id: String,
    /// Where to send the response (`return`). When absent, the DS uses the
    /// SP's default DiscoveryResponse endpoint from metadata.
    pub return_url: Option<String>,
    /// Query parameter name for the chosen IdP (`returnIDParam`).
    pub return_id_param: String,
    /// The discovery protocol policy (`policy`). `None` means the default
    /// single-IdP policy.
    pub policy: Option<String>,
    /// Whether the DS must not interact with the user (`isPassive`).
    pub is_passive: bool,
}

impl DiscoveryServiceRequest {
    /// Whether the requested policy is one this implementation supports
    /// (absent or the single-IdP policy).
    pub fn is_supported_policy(&self) -> bool {
        match &self.policy {
            None => true,
            Some(p) => p == DISCOVERY_POLICY_SINGLE,
        }
    }
}

/// Parse a discovery service request from the request's query string.
pub fn parse_discovery_service_request(
    query: &str,
) -> Result<DiscoveryServiceRequest, ProfileError> {
    let mut entity_id = None;
    let mut return_url = None;
    let mut return_id_param = None;
    let mut policy = None;
    let mut is_passive = false;

    for (key, value) in parse_query_string_raw(query) {
        let value = url_decode(value)?;
        match key {
            "entityID" => entity_id = Some(value),
            "return" => return_url = Some(value),
            "returnIDParam" => return_id_param = Some(value),
            "policy" => policy = Some(value),
            "isPassive" => is_passive = value == "true" || value == "1",
            _ => {}
        }
    }

    Ok(DiscoveryServiceRequest {
        entity_id: entity_id.ok_or(ProfileError::DiscoveryMissingEntityId)?,
        return_url,
        return_id_param: return_id_param.unwrap_or_else(|| DEFAULT_RETURN_ID_PARAM.to_string()),
        policy,
        is_passive,
    })
}

/// A DiscoveryResponse endpoint registered in SP metadata.
#[derive(Debug, Clone, PartialEq)]
pub struct DiscoveryResponseEndpoint {
    /// The endpoint location (the allowed return URL base).
    pub location: String,
    /// The endpoint index.
    pub index: u16,
    /// Whether this is the default endpoint.
    pub is_default: bool,
}

/// Extract `idpdisc:DiscoveryResponse` endpoints from the raw XML of an SP
/// role's `<Extensions>` element (see
/// [`Extensions::raw_xml`](crate::metadata::types::extensions::Extensions)).
///
/// The fragment is parsed with common metadata-extension prefixes
/// pre-declared, so extensions that rely on prefixes declared on an ancestor
/// element still parse.
pub fn parse_discovery_response_endpoints(
    extensions_raw_xml: &str,
) -> Result<Vec<DiscoveryResponseEndpoint>, ProfileError> {
    if extensions_raw_xml.trim().is_empty() {
        return Ok(vec![]);
    }

    // Wrap the fragment so it has a single root, pre-declaring prefixes that
    // are conventionally bound in SAML metadata documents.
    let wrapped = format!(
        concat!(
            "<w xmlns:idpdisc=\"{idpdisc}\"",
            " xmlns:md=\"urn:oasis:names:tc:SAML:2.0:metadata\"",
            " xmlns:mdui=\"urn:oasis:names:tc:SAML:metadata:ui\"",
            " xmlns:mdrpi=\"urn:oasis:names:tc:SAML:metadata:rpi\"",
            " xmlns:mdattr=\"urn:oasis:names:tc:SAML:metadata:attribute\"",
            " xmlns:shibmd=\"urn:mace:shibboleth:metadata:1.0\"",
            " xmlns:alg=\"urn:oasis:names:tc:SAML:metadata:algsupport\"",
            " xmlns:init=\"urn:oasis:names:tc:SAML:profiles:SSO:request-init\"",
            " xmlns:saml=\"urn:oasis:names:tc:SAML:2.0:assertion\"",
            " xmlns:ds=\"http://www.w3.org/2000/09/xmldsig#\"",
            ">{body}</w>"
        ),
        idpdisc = IDPDISC_NS,
        body = extensions_raw_xml
    );

    let doc = uppsala::parse(&wrapped)
        .map_err(|e| ProfileError::Metadata(format!("cannot parse Extensions XML: {e}")))?;
    let root = doc
        .document_element()
        .ok_or_else(|| ProfileError::Metadata("empty Extensions XML".to_string()))?;

    let mut endpoints = Vec::new();
    collect_discovery_responses(&doc, root, &mut endpoints);
    Ok(endpoints)
}

fn collect_discovery_responses(
    doc: &uppsala::Document<'_>,
    node: uppsala::NodeId,
    out: &mut Vec<DiscoveryResponseEndpoint>,
) {
    for child in doc.children_iter(node) {
        if let Some(elem) = doc.element(child) {
            if elem.matches_name_ns(IDPDISC_NS, "DiscoveryResponse") {
                // `index` is required on IndexedEndpoint. Skip endpoints with
                // a missing or unparseable index instead of defaulting to 0,
                // which could promote malformed metadata to the lowest-index
                // (default) endpoint.
                let location = doc.get_attribute(child, "Location");
                let index = doc
                    .get_attribute(child, "index")
                    .and_then(|v| v.parse().ok());
                if let (Some(location), Some(index)) = (location, index) {
                    let is_default = doc
                        .get_attribute(child, "isDefault")
                        .is_some_and(|v| v == "true" || v == "1");
                    out.push(DiscoveryResponseEndpoint {
                        location: location.to_string(),
                        index,
                        is_default,
                    });
                }
            }
            collect_discovery_responses(doc, child, out);
        }
    }
}

/// The default DiscoveryResponse endpoint: isDefault=true, else lowest index.
pub fn default_discovery_response_endpoint(
    endpoints: &[DiscoveryResponseEndpoint],
) -> Option<&DiscoveryResponseEndpoint> {
    endpoints
        .iter()
        .find(|e| e.is_default)
        .or_else(|| endpoints.iter().min_by_key(|e| e.index))
}

/// Verify a `return` URL against the SP's registered DiscoveryResponse
/// endpoints (phishing protection, sstc-saml-idp-discovery section 2.4.1).
///
/// The scheme, host, port and path of the return URL MUST match a registered
/// Location; any registered query string must be preserved, and MAY be
/// extended with additional parameters.
pub fn verify_return_url(return_url: &str, registered: &[DiscoveryResponseEndpoint]) -> bool {
    let (return_base, return_query) = split_url_query(return_url);
    registered.iter().any(|e| {
        let (registered_base, registered_query) = split_url_query(&e.location);
        return_base == registered_base
            && match registered_query {
                None => true,
                Some(expected) => {
                    return_query == Some(expected)
                        || return_query
                            .is_some_and(|actual| actual.starts_with(&format!("{expected}&")))
                }
            }
    })
}

fn split_url_query(url: &str) -> (&str, Option<&str>) {
    match url.split_once('?') {
        Some((base, query)) => (base, Some(query)),
        None => (url, None),
    }
}

/// Build the DS -> SP redirect URL answering a discovery request.
///
/// - The return URL is taken from the request, falling back to the SP's
///   default DiscoveryResponse endpoint.
/// - When `registered` is non-empty, the return URL MUST match a registered
///   endpoint or the request is rejected.
/// - `selected_idp = None` (e.g. an isPassive request with no known IdP)
///   redirects back without the returnIDParam, as the profile requires.
pub fn create_discovery_service_response(
    request: &DiscoveryServiceRequest,
    registered: &[DiscoveryResponseEndpoint],
    selected_idp: Option<&str>,
) -> Result<String, ProfileError> {
    let return_url = match &request.return_url {
        Some(url) => {
            if !registered.is_empty() && !verify_return_url(url, registered) {
                return Err(ProfileError::DiscoveryReturnUrlNotRegistered(url.clone()));
            }
            url.clone()
        }
        None => default_discovery_response_endpoint(registered)
            .map(|e| e.location.clone())
            .ok_or(ProfileError::DiscoveryNoReturnUrl)?,
    };

    Ok(match selected_idp {
        Some(idp) => build_return_url(&return_url, idp, &request.return_id_param),
        None => return_url,
    })
}

/// Simple URL encoding for the entity ID.
fn url_encode(s: &str) -> String {
    let mut result = String::with_capacity(s.len() * 2);
    for byte in s.bytes() {
        match byte {
            b'A'..=b'Z' | b'a'..=b'z' | b'0'..=b'9' | b'-' | b'_' | b'.' | b'~' => {
                result.push(byte as char);
            }
            _ => {
                result.push_str(&format!("%{byte:02X}"));
            }
        }
    }
    result
}

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

    #[test]
    fn test_encode_decode_roundtrip() {
        let entity_id = "https://idp.example.com";
        let encoded = encode_idp_entity_id(entity_id);
        let decoded = decode_idp_entity_id(&encoded).unwrap();
        assert_eq!(decoded, entity_id);
    }

    #[test]
    fn test_build_parse_cookie() {
        let idps = &["https://idp1.example.com", "https://idp2.example.com"];
        let cookie = build_cookie_value(idps);
        let parsed = parse_cookie_value(&cookie).unwrap();
        assert_eq!(parsed, idps);
    }

    #[test]
    fn test_parse_empty_cookie() {
        let parsed = parse_cookie_value("").unwrap();
        assert!(parsed.is_empty());
    }

    #[test]
    fn test_most_recent_idp() {
        let idps = &["https://idp1.example.com", "https://idp2.example.com"];
        let cookie = build_cookie_value(idps);
        let recent = most_recent_idp(&cookie).unwrap();
        assert_eq!(recent, Some("https://idp2.example.com".to_string()));
    }

    #[test]
    fn test_add_idp_new() {
        let cookie = add_idp_to_cookie(None, "https://idp1.example.com").unwrap();
        let idps = parse_cookie_value(&cookie).unwrap();
        assert_eq!(idps, vec!["https://idp1.example.com"]);
    }

    #[test]
    fn test_add_idp_move_to_end() {
        let initial = build_cookie_value(&["https://idp1.example.com", "https://idp2.example.com"]);
        let cookie = add_idp_to_cookie(Some(&initial), "https://idp1.example.com").unwrap();
        let idps = parse_cookie_value(&cookie).unwrap();
        assert_eq!(
            idps,
            vec!["https://idp2.example.com", "https://idp1.example.com",]
        );
    }

    #[test]
    fn test_build_return_url() {
        let url = build_return_url(
            "https://sp.example.com/ds",
            "https://idp.example.com",
            "entityID",
        );
        assert!(url.starts_with("https://sp.example.com/ds?entityID="));
        assert!(url.contains("https%3A%2F%2Fidp.example.com"));
    }

    #[test]
    fn test_build_return_url_existing_query() {
        let url = build_return_url(
            "https://sp.example.com/ds?foo=bar",
            "https://idp.example.com",
            "entityID",
        );
        assert!(url.contains("&entityID="));
    }

    // ── Discovery Service protocol tests ────────────────────────────────────

    fn registered_endpoints() -> Vec<DiscoveryResponseEndpoint> {
        vec![
            DiscoveryResponseEndpoint {
                location: "https://sp.example.com/disco".to_string(),
                index: 1,
                is_default: false,
            },
            DiscoveryResponseEndpoint {
                location: "https://sp.example.com/disco-default".to_string(),
                index: 0,
                is_default: true,
            },
        ]
    }

    #[test]
    fn test_parse_discovery_service_request() {
        let query = "entityID=https%3A%2F%2Fsp.example.com&return=https%3A%2F%2Fsp.example.com%2Fdisco%3Fsid%3D42&returnIDParam=idp&isPassive=true";
        let req = parse_discovery_service_request(query).unwrap();
        assert_eq!(req.entity_id, "https://sp.example.com");
        assert_eq!(
            req.return_url.as_deref(),
            Some("https://sp.example.com/disco?sid=42")
        );
        assert_eq!(req.return_id_param, "idp");
        assert!(req.is_passive);
        assert!(req.is_supported_policy());
    }

    #[test]
    fn test_parse_discovery_service_request_defaults() {
        let req = parse_discovery_service_request("entityID=https%3A%2F%2Fsp.example.com").unwrap();
        assert_eq!(req.return_id_param, DEFAULT_RETURN_ID_PARAM);
        assert!(req.return_url.is_none());
        assert!(!req.is_passive);
    }

    #[test]
    fn test_parse_discovery_service_request_is_passive_numeric() {
        // xs:boolean also admits the literal "1"
        let req =
            parse_discovery_service_request("entityID=https%3A%2F%2Fsp.example.com&isPassive=1")
                .unwrap();
        assert!(req.is_passive);

        let req =
            parse_discovery_service_request("entityID=https%3A%2F%2Fsp.example.com&isPassive=0")
                .unwrap();
        assert!(!req.is_passive);
    }

    #[test]
    fn test_parse_discovery_service_request_missing_entity_id() {
        let result = parse_discovery_service_request("return=https%3A%2F%2Fsp.example.com");
        assert!(matches!(
            result,
            Err(ProfileError::DiscoveryMissingEntityId)
        ));
    }

    #[test]
    fn test_parse_discovery_response_endpoints() {
        let extensions = r#"<idpdisc:DiscoveryResponse xmlns:idpdisc="urn:oasis:names:tc:SAML:profiles:SSO:idp-discovery-protocol" Binding="urn:oasis:names:tc:SAML:profiles:SSO:idp-discovery-protocol" index="0" isDefault="true" Location="https://sp.example.com/disco"/>"#;
        let endpoints = parse_discovery_response_endpoints(extensions).unwrap();
        assert_eq!(endpoints.len(), 1);
        assert_eq!(endpoints[0].location, "https://sp.example.com/disco");
        assert!(endpoints[0].is_default);
    }

    #[test]
    fn test_parse_discovery_response_endpoints_undeclared_prefix() {
        // Prefix declared on an ancestor in the original document — the
        // parser pre-declares the conventional idpdisc prefix.
        let extensions = r#"<idpdisc:DiscoveryResponse index="2" Location="https://sp.example.com/d2"/><mdui:UIInfo/>"#;
        let endpoints = parse_discovery_response_endpoints(extensions).unwrap();
        assert_eq!(endpoints.len(), 1);
        assert_eq!(endpoints[0].index, 2);
    }

    #[test]
    fn test_parse_discovery_response_endpoints_empty() {
        assert!(parse_discovery_response_endpoints("").unwrap().is_empty());
    }

    #[test]
    fn test_parse_discovery_response_endpoints_invalid_index_skipped() {
        // Endpoints with a missing or unparseable index must not be silently
        // treated as index 0 (the default-endpoint slot).
        let extensions = concat!(
            r#"<idpdisc:DiscoveryResponse Location="https://sp.example.com/no-index"/>"#,
            r#"<idpdisc:DiscoveryResponse index="bogus" Location="https://sp.example.com/bad-index"/>"#,
            r#"<idpdisc:DiscoveryResponse index="1" Location="https://sp.example.com/disco"/>"#,
        );
        let endpoints = parse_discovery_response_endpoints(extensions).unwrap();
        assert_eq!(endpoints.len(), 1);
        assert_eq!(endpoints[0].location, "https://sp.example.com/disco");
        assert_eq!(endpoints[0].index, 1);
    }

    #[test]
    fn test_verify_return_url() {
        let registered = registered_endpoints();
        // Exact match
        assert!(verify_return_url(
            "https://sp.example.com/disco",
            &registered
        ));
        // Query string extension is allowed
        assert!(verify_return_url(
            "https://sp.example.com/disco?sid=42",
            &registered
        ));
        // Different path is rejected
        assert!(!verify_return_url(
            "https://sp.example.com/other",
            &registered
        ));
        // Different host is rejected
        assert!(!verify_return_url(
            "https://evil.example.com/disco",
            &registered
        ));
    }

    #[test]
    fn test_verify_return_url_preserves_registered_query() {
        let registered = vec![DiscoveryResponseEndpoint {
            location: "https://sp.example.com/disco?sid=42".to_string(),
            index: 0,
            is_default: true,
        }];

        assert!(verify_return_url(
            "https://sp.example.com/disco?sid=42&entityID=https%3A%2F%2Fidp.example.com",
            &registered,
        ));
        assert!(!verify_return_url(
            "https://sp.example.com/disco?sid=99&entityID=https%3A%2F%2Fidp.example.com",
            &registered,
        ));
    }

    #[test]
    fn test_create_discovery_service_response_selected() {
        let req = DiscoveryServiceRequest {
            entity_id: "https://sp.example.com".to_string(),
            return_url: Some("https://sp.example.com/disco?sid=42".to_string()),
            return_id_param: "entityID".to_string(),
            policy: None,
            is_passive: false,
        };
        let url = create_discovery_service_response(
            &req,
            &registered_endpoints(),
            Some("https://idp.example.com"),
        )
        .unwrap();
        assert!(url.starts_with("https://sp.example.com/disco?sid=42&entityID="));
        assert!(url.contains("https%3A%2F%2Fidp.example.com"));
    }

    #[test]
    fn test_create_discovery_service_response_passive_none() {
        let req = DiscoveryServiceRequest {
            entity_id: "https://sp.example.com".to_string(),
            return_url: Some("https://sp.example.com/disco".to_string()),
            return_id_param: "entityID".to_string(),
            policy: None,
            is_passive: true,
        };
        let url = create_discovery_service_response(&req, &registered_endpoints(), None).unwrap();
        assert_eq!(url, "https://sp.example.com/disco");
    }

    #[test]
    fn test_create_discovery_service_response_unregistered_return() {
        let req = DiscoveryServiceRequest {
            entity_id: "https://sp.example.com".to_string(),
            return_url: Some("https://evil.example.com/phish".to_string()),
            return_id_param: "entityID".to_string(),
            policy: None,
            is_passive: false,
        };
        let result = create_discovery_service_response(
            &req,
            &registered_endpoints(),
            Some("https://idp.example.com"),
        );
        assert!(matches!(
            result,
            Err(ProfileError::DiscoveryReturnUrlNotRegistered(_))
        ));
    }

    #[test]
    fn test_create_discovery_service_response_default_endpoint() {
        let req = DiscoveryServiceRequest {
            entity_id: "https://sp.example.com".to_string(),
            return_url: None,
            return_id_param: "entityID".to_string(),
            policy: None,
            is_passive: false,
        };
        let url = create_discovery_service_response(
            &req,
            &registered_endpoints(),
            Some("https://idp.example.com"),
        )
        .unwrap();
        assert!(url.starts_with("https://sp.example.com/disco-default?entityID="));
    }

    #[test]
    fn test_create_discovery_service_response_no_return_available() {
        let req = DiscoveryServiceRequest {
            entity_id: "https://sp.example.com".to_string(),
            return_url: None,
            return_id_param: "entityID".to_string(),
            policy: None,
            is_passive: false,
        };
        let result = create_discovery_service_response(&req, &[], Some("https://idp.example.com"));
        assert!(matches!(result, Err(ProfileError::DiscoveryNoReturnUrl)));
    }

    #[test]
    fn test_unsupported_policy_detected() {
        let req = DiscoveryServiceRequest {
            entity_id: "https://sp.example.com".to_string(),
            return_url: None,
            return_id_param: "entityID".to_string(),
            policy: Some("urn:example:custom-policy".to_string()),
            is_passive: false,
        };
        assert!(!req.is_supported_policy());
    }
}