PHPackages                             trunkstar/oauth2-microsoft - 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. trunkstar/oauth2-microsoft

ActiveLibrary[Authentication &amp; Authorization](/categories/authentication)

trunkstar/oauth2-microsoft
==========================

Microsoft OAuth 2.0 Client Provider for The PHP League OAuth2-Client

0.1.1(3y ago)324.1k—4.5%31MITPHPPHP &gt;=8.0

Since Nov 3Pushed 3y agoCompare

[ Source](https://github.com/Trunkstar/oauth2-microsoft)[ Packagist](https://packagist.org/packages/trunkstar/oauth2-microsoft)[ RSS](/packages/trunkstar-oauth2-microsoft/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (2)Dependencies (2)Versions (3)Used By (1)

Microsoft Provider for OAuth 2.0 Client
=======================================

[](#microsoft-provider-for-oauth-20-client)

[![Latest Version](https://camo.githubusercontent.com/da6e4c1089c5496743681ebd8d3061e4d01a9ada02dd4d7b547e40bfc69ea359/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f72656c656173652f5472756e6b737461722f6f61757468322d6d6963726f736f66742e7376673f7374796c653d666c61742d737175617265)](https://github.com/Trunkstar/oauth2-microsoft/releases)[![Total Downloads](https://camo.githubusercontent.com/1a2df83e79862c550538c70560119a144a0d0a10605462a73ca48733fde306a3/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f5472756e6b737461722f6f61757468322d6d6963726f736f66742e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/Trunkstar/oauth2-microsoft)[![Software License](https://camo.githubusercontent.com/ee037a0ec32eb034577d3e9b98150534ecfa57e2776b0bac14b8d384a6ad72fd/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f5472756e6b737461722f6f61757468322d6d6963726f736f66742e7376673f7374796c653d666c61742d737175617265)](LICENSE.md)

This package provides Microsoft OAuth 2.0 support for the PHP League's [OAuth 2.0 Client](https://github.com/thephpleague/oauth2-client).

Installation
------------

[](#installation)

To install, use composer:

```
composer require trunkstar/oauth2-microsoft

```

Usage
-----

[](#usage)

Usage is the same as The League's OAuth client, using `\Trunkstar\OAuth2\Client\Provider\Microsoft` as the provider.

### Authorization Code Flow

[](#authorization-code-flow)

```
$provider = new Trunkstar\OAuth2\Client\Provider\Microsoft([
    // Required
    'clientId'                  => '{microsoft-client-id}',
    'clientSecret'              => '{microsoft-client-secret}',
    'redirectUri'               => 'https://example.com/callback-url', // (deprecated)
    // Optional
    'urlAuthorize'              => 'https://login.microsoftonline.com/organizations/oauth2/v2.0/authorize',
    'urlAccessToken'            => 'https://login.microsoftonline.com/organizations/oauth2/v2.0/token',
    'urlResourceOwnerDetails'   => 'https://graph.microsoft.com/v1.0/me'
]);

if (!isset($_GET['code'])) {

    // If we don't have an authorization code then get one
    $authUrl = $provider->getAuthorizationUrl();
    $_SESSION['oauth2state'] = $provider->getState();
    header('Location: '.$authUrl);
    exit;

// Check given state against previously stored one to mitigate CSRF attack
} elseif (empty($_GET['state']) || ($_GET['state'] !== $_SESSION['oauth2state'])) {

    unset($_SESSION['oauth2state']);
    exit('Invalid state');

} else {

    // Try to get an access token (using the authorization code grant)
    $token = $provider->getAccessToken('authorization_code', [
        'code' => $_GET['code']
    ]);

    // Optional: Now you have a token you can look up a users profile data
    try {

        // We got an access token, let's now get the user's details
        $user = $provider->getResourceOwner($token);

        // Use these details to create a new profile
        printf('Hello %s!', $user->getFirstname());

    } catch (Exception $e) {

        // Failed to get user details
        exit('Oh dear...');
    }

    // Use this to interact with an API on the users behalf
    echo $token->getToken();
}
```

#### Managing Scopes and State

[](#managing-scopes-and-state)

When creating your Microsoft authorization URL, you can specify the state and scopes your application may authorize.

```
$options = [
    'state' => 'OPTIONAL_CUSTOM_CONFIGURED_STATE',
    'scope' => ['User.Read'] // array or string
];

$authorizationUrl = $provider->getAuthorizationUrl($options);
```

If neither are defined, the provider will utilize internal defaults.

Testing
-------

[](#testing)

```
$ ./vendor/bin/phpunit
```

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](https://github.com/Trunkstar/oauth2-microsoft/blob/master/CONTRIBUTING.md) for details.

Credits
-------

[](#credits)

- [Steven Maguire](https://github.com/Trunkstar)
- [All Contributors](https://github.com/Trunkstar/oauth2-microsoft/contributors)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](https://github.com/Trunkstar/oauth2-microsoft/blob/master/LICENSE) for more information.

###  Health Score

29

—

LowBetter than 59% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity33

Limited adoption so far

Community12

Small or concentrated contributor base

Maturity42

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 88.2% 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 ~19 days

Total

2

Last Release

1273d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/d6b568fc536653e6f22a3740f559a0ff1c2adf7892a7c764d8a6dbce49c0bc45?d=identicon)[Gyvastis](/maintainers/Gyvastis)

---

Top Contributors

[![stevenmaguire](https://avatars.githubusercontent.com/u/1851973?v=4)](https://github.com/stevenmaguire "stevenmaguire (30 commits)")[![Gyvastis](https://avatars.githubusercontent.com/u/3257577?v=4)](https://github.com/Gyvastis "Gyvastis (4 commits)")

---

Tags

clientoauthoauth2microsoftauthorizationauthorisation

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/trunkstar-oauth2-microsoft/health.svg)

```
[![Health](https://phpackages.com/badges/trunkstar-oauth2-microsoft/health.svg)](https://phpackages.com/packages/trunkstar-oauth2-microsoft)
```

###  Alternatives

[thenetworg/oauth2-azure

Azure Active Directory OAuth 2.0 Client Provider for The PHP League OAuth2-Client

2509.6M48](/packages/thenetworg-oauth2-azure)[stevenmaguire/oauth2-keycloak

Keycloak OAuth 2.0 Client Provider for The PHP League OAuth2-Client

2275.9M27](/packages/stevenmaguire-oauth2-keycloak)[stevenmaguire/oauth2-microsoft

Microsoft OAuth 2.0 Client Provider for The PHP League OAuth2-Client

742.3M12](/packages/stevenmaguire-oauth2-microsoft)[patrickbussmann/oauth2-apple

Sign in with Apple OAuth 2.0 Client Provider for The PHP League OAuth2-Client

1132.5M6](/packages/patrickbussmann-oauth2-apple)[mollie/oauth2-mollie-php

Mollie Provider for OAuth 2.0 Client

251.7M1](/packages/mollie-oauth2-mollie-php)[omines/oauth2-gitlab

GitLab OAuth 2.0 Client Provider for The PHP League OAuth2-Client

36721.5k13](/packages/omines-oauth2-gitlab)

PHPackages © 2026

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