ciallo-security

    Pure MoonBit CORS, CSRF, Helmet, JWT (HS256), PASETO v4.local, Argon2id/PBKDF2 password hashing, and RBAC/ABAC guards for unmbt/ciallo

    security
    jwt
    paseto
    argon2
    cors
    csrf
    rbac
    ciallo
    Download zip
    Author
    Version
    0.1.1
    License
    MIT
    Last updated
    13 hours ago
    Downloads
    8

    #unmbt/ciallo-security

    Pure MoonBit security suite for unmbt/ciallo: CORS, Helmet headers, double-submit CSRF, JWT (HS256), PASETO v4.local, Argon2id/PBKDF2 password hashing, and RBAC/ABAC guards.

    PasswordHasher

    pub(open) trait PasswordHasher {
    fn hash_password(Self, password : StringView, salt~ : BytesView) -> String raise SecurityError
    fn verify_password(Self, password : StringView, encoded : StringView) -> Bool raise SecurityError
    }

    SecurityError

    pub(all) suberror SecurityError {
    MalformedToken(String)
    InvalidSignature
    TokenExpired(exp~ : Int64, now~ : Int64)
    TokenNotYetValid(nbf~ : Int64, now~ : Int64)
    InvalidClaim(claim~ : String, message~ : String)
    InvalidPasswordHash(String)
    CryptoFailure(String)
    } derive(Eq,
    Debug
    )

    Argon2idHasher

    pub struct Argon2idHasher {
    memory_kib : Int
    iterations : Int
    tag_len : Int
    } derive(Eq,
    Debug
    )

    Pure MoonBit Argon2id (RFC 9106, p=1) memory-hard password hasher.

    Argon2idHasher::Argon2idHasher

    fn Argon2idHasher::Argon2idHasher(memory_kib? : Int, iterations? : Int, tag_len? : Int) -> Argon2idHasher

    Argon2idHasher::hash_password

    fn Argon2idHasher::hash_password(self : Argon2idHasher, password : StringView, salt~ : BytesView) -> String raise SecurityError

    Argon2idHasher::verify_password

    fn Argon2idHasher::verify_password(_self : Argon2idHasher, password : StringView, encoded : StringView) -> Bool raise SecurityError

    AuthPrincipal

    pub(all) struct AuthPrincipal {
    subject : String
    roles : Array[String]
    claims : JwtClaims
    } derive(Eq,
    Debug
    )

    CorsOptions

    pub(all) struct CorsOptions {
    allowed_origins : Array[String]
    allowed_methods : Array[String]
    allowed_headers : Array[String]
    exposed_headers : Array[String]
    allow_credentials : Bool
    max_age_secs : Int?
    } derive(Eq,
    Debug
    )

    CorsOptions::CorsOptions

    fn CorsOptions::CorsOptions(allowed_origins? : Array[String], allowed_methods? : Array[String], allowed_headers? : Array[String], exposed_headers? : Array[String], allow_credentials? : Bool, max_age_secs? : Int) -> CorsOptions

    CsrfBundle

    pub struct CsrfBundle {
    options : CsrfOptions
    }

    Double-submit cookie + header CSRF protection bundle.

    CsrfBundle::CsrfBundle

    fn CsrfBundle::CsrfBundle(options? : CsrfOptions) -> CsrfBundle

    CsrfOptions

    pub(all) struct CsrfOptions {
    cookie_name : String
    header_name : String
    allowed_origins : Array[String]
    } derive(Eq,
    Debug
    )

    CsrfOptions::CsrfOptions

    fn CsrfOptions::CsrfOptions(cookie_name? : String, header_name? : String, allowed_origins? : Array[String]) -> CsrfOptions

    HelmetBundle

    pub struct HelmetBundle {
    options : HelmetOptions
    }

    HelmetOptions

    pub(all) struct HelmetOptions {
    frame_options : String
    referrer_policy : String
    hsts : String?
    content_security_policy : String?
    coop : String
    permissions_policy : String?
    } derive(Eq,
    Debug
    )

    HelmetOptions::HelmetOptions

    fn HelmetOptions::HelmetOptions(frame_options? : String, referrer_policy? : String, hsts? : String, content_security_policy? : String, coop? : String, permissions_policy? : String) -> HelmetOptions

    JwtClaims

    pub(all) struct JwtClaims {
    sub : String?
    iss : String?
    aud : String?
    exp : Int64?
    nbf : Int64?
    iat : Int64?
    jti : String?
    roles : Array[String]
    extra : Map[String,
    Json
    ]
    } derive(Eq,
    Debug
    )

    JwtClaims::JwtClaims

    fn JwtClaims::JwtClaims(sub? : String, iss? : String, aud? : String, exp? : Int64, nbf? : Int64, iat? : Int64, jti? : String, roles? : Array[String], extra? : Map[String,
    Json
    ]) -> JwtClaims

    JwtClaims::from_json_value

    JwtClaims::to_json

    Pbkdf2Hasher

    pub struct Pbkdf2Hasher {
    iterations : Int
    dk_len : Int
    } derive(Eq,
    Debug
    )

    PBKDF2-HMAC-SHA256 PasswordHasher outputting PHC $pbkdf2-sha256$i=...$salt$hash.

    Pbkdf2Hasher::Pbkdf2Hasher

    fn Pbkdf2Hasher::Pbkdf2Hasher(iterations? : Int, dk_len? : Int) -> Pbkdf2Hasher

    Pbkdf2Hasher::hash_password

    fn Pbkdf2Hasher::hash_password(self : Pbkdf2Hasher, password : StringView, salt~ : BytesView) -> String raise SecurityError

    Pbkdf2Hasher::verify_password

    fn Pbkdf2Hasher::verify_password(_self : Pbkdf2Hasher, password : StringView, encoded : StringView) -> Bool raise SecurityError

    jwt_auth_guard

    fn jwt_auth_guard(secret~ : Bytes, clock? : &
    Clock
    , issuer? : String, audience? : String, leeway_secs? : Int64) -> (async (
    Context
    ) ->
    Response
    ?)

    Creates an EarlyHook that verifies Authorization: Bearer <jwt> and sets principal_key.

    mount_cors

    Mounts root-level CORS preflight (OPTIONS -> 204) and response headers onto app.

    paseto_v4_decrypt

    fn paseto_v4_decrypt(token : StringView, key~ : BytesView, implicit_assertion? : BytesView) -> (Bytes, Bytes) raise SecurityError

    PASETO v4.local decryption and tag verification.

    paseto_v4_encrypt

    fn paseto_v4_encrypt(payload : BytesView, key~ : BytesView, nonce~ : BytesView, footer? : BytesView, implicit_assertion? : BytesView) -> String raise SecurityError

    PASETO v4.local symmetric authenticated encryption (XChaCha20 + BLAKE2b-256). Requires a 32-byte key and a 32-byte random nonce.

    require_any_role

    fn require_any_role(allowed : Array[String]) -> (async (
    Context
    ) ->
    Response
    ?)

    RBAC Guard: requires at least one of the specified roles.

    require_policy

    fn require_policy(policy : (
    Context
    , AuthPrincipal) -> Bool, detail? : String) -> (async (
    Context
    ) ->
    Response
    ?)

    ABAC Guard: evaluates an arbitrary attribute-based policy over Context and AuthPrincipal.

    require_roles

    fn require_roles(required : Array[String]) -> (async (
    Context
    ) ->
    Response
    ?)

    RBAC Guard: requires the authenticated principal to hold ALL specified roles. Short-circuits unauthorized requests with RFC 9457 403 Forbidden.

    sign_jwt_hs256

    fn sign_jwt_hs256(claims : JwtClaims, secret~ : BytesView) -> String

    Signs a JWT using RFC 7519 HS256 (HMAC-SHA256).

    verify_jwt_hs256

    fn verify_jwt_hs256(token : StringView, secret~ : BytesView, now_secs? : Int64, issuer? : String, audience? : String, leeway_secs? : Int64) -> JwtClaims raise SecurityError

    Verifies an HS256 JWT signature and standard temporal/issuer/audience claims.