在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. Mongodb(1)如何存储以及简介

    在学习一个数据库之前应该了解这个数据库是如何存储的,是不是适合你所需要的存储方式. 如果上来就说命令,理解起来似乎有点麻烦. 不管学习什么数据库都应该学习其中的基础概念,在mongodb中基本的概念是 ...

  2. 聚合查询中的Group by

    2005版本以上的sql server 查询中如果既包含聚合函数列(诸如sum ,count,avg,max等)又存在一般列的情况,则查询字符串结尾必须包含Group By [某一般列].  其实微软 ...

  3. java小程序实例 闰年

    判断闰年. package com.test; import java.util.Scanner; import org.junit.Test; public class TestRunNian { ...

  4. IE10修改select样式

    一般我们针对webkit内核的,都使用-webkit-appearance来控制一些表单元素. 但是IE10如何来控制呢? select::-ms-expand{display:none;}

  5. [SQL]SQL语言入门级教材_SQL语法参考手册(三)

    SQL 语法参考手册 DB2 提供了关连式资料库的查询语言 SQL (Structured Query Language),是一种非常口语化.既易学又易懂的语法. 此语言几乎是每个资料库系统都必须提供 ...

  6. SAP

    http://www.itpub.net/thread-1328005-1-1.html http://blog.sina.com.cn/s/blog_4b75f26e0100b52a.html SA ...

  7. 2013 ACM 通化邀请赛 A. Tutor

    A. Tutor Description Lilin was a student of Tonghua Normal University. She is studying at University ...

  8. 学习Webservice之入天气小试

    主要方法是:通过程序中设置代理用公司内网访问外部Webservice public InputStream getSoapInputStream(String url) { InputStream i ...

  9. 模拟一下goldengate中断后,重新同步操作

    模拟一下goldengata中断后,重新同步操作:     1.关掉源端抽取进程 GGSCI (20081122-2105) 15> info all Program     Status    ...

  10. transform的使用

    *:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } ...