Giter Site home page Giter Site logo

ibra4 / api-key Goto Github PK

View Code? Open in Web Editor NEW
2.0 1.0 0.0 11 KB

Laravel package provides a simple API key authentication mechanism for your Laravel applications. It allows you to protect your API endpoints by validating API keys sent with each request

PHP 100.00%
api-key authentication laravel php

api-key's Introduction

StandWithPalestineBadge

Laravel API Key Authentication Package

This Laravel package provides a simple API key authentication mechanism for your Laravel applications. It allows you to protect your API endpoints by validating API keys sent with each request.

Requirements

  • PHP 7.3 or higher

Tested on Laravel ^8.75

Installation

You can install this package via Composer:

composer require ibra4/api-key

Next, you should publish the package's configuration file:

php artisan vendor:publish --tag=api_key

Then run migrations

php artisan migrate

This command will publish the api_key.php configuration file to your config directory.

Configuration

After publishing the configuration file, you can modify the settings in config/api_key.php to fit your application's requirements. This file allows you to define various aspects of API key authentication, such as key length, expiration duration, etc.

Usage

Prepare your model

  • Implement HasApiKeyInterface interface
  • Use HasApiKey trait
    <?php 
    
    + use Ibra\ApiKey\Interfaces\HasApiKeyInterface;
    + use Ibra\ApiKey\Traits\HasApiKey;
    - class User extends Authenticatable {
    + class User extends Authenticatable implements HasApiKeyInterface {
    +    use HasApiKey;
    
    }

Generating API Keys

To generate an API key, you can use the provided artisan command:

php artisan api_key:generate ibra 1

This command will generate a new API key and associate it with a App\Models\User model with id 1.

Arguments

  • client_id: client's name
  • id: Model id
  • model (optional): Model class name (default: App\Models\User)
  • description (optional): Description

Protecting Routes

To protect your API routes with API key authentication, you can use the simple_api_key middleware provided by this package. Simply apply this middleware to the routes you want to protect:

Route::middleware('simple_api_key')->get('/api/resource', 'ResourceController@index');

This middleware will verify the API key sent with each request and authenticate the associated user.

Deactivating API Keys

You can deactivate an API key using the provided artisan command:

php artisan api-key:deactivate {client_id}

Replace {client_id} with the API key you want to deactivate.

Removing API Keys

To remove an API key from the system, you can use the following artisan command:

php artisan api-key:remove {client_id}

Replace {client_id} with the API key you want to remove.

Listing API Keys

You can list all API keys stored in the database using the following artisan command:

php artisan api-key:list

This command will display a list of all API keys along with their associated user and status, like.

client_id description model model_id key is_active expires_at created_at
ibrahim Hello World App\Models\User 2 6af97902bfb6f1c15fea8e079babeca731ee9fb04dd08bb7b6efb80baaed1eb6 1 2024-04-19T18:25:58.000000Z 2024-03-20T18:25:58.000000Z
lara App\Models\Client 1 daabe8a2ed4b84f2156a12dca5b29d8aa4b8fbf4b27813aac077bdc654f57c7b 0 2024-04-19T18:33:15.000000Z 2024-03-20T18:33:15.000000Z

Middleware Logic

The ApiKeyMiddleware included in this package is responsible for authenticating API requests based on the provided API key. It checks the validity and status of the API key and logs in the associated user if the key is valid and active.

Contributing

Contributions are welcome! If you encounter any issues or have suggestions for improvements, please feel free to open an issue or submit a pull request on GitHub.

License

This package is open-source software licensed under the MIT license.

Credits

This package is developed and maintained by Ibrahim Hammad.


Feel free to add any additional sections or customize the content as per your project's requirements. This README provides a basic overview of the package and its usage.

api-key's People

Contributors

ibra4 avatar

Stargazers

 avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.