Skip to content

v1.x

Latest
Compare
Choose a tag to compare
@bertugfahriozer bertugfahriozer released this 27 Oct 12:04

Codeigniter 4 OAuth2 Library

This is an OAuth2 library for use with CodeIgniter 4. It allows users to authorize and authenticate with third-party applications.

Features

  • Easily configure and deploy an OAuth2 server application.
  • Support for authorizing and authenticating with third-party applications for users.
  • Integration with any client application that supports the OAuth2 protocol.
  • Access authorization mechanisms to secure users' abilities.

Installation

To add the library to your project, follow these steps:

  1. Navigate to your project files.

  2. Use Composer to add the library to your project with the following command:

    composer require bertugfahriozer/ci4oauth2

  3. To create the required database tables, run the following command:

    php spark migrate -all

  4. You will need to create a configuration file. Run the following command to generate a config file:

    php spark make:config

The OAuth2 library is now ready to use in your project!

Usage

Adding a Filter

We'll include the initial filter. The file to be included is "application/Config/Filter.php".

<?php namespace App\Config;

class Filters extends BaseConfig {
    public array $aliases = [
        ...
        'oauthfilter' => \ci4oauth2\Filters\OauthFilter::class
    ];
    
    ...
    public array $filters = [
        'oauthfilter' => ['before' => ['api','api/*']]
    ];
}

URI Routing

In this section, an example URI structure to be added to the "App/Config/Routes.php" file is provided.

$routes->group('api', ['namespace' => 'App\Controllers'], static function ($routes) {
    $routes->resource('blog', ['only' => ['index', 'show', 'create', 'update', 'delete']]);
});

Example Usage

Below is a simple example of using the library:

<?php

namespace App\Controllers;

use CodeIgniter\Controller;
use ci4oauth2\Libraries\Oauth;

class AuthController extends Controller {
    // ...
}

You can extend these example methods to create users in the database and use different authorization types based on your project's requirements.

Example Folder

You can copy and test the code found in this folder after including the library. The example folder path is "ci4oauth2/example".

Authorization Types

  • Authorization Code: Used when an application wants to access protected resources on behalf of a user (i.e., a 3rd party user). Example Request

  • Client Credentials: Used when an application wants to access protected resources under its control (i.e., no third-party). Example Request

  • User Credentials (Password): Used when a user has a trusted relationship with the client and can provide credentials directly. Example Request

  • Refresh Token: Used to obtain additional access tokens to extend the user's access rights. Example Request

  • JWT Bearer (JWT Bearer Token): Used when the application wants to obtain access tokens without transmitting sensitive information. Example Request

Contribution

If you have any issues or feature requests related to this library on GitHub, please report them using the GitHub issue tracker. If you'd like to contribute to the project, please submit a pull request.

License

This library is licensed under the MIT License.

Full Changelog: 0.5.1...1.1.3