pinax-network / substreams-sink-clickhouse

Substreams ClickHouse Sink
MIT License
3 stars 0 forks source link

Argon2 authentication `Bun.password` #51

Closed DenisCarriere closed 10 months ago

DenisCarriere commented 10 months ago

Replacement for AuthKey logic using Bun.password

// https://bun.sh/guides/util/hash-a-password
import { config } from "../config.js";
import { InvalidRequest, NoAuthorization, Unauthorized, getBearer } from "./bearerAuth.js";

export function beforeHandle(request: Request) {
    if ( !config.AUTH_KEY ) return;
    const bearer = getBearer(request);
    if (!bearer) return NoAuthorization;

    try {
        const verify = Bun.password.verifySync(config.AUTH_KEY, bearer);
        if ( !verify ) return Unauthorized;
    } catch (e) {
        return InvalidRequest;
    }
}

Reference

Argon2 is one of the safest cryptographic algorithm for password encryption https://www.boldare.com/blog/how-to-improve-user-password-security-with-argon2/

StreamingFast (previously EOS Canada) had used this for their command line wallet https://github.com/eoscanada/eosc

JulienR1 commented 10 months ago

Implemented in #58