Skip to content
This repository was archived by the owner on Sep 20, 2022. It is now read-only.

Commit 2fe3540

Browse files
committed
upload first version
1 parent a5b8dcb commit 2fe3540

34 files changed

+2258
-0
lines changed

LICENSE

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
The MIT License (MIT)
2+
Copyright (c) 2012 Chekun and Yoozi Inc., http://www.yoozi.cn
3+
4+
Permission is hereby granted, free of charge, to any person obtaining a copy
5+
of this software and associated documentation files (the “Software”), to deal
6+
in the Software without restriction, including without limitation the rights
7+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
copies of the Software, and to permit persons to whom the Software is
9+
furnished to do so, subject to the following conditions:
10+
11+
The above copyright notice and this permission notice shall be included in
12+
all copies or substantial portions of the Software.
13+
14+
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20+
THE SOFTWARE.

Oauth2.php

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
2+
3+
/**
4+
* Oauth2 SocialAuth for CodeIgniter
5+
* 修改自 https://github.com/philsturgeon/codeigniter-oauth2
6+
*
7+
* @author chekun <[email protected]>
8+
*/
9+
10+
require APPPATH.'libraries/oauth2/OAuth2.php';
11+
12+
/* End of file Oauth2.php */
13+
/* Location: ./application/libraries/Oauth2.php */

README.md

+111
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
# Oauth2 SocialAuth for CodeIgniter
2+
3+
## 关于
4+
5+
本程序修改自[codeigniter-oauth2](https://github.com/philsturgeon/codeigniter-oauth2).
6+
代码默认适配codeigniter框架,简单修改可以适用于任何框架或者非框架使用。有任何疑问或想法请issue或者pull request。
7+
8+
9+
## 修改点
10+
11+
* 可以运行与spark或者none-spark环境下。
12+
* 增加若干参数,支持国内各大平台。
13+
* 加入csrf验证
14+
* 原版providers被移动到provides/beyond the wall/文件夹中,使用者可根据需求自行移动出来使用。
15+
16+
## 新增的providers
17+
18+
* 新浪微博
19+
* QQ
20+
* 百度
21+
* 360
22+
* 网易微博
23+
* 搜狐微博
24+
* 豆瓣
25+
* 天翼
26+
* 人人
27+
* 移动微博
28+
29+
## 演示站点
30+
31+
[查看演示请戳这里](http://oauth24codeigniter.sinaapp.com/)
32+
33+
## 使用说明
34+
35+
* 将Oauth2.php和oauth2文件夹扔进libraries里
36+
* 典型的控制器代码
37+
38+
```php
39+
<?php defined('BASEPATH') OR exit('No direct script access allowed');
40+
41+
class Sns extends CI_Controller {
42+
43+
public function __construct()
44+
{
45+
parent::__construct();
46+
$this->session->userdata('is_login') AND redirect();
47+
}
48+
49+
public function session($provider = '')
50+
{
51+
$this->config->load('oauth2');
52+
$allowed_providers = $this->config->item('oauth2');
53+
if ( ! $provider OR ! isset($allowed_providers[$provider]))
54+
{
55+
$this->session->set_flashdata('info', '暂不支持'.$provider.'方式登录.');
56+
redirect();
57+
return;
58+
}
59+
$this->load->library('oauth2');
60+
$provider = $this->oauth2->provider($provider, $allowed_providers[$provider]);
61+
$args = $this->input->get();
62+
if ($args AND !isset($args['code']))
63+
{
64+
$this->session->set_flashdata('info', '授权失败了,可能由于应用设置问题或者用户拒绝授权.<br />具体原因:<br />'.json_encode($args));
65+
redirect();
66+
return;
67+
}
68+
$code = $this->input->get('code', TRUE);
69+
if ( ! $code)
70+
{
71+
$provider->authorize();
72+
return;
73+
}
74+
else
75+
{
76+
try
77+
{
78+
$token = $provider->access($code);
79+
$sns_user = $provider->get_user_info($token);
80+
if (is_array($sns_user))
81+
{
82+
$this->session->set_flashdata('info', '登录成功');
83+
$this->session->set_userdata('user', $sns_user);
84+
$this->session->set_userdata('is_login', TRUE);
85+
}
86+
else
87+
{
88+
$this->session->set_flashdata('info', '获取用户信息失败');
89+
}
90+
}
91+
catch (OAuth2_Exception $e)
92+
{
93+
$this->session->set_flashdata('info', '操作失败<pre>'.$e.'</pre>');
94+
}
95+
}
96+
redirect();
97+
}
98+
}
99+
100+
/* End of file sns.php */
101+
/* Location: ./application/controllers/sns.php */
102+
```
103+
104+
## LICENSE
105+
Copyright (c) 2013 [chekun](https://github.com/chekun).
106+
107+
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:
108+
109+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
110+
111+
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.

oauth2/Exception.php

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php
2+
3+
/**
4+
* OAuth2.0 draft v10 exception handling.
5+
*
6+
* @author Originally written by Naitik Shah <[email protected]>.
7+
* @author Update to draft v10 by Edison Wong <[email protected]>.
8+
*/
9+
class OAuth2_Exception extends Exception {
10+
11+
/**
12+
* The result from the API server that represents the exception information.
13+
*/
14+
protected $result;
15+
16+
/**
17+
* Make a new API Exception with the given result.
18+
*
19+
* @param $result
20+
* The result from the API server.
21+
*/
22+
public function __construct($result)
23+
{
24+
$this->result = $result;
25+
26+
$code = isset($result['code']) ? $result['code'] : 0;
27+
28+
if (isset($result['error']))
29+
{
30+
// OAuth 2.0 Draft 10 style
31+
$message = $result['error'];
32+
}
33+
elseif (isset($result['message']))
34+
{
35+
// cURL style
36+
$message = $result['message'];
37+
}
38+
else
39+
{
40+
$message = 'Unknown Error.';
41+
}
42+
43+
parent::__construct($message, $code);
44+
}
45+
46+
/**
47+
* Returns the associated type for the error. This will default to
48+
* 'Exception' when a type is not available.
49+
*
50+
* @return string The type for the error.
51+
*/
52+
public function getType()
53+
{
54+
if (isset($this->result['error']))
55+
{
56+
$message = $this->result['error'];
57+
if (is_string($message))
58+
{
59+
// OAuth 2.0 Draft 10 style
60+
return $message;
61+
}
62+
}
63+
return 'Exception';
64+
}
65+
66+
/**
67+
* To make debugging easier.
68+
*
69+
* @return string The string representation of the error.
70+
*/
71+
public function __toString()
72+
{
73+
$str = $this->getType() . ': ';
74+
if ($this->code != 0)
75+
{
76+
$str .= $this->code . ': ';
77+
}
78+
return $str . $this->message;
79+
}
80+
81+
}

oauth2/OAuth2.php

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
include('Exception.php');
4+
include('Token.php');
5+
include('Provider.php');
6+
7+
/**
8+
* OAuth2.0
9+
*
10+
* @author Phil Sturgeon < @philsturgeon >
11+
*/
12+
class OAuth2 {
13+
14+
/**
15+
* Create a new provider.
16+
*
17+
* // Load the Twitter provider
18+
* $provider = $this->oauth2->provider('twitter');
19+
*
20+
* @param string $name provider name
21+
* @param array $options provider options
22+
* @return OAuth2_Provider
23+
*/
24+
public static function provider($name, array $options = NULL)
25+
{
26+
$name = ucfirst(strtolower($name));
27+
28+
include_once 'Provider/'.$name.'.php';
29+
30+
$class = 'OAuth2_Provider_'.$name;
31+
32+
return new $class($options);
33+
}
34+
35+
}

0 commit comments

Comments
 (0)