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
//! A test server for running as a SAML IdP
//!
//! Example configuration file:
//!
//! ```json
//! {
//! "bind_address" : "0.0.0.0",
//! "hostname" : "example.com",
//! "tls_cert_path" : "~/certs/fullchain.pem",
//! "tls_key_path" : "~/certs/privkey.pem",
//! }
//!

#![forbid(unsafe_code)]
#![deny(missing_debug_implementations)]

use saml_rs::assertion::AssertionAttribute;
use saml_rs::metadata::{generate_metadata_xml, SamlMetadata};
use saml_rs::response::{AuthNStatement, ResponseElements};
use saml_rs::sp::SamlBinding;
use saml_rs::SamlQuery;

use chrono::{DateTime, Duration, NaiveDate, Utc};

use tide::log;
use tide::utils::After;
use tide::{Request, Response};
use tide_openssl::TlsListener;

use std::fs::File;
use std::io::ErrorKind;
use std::str::{from_utf8, FromStr};

use http_types::Mime;

pub mod util;

use util::*;
// use util::do_nothing;

#[async_std::main]
/// Spins up a test server
///
/// This uses HTTPS if you specify `TIDE_CERT_PATH` and `TIDE_KEY_PATH` environment variables.
async fn main() -> tide::Result<()> {
    let config_path: String = shellexpand::tilde("~/.config/saml_test_server").into_owned();

    let server_config = util::ServerConfig::from_filename_and_env(config_path);

    let app_state: AppState = server_config.clone().into();

    let mut app = tide::with_state(app_state);

    tide::log::with_level(tide::log::LevelFilter::Debug);

    // driftwood adds simple Apache-Style logs
    app.with(driftwood::ApacheCombinedLogger);

    app.with(After(|mut res: Response| async {
        if let Some(err) = res.downcast_error::<async_std::io::Error>() {
            log::debug!("asdfadsfadsf {:?}", err);
            let msg = match err.kind() {
                ErrorKind::NotFound => {
                    format!("Error, Not Found: {:?}", err)
                }
                _ => "Unknown Error".to_string(),
            };
            res.set_body(msg);
        }

        Ok(res)
    }));

    let mut saml_process = app.at("/SAML");
    // TODO: implement support for SAML Artifact
    // saml_process.at("/Artifact").get(do_nothing);
    saml_process.at("/Metadata").get(saml_metadata_get);
    // saml_process.at("/sign").get(test_sign);
    // TODO: implement SAML Logout
    // saml_process.at("/Logout").get(do_nothing);
    // TODO: implement SAML idp, used the entityID
    // saml_process.at("/idp").post(do_nothing);
    // TODO: implement SAML POST endpoint
    // saml_process.at("/POST").post(saml_post_binding);

    saml_process.at("/Redirect").get(saml_redirect_get);

    let _app = {
        let tls_cert: String =
            shellexpand::tilde(&server_config.tls_cert_path.as_str()).into_owned();
        let tls_key: String = shellexpand::tilde(&server_config.tls_key_path.as_str()).into_owned();
        match File::open(&tls_cert) {
            Ok(_) => log::info!("Successfully loaded cert from {:?}", tls_cert),
            Err(error) => {
                log::error!(
                    "Failed to load cert from {:?}, bailing: {:?}",
                    tls_cert,
                    error
                );
                std::process::exit(1);
            }
        }
        match File::open(&tls_key) {
            Ok(_) => log::info!("Successfully loaded key from {:?}", tls_key),
            Err(error) => {
                log::error!(
                    "Failed to load key from {:?}, bailing: {:?}",
                    tls_key,
                    error
                );
                std::process::exit(1);
            }
        }
        log::info!("Starting up server");
        log::debug!("Server config: {:?}", server_config);
        app.listen(
            TlsListener::build()
                .addrs(format!("{}:443", &server_config.bind_address))
                .cert(tls_cert)
                .key(tls_key),
        )
        .await?
    };
    Ok(())
}

// use saml_rs::cert::strip_cert_headers;
/// Provides a GET response for the metadata URL
async fn saml_metadata_get(req: Request<AppState>) -> tide::Result {
    let cert_path = &req.state().saml_cert_path;
    let certificate = saml_rs::sign::load_public_cert_from_filename(cert_path)
    .unwrap();

    let entity_id = String::from(&req.state().hostname);

    Ok(generate_metadata_xml(SamlMetadata::new(
        &req.state().hostname,
        None,
        Some(entity_id),
        None,
        None,
        None,
        Some(certificate),
    ))
    .into())
}

/// Handles a POST binding
/// ```html
/// <form method="post" action="https://idp.example.org/SAML2/SSO/POST" ...>
///     <input type="hidden" name="SAMLRequest" value="''request''" />
///     ... other input parameter....
/// </form>
/// ```
pub async fn saml_post_binding(req: tide::Request<AppState>) -> tide::Result {
    Ok(tide::Response::builder(203)
        .body(format!("SAMLRequest: {:?}", req))
        .content_type(Mime::from_str("text/html;charset=utf-8").unwrap())
        // .header("custom-header", "value")
        .build())
}

/// SAML requests or responses transmitted via HTTP Redirect have a SAMLRequest or SAMLResponse query string parameter, respectively. Before it's sent, the message is deflated (without header and checksum), base64-encoded, and URL-encoded, in that order. Upon receipt, the process is reversed to recover the original message.
pub async fn saml_redirect_get(req: tide::Request<AppState>) -> tide::Result {
    let query: SamlQuery = match req.query() {
        Ok(val) => val,
        Err(e) => {
            log::error!("Missing SAMLRequest request in saml_redirect_get {:?}", e);
            return Err(tide::Error::from_str(
                tide::StatusCode::BadRequest,
                "Missing SAMLRequest",
            ));
        }
    };

    let mut response_body = String::from(
        r#"<!DOCTYPE html>
        <html lang="en"><head><title>saml_redirect_get</title></head><body>"#,
    );
    // I'm not sure why this is here but I better not lose it
    // https://www.w3.org/TR/2002/REC-xmldsig-core-20020212/xmldsig-core-schema.xsd#rsa-sha1

    match query.Signature {
        Some(ref value) => {
            log::debug!("Found a signature! {:?}", value);
            let sigalg = match query.SigAlg {
                Some(ref value) => String::from(value),
                None => {
                    // we should probably bail on this request if we got a signature without an algorithm...
                    // or maybe there's a way to specify it in the SP metadata???

                    return Err(tide::Error::from_str(
                        tide::StatusCode::BadRequest,
                        "Found signature without a sigalg in a redirect authn request.",
                    ));
                }
            };
            log::debug!("SigAlg found: {:?}", &sigalg);
        }
        _ => {
            log::debug!("Didn't find a signature in this request.");
        }
    }

    let samlrequest = query.SAMLRequest.unwrap();

    let base64_decoded_samlrequest =
        match saml_rs::decode_authn_request_base64_encoded(samlrequest.to_string()) {
            Ok(val) => val,
            Err(error) => {
                return Err(tide::Error::from_str(
                    tide::StatusCode::BadRequest,
                    error.message,
                ));
            }
        };
    log::debug!(
        "about to parse authn request: {:?}",
        base64_decoded_samlrequest
    );

    let parsed_saml_request: saml_rs::AuthnRequest =
        match saml_rs::parse_authn_request(&base64_decoded_samlrequest) {
            Ok(val) => val,
            Err(err) => {
                eprintln!("{:?}", err);
                return Err(tide::Error::from_str(tide::StatusCode::BadRequest, err));
            }
        };

    let service_provider = match req
        .state()
        .service_providers
        .contains_key(&parsed_saml_request.issuer)
    {
        true => {
            let value = req
                .state()
                .service_providers
                .get(&parsed_saml_request.issuer);
            value
        }
        false => {
            return Err(tide::Error::from_str(
                tide::StatusCode::BadRequest,
                "Unable to find SP for request.".to_string(),
            ))
        }
    };

    let mut _form_target = String::new();

    response_body.push_str("<p style='color: darkgreen'>found SP in state!</p>");
    response_body.push_str(&format!("<p>{:?}</p>", service_provider));
    // find the consumer
    for service in &service_provider.unwrap().services {
        match service.servicetype {
            saml_rs::sp::SamlBindingType::AssertionConsumerService => {
                log::debug!("acs: {:?}", service);
                match &service.binding {
                    SamlBinding::HttpRedirect | SamlBinding::HttpPost => {
                        log::debug!(
                            "Found form target, type is {:?}, destination is: {}",
                            &service.binding,
                            service.location
                        );
                        _form_target = service.location.to_string();
                    } // _ => {
                      //     log::debug!("not it!");
                      // }
                }
            }
            saml_rs::sp::SamlBindingType::SingleLogoutService => {
                log::debug!("sso");
            }
        }
    }

    response_body.push_str("<h2>Known issuers</h2><ul>");
    for issuer in req.state().service_providers.keys() {
        response_body.push_str(&format!("<li>{}</li>", issuer));
    }
    response_body.push_str("</ul>");

    response_body.push_str(&format!(
        "<p>relay_state: {:?}</p>",
        parsed_saml_request.relay_state
    ));
    response_body.push_str(&format!(
        "<p>issue_instant: {:?}</p>",
        parsed_saml_request.issue_instant
    ));
    response_body.push_str(&format!(
        "consumer_service_url: {:?}<br />",
        parsed_saml_request.consumer_service_url
    ));
    let unset_value = String::from("unset");
    response_body.push_str(&format!("issuer: {:?}<br />", parsed_saml_request.issuer));

    if let Some(value) = query.Signature {
        response_body.push_str(&format!("<p>Original Signature field: <br />{}</p>", value))
    };
    if let Some(value) = query.SigAlg {
        response_body.push_str(&format!("<p>Original SigAlg field: <br />{}</p>", value))
    };

    // response_body.push_str(&format!("Referer: {:?}<br />", referer));
    response_body.push_str(&format!(
        "<p>Original SAMLRequest field: <br />{}</p>",
        samlrequest
    ));
    // #[allow(clippy::or_fun_call)]
    let relay_state = match query.RelayState {
        Some(value) => value,
        None => unset_value,
    };
    response_body.push_str(&format!("RelayState: {}<br />", relay_state));

    // start building the actual response

    let authn_instant =
        DateTime::<Utc>::from_utc(NaiveDate::from_ymd(2014, 7, 17).and_hms(1, 1, 48), Utc);
    // 2024-07-17T09:01:48Z
    // adding three years including skip years
    let session_expiry =
        match DateTime::<Utc>::from_utc(NaiveDate::from_ymd(2014, 7, 17).and_hms(9, 1, 48), Utc)
            .checked_add_signed(Duration::days(3653))
        {
            Some(value) => value,
            _ => Utc::now(),
        };

    // TODO: work out where the AuthNStatement goes
    let authnstatement = AuthNStatement {
        instant: authn_instant,
        session_index: String::from("_be9967abd904ddcae3c0eb4189adbe3f71e327cf93"),
        classref: String::from("urn:oasis:names:tc:SAML:2.0:ac:classes:Password"),
        expiry: Some(session_expiry),
    };

    let responseattributes = [
        AssertionAttribute::basic("uid", ["yaleman"].to_vec()),
        AssertionAttribute::basic("mail", ["yaleman@ricetek.net"].to_vec()),
        AssertionAttribute::basic(
            "eduPersonAffiliation",
            ["users", "examplerole1", "admin"].to_vec(),
        ),
    ]
    .to_vec();

    let signing_key = Some(req.state().saml_signing_key.to_owned());
    let response = ResponseElements {
        issuer: req.state().hostname.to_string(),
        response_id: ResponseElements::default().assertion_id,
        // issue_instant: DateTime::<Utc>::from_utc(
        //     NaiveDate::from_ymd(2014, 7, 17).and_hms(1, 1, 48),
        //     Utc,
        // ),
        issue_instant: Utc::now(),
        relay_state: parsed_saml_request.relay_state,
        attributes: responseattributes,
        // TODO: figure out if this should be the ACS found in _form_target above or the parsed_saml_request.consumer_service_url
        // destination: form_target,
        destination: parsed_saml_request.consumer_service_url.to_string(),
        authnstatement,
        assertion_id: ResponseElements::default().assertion_id,
        service_provider: service_provider.unwrap().to_owned(),
        assertion_consumer_service: Some(parsed_saml_request.consumer_service_url),

        session_length_seconds: 30,
        status: saml_rs::constants::StatusCode::Success,
        sign_assertion: true,
        sign_message: false,
        signing_key,
        signing_cert: Some(req.state().saml_signing_cert.to_owned()),
        // TODO: write a test case that actually signs the response
    };

    response_body.push_str(&generate_login_form(response, relay_state));
    // res.set_body(response_body);

    response_body.push_str("</html>");
    Ok(tide::Response::builder(203)
        .body(response_body)
        .content_type(Mime::from_str("text/html;charset=utf-8").unwrap())
        // .header("custom-header", "value")
        .build())
}

/// Generate a fake login form for the user to interact with
///
/// TODO: These responses have to be signed
pub fn generate_login_form(response: ResponseElements, relay_state: String) -> String {
    let mut context = tera::Context::new();

    context.insert("form_action", &response.destination);
    let response_data = response.base64_encoded_response();
    let saml_response = from_utf8(&response_data).unwrap().to_string();
    context.insert("SAMLResponse", &saml_response);
    context.insert("RelayState", &relay_state);

    let template = String::from(
        r#"<p>{{SAMLResponse | safe}}</p>
<form method="post" action="{{form_action | safe}}">
    <input type="hidden" name="SAMLResponse" value="{{SAMLResponse | safe}}" />
    <input type="hidden" name="RelayState" value="{{RelayState}}" />
    <h1>Fancy example login form</h1>
    <p>Username: <input type='text' name='username' /></p>
    <p>Password: <input type='password' name='password' /></p>
    <p><input type="submit" value="Submit" /></p>
</form>"#,
    );

    tera::Tera::one_off(&template, &context, true)
        .unwrap_or_else(|_| String::from("Couldn't generate login form"))
}

// TODO: reimplement test_sign
// pub async fn test_sign(req: Request<AppState>) -> tide::Result {
//     saml_rs::sign::sign_data(
//         req.state().tls_cert_path.to_string(),
//         req.state().tls_key_path.to_string(),
//         "hello world".as_bytes(),
//     );
//     Ok(tide::Response::builder(200)
//         .body("Signing things")
//         .content_type(Mime::from_str("text/html;charset=utf-8").unwrap())
//         // .header("custom-header", "value")
//         .build())
// }