Class ::nsmcp::BearerAuthenticator (public)

 ::nx::Class ::nsmcp::BearerAuthenticator[i]

Defined in /usr/local/ns/tcl/nsmcp/security.tcl

Only the table name enters the blueprint. Credential records live in nsv.

Testcases:
No testcase defined.
Source code:
    :property store:required

    :public method issue {principal permissions targets expires} {
        #
        # Create and grant a new bearer credential.
        #
        # Generate a high-entropy token, store only its digest and grants, and
        # return the secret to the administrator once.
        #
        # @param principal Authenticated principal identifier.
        # @param permissions Explicit permission names granted to the credential.
        # @param targets Server names which the credential may target.
        # @param expires Expiry timestamp in epoch seconds, or zero for no expiry.
        #
        # @return A dictionary containing token, digest and expires.
        #

        # Explicit administrative operation; never called automatically at init.
        # Return the secret once, without logging it or storing it in nsv.
        set token [ns_crypto::randombytes -encoding hex 32]
        set digest [:grant $token $principal $permissions $targets $expires]
        return [dict create token $token digest $digest expires $expires]
    }

    :public method grant {token principal permissions targets expires} {
        #
        # Grant a supplied bearer token to a principal and target set.
        #
        # Validate token length and expiry, then store the digest-indexed grant
        # without retaining the token itself.
        #
        # @param token High-entropy bearer token; only its digest is retained.
        # @param principal Authenticated principal identifier.
        # @param permissions Explicit permission names granted to the credential.
        # @param targets Server names which the credential may target.
        # @param expires Expiry timestamp in epoch seconds, or zero for no expiry.
        #
        # @return The hexadecimal token digest.
        #

        if {[string length $token] < 32 || $principal eq "" ||
            ![string is wideinteger -strict $expires] || ($expires != 0 && $expires <= [clock seconds])} {
            error "expected a high-entropy token, named principal and expiry of zero or a future timestamp"
        }
        set digest [ns_crypto::md string -digest sha256 -- $token]
        nsv_set ${:store} $digest [dict create principal $principal  permissions $permissions targets $targets expires $expires]
        return $digest
    }

    :public method revoke {digest} {
        #
        # Revoke a digest-indexed credential.
        #
        # Remove the retained grant if present; do not require the original bearer
        # token.
        #
        # @param digest Hexadecimal SHA-256 credential digest.
        #
        # @return The shared-store deletion result.
        #

        nsv_unset -nocomplain ${:store} $digest
    }

    :public method credentialInfo {digest} {
        #
        # Read retained metadata for a credential digest.
        #
        # Return only the digest-indexed grant record, never a bearer token.
        #
        # @param digest Hexadecimal SHA-256 credential digest.
        #
        # @return The credential record, or an empty result when it is absent.
        #

        if {![nsv_get ${:store} $digest record]} {
            return {}
        }
        return $record
    }

    :public method authenticate {authorization server} {
        #
        # Authenticate a bearer header for the selected server.
        #
        # Validate token syntax, resolve its digest and check expiry before creating
        # a request-owned context.
        #
        # @param authorization Authorization header supplied by the caller.
        # @param server Selected NaviServer virtual-server name.
        #
        # @return A RequestContext object, or an HTTP 401 error on rejection.
        #

        if {![regexp {^Bearer ([A-Za-z0-9._~+/-]+=*)$} $authorization -> token]} {
            return [next]
        }
        set digest [ns_crypto::md string -digest sha256 -- $token]
        if {![nsv_get ${:store} $digest record] ||
            ([dict get $record expires] != 0 && [dict get $record expires] <= [clock seconds])} {
            return [next]
        }
        return [::nsmcp::RequestContext new  -principal [dict get $record principal]  -permissions [dict get $record permissions]  -targets [dict get $record targets] -server $server]
    }
XQL Not present:
Generic, PostgreSQL, Oracle
[ hide source ] | [ make this the default ]
Show another procedure: