jkbennemann/laravel-4-social-oauth

Laravel 4 的 OAuth 服务提供者

1.0 2020-07-26 18:18 UTC

This package is auto-updated.

Last update: 2024-09-27 03:42:11 UTC


README

oauth-laravel-4 是一个简单的 Laravel 4 服务提供者,用于 socialconnect/auth,它为 PHP 5.3+ 提供OAuth支持,并且非常容易与任何需要OAuth客户端的项目集成。

支持的服务

该库支持OAuth 1.x和OAuth 2.0兼容的服务。目前实现的服务列表可以在他们的网站上找到此处。以下是一些示例

包含的服务实现

  • Steam OpenID
  • Atlassian OAuth1
  • 500px OAuth1
  • Trello OAuth1
  • Tumblr OAuth1
  • Twitter OAuth1
  • Amazon OAuth2
  • Facebook OAuth2 3.3
  • Vk (ВКонтакте) OAuth2 5.100
  • Instagram OAuth2
  • Google OAuth2
  • GitHub OAuth2
  • GitLab OAuth2
  • Slack OAuth2
  • BitBucket OAuth2
  • Twitch OAuth2
  • Vimeo OAuth2
  • DigitalOcean OAuth2
  • Yandex OAuth2
  • MailRu OAuth2
  • Microsoft (MSN) OAuth2
  • Meetup OAuth2
  • Odnoklassniki OAuth2
  • Discord OAuth2
  • SmashCast OAuth2
  • Steein OAuth2
  • LinkedIn OAuth2
  • Yahoo! OAuth2
  • Wordpress OAuth2
  • Google OpenIDConnect
  • PixelIn OpenIDConnect
  • 更多即将到来!

要了解更多关于 SocialConnect/auth 的信息,请点击此处

安装

使用 composer 安装此包。

$ composer require jkbennemann/oauth-laravel-4

注册包

app/config/app.php 中找到的 providers 数组中注册服务提供者

'providers' => array(
	// ...
	
	'Jkbennemann\OAuth\OAuthServiceProvider',
)

app/config/app.php 中找到的 aliases 数组中添加别名

'aliases' => array(
	// ...
	
	'OAuth' => 'Jkbennemann\OAuth\Facade\OAuth',
)

配置

oauth-4-laravel 有两种配置方式。您可以选择最方便的一种。您可以使用通过 artisan 命令(选项1)生成的包配置文件,或者您可以直接在您的 app/config/ 目录中创建一个名为 laravel-4-social-oauth.php 的配置文件(选项2)。

选项 1

使用 artisan 命令为包创建配置文件

$ php artisan config:publish jkbennemann/laravel-4-social-oauth

选项 2

在配置目录中手动创建 app/config/laravel-4-social-oauth.php 并将以下代码放入其中。

<?php 

return array( 
	
	/*
	|--------------------------------------------------------------------------
	| OAuth Config
	|--------------------------------------------------------------------------
    */

    /*
    |--------------------------------------------------------------------------
    | Note: ${provider} will be resolved automatically accordingly
    |--------------------------------------------------------------------------
    */
	'redirect_base_uri' => 'https://you-callback-url.local/login/${provider}',

	/*
    |--------------------------------------------------------------------------
	| Provider
    |--------------------------------------------------------------------------
	 */
	'provider' => array(

        /**
         * Amazon
         */
        'amazon' => array(
            'applicationId'     => '',
            'applicationSecret' => '',
            'scope'         => array()
        ),

        /**
         * Atlassian
         */
        'atlassian' => array(
            'applicationId'     => '',
            'applicationSecret' => ''
        ),

        /**
         * Bitbucket
         */
        'bitbucket' => array(
            'applicationId'     => '',
            'applicationSecret' => '',
            'scope'         => array(
                'account'
            )
        ),

        /**
         * GitHub
         */
        'github' => array(
            'applicationId'     => '',
            'applicationSecret' => '',
            'scope'         => array(
                'user',
                'email'
            ),
            'options'       => array(
                'fetch_emails'   => true
            )
        ),

    )
);

凭据

将您的凭据添加到 app/config/laravel-4-social-oauth.php

用法

基本用法

只需按照以下步骤操作,您将能够获得一个 服务类对象,遵循以下规则

$service = OAuth::provider('github');

可选地,添加一个第二个参数,指定服务需要重定向到的URL,否则将重定向到当前URL。

$service = OAuth::provider('Github','http://url.to.redirect.to');

用法示例

Github

配置:将您的 Github 凭据添加到配置文件

在您的控制器中使用以下代码

/**
 * Login user with github
 *
 * @return void
 */

public function loginWithGithub() {
	
	// get data from input
	$code = Input::get( 'code' );
	
	// get github service
	$service = OAuth::provider( 'github' );
	
	// check if code is valid
	
	// if code is provided get user data and sign in
	if ( !empty( $code ) ) {
		
        // This was a callback request from github, get the token
        $accessToken  = $service->getAccessTokenByRequestParameters(Input::all());

        // fetch information for authorized token
        $user = $service->getIdentity($accessToken);
        
        var_dump($user);

        //perform user lookup e.g. User::findByEmail($user->email);
        //login User
        //redirect the user to any page
        
        exit();	
	} else {
	    // if not ask for permission first
		// get service authorization
		$url = $service->makeAuthUrl();
		
		// return to auth page
		 return Redirect::to($url);
	}

}