OAuth Service Provider for Laravel 5

Overview

OAuth wrapper for Laravel 5

oauth-5-laravel is a simple laravel 5 service provider (wrapper) for Lusitanian/PHPoAuthLib which provides oAuth support in PHP 5.4+ and is very easy to integrate with any project which requires an oAuth client.

Was first developed by Artdarek for Laravel 4 and I ported it to Laravel 5.


Supported services

The library supports both oAuth 1.x and oAuth 2.0 compliant services. A list of currently implemented services can be found below. More services will be implemented soon.

Included service implementations:

  • OAuth1
    • BitBucket
    • Etsy
    • FitBit
    • Flickr
    • Scoop.it!
    • Tumblr
    • Twitter
    • Xing
    • Yahoo
  • OAuth2
    • Amazon
    • BitLy
    • Box
    • Dailymotion
    • Dropbox
    • Facebook
    • Foursquare
    • GitHub
    • Google
    • Harvest
    • Heroku
    • Instagram
    • LinkedIn
    • Mailchimp
    • Microsoft
    • PayPal
    • Pocket
    • Reddit
    • RunKeeper
    • SoundCloud
    • Vkontakte
    • Yammer
  • more to come!

To learn more about Lusitanian/PHPoAuthLib go here

Installation

Add oauth-5-laravel to your composer.json file:

"require": {
  "oriceon/oauth-5-laravel": "dev-master"
}

Use composer to install this package.

$ composer update

Registering the Package

Register the service provider within the providers array found in config/app.php:

'providers' => [
	// ...
	
	'Artdarek\OAuth\OAuthServiceProvider',
]

Add an alias within the aliases array found in config/app.php:

'aliases' => [
	// ...
	
	'OAuth' => 'Artdarek\OAuth\Facade\OAuth',
]

Configuration

There are two ways to configure oauth-5-laravel. You can choose the most convenient way for you. You can use package config file which can be generated through command line by artisan (option 1) or you can simply create a config file called oauth-5-laravel.php in your config directory (option 2).

Option 1

Create configuration file for package using artisan command

$ php artisan vendor:publish

Option 2

Create configuration file manually in config directory config/oauth-5-laravel.php and put there code from below.

<?php
return [ 
	
	/*
	|--------------------------------------------------------------------------
	| oAuth Config
	|--------------------------------------------------------------------------
	*/

	/**
	 * Storage
	 */
	'storage' => 'Session', 

	/**
	 * Consumers
	 */
	'consumers' => [

		/**
		 * Facebook
		 */
		'Facebook' => [
		    'client_id'     => '',
		    'client_secret' => '',
		    'scope'         => [],
		],		

	]

];

Credentials

Add your credentials to config/oauth-5-laravel.php (depending on which option of configuration you choose)

The Storage attribute is optional and defaults to Session. Other options.

Usage

Basic usage

Just follow the steps below and you will be able to get a service class object with this one rule:

$fb = \OAuth::consumer('Facebook');

Optionally, add a second parameter with the URL which the service needs to redirect to, otherwise it will redirect to the current URL.

$fb = \OAuth::consumer('Facebook', 'http://url.to.redirect.to');

Usage examples

###Facebook:

Configuration: Add your Facebook credentials to config/oauth-5-laravel.php

'Facebook' => [
    'client_id'     => 'Your Facebook client ID',
    'client_secret' => 'Your Facebook Client Secret',
    'scope'         => ['email','read_friendlists','user_online_presence'],
],	

In your Controller use the following code:

public function loginWithFacebook(Request $request)
{
	// get data from request
	$code = $request->get('code');
	
	// get fb service
	$fb = \OAuth::consumer('Facebook');
	
	// check if code is valid
	
	// if code is provided get user data and sign in
	if ( ! is_null($code))
	{
		// This was a callback request from facebook, get the token
		$token = $fb->requestAccessToken($code);
		
		// Send a request with it
		$result = json_decode($fb->request('/me'), true);
		
		$message = 'Your unique facebook user id is: ' . $result['id'] . ' and your name is ' . $result['name'];
		echo $message. "<br/>";
		
		//Var_dump
		//display whole array.
		dd($result);
	}
	// if not ask for permission first
	else
	{
		// get fb authorization
		$url = $fb->getAuthorizationUri();
		
		// return to facebook login url
		return redirect((string)$url);
	}
}

###Google:

Configuration: Add your Google credentials to config/oauth-5-laravel.php

'Google' => [
    'client_id'     => 'Your Google client ID',
    'client_secret' => 'Your Google Client Secret',
    'scope'         => ['userinfo_email', 'userinfo_profile'],
],	

In your Controller use the following code:

public function loginWithGoogle(Request $request)
{
	// get data from request
	$code = $request->get('code');
	
	// get google service
	$googleService = \OAuth::consumer('Google');
	
	// check if code is valid
	
	// if code is provided get user data and sign in
	if ( ! is_null($code))
	{
		// This was a callback request from google, get the token
		$token = $googleService->requestAccessToken($code);
		
		// Send a request with it
		$result = json_decode($googleService->request('https://www.googleapis.com/oauth2/v1/userinfo'), true);
		
		$message = 'Your unique Google user id is: ' . $result['id'] . ' and your name is ' . $result['name'];
		echo $message. "<br/>";
		
		//Var_dump
		//display whole array.
		dd($result);
	}
	// if not ask for permission first
	else
	{
		// get googleService authorization
		$url = $googleService->getAuthorizationUri();
		
		// return to google login url
		return redirect((string)$url);
	}
}

###Twitter:

Configuration: Add your Twitter credentials to config/oauth-5-laravel.php

'Twitter' => [
    'client_id'     => 'Your Twitter client ID',
    'client_secret' => 'Your Twitter Client Secret',
    // No scope - oauth1 doesn't need scope
],

In your Controller use the following code:

public function loginWithTwitter(Request $request)
{
	// get data from request
	$token  = $request->get('oauth_token');
	$verify = $request->get('oauth_verifier');
	
	// get twitter service
	$tw = \OAuth::consumer('Twitter');
	
	// check if code is valid
	
	// if code is provided get user data and sign in
	if ( ! is_null($token) && ! is_null($verify))
	{
		// This was a callback request from twitter, get the token
		$token = $tw->requestAccessToken($token, $verify);
		
		// Send a request with it
		$result = json_decode($tw->request('account/verify_credentials.json'), true);
		
		$message = 'Your unique Twitter user id is: ' . $result['id'] . ' and your name is ' . $result['name'];
		echo $message. "<br/>";
		
		//Var_dump
		//display whole array.
		dd($result);
	}
	// if not ask for permission first
	else
	{
		// get request token
		$reqToken = $tw->requestRequestToken();
		
		// get Authorization Uri sending the request token
		$url = $tw->getAuthorizationUri(['oauth_token' => $reqToken->getRequestToken()]);

		// return to twitter login url
		return redirect((string)$url);
	}
}

###Linkedin:

Configuration: Add your Linkedin credentials to config/oauth-5-laravel.php

'Linkedin' => [
    'client_id'     => 'Your Linkedin API ID',
    'client_secret' => 'Your Linkedin API Secret',
],

In your Controller use the following code:

 public function loginWithLinkedin(Request $request)
 {
	// get data from request
	$code = $request->get('code');

	$linkedinService = \OAuth::consumer('Linkedin');


	if ( ! is_null($code))
	{
		// This was a callback request from linkedin, get the token
		$token = $linkedinService->requestAccessToken($code);

		// Send a request with it. Please note that XML is the default format.
		$result = json_decode($linkedinService->request('/people/~?format=json'), true);

		// Show some of the resultant data
		echo 'Your linkedin first name is ' . $result['firstName'] . ' and your last name is ' . $result['lastName'];

		//Var_dump
		//display whole array.
		dd($result);

	}
	// if not ask for permission first
	else
	{
		// get linkedinService authorization
		$url = $linkedinService->getAuthorizationUri(['state'=>'DCEEFWF45453sdffef424']);

		// return to linkedin login url
		return redirect((string)$url);
	}
}

###Yahoo:

Configuration: Add your Yahoo credentials to config/oauth-5-laravel.php

'Yahoo' => [
	'client_id'     => 'Your Yahoo API KEY',
	'client_secret' => 'Your Yahoo API Secret',
],

In your Controller use the following code:

public function loginWithYahoo(Request $request)
{
	// get data from request
    $token  = $request->get('oauth_token');
    $verify = $request->get('oauth_verifier');

    \OAuth::setHttpClient('CurlClient');

    // get yahoo service
    $yh = \OAuth::consumer('Yahoo');

    // if code is provided get user data and sign in
    if ( ! is_null($token) && ! is_null($verify))
    {
		// This was a callback request from yahoo, get the token
		$token = $yh->requestAccessToken($token, $verify);

		$xid = [$token->getExtraParams()];
		$result = json_decode($yh->request('https://social.yahooapis.com/v1/user/' . $xid[0]['xoauth_yahoo_guid'] . '/profile?format=json'), true);

		//Var_dump
		//display whole array.
		dd($result);
    }
    // if not ask for permission first
    else
    {
        // get request token
        $reqToken = $yh->requestRequestToken();

        // get Authorization Uri sending the request token
        $url = $yh->getAuthorizationUri(['oauth_token' => $reqToken->getRequestToken()]);

        // return to yahoo login url
        return redirect((string)$url);
    }
}

More usage examples:

For examples go here

You might also like...
PHP 5.3+ oAuth 1/2 Client Library

PHPoAuthLib NOTE: I'm looking for someone who could help to maintain this package alongside me, just because I don't have a ton of time to devote to i

OAuth 1 Client

OAuth 1.0 Client OAuth 1 Client is an OAuth RFC 5849 standards-compliant library for authenticating against OAuth 1 servers. It has built in support f

The first PHP Library to support OAuth for Twitter's REST API.

THIS IS AN MODIFIED VERSION OF ABRAHAMS TWITTER OAUTH CLASS The directories are structured and the class uses PHP5.3 namespaces. Api.php has a new

OAuth client integration for Symfony. Supports both OAuth1.0a and OAuth2.

HWIOAuthBundle The HWIOAuthBundle adds support for authenticating users via OAuth1.0a or OAuth2 in Symfony. Note: this bundle adds easy way to impleme

Kaiju is an open source verification bot based on Discord's OAuth written in C# and PHP, with the functionality of being able to integrate the user to a new server in case yours is suspended.
Kaiju is an open source verification bot based on Discord's OAuth written in C# and PHP, with the functionality of being able to integrate the user to a new server in case yours is suspended.

What is Kaiju? Kaiju is an open source verification bot for Discord servers, based on OAuth and with permission for the server owner, to be able to mi

The most popular PHP library for use with the Twitter OAuth REST API.
The most popular PHP library for use with the Twitter OAuth REST API.

TwitterOAuth The most popular PHP library for Twitter's OAuth REST API. See documentation at https://twitteroauth.com. PHP versions listed as "active

This module is intended to provide oauth authentication to freescout.

OAuth FreeScout This module is intended to provide oauth authentication to freescout. Module was tested on keycloak oauth provider with confidential o

The Salla OAuth Client library is designed to provide client applications with secure delegated access to Salla Merchant stores.
The Salla OAuth Client library is designed to provide client applications with secure delegated access to Salla Merchant stores.

Salla Provider for OAuth 2.0 Client This package provides Salla OAuth 2.0 support for the PHP League's OAuth 2.0 Client. To use this package, it will

Twitter OAuth API for PHP 5.3+

README The Wid'op OAuth library is a modern PHP 5.3+ API allowing you to easily obtain a Twitter access token. For now, it supports OAuth Web & Applic

Owner
null
OAuth Service Provider for Laravel 5

OAuth wrapper for Laravel 5 oauth-5-laravel is a simple laravel 5 service provider (wrapper) for Lusitanian/PHPoAuthLib which provides oAuth support i

null 2 Sep 19, 2018
Laravel wrapper around OAuth 1 & OAuth 2 libraries.

Introduction Laravel Socialite provides an expressive, fluent interface to OAuth authentication with Facebook, Twitter, Google, LinkedIn, GitHub, GitL

The Laravel Framework 5.2k Dec 27, 2022
PHPoAuthLib provides oAuth support in PHP 7.2+ and is very easy to integrate with any project which requires an oAuth client.

PHPoAuthLib NOTE: I'm looking for someone who could help to maintain this package alongside me, just because I don't have a ton of time to devote to i

David Desberg 1.1k Dec 27, 2022
EAuth extension allows to authenticate users by the OpenID, OAuth 1.0 and OAuth 2.0 providers

EAuth extension allows to authenticate users with accounts on other websites. Supported protocols: OpenID, OAuth 1.0 and OAuth 2.0.

Maxim Zemskov 330 Jun 3, 2022
Buddy Provider for the OAuth 2.0 Client

Buddy Provider for OAuth 2.0 Client This package provides Buddy OAuth 2.0 support for the PHP League's OAuth 2.0 Client. Installation To install, use

Buddy 0 Jan 19, 2021
Easy integration with OAuth 2.0 service providers.

OAuth 2.0 Client This package provides a base for integrating with OAuth 2.0 service providers. The OAuth 2.0 login flow, seen commonly around the web

The League of Extraordinary Packages 3.4k Dec 31, 2022
A Laravel 5 package for OAuth Social Login/Register implementation using Laravel socialite and (optionally) AdminLTE Laravel package

laravel-social A Laravel 5 package for OAuth Social Login/Register implementation using Laravel socialite and (optionally) AdminLTE Laravel package. I

Sergi Tur Badenas 42 Nov 29, 2022
An OAuth 2.0 bridge for Laravel and Lumen [DEPRECATED FOR LARAVEL 5.3+]

OAuth 2.0 Server for Laravel (deprecated for Laravel 5.3+) Note: This package is no longer maintaned for Laravel 5.3+ since Laravel now features the P

Luca Degasperi 2.4k Jan 6, 2023
Social OAuth Authentication for Laravel 5. drivers: facebook, github, google, linkedin, weibo, qq, wechat and douban

Social OAuth Authentication for Laravel 5. drivers: facebook, github, google, linkedin, weibo, qq, wechat and douban

安正超 330 Nov 14, 2022
A spec compliant, secure by default PHP OAuth 2.0 Server

PHP OAuth 2.0 Server league/oauth2-server is a standards compliant implementation of an OAuth 2.0 authorization server written in PHP which makes work

The League of Extraordinary Packages 6.2k Jan 4, 2023