auth::token::redeem (public)

 auth::token::redeem -purpose purpose -token token -callback callback

Defined in packages/acs-authentication/tcl/token-procs.tcl

Redeem a token for the expected purpose. The callback is a trusted command prefix. It receives the user ID and stored context as its final arguments and runs in the token transaction. It must enforce the operation's authorization rules. A normal return consumes the token. An error rolls back the operation and leaves the token available to retry until expiry. External side effects belong after commit.

Switches:
-purpose (required)
-token (required)
-callback (required)
Returns:
A dictionary with status invalid, or status redeemed and the callback result.

Partial Call Graph (max 5 caller/called nodes):
auth::token::lock_account auth::token::lock_account (private) db_0or1row db_0or1row (public) db_dml db_dml (public) db_string db_string (public) db_transaction db_transaction (public) auth::token::redeem auth::token::redeem auth::token::redeem->auth::token::lock_account auth::token::redeem->db_0or1row auth::token::redeem->db_dml auth::token::redeem->db_string auth::token::redeem->db_transaction

Testcases:
No testcase defined.
Source code:
    if {![llength $callback]} {
        error "Action token redemption requires a callback"
    }
    set invalid {status invalid}
    if {![regexp {^[0-9a-f]{64}$} $token]} {
        return $invalid
    }
    set digest [ns_crypto::md string -digest sha256 -encoding hex $token]
    set user_id [db_string find_token_account {
        select user_id from auth_action_tokens where token_digest = :digest and purpose = :purpose
    } -default ""]
    if {$user_id eq ""} {
        return $invalid
    }

    set result $invalid
    db_transaction {
        if {[auth::token::lock_account $user_id]} {
            set now [clock seconds]
            if {[db_0or1row find_redeemable_token {
                select token_context from auth_action_tokens
                 where user_id = :user_id and purpose = :purpose
                   and token_digest = :digest and expires_at > :now
            }]} {
                set value [{*}$callback $user_id $token_context]
                db_dml consume_token {
                    delete from auth_action_tokens
                     where user_id = :user_id and purpose = :purpose and token_digest = :digest
                }
                set result [dict create status redeemed result $value]
            }
        }
    }
    return $result
XQL Not present:
Generic, PostgreSQL, Oracle
[ hide source ] | [ make this the default ]
Show another procedure: