csna_wechat.module
2.31 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<?php
/**
* @file
* The CSNA Wechat module is a sub-module of the Chinese Social Networks
* Authentication (CSNA) framework allowing integration with the Wechat OAuth
* protocol for Authentication/Login.
*/
/**
* Implements hook_csna_provider_info().
*/
function csna_wechat_csna_provider_info() {
$providers = array();
$providers['wechat'] = array(
'title' => t('Wechat'),
'description' => t('Set your <strong>Wechat authorized KEY</strong> and <strong>SECRET</strong> below.<br/>If you want to use an API key, you can get one at the !keylink.', array('!keylink' => l(t('Wechat API website'), 'https://open.weixin.qq.com/', array('attributes' => array('target' => '_blank'))))),
'display_title' => csna_get_wechat_button_image(),
'type' => CSNA_OAUTH2,
'authorize_uri' => 'https://open.weixin.qq.com/connect/qrconnect',
'access_token_uri' => 'https://api.weixin.qq.com/sns/oauth2/access_token',
'info_uri' => 'https://api.weixin.qq.com/sns/userinfo',
);
return $providers;
}
/**
* Helper function for returning a themed image for the Wechat display title.
*/
function csna_get_wechat_button_image() {
$image_arr = array(
'path' => drupal_get_path('module', 'csna_wechat') . '/wechat_120x24.png',
'title' => t('Sign in with Wechat'),
'alt' => t('Sign in with Wechat'),
);
return theme('image', $image_arr);
}
/**
* Implements hook_csna_provider_callback().
*/
function csna_wechat_csna_provider_callback($provider, $request) {
if (isset($request['code'])) {
$parameters = array(
'appid' => $provider['key'],
'secret' => $provider['secret'],
'grant_type' => 'authorization_code',
'code' => $request['code'],
);
$param_data = array(
'method' => 'POST',
'data' => drupal_http_build_query($parameters),
);
$http = drupal_http_request(url($provider['access_token_uri'], array('query' => $parameters)), $param_data);
$data = json_decode($http->data);
$http = drupal_http_request(url($provider['info_uri'], array('query' => array('access_token' => $data->access_token, 'openid' => $data->openid))));
$info = json_decode($http->data);
if (!isset($info->openid)) {
return FALSE;
}
return array(
'access_token' => $data->access_token,
'uid' => $info->openid,
'name' => $info->nickname,
);
}
}