PHPackages                             andres-ml/cakephp-oauth2 - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Authentication &amp; Authorization](/categories/authentication)
4. /
5. andres-ml/cakephp-oauth2

ActiveCakephp-plugin[Authentication &amp; Authorization](/categories/authentication)

andres-ml/cakephp-oauth2
========================

CakePHP 3.6 authentication using the league/oauth2-client family

v2.0.0(2y ago)01.5kMITPHP

Since Oct 5Pushed 2y agoCompare

[ Source](https://github.com/andres-ml/OAuth2)[ Packagist](https://packagist.org/packages/andres-ml/cakephp-oauth2)[ Docs](https://github.com/usemuffin/oauth2)[ RSS](/packages/andres-ml-cakephp-oauth2/feed)WikiDiscussions release Synced yesterday

READMEChangelog (1)Dependencies (4)Versions (5)Used By (0)

OAuth2
======

[](#oauth2)

[![Build Status](https://camo.githubusercontent.com/91b6900536ba05f632c309316d0a8168b183fb68f32409e015380cf97523ed33/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f5573654d756666696e2f4f41757468322f6d61737465722e7376673f7374796c653d666c61742d737175617265)](https://travis-ci.org/UseMuffin/OAuth2)[![Coverage](https://camo.githubusercontent.com/f57fdac5850ae1815687399a529dfc44e315bc60cdffa727a297eccca55c34e8/68747470733a2f2f696d672e736869656c64732e696f2f636f766572616c6c732f5573654d756666696e2f4f41757468322f6d61737465722e7376673f7374796c653d666c61742d737175617265)](https://coveralls.io/r/UseMuffin/OAuth2)[![Total Downloads](https://camo.githubusercontent.com/7a7ef37cab960497dde332af6875b7b74500c6373965762408dc852866717fc7/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6d756666696e2f6f61757468322e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/muffin/oauth2)[![License](https://camo.githubusercontent.com/942e017bf0672002dd32a857c95d66f28c5900ab541838c6c664442516309c8a/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d626c75652e7376673f7374796c653d666c61742d737175617265)](LICENSE)

[CakePHP 3](http://cakephp.org) authentication using the [league/oauth2-client](https://github.com/thephpleague/oauth2-client).

Install
-------

[](#install)

Using [Composer](http://getcomposer.org):

```
composer require muffin/oauth2:dev-master

```

You then need to load the plugin. You can use the shell command:

```
bin/cake plugin load Muffin/OAuth2

```

or by manually adding statement shown below to `bootstrap.php`:

```
Plugin::load('Muffin/OAuth2');
```

Wait, you're not done yet. This plugin will **NOT** require any of the clients. You will have to do it yourself:

```
composer require "league/oauth2-github:^1.0@dev"
```

Usage
-----

[](#usage)

First, start by defining the providers:

```
// either in `config/bootstrap.php`
Configure::write('Muffin/OAuth2', [
    'providers' => [
        'github' => [
            'className' => 'League\OAuth2\Client\Provider\Github',
            // all options defined here are passed to the provider's constructor
            'options' => [
                'clientId' => 'foo',
                'clientSecret' => 'bar',
            ],
            'mapFields' => [
                'username' => 'login', // maps the app's username to github's login
            ],
            // ... add here the usual AuthComponent configuration if needed like fields, etc.
        ],
    ],
]);

// or in `src/Controller/AppController.php`
$this->loadComponent('Auth', [
    'authenticate' => [
        // ...
        'Muffin/OAuth2.OAuth' => [
            'providers' => [
                // the array from example above
            ],
        ],
    ],
]);
```

Upon successful authorization, and if the user has no local instance, an event (`Muffin/OAuth2.newUser`) is triggered. Use it to create a user like so:

```
// bootstrap.php
use Cake\Event\Event;
use Cake\ORM\TableRegistry;
EventManager::instance()->on('Muffin/OAuth2.newUser', [TableRegistry::get('Users'), 'createNewUser']);

// UsersTable.php
use Cake\Event\Event;
use League\OAuth2\Client\Provider\AbstractProvider;
public function createNewUser(Event $event, AbstractProvider $provider, array $data)
{
    $entity = $this->newEntity($data);
    $this->save($entity);
    return $entity->toArray(); // user data to be used in session
}
```

Finally, once token is received, the `Muffin/OAuth2.afterIdentify` event is triggered. Use this to update your local tokens for example:

```
// bootstrap.php
use Cake\Event\Event;
use Cake\ORM\TableRegistry;
EventManager::instance()->on('Muffin/OAuth2.afterIdentify', [TableRegistry::get('Tokens'), 'createOrUpdate']);

// TokensTable.php
use Cake\Event\Event;
use League\OAuth2\Client\Provider\AbstractProvider;

public function createOrUpdate(Event $event, AbstractProvider $provider, array $data)
{
    // ...
    return; // void
}
```

Next up, you need to create a route that will be used by all providers:

```
// config/routes.php

Router::connect(
    '/oauth/:provider',
    ['controller' => 'users', 'action' => 'login'],
    ['provider' => implode('|', array_keys(Configure::read('Muffin/OAuth2.providers')))]
);
```

Now, if you have already read the book's `AuthComponent` documentation, you should be familiar with how to add the new authentication object to it:

```
// src/Controller/AppController.php
$this->load('Auth', [
    'authenticate' => [
        'Form',
        'Muffin/OAuth2.OAuth',
    ]
]);
```

Patches &amp; Features
----------------------

[](#patches--features)

- Fork
- Mod, fix
- Test - this is important, so it's not unintentionally broken
- Commit - do not mess with license, todo, version, etc. (if you do change any, bump them into commits of their own that I can ignore when I pull)
- Pull request - bonus point for topic branches

To ensure your PRs are considered for upstream, you MUST follow the [CakePHP coding standards](http://book.cakephp.org/3.0/en/contributing/cakephp-coding-conventions.html).

Bugs &amp; Feedback
-------------------

[](#bugs--feedback)

License
-------

[](#license)

Copyright (c) 2015, [Use Muffin](http://usemuffin.com) and licensed under [The MIT License](http://www.opensource.org/licenses/mit-license.php).

###  Health Score

30

—

LowBetter than 64% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity14

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity66

Established project with proven stability

 Bus Factor1

Top contributor holds 68.4% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~2519 days

Total

2

Last Release

1023d ago

Major Versions

v1.0.0 → v2.0.02023-07-25

### Community

Maintainers

![](https://www.gravatar.com/avatar/485d3a19a3843896470f8d9d516c6b4af2d50a8d19b7dd5d4e197358304a55ae?d=identicon)[andres-ml](/maintainers/andres-ml)

---

Top Contributors

[![jadb](https://avatars.githubusercontent.com/u/33527?v=4)](https://github.com/jadb "jadb (13 commits)")[![andres-ml](https://avatars.githubusercontent.com/u/21081145?v=4)](https://github.com/andres-ml "andres-ml (4 commits)")[![terrabruder](https://avatars.githubusercontent.com/u/1594593?v=4)](https://github.com/terrabruder "terrabruder (2 commits)")

---

Tags

leagueauthAuthenticationcakephpoauth2muffin

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/andres-ml-cakephp-oauth2/health.svg)

```
[![Health](https://phpackages.com/badges/andres-ml-cakephp-oauth2/health.svg)](https://phpackages.com/packages/andres-ml-cakephp-oauth2)
```

###  Alternatives

[league/oauth2-server

A lightweight and powerful OAuth 2.0 authorization and resource server library with support for all the core specification grants. This library will allow you to secure your API with OAuth and allow your applications users to approve apps that want to access their data from your API.

6.6k136.0M248](/packages/league-oauth2-server)[cakedc/users

Users Plugin for CakePHP

524897.0k16](/packages/cakedc-users)[muffin/oauth2

CakePHP 3 authentication using the league/oauth2-client family

27103.4k](/packages/muffin-oauth2)[chervand/yii2-oauth2-server

OAuth 2.0 server for Yii 2.0 with MAC tokens support.

1524.2k1](/packages/chervand-yii2-oauth2-server)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
