Explorer

definy.read-rpc-authorization

description
Verify the Authorization header of the request (docs/rpc-authorization.md): anonymous if there is none, invalid (the reason) if the form, the time (more than 300 seconds off) or the Ed25519 signature is wrong, account (the account ID) if it is right
type
Fn(request: @http.request, then: Fn(authorization: @definy.rpc-authorization) -> @definy.service-io) -> @definy.service-io
expression
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"))
                              }
                          })
                      }
                  }
              }
          })
      }
  }
visibility
public
definy.read-rpc-authorization (project definy, branch main, snapshot l0VFjJZP174d8CbZWFoujAGTYjgsQu4uTq5uWCZaXgo)
id: rhL-VFnlZtXn9czXTSXlN8yntNp-_FCj3C1JttOLh9g
content hash: FqJue6vNguYl3Faf-nzEEjKzJJ-3tKZ_RMzRGeSOX9I