From 3ccf780e00b9598a1893d27aeb0eb36fa4fafbaf Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Mon, 10 Apr 2023 22:13:49 -0700 Subject: [PATCH] AuthxCredential.create - Multiple changes * Fix type of `$ttl` * Make `$ttl` default visible * Don't expose `scope` until there's clearer goal * Return a credential instead of bare token --- .../Api4/Action/AuthxCredential/Create.php | 26 +++++-------------- 1 file changed, 7 insertions(+), 19 deletions(-) diff --git a/ext/authx/Civi/Api4/Action/AuthxCredential/Create.php b/ext/authx/Civi/Api4/Action/AuthxCredential/Create.php index 1d6c312f2e..d8fe37ce19 100644 --- a/ext/authx/Civi/Api4/Action/AuthxCredential/Create.php +++ b/ext/authx/Civi/Api4/Action/AuthxCredential/Create.php @@ -19,12 +19,11 @@ use Civi\Api4\Generic\Result; * * @method int getContactId() Get contact ID param (required) * @method $this setContactId(int $contactId) Set the Contact Id - * @method $this setTtl(string $ttl) Set TTL param - * @method string getTtl() get the TTL param; - * @method $this setScope(string $scope) Set the JWT Scope - * @method string getScope() get the JWT scopes + * @method $this setTtl(int $ttl) Set TTL param + * @method int getTtl() get the TTL param; */ class Create extends \Civi\Api4\Generic\AbstractAction { + /** * ID of contact * @@ -38,31 +37,20 @@ class Create extends \Civi\Api4\Generic\AbstractAction { * * @var int */ - protected $ttl = NULL; - - - /** - * Scopes for the JWT - * - * @var string - */ - protected $scope = NULL; + protected $ttl = 300; /** * @param \Civi\Api4\Generic\Result $result */ public function _run(Result $result) { - $ttl = $this->ttl ?: 300; - $scope = $this->scope ?: 'authx'; - $token = \Civi::service('crypto.jwt')->encode([ - 'exp' => time() + $ttl, + 'exp' => time() + $this->ttl, 'sub' => 'cid:' . $this->contactId, - 'scope' => $scope, + 'scope' => 'authx', ]); $result[] = [ - 'token' => $token, + 'cred' => 'Bearer ' . $token, ]; } -- 2.25.1