Giter Club home page Giter Club logo

oauth2-socialauth-for-codeigniter's Introduction

Oauth2 SocialAuth for CodeIgniter

关于

本程序修改自codeigniter-oauth2. 代码默认适配codeigniter框架,简单修改可以适用于任何框架或者非框架使用。有任何疑问或想法请issue或者pull request。

修改点

  • 可以运行与spark或者none-spark环境下。
  • 增加若干参数,支持国内各大平台。
  • 加入csrf验证
  • 原版providers被移动到provides/beyond the wall/文件夹中,使用者可根据需求自行移动出来使用。

新增的providers

  • 新浪微博
  • QQ互联
  • 腾讯微博
  • 百度
  • 360
  • 网易微博
  • 搜狐微博
  • 豆瓣
  • 淘宝
  • 天翼
  • 人人
  • 移动微博
  • 飞信
  • 开心网
  • 多说评论系统

演示站点

查看演示请戳这里

example文件夹为演示站点的源代码

使用说明

  • 将Oauth2.php和oauth2文件夹扔进libraries里
  • application/config建立oauth2.php,内容如下
//xx代表每个provider的唯一标识,也就是文件名的小写
$config['oauth2']['xx'] = array(
	'id' => 'your app client id',
	'secret' => 'your app client secret'
);
$config['oauth2']['xxx'] = array(
	'id' => 'your app client id',
	'secret' => 'your app client secret'
);
  • 典型的控制器代码
<?php defined('BASEPATH') OR exit('No direct script access allowed');

class Sns extends CI_Controller {

    public function __construct()
    {
          parent::__construct();
        $this->session->userdata('is_login') AND redirect();
    }

    public function session($provider = '')
    {
        $this->config->load('oauth2');
        $allowed_providers = $this->config->item('oauth2');
        if ( ! $provider OR ! isset($allowed_providers[$provider]))
        {
            $this->session->set_flashdata('info', '暂不支持'.$provider.'方式登录.');
            redirect();
            return;
        }
        $this->load->library('oauth2');
        $provider = $this->oauth2->provider($provider, $allowed_providers[$provider]);
        $args = $this->input->get();
        if ($args AND !isset($args['code']))
        {
            $this->session->set_flashdata('info', '授权失败了,可能由于应用设置问题或者用户拒绝授权.<br />具体原因:<br />'.json_encode($args));
            redirect();
            return;
        }
        $code = $this->input->get('code', TRUE);
        if ( ! $code)
        {
            $provider->authorize();
            return;
        }
        else
        {
            try
            {
                $token = $provider->access($code);
                $sns_user = $provider->get_user_info($token);
                if (is_array($sns_user))
                {
                    $this->session->set_flashdata('info', '登录成功');
                    $this->session->set_userdata('user', $sns_user);
                    $this->session->set_userdata('is_login', TRUE);
                }
                else
                {
                    $this->session->set_flashdata('info', '获取用户信息失败');
                }
            }
            catch (OAuth2_Exception $e)
            {
                $this->session->set_flashdata('info', '操作失败<pre>'.$e.'</pre>');
            }
        }
        redirect();
    }
}

/* End of file sns.php */
/* Location: ./application/controllers/sns.php */
  • 参数返回说明
<?php 
//注意由于各个平台不一致,不是所有的参数都有值
return array(
    'via' => '', // provider 唯一标示
    'uid' => '', // 用户在对应平台的唯一标识
    'screen_name' => '', //用户的显示名称,一般为昵称
    'name' => '', // 用户的其它名称
    'location' => '', //用户所在地
    'description' => '', // 用户自我介绍
    'image' => '', // 头像地址
    'access_token' => '', // access_token
    'expire_at' => '', // access_token 对应过期时间
    'refresh_token' => '' // refresh_token
);

LICENSE

Copyright (c) 2013 chekun.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Bitdeli Badge

oauth2-socialauth-for-codeigniter's People

Contributors

bitdeli-chef avatar chekun avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

oauth2-socialauth-for-codeigniter's Issues

加载oauth2类库的问题

感谢作者的插件,但是不知道是不是我使用的姿势不对,安装说明在controller里加载oauth2或者直接在autoload里加载,始终都无法调用OAuth2类上的provider静态方法。

当我在controller中var_dump($this->oauth2)时,得到的是以下结果:

object(OAuth2)#14 (0) { } 

对象上并没有类上的静态方法。于是怀疑是否是因为静态方法无法加载,google之找到一个帖子Use a static method from a personal library without instance,里面说通过libraries加载的类库都会自动实例化成一个对象,不知道是否是这个问题?

但是我照老外说的改成helper方式加载也不行。

我使用的CI版本是2.1.x。

一次离奇的案件!

作者您好,首先感谢您的优秀类库,它给我带来了很大的帮助!
但在使用过程中,碰到了一个问题:

我有两个站点a,b,分别申请了不同的weibo api
a站点微博登录成功了,然后把整个项目复制一份放到b站点,
修改好相应的参数(config/config.php, config/oauth2.php)之后,
在b站点微博登录时却报

“你所访问的站点在新浪微博的认证失败(error:invalid_request)”

请问应该怎样解决,谢谢!

* 两台服务器环境php皆是5.3.1x
* 操作系统a是centos,b是win2003
* qq也不行报:redirect uri is illegal(100010)
* 如需了解详细站点地址可私信

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.