Generate Api Key Php Laravel

 

Application Key. The next thing you should do after installing Laravel is set your application key to a random string. If you installed Laravel via Composer or the Laravel installer, this key has already been set for you by the php artisan key:generate command. Typically, this string should be 32 characters long. Actually I borrowed ideas from this post PHP: How to generate a random, unique, alphanumeric string? To come out with the methods above but the post does not entirely address my issues. My goal is to generate a string of length say 6 to 8 (Alphanumeric and readable). This string would be use by my admin for query purposes. Create an API Endpoint to Generate Tokens. Creating APIs in Laravel is a pretty seamless process. As a modern web application framework, Laravel provides the necessary scaffolding to create the controller and route needed to generate tokens on the server-side. Mar 17, 2019  If you want to create web services with php than i will must suggest to use laravel 5.8 to create apis because laravel provide structure with authentication using passport. Based on structure it will become a very easily way to create rest apis. Laravel makes API authentication a breeze using Laravel Passport, which provides a full OAuth2 server implementation for your Laravel application in a matter of minutes. Passport is built on top of the League OAuth2 server that is maintained by Andy Millington and Simon Hamp. Sep 18, 2019  In this article, we learned how to install Composer, Laravel, Docker, and how we can leverage containers to create our Laravel API. We used Docker Compose to create a Laravel application and MySQL service, we can now save our data to a database and have it persist. We created Photo Controller that lets us build powerful yet simple APIs in Laravel.

Installation

Run composer require ejarnutowski/laravel-api-key.

In your config/app.php file, add the Laravel API Key service provider to the end of the providers array.

Publish the migration files

Run the migrations

3 new database tables will be created:

  • api_keys
  • api_key_access_events
  • api_key_admin_events

Managing Keys

Generate a new key using php artisan apikey:generate {name}. The name argument is the name of your API key. All new keys are active by default.

Deactivate a key using php artisan apikey:deactivate {name}.

Activate a key using php artisan apikey:activate {name}.

Delete a key. You'll be asked to confirm. Keys are soft-deleted for record keeping. Generate a consumer key twitter.

List all keys. The -D or --deleted flag includes deleted keys

Usage

Implementing Authorization

A new auth.apikey route middleware has been registered for you to use in your routes or controllers. Below are examples on how to use middleware, but for detailed information, check out Middleware in the Laravel Docs.

Route example

Controller example Generating a short-lived key by hashing a long-term key.

Laravel Api Tutorial

Authorizing Requests

In order to pass the auth.apikey middleware, requests must include an X-Authorization header as part of the request, with its value being an active API key.

Unauthorized Requests

Requests that do not pass authorization will receive an HTTP 401 Status Code with the following response

Event History

All API requests that pass authorization are logged in the api_key_access_events table. A record is created for each request with the following information:

Generate Api Key Php Laravel Login

  • api_key_id
  • ip_address
  • url
  • created_at
  • updated_at

Any time an API key is generated, activated, deactivated, or deleted, a record is logged in the api_key_admin_events table. Each record contains the following information:

  • api_key_id
  • ip_address
  • event
  • created_at
  • updated_at

License

Laravel Api Routes

The Laravel API Key package is open-sourced software licensed under the MIT license.