SkillAgentSearch skills...

Socialite

Socialite is an OAuth2 Authentication tool. It is inspired by laravel/socialite, you can easily use it without Laravel.

Install / Use

/learn @overtrue/Socialite

README

Socialite

Socialite 是一个 OAuth2 认证工具。 它的灵感来源于 laravel/socialite , 你可以很轻易的在任何 PHP 项目中使用它。英文文档

GitHub release (latest SemVer) GitHub License Packagist Downloads

Sponsor me

该工具现已支持平台有:Apple,Facebook,Github,Google,Linkedin,Outlook,QQ,TAPD,支付宝,淘宝,百度,钉钉,微博,微信,抖音,飞书,Lark,豆瓣,企业微信,腾讯云,Line,Gitee,Coding,Twitter。

如果你喜欢我的项目并想支持我,点击这里 :heart:

版本要求

PHP >= 8.0.2

安装

composer require "overtrue/socialite" -vvv

使用指南

用户只需要创建相应配置变量,然后通过工具为各个平台创建认证应用,并轻松获取该平台的 access_token 和用户相关信息。工具实现逻辑详见参照各大平台 OAuth2 文档。

工具使用大致分为以下几步:

  1. 配置平台设置
  2. 创建对应平台应用
  3. 让用户跳转至平台认证
  4. 服务器收到平台回调 Code,使用 Code 换取平台处用户信息(包括 access_token)

为 Laravel 用户创建的更方便的整合的包: overtrue/laravel-socialite

authorize.php: 让用户跳转至平台认证

<?php

use Overtrue\Socialite\SocialiteManager;

$config = [
    'github' => [
        'client_id'     => 'your-app-id',
        'client_secret' => 'your-app-secret',
        'redirect_uri' => 'http://localhost/socialite/callback.php',
    ],
];

$socialite = new SocialiteManager($config);

$url = $socialite->create('github')->redirect();

return redirect($url);

callback.php:

<?php

use Overtrue\Socialite\SocialiteManager;

$config = [
    'github' => [
        'client_id' => 'your-app-id',
        'client_secret' => 'your-app-secret',
        'redirect_uri' => 'http://localhost/socialite/callback.php',
    ],
];

$socialite = new SocialiteManager($config);

$code = request()->query('code');

$user = $socialite->create('github')->userFromCode($code);

$user->getId();        // 1472352
$user->getNickname();  // "overtrue"
$user->getUsername();  // "overtrue"
$user->getName();      // "安正超"
$user->getEmail();     // "anzhengchao@gmail.com"
...

配置

为每个平台设置相同的键值对后就能开箱即用:client_id, client_secret, redirect_uri。 同时兼容旧键名:redirectredirect_url

示例:

$config = [
  'weibo' => [
    'client_id'     => 'your-app-id',
    'client_secret' => 'your-app-secret',
    'redirect_uri' => 'http://localhost/socialite/callback.php',
  ],
  'facebook' => [
    'client_id'     => 'your-app-id',
    'client_secret' => 'your-app-secret',
    'redirect_uri' => 'http://localhost/socialite/callback.php',
  ],
];

自定义应用名

你可以使用任意你喜欢的名字对每个平台进行命名,比如说 foo, 采用别名的方法后需要在配置中多设置一个 provider 键,这样才能告诉工具包如何正确找到你想要的程序:

$config = [
  // 为 github 应用起别名为 foo
    'foo' => [
        'provider'    => 'github',  // <-- provider name
        'client_id'   => 'your-app-id',
        'client_secret' => 'your-app-secret',
        'redirect_uri' => 'http://localhost/socialite/callback.php',
    ],

    // 另外一个名字叫做 bar 的 github 应用
    'bar' => [
        'provider'    => 'github',  // <-- provider name
        'client_id'   => 'your-app-id',
        'client_secret' => 'your-app-secret',
        'redirect_uri' => 'http://localhost/socialite/callback.php',
    ],

    //...
];

$socialite = new SocialiteManager($config);

$appFoo = $socialite->create('foo');
$appBar = $socialite->create('bar');

扩展自定义服务提供程序

你可以很容易的从自定义的服务提供中创建应用,只需要遵循如下两点:

  1. 使用自定义创建器

    如下代码所示,为 foo 应用定义了服务提供名,但是工具本身还未支持,所以使用创建器 extend(),以闭包函数的形式为该服务提供创建一个实例。

$config = [
    'foo' => [
        'provider' => 'myprovider',  // <-- 一个工具还未支持的服务提供程序
        'client_id' => 'your-app-id',
        'client_secret' => 'your-app-secret',
        'redirect_uri' => 'http://localhost/socialite/callback.php',
    ],
];

$socialite = new SocialiteManager($config);

$socialite->extend('myprovider', function(array $config) {
    return new MyCustomProvider($config);
});

$app = $socialite->create('foo');
  1. 使用服务提供类

[!IMPORTANT]

👋🏻 你的自定义服务提供类必须实现 Overtrue\Socialite\Contracts\ProviderInterface 接口

class MyCustomProvider implements \Overtrue\Socialite\Contracts\ProviderInterface
{
    //...
}

接下来为 provider 设置该类名让工具可以找到该类并实例化:

$config = [
    'foo' => [
        'provider'    => MyCustomProvider::class,  // <-- 类名
        'client_id'   => 'your-app-id',
        'client_secret' => 'your-app-secret',
        'redirect_uri' => 'http://localhost/socialite/callback.php',
    ],
];

$socialite = new SocialiteManager($config);
$app = $socialite->create('foo');

平台

不同的平台有不同的配置方法,为了确保工具的正常运行,所以请确保你所使用的平台的配置都是如期设置的。

支付宝

请按如下方式配置

$config = [
  'alipay' => [
    // 这个键名还能像官方文档那样叫做 'app_id'
    'client_id' => 'your-app-id',

    // 请根据官方文档,在官方管理后台配置 RSA2
    // 注意: 这是你自己的私钥
    // 注意: 不允许私钥内容有其他字符
    // 建议: 为了保证安全,你可以将文本信息从磁盘文件中读取,而不是在这里明文
    'rsa_private_key' => 'your-rsa-private-key',

    // 确保这里的值与你在服务后台绑定的地址值一致
    // 推荐使用 'redirect_uri',同时兼容 'redirect' 与 'redirect_url'
    'redirect_uri' => 'http://localhost/socialite/callback.php',

    // 沙箱模式接入地址见 https://opendocs.alipay.com/open/220/105337#%E5%85%B3%E4%BA%8E%E6%B2%99%E7%AE%B1
    'sandbox' => false,
  ]
  ...
];

$socialite = new SocialiteManager($config);

$user = $socialite->create('alipay')->userFromCode('here is auth code');

// 详见文档后面 "User interface"
$user->getId();        // 1472352
$user->getNickname();  // "overtrue"
$user->getUsername();  // "overtrue"
$user->getName();      // "安正超"
...

本工具暂时只支持 RSA2 个人私钥认证方式。

钉钉

如文档所示

注意:该工具仅支持 QR code 连接到第三方网站,用来获取用户信息(opeid, unionid 和 nickname)

$config = [
  'dingtalk' => [
      // or 'app_id'
      'client_id' => 'your app id',

      // or 'app_secret'
      'client_secret' => 'your app secret',

      // 兼容旧键名:'redirect' 或 'redirect_url'
      'redirect_uri' => 'redirect URL'
  ]
];

$socialite = new SocialiteManager($config);

$user = $socialite->create('dingtalk')->userFromCode('here is auth code');

// 详见文档后面 "User interface"
$user->getId();        // 1472352
$user->getNickname();  // "overtrue"
$user->getUsername();  // "overtrue"
$user->getName();      // "安正超"
...

抖音

注意: 使用抖音服务提供的时候,如果你想直接使用 access_token 获取用户信息时,请先设置 openid。 先调用 withOpenId() 再调用 userFromToken()

$config = [
  'douyin' => [
      'client_id' => 'your app id',

      'client_secret' => 'your app secret',

      'redirect_uri' => 'redirect URL'
  ]
];

$socialite = new SocialiteManager($config);

$user = $socialite->create('douyin')->userFromCode('here is auth code');

$user = $socialite->create('douyin')->withOpenId('openId')->userFromToken('here is the access token');

头条

注意: 使用头条服务提供的时候,如果你想直接使用 access_token 获取用户信息时,请先设置 openid。 先调用 withOpenId() 再调用 userFromToken()

$config = [
  'toutiao' => [
    'client_id' => 'your app id',
    'client_secret' => 'your app secret',
    'redirect_uri' => 'redirect URL'
  ]
];

$socialite = new SocialiteManager($config);

$user = $socialite->create('toutiao')->userFromCode('here is auth code');
$user = $socialite->create('toutiao')->withOpenId('openId')->userFromToken('here is the access token');

西瓜

注意: 使用西瓜服务提供的时候,如果你想直接使用 access_token 获取用户信息时,请先设置 openid。 先调用 withOpenId() 再调用 userFromToken()

$config = [
  'xigua' => [
    'client_id' => 'your app id',
    'client_secret' => 'your app secret',
    'redirect_uri' => 'redirect URL'
  ]
];

$socialite = new SocialiteManager($config);

$user = $socialite->create('xigua')->userFromCode('here is auth code');
$user = $socialite->create('xigua')->withOpenId('openId')->userFromToken('here is the access token');

百度

其他配置没啥区别,在用法上,可以很轻易的选择重定向登录页面的模式,通过 withDisplay()

  • **page:**全屏形式的授权页面 (默认),适用于 web 应用。
  • popup: 弹框形式的授权页面,适用于桌面软件应用和 web 应用。
  • dialog: 浮层形式的授权页面,只能用于站内 web 应用。
  • mobile: Iphone/Android 等智能移动终端上用的授权页面,适用于 Iphone/Android 等智能移动终端上的应用。
  • tv: 电视等超大显示屏使用的授权页面。
  • pad: IPad/Android 等智能平板电脑使用的授权页面。
$authUrl = $socialite->create('baidu')->withDisplay('mobile')->redirect();

popup 模式是工具内默认的使用模式。basic 是默认使用的 scopes 值。

飞书

通过一些简单的方法配置 app_ticket 就能使用内部应用模式

$config = [
    'feishu' => [
        // or 'app_id'
        'client_id' => 'your app id',

        // or 'app_secret'
        'client_secret' => 'your app secret',

        // 兼容旧键名:'redirect' 或 'redirect_url'
        'redirect_uri' => 'redirect URL',

        // 如果你想使用使用内部应用的方式获取 app_access_token
        // 对这个键设置了 'internal' 值那么你已经开启了内部应用模式
        'app_mode' => 'internal'
    ]
];

$socialite = new SocialiteManager($config);

$feishuDriver = $socialite->create('feishu');

$feishuDriver->withInternalAppMode()->userFromCode('here is code');
$feishuDriver->withDefaultMode()->withAppTicket('app_ticket')->userFromCode('here is code');

Lark

通过一些简单的方法配置 app_ticket 就能使用内部应用模式

$config = [
    'lark' => [
        // or 'app_id'
        'client_id' => 'your app id',

        // or 'app_secret'
        'client_secret' => 'your app secret',

        // 兼容旧键名:'redirect' 或 'redirect_url'
        'redirect_uri' => 'redirect URL',

        // 如果你想使用使用内部应用的方式获取 app_access_token
        // 对这个键设

Related Skills

View on GitHub
GitHub Stars1.4k
CategoryDevelopment
Updated7d ago
Forks245

Languages

PHP

Security Score

100/100

Audited on Mar 16, 2026

No findings