背景
公司使用企业微信做为办公管理系统。为了公司人事相关研发系统统一授权认证,实现业务系统账号与企业微信挂钩,做到统一管理授权,以此才有了禅道与企业微信集成单点登录研究。
集成步骤
集成步骤大致分为三个部分:
一、企业微信创建配置应用
首先登录企业微信管理,后在“我的企业》企业信息”中获取企业ID
其次在应用管理中创建应用,获取AgentId、Secret等应用接口授权信息。配置可见范围、管理员、及应用主页地址。应用主页地址为禅道sso.php的访问地址。
以及配置开发者接口启用配置开发者接口可信域名,企业微信授权登录授权回调域,及企业可信IP
二、禅道配置第三方应用免密登录
详细配置见官方文档禅道配置第三方应用免密登录
三、禅道配置更新集成登录代码
在禅道的安装目录(例如:/data/zentao/zentaopms/www/)下创建sso.php文件,复制以下代码至sso.php中,并根据一、二点获取的相关信息以下配置内容:
define('CORPID', "企业ID");
define('AGENTID', "应用ID");
define('CORPSECRET', "应用Secret");
define('ZENTAO_PREURL', "禅道应用跟地址(例如:https://www.zentao.net)");
define('ZENTAO_CODE', "禅道第三方应用代号");
define('ZENTAO_SECRET', "禅道第三方应用秘钥");
禅道集成登录项目代码如下:
<?php
error_reporting(1);
define('IN_SYS', true);
define('CORPID', "企业ID");
define('AGENTID', "应用ID");
define('CORPSECRET', "应用Secret");
define('ZENTAO_PREURL', "禅道应用跟地址(例如:https://www.zentao.net)");
define('ZENTAO_CODE', "禅道第三方应用代号");
define('ZENTAO_SECRET', "禅道第三方应用秘钥");
function HTTPGET($url){
//初始化
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 120);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
//执行请求
$content = curl_exec($ch);
curl_close($ch);
$content = is_json($content) ? json_decode($content,true) : $content;
return $content;
}
function HTTPPOST($url, $parm){
//初始化
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 120);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_POST, 1);
$json_data = json_encode($parm);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data);
//执行请求
$content = curl_exec($ch);
curl_close($ch);
$content = is_json($content) ? json_decode($content,true) : $content;
return $content;
}
function is_json($string) {
json_decode($string);
return (json_last_error() == JSON_ERROR_NONE);
}
$code = $_GET["code"];
if($code == null || $code == "" ) {
$oauthUrl = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" . CORPID . "&redirect_uri=" . ZENTAO_PREURL . "/sso.php&response_type=code&scope=snsapi_privateinfo&agentid=".AGENTID."&state=".AGENTID."#wechat_redirect";
header("Location:" . $oauthUrl);
exit;
}
//获取Token
$resJson = HTTPGET("https://qyapi.weixin.qq.com/cgi-bin/gettoken?".http_build_query(
array('corpid'=> CORPID,'corpsecret'=> CORPSECRET))
);
$accessToken = $resJson["access_token"];
//获取用户身份票据
$resJson = HTTPGET("https://qyapi.weixin.qq.com/cgi-bin/auth/getuserinfo?".http_build_query(
array('access_token' => $accessToken,'code' => $code))
);
$user_ticket = $resJson["user_ticket"];
$userid = $resJson["userid"];
if($userid == null) {
$userid = "nobody";
}
$resJson = HTTPPOST("https://qyapi.weixin.qq.com/cgi-bin/auth/getuserdetail?access_token=".$accessToken, array(
'user_ticket' => $user_ticket
));
$account = $userid;
$email = $resJson["email"];
if($email != null && $email != "" ) {
$account = str_replace(".", "", explode("@", $email)[0]);
}
$code = ZENTAO_CODE;
$key = ZENTAO_SECRET;
$time = time();
$token = md5($code . $key . $time);
$ssoUrl = ZENTAO_PREURL . "/api.php?m=user&f=apilogin&account=".$account."&code=".$code."&time=".$time."&token=".$token;
header("Location:" . $ssoUrl);
exit;