Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-auth

Source of release 0.6.1

0.6.1 / tests / password_storage.lucb

from luce_auth import auth
from luce_prism import prism
from luce_auth import password

func inspect(path: str, expected: password.Profile) -> !:
    let store = try prism.Store.open(path, token = "profile-fixture")
    defer store.release()
    let snapshot = try store.value().snapshot()
    defer snapshot.release()
    let record = try snapshot.value().get("/users/alice", "password_record")
    defer record.release()
    assert(try password.profile_of(record.value().bytes(), true) == expected)

func create(path: str, test_only: bool) -> !:
    let authority = try auth.Authority.open(path, "profile-fixture", test_only = test_only)
    defer authority.release()
    try authority.value().bootstrap("alice", "profile fixture password")

func login(path: str, test_only: bool, expected: bool) -> !:
    let authority = try auth.Authority.open(path, "profile-fixture", test_only = test_only)
    defer authority.release()
    let token = authority.value().login("alice", "profile fixture password") catch failure:
        assert(not expected and failure.code == auth.invalid)
        return
    defer token.release()
    assert(expected)
    let who = try authority.value().verify(try token.value().text_at())
    defer who.release()
    assert(try who.value().text_at() == "alice")

func corrupt(path: str) -> !:
    let store = try prism.Store.open(path, token = "profile-fixture")
    defer store.release()
    let tx = try store.value().begin()
    defer tx.release()
    var wire: u8[64]
    let record = try prism.Value.array(prism.DType.uint8, [64], wire)
    defer record.release()
    try tx.value().set("/users/alice", "password_record", record)
    _ = try tx.value().commit()
    _ = try store.value().bake()

func session_policy(path: str) -> !:
    let permissive = try auth.Authority.open(path, "profile-fixture", test_only = true)
    defer permissive.release()
    let token = try permissive.value().login("alice", "profile fixture password")
    defer token.release()
    permissive.value().close()
    let strict = try auth.Authority.open(path, "profile-fixture")
    defer strict.release()
    var denied = false
    let who = strict.value().verify(try token.value().text_at()) catch failure:
        assert(failure.code == auth.invalid)
        denied = true
        recover try prism.Value.text("rejected")
    defer who.release()
    assert(denied)

pub func main(arguments: str[]) -> i32!:
    assert(arguments.length == 3)
    try create(arguments[1], false)
    try inspect(arguments[1], password.Profile.interactive)
    try login(arguments[1], false, true)
    try create(arguments[2], true)
    try inspect(arguments[2], password.Profile.test_only)
    try login(arguments[2], false, false)
    try login(arguments[2], true, true)
    try session_policy(arguments[2])
    try corrupt(arguments[2])
    try login(arguments[2], true, false)
    print("PASS persisted password profiles, production default, test opt-in and corrupt-record rejection")
    return 0