4spacesdk / ci4authextension
Easy OAuth2 integration with CodeIgniter 4
Installs: 1 694
Dependents: 0
Suggesters: 0
Security: 0
Stars: 4
Watchers: 4
Forks: 2
Open Issues: 0
Requires
- php: >=7.2
- 4spacesdk/ci4debugtool: ^1.0
- bshaffer/oauth2-server-php: v1.13.0
This package is auto-updated.
Last update: 2024-04-30 09:13:03 UTC
README
Installation
Step 1)
composer require 4spacesdk/ci4authextension
Step 2)
Create new file app/Config/AuthExtension.php
and add this content
<?php namespace Config; use CodeIgniter\Config\BaseConfig; class AuthExtension extends BaseConfig { /* * Specify the database group */ public $dbGroupName = 'default'; /* * If true, AuthExtension will extend routes with default endpoints * Check CI4AuthExtension/Hooks/PreController.php for details */ public $autoRoute = true; /* * OAuth Access token life time in seconds */ public $oauthAccessTokenLifeTime = HOUR; }
Step 3)
Add this line to your application/Config/Events.php
file
Events::on('pre_system', [\AuthExtension\Hooks\PreController::class, 'execute']); Events::on('pre_command', [\AuthExtension\Hooks\PreController::class, 'execute']);
Step 4)
Add migration file and add this line to up()
: \AuthExtension\Migration\Setup::migrateUp();
and this line to down()
: \AuthExtension\Migration\Setup::migrateDown();
.
Step 5)
Seed new users, ex:
$user = new User(); $user->first_name = 'Firstname'; $user->last_name = 'Lastname'; $user->username = 'some@email.com'; $user->password = password_hash('secret password', PASSWORD_BCRYPT); $user->save();
Step 6)
Add a controller and view for simple username/password login.
You can either use your own check login algorithm or use $loginResponse = AuthExtension::login($username, $password);
which will return one of these constants and set user_id
in session storage.
class LoginResponse { const Success = 'Success'; const RenewPassword = 'RenewPassword'; const WrongPassword = 'WrongPassword'; const UnknownUser = 'UnknownUser'; }
Authorize with session
$user = AuthExtension::checkSession();
$user
is either FALSE
or the authorized User.
Authorize with OAuth2
If you enable autoRoute in Config you can authorize by calling /check
with access_token
as query parameter or header.
Check AuthExtension\Hooks\PreController
for more routes.