Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/jdelaune/oauth2-client-bundle

Symfony 2-4 OAuth2 Client Bundle
https://github.com/jdelaune/oauth2-client-bundle

oauth2 oauth2-client symfony symfony2 symfony3 symfony4

Last synced: 24 days ago
JSON representation

Symfony 2-4 OAuth2 Client Bundle

Awesome Lists containing this project

README

        

# OAuth2 Client Bundle

OAuth2 Client Bundle for Symfony 2-5.

## Overview

Allow for the protection of resources via OAuth2. Provides two Symfony firewalls. One for checking bearer access tokens for securing API application. The access tokens can be provided via a header (recommended) or query e.g. `Authorization: Bearer {Access Token}` or `http://example.com/resource?access_token={Access Token}`. The other firewall is for securing web applications via the authorization code grant type.

## Installation

### Step 1: Add package to Composer

Add the bundle to your composer.json:

``` js
{
"require": {
"jdelaune/oauth2-client-bundle": "^5.0"
}
}
```

Now tell composer to download the bundle by running the command:

``` bash
$ php composer.phar update jdelaune/oauth2-client-bundle
```

Composer will install the bundle to your project's `vendor/jdelaune` directory.

### Step 2: Enable the bundle

Enable the bundle in the kernel:

``` php
get('security.context')->getToken();
$token->getAccessToken(); // The access token
$token->getRefreshToken(); // The refresh token
$token->getExpiresAt(); // Expiry datetime object
$token->getExpiresIn(); // Seconds until the access token expires
```

## The OAuth2User

The client bundle will provide an `OAuth2User` object for any secured path in your controllers.

Scopes will be turned into roles automatically, e.g. a scope of `email` would result in a role of `ROLE_EMAIL`.

There are additional getters available on the `OAuth2User` object:

``` php
$user = $this->getUser();
$user->getClientId(); // Client ID
$user->getUserId(); // User ID
$user->isUser(); // True if user, false if client only
$user->getUsername(); // Client ID if client only, or User ID if user
$user->getScopes(); // Array of scopes
$user->getAccessToken(); // The access token
```