Skip to content

Conversation

@lonnieezell
Copy link
Member

Added the first of a handful of guides stepping through setup and use Access Tokens to handle various situations. This one covers generic API requests.

Along the way I discovered that the TokenAuth filter didn't support checking against a scope, so I updated the filter to handle a single permission added when used as a route filter.

$routes->get('users', 'UserController::list', ['filter' => 'tokens:users-read']);

@kenjis kenjis added enhancement New feature or request documentation Improvements or additions to documentation labels Sep 2, 2022
@jozefrebjak
Copy link
Contributor

Added the first of a handful of guides stepping through setup and use Access Tokens to handle various situations. This one covers generic API requests.

Along the way I discovered that the TokenAuth filter didn't support checking against a scope, so I updated the filter to handle a single permission added when used as a route filter.

$routes->get('users', 'UserController::list', ['filter' => 'tokens:users-read']);

@lonnieezell Thanks for this PR. It will be nice to have an CLI command to generate Token for user at start to test functionality before implementing the whole flow with user interface to interact with API access.

@MGatner
Copy link
Member

MGatner commented Sep 2, 2022

This looks great! It's big, and I am short on time so will have to come back to it. Ideally you pull the token changes out to its own PR to make this smaller for review 🫣

Co-authored-by: MGatner <mgatner@icloud.com>
@lonnieezell
Copy link
Member Author

@MGatner doc changes integrated.

@kenjis
Copy link
Member

kenjis commented Sep 7, 2022

The following errors have nothing to do with this PR. I send #423 to fix.

Run vendor/bin/phpstan analyze
Note: Using configuration file /home/runner/work/shield/shield/phpstan.neon.dist.
   0/119 [░░░░░░░░░░░░░░░░░░░░░░░░░░░░]   0%
 119/119 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓] 100%

Error: Cannot call method isBefore() on array|float|int<min, -1>|int<1, max>|object|string|true.
Error: Cannot call method format() on array|bool|float|int|object|string.
Error: Cannot call method getTimestamp() on array|bool|float|int|object|string.
Error: Cannot call method getTimestamp() on array|bool|float|int|object|string.
 ------ ------------------------------------------------------------------ 
  Line   src/Authentication/Authenticators/AccessTokens.php                
 ------ ------------------------------------------------------------------ 
  121    Cannot call method isBefore() on array|float|int<min, -1>|int<1,  
         max>|object|string|true.                                          
 ------ ------------------------------------------------------------------ 

 ------ -------------------------------------------------------------------- 
  Line   src/Models/UserModel.php                                            
 ------ -------------------------------------------------------------------- 
  329    Cannot call method format() on array|bool|float|int|object|string.  
 ------ -------------------------------------------------------------------- 

 ------ -------------------------------------- 
  Line   tests/Controllers/LoginTest.php       
 ------ -------------------------------------- 
  92     Cannot call method getTimestamp() on  
         array|bool|float|int|object|string.   
  154    Cannot call method getTimestamp() on  
         array|bool|float|int|object|string.   
 ------ -------------------------------------- 

Error:  [ERROR] Found 4 errors              

https://github.com/codeigniter4/shield/runs/8220322852?check_suite_focus=true


```php
$routes->get('/access/token', static function() {
$token = auth()->user()->generateAccessToken(request()->getVar('token_name));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

request() ?
Do you mean service('request') ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't mind him, Laravel gets the bills paid. Yes he meant to use the service.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dang it :) I've considered adding a request() and response() helper method for years on this framework, but never did. I actually had to go double-check the code to ensure we didn't have it lol. But, yes, that's what I meant.

@MGatner
Copy link
Member

MGatner commented Sep 7, 2022

We need a review process for reviews 🤦‍♂️ Thanks for catching wha to missed @kenjis.
@lonnieezell let us know if you'd just like us to finish this up, it's all tweaks.

Co-authored-by: kenjis <kenji.uui@gmail.com>
@lonnieezell
Copy link
Member Author

@MGatner if you guys have the time and/or desire feel free to finish it up. I merged the suggestions.

Copy link
Member

@MGatner MGatner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kenjis or @datamweb one last review and merge?

@MGatner MGatner merged commit 25f6155 into develop Sep 8, 2022
@MGatner MGatner deleted the token-guides branch September 8, 2022 10:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants