在PHP应用中简化OAuth2.0身份验证集成:OAuth 2.0 Client

 

这个包能够让你以很简单的方式在在PHP应用中集成OAuth2.0身份验证。
用法

验证代码流程

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
$provider = new League\OAuth2\Client\Provider\<ProviderName>(array(
    'clientId'  =>  'XXXXXXXX',
    'clientSecret'  =>  'XXXXXXXX',
    'redirectUri'   =>  'https://your-registered-redirect-uri/',
    'scopes' => array('email', '...', '...'),
));
 
if ( ! isset($_GET['code'])) {
 
    // If we don't have an authorization code then get one
    header('Location: '.$provider->getAuthorizationUrl());
    exit;
 
} else {
 
    // Try to get an access token (using the authorization code grant)
    $token = $provider->getAccessToken('authorization_code', [
        'code' => $_GET['code']
    ]);
 
    // If you are using Eventbrite you will need to add the grant_type parameter (see below)
    $token = $provider->getAccessToken('authorization_code', [
        'code' => $_GET['code'],
        'grant_type' => 'authorization_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
        $userDetails = $provider->getUserDetails($token);
 
        // Use these details to create a new profile
        printf('Hello %s!', $userDetails->firstName);
 
    } catch (Exception $e) {
 
        // Failed to get user details
        exit('Oh dear...');
    }
 
    // Use this to interact with an API on the users behalf
    echo $token->accessToken;
 
    // Use this to get a new access token if the old one expires
    echo $token->refreshToken;
 
    // Number of seconds until the access token will expire, and need refreshing
    echo $token->expires;
}

Refreshing a Token

1
2
3
4
5
6
7
8
$provider = new League\OAuth2\Client\Provider\<ProviderName>(array(
    'clientId'  =>  'XXXXXXXX',
    'clientSecret'  =>  'XXXXXXXX',
    'redirectUri'   =>  'https://your-registered-redirect-uri/'
));
 
$grant = new \League\OAuth2\Client\Grant\RefreshToken();
$token = $provider->getAccessToken($grant, ['refresh_token' => $refreshToken]);

Built-In Providers

这个包目前已经内置了支持:

  • Eventbrite
  • Facebook
  • Github
  • Google
  • Instagram
  • LinkedIn
  • Microsoft

项目主页:http://www.open-open.com/lib/view/home/1417502997514

在PHP应用中简化OAuth2.0身份验证集成:OAuth 2.0 Client的更多相关文章

  1. WebApi 增加身份验证 (OAuth 2.0方式)

    1,在Webapi项目下添加如下引用: Microsoft.AspNet.WebApi.Owin Owin Microsoft.Owin.Host.SystemWeb Microsoft.Owin.S ...

  2. 在ASP.NET Core 2.0中使用Facebook进行身份验证

    已经很久没有更新自己的技术博客了,自从上个月末来到天津之后把家安顿好,这个月月初开始找工作,由于以前是做.NET开发的,所以找的还是.NET工作,但是天津这边大多还是针对to B(企业)进行定制开发的 ...

  3. MVC5个人用户账户身份验证集成google和facebook的OAuth2登陆

    最终效果 官方文档:MVC 5 App with Facebook, Twitter, LinkedIn and Google OAuth2 Sign-on 内容简介:上面传送门的博客中讲解了如何在M ...

  4. Angular 应用中的登陆与身份验证

    Angular 经常会被用到后台和管理工具的开发,这两类都会需要对用户进行鉴权.而鉴权的第一步,就是进行身份验证.由于 Angular 是单页应用,会在一开始,就把大部分的资源加载到浏览器中,所以就更 ...

  5. ASP.NET站点Windows身份验证集成AD域,非LDAP

    站点集成AD域验证 服务器机器入域 计算机右键属性-->“更改设置”-->“更改”-->填写所属域,确认后重启机器生效. 部署测试站点,localhost.ip.域名三种方式登录效果 ...

  6. MVC中使用AuthorizeAttribute做身份验证操作

    代码顺序为:OnAuthorization-->AuthorizeCore-->HandleUnauthorizedRequest 如果AuthorizeCore返回false时,才会走H ...

  7. MVC中使用AuthorizeAttribute做身份验证操作【转】

    http://blog.csdn.net/try530/article/details/7782704 代码顺序为:OnAuthorization-->AuthorizeCore-->Ha ...

  8. 在Laravel中使用Middleware进行身份验证

    新建一个中间件: 方法写在handle中 判断用户是否登录而且是否是管理员,不是的话返回到主页 新建判断是否为管理员的方法 在kernel定义一个中间件,key是admin 注册群组路由:prefix ...

  9. 从零搭建一个IdentityServer——聊聊Asp.net core中的身份验证与授权

    OpenIDConnect是一个身份验证服务,而Oauth2.0是一个授权框架,在前面几篇文章里通过IdentityServer4实现了基于Oauth2.0的客户端证书(Client_Credenti ...

随机推荐

  1. mysql 批量插入

    对于批量插入: 1.在建立唯一索引的情况下,,从前往后,如果遇到索引重复错误 则停止插入(前面的插入成功),错误后面的即使正确也不会插入 方法1:insert igore 后 解决此问题 (ignor ...

  2. OSTimeDelay(1)

    当OS_TICKS_PER_SEC=100HZ ,即每个时钟节拍10ms;如果想延时10ms而调用OSTimeDelay(1)是不行的,调用该函数至少要2个时钟节拍才不会因时间太短,调度跟不上而产生错 ...

  3. ylbtech-Recode(记录)-数据库设计

    ylbtech-dbs:ylbtech-Recode(记录)-数据库设计 -- =============================================-- DatabaseName ...

  4. LInux SSH远程文件/目录传输命令scp(转载)

    From:http://www.vpser.net/manage/scp.html 相信各位VPSer在使用VPS是经常会在不同VPS间相互备份数据或转移数据.一般情况下VPS都已安装了Ngnix或者 ...

  5. android之AutoCompleteTextView控件用法

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools=&q ...

  6. Python标准库09 当前进程信息 (os包)

    作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 我们在Linux的概念与体系,多次提及进程的重要性.Python的os包中有查询和 ...

  7. vmware虚拟机挂起后无法再恢复(转)

    之前一直使用vmware调试程序,但有一天它被挂起后,就一直无法恢复. 提示: 无法获取该配置文件上的排他锁 另一个VMware进程可能正在使用此配置文件. 后来在google里查了一下,发现其实是一 ...

  8. rand()和srand()区别

    标准库<cstdlib>提供两个帮助生成伪随机数的函数: 函数一:int rand(void):从srand (seed)中指定的seed开始,返回一个[seed, RAND_MAX(0x ...

  9. struts2访问servlet API

    搭建环境: 引入jar包,src下建立struts.xml文件 项目配置文件web.xml. web.xml: <?xml version="1.0" encoding=&q ...

  10. Spring配置项<context:annotation-config/>说明

    配置applicationContext.xml时经常会看到: <context:annotation-config/> 它的作用是隐式地向Spring容器注册AutowiredAnnot ...