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
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements.  See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership.  The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License.  You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied.  See the License for the
// specific language governing permissions and limitations
// under the License.

use std::io::prelude::*;

use std::collections::HashSet;
use std::convert::TryFrom;
use std::io::BufReader;
use teaclave_types::{FunctionArguments, FunctionRuntime};

#[derive(Default)]
pub struct PasswordCheck;

#[derive(serde::Deserialize)]
struct PasswordCheckArguments;

impl TryFrom<FunctionArguments> for PasswordCheckArguments {
    type Error = anyhow::Error;

    fn try_from(arguments: FunctionArguments) -> Result<Self, Self::Error> {
        use anyhow::Context;
        serde_json::from_str(&arguments.into_string()).context("Cannot deserialize arguments")
    }
}

impl PasswordCheck {
    pub const NAME: &'static str = "builtin-password-check";

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

    pub fn run(&self, _: FunctionArguments, runtime: FunctionRuntime) -> anyhow::Result<String> {
        let password_file = runtime.open_input("password")?;
        let password = BufReader::new(password_file)
            .lines()
            .next()
            .unwrap()
            .unwrap()
            .trim()
            .to_owned();
        let exposed_passwords_file = runtime.open_input("exposed_passwords")?;
        let exposed_passwords: HashSet<String> = BufReader::new(exposed_passwords_file)
            .lines()
            .map(|l| l.expect("Could not parse line").trim().to_string())
            .collect::<Vec<String>>()
            .iter()
            .cloned()
            .collect();
        if exposed_passwords.contains(&password) {
            Ok("true".to_string())
        } else {
            Ok("false".to_string())
        }
    }
}

#[cfg(feature = "enclave_unit_test")]
pub mod tests {
    use super::*;
    use std::path::Path;
    use teaclave_crypto::*;
    use teaclave_runtime::*;
    use teaclave_test_utils::*;
    use teaclave_types::*;

    pub fn run_tests() -> bool {
        run_tests!(test_password_check)
    }

    fn test_password_check() {
        let password_input = Path::new("fixtures/functions/password_check/password.txt");
        let exposed_passwords_input =
            Path::new("fixtures/functions/password_check/exposed_passwords.txt");
        let arguments = FunctionArguments::default();

        let input_files = StagedFiles::new(hashmap!(
            "password" => StagedFileInfo::new(password_input, TeaclaveFile128Key::random(), FileAuthTag::mock()),
            "exposed_passwords" => StagedFileInfo::new(exposed_passwords_input, TeaclaveFile128Key::random(), FileAuthTag::mock()),
        ));
        let output_files = StagedFiles::new(hashmap!());
        let runtime = Box::new(RawIoRuntime::new(input_files, output_files));

        let result = PasswordCheck::new().run(arguments, runtime).unwrap();

        assert_eq!(result, "true");
    }
}