4spacesdk / ci4authextension
Easy OAuth2 integration with CodeIgniter 4
Installs: 1 764
Dependents: 0
Suggesters: 0
Security: 0
Stars: 4
Watchers: 4
Forks: 2
Open Issues: 0
Requires
- php: >=7.2
- ext-openssl: *
- 4spacesdk/ci4debugtool: 1.0.8
- bshaffer/oauth2-server-php: v1.14.1
- kelvinmo/simplejwt: ^0.5.3
This package is auto-updated.
Last update: 2024-11-12 12:25:56 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 string $dbGroupName = 'default'; /* * If true, AuthExtension will extend routes with default endpoints * Check CI4AuthExtension/Hooks/PreController.php for details */ public bool $autoRoute = true; /* * OAuth Access token lifetime in seconds */ public int $oauthAccessTokenLifeTime = 15 * MINUTE; /* * OAuth Access token lifetime in seconds */ public int $oauthRefreshTokenLifeTime = 7 * DAY; /* * Path to login page */ public string $loginPage = '/login'; }
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.