Functional Weave
Code in Rust

auth.password-policy@1.0.1

impl/rust/password_policy.rs

1,488 bytes · the Rust implementation · view raw

Imports name this capability’s declared dependencies, which fune builds next to it in your project; each one links to its page.

use super::funejson::Value;  ← the fune runtime: the JSON value the test vectors use; fune build keeps it only where a signature takes one
use super::auth_password_policy_data::POLICIES;  ← this capability’s own data, compiled from data/policies.json into the same file by fune build

/// A named policy from the data, so a browser and an API that both ask for
/// "nist-800-63b-4-single-factor" enforce exactly the same numbers.
///
/// # Panics
/// Panics on a name the data does not have.
pub fn password_policy(name: &str) -> PasswordPolicy {
    for row in POLICIES {
        if row.name == name {
            return PasswordPolicy {
                name: row.name.to_string(),
                min_length: row.min_length,
                max_length: row.max_length,
                min_character_classes: row.min_character_classes,
                block_common: row.block_common,
                block_personal: row.block_personal,
            };
        }
    }
    let known: Vec<&str> = POLICIES.iter().map(|row| row.name).collect();
    panic!("unknown password policy \"{}\"; known policies: {}", name, known.join(", "));
}

pub fn password_policy_to_value(policy: &PasswordPolicy) -> Value {
    Value::obj(vec![
        ("name", Value::str(&policy.name)),
        ("minLength", Value::Int(policy.min_length)),
        ("maxLength", Value::Int(policy.max_length)),
        ("minCharacterClasses", Value::Int(policy.min_character_classes)),
        ("blockCommon", Value::Bool(policy.block_common)),
        ("blockPersonal", Value::Bool(policy.block_personal)),
    ])
}

pub fn fune_vector(args: &[Value]) -> Value {
    password_policy_to_value(&password_policy(args[0].as_str()))
}