fn(request$0, then$1) =>
let header$2 = match @http.field-get(fields: $0.headers, name: "authorization") {
#some(value$3) =>
@definy.string-trim(value: $3),
#none(_) =>
""
};
match @core.equal(left: $2, right: "") {
true =>
$1(authorization: #anonymous<@definy.rpc-authorization>),
false =>
match @definy.parse-rpc-authorization(header: $2) {
#none(_) =>
$1(
authorization: #invalid<@definy.rpc-authorization>("Authorization must be 'Definy-Ed25519 <account>.<time>.<sig>'")
),
#some(credential$4) =>
#read_time<@definy.service-io>({
then: fn(now_ms$5) =>
match @core.less-than(
left: 300,
right: @std.abs(x: @core.minus(left: @core.divide(left: $5, right: 1000), right: $4.time_seconds))
) {
true =>
$1(
authorization: #invalid<@definy.rpc-authorization>("Authorization time is out of the allowed range")
),
false =>
match @std.base64-to-bytes(text: $4.account_id) {
#none(_) =>
$1(authorization: #invalid<@definy.rpc-authorization>("Authorization account must be Base64")),
#some(public_key$6) =>
match @std.base64-to-bytes(text: $4.signature) {
#none(_) =>
$1(authorization: #invalid<@definy.rpc-authorization>("Authorization signature must be Base64")),
#some(signature$7) =>
let body$8 = @std.bytes-to-string(bytes: $0.body);
#verify_ed25519<@definy.service-io>({
public_key: $6,
signature: $7,
message: @definy.rpc-authorization-message(
method: @http.method-text(method: $0.method),
path: @http.path(request: $0),
time_seconds: $4.time_seconds,
body: $8
),
then: fn(valid$9) =>
match $9 {
true =>
$1(authorization: #account<@definy.rpc-authorization>($4.account_id)),
false =>
$1(authorization: #invalid<@definy.rpc-authorization>("Authorization signature verification failed"))
}
})
}
}
}
})
}
}