Skip to content

Commit

Permalink
chore: add security functions
Browse files Browse the repository at this point in the history
  • Loading branch information
mnavarrocarter committed Sep 22, 2023
1 parent 97899cf commit 93ac7f9
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions security/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,41 @@
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Castor\Security;

use Castor\Context;
use Castor\MissingContextValue;

/** @internal */
enum CtxKey
{
/** @internal */
case Principal;
}

function withPrincipal(Context $ctx, Principal $principal): Context
{
return Context\withValue($ctx, CtxKey::Principal, $principal);
}

function getPrincipal(Context $ctx): Principal
{
return $ctx->value(CtxKey::Principal) ??
throw MissingContextValue::forKey(CtxKey::Principal);
}

function lookupPrincipal(Context $ctx): ?Principal
{
return $ctx->value(CtxKey::Principal);
}

function getIdentity(Context $ctx): Identity
{
return getPrincipal($ctx)->getIdentity();
}

function lookupIdentity(Context $ctx): ?Identity
{
return lookupPrincipal($ctx)?->getIdentity();
}

0 comments on commit 93ac7f9

Please sign in to comment.