ExtLoginController.class.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. namespace Api\Controller;
  3. use Think\Controller;
  4. class ExtLoginController extends BaseController {
  5. public function oauth2(){
  6. $provider = new \League\OAuth2\Client\Provider\GenericProvider([
  7. 'clientId' => 'a36df4c9-5ed4-440b-8f69-7535d2947213', // The client ID assigned to you by the provider
  8. 'clientSecret' => 'F2m6MjIwNTIwMjEyMjE3NDYxMTM8Lr', // The client password assigned to you by the provider
  9. 'redirectUri' => 'http://192.168.8.160:8280/showdoc/server/?s=/api/ExtLogin/oauth2',
  10. 'urlAuthorize' => 'https://192.168.8.160:8443/maxkey/authz/oauth/v20/authorize',
  11. 'urlAccessToken' => 'https://192.168.8.160:8443/maxkey/authz/oauth/v20/token',
  12. 'urlResourceOwnerDetails' => 'https://192.168.8.160:8443/maxkey/authz/oauth/v20/resource',
  13. ],[
  14. 'httpClient' => new \GuzzleHttp\Client(['verify' => false]),
  15. ]);
  16. // If we don't have an authorization code then get one
  17. if (!isset($_GET['code'])) {
  18. // Fetch the authorization URL from the provider; this returns the
  19. // urlAuthorize option and generates and applies any necessary parameters
  20. // (e.g. state).
  21. $authorizationUrl = $provider->getAuthorizationUrl();
  22. // Get the state generated for you and store it to the session.
  23. $_SESSION['oauth2state'] = $provider->getState();
  24. // Redirect the user to the authorization URL.
  25. header('Location: ' . $authorizationUrl);
  26. exit;
  27. // Check given state against previously stored one to mitigate CSRF attack
  28. } elseif (empty($_GET['state']) || (isset($_SESSION['oauth2state']) && $_GET['state'] !== $_SESSION['oauth2state'])) {
  29. if (isset($_SESSION['oauth2state'])) {
  30. unset($_SESSION['oauth2state']);
  31. }
  32. exit('Invalid state');
  33. } else {
  34. try {
  35. // Try to get an access token using the authorization code grant.
  36. $accessToken = $provider->getAccessToken('authorization_code', [
  37. 'code' => $_GET['code']
  38. ]);
  39. // We have an access token, which we may use in authenticated
  40. // requests against the service provider's API.
  41. echo 'Access Token: ' . $accessToken->getToken() . "<br>";
  42. echo 'Refresh Token: ' . $accessToken->getRefreshToken() . "<br>";
  43. echo 'Expired in: ' . $accessToken->getExpires() . "<br>";
  44. echo 'Already expired? ' . ($accessToken->hasExpired() ? 'expired' : 'not expired') . "<br>";
  45. $res = http_post('https://192.168.8.160:8443/maxkey/api/oauth/v20/me',array(
  46. "access_token"=>$accessToken->getToken()
  47. ));
  48. var_dump($res);
  49. } catch (\League\OAuth2\Client\Provider\Exception\IdentityProviderException $e) {
  50. // Failed to get the access token or user details.
  51. exit($e->getMessage());
  52. }
  53. }
  54. }
  55. public function cas(){
  56. define("CAS_VERSION_1_0", '1.0');
  57. define("CAS_VERSION_2_0", '2.0');
  58. define("CAS_VERSION_3_0", '3.0');
  59. # 2 开启phpCAS debug
  60. \phpCAS::setDebug();
  61. # 3 初始化phpCAS,参数说明:
  62. # a) CAS协议版本号
  63. # b) cas server的域名
  64. # c) cas server的端口号
  65. # d) cas server的项目访问路径
  66. \phpCAS::client(CAS_VERSION_2_0, '192.168.8.160', 8443, '/maxkey/authz/cas/');
  67. # 4 开启设置证书验证。如果是开发环境可将此注释,如果是生产环境为了安全性建议将此开启
  68. // phpCAS::setCasServerCACert($cas_server_ca_cert_path);
  69. # 5 不为CAS服务器设置SSL验证
  70. # 为了快速测试,您可以禁用CAS服务器的SSL验证。此建议不建议用于生产环境。验证CAS服务器对CAS协议的安全性至关重要!
  71. \phpCAS::setNoCasServerValidation();
  72. # 6 这里会检测服务器端的退出的通知,就能实现php和其他语言平台间同步登出了
  73. # 处理登出请求。cas服务端会发送请求通知客户端。如果没有同步登出,可能是服务端跟客户端无法通信(比如我的客户端是localhost, 服务端在云上)
  74. \phpCAS::handleLogoutRequests();
  75. # 7 进行CAS服务验证,这个方法确保用户是否验证过,如果没有验证则跳转到验证界面
  76. # 这个是强制认证模式,查看 CAS.php 可以找到几种不同的方式:
  77. # a) forceAuthentication - phpCAS::forceAuthentication();
  78. # b) checkAuthentication - phpCAS::checkAuthentication();
  79. # c) renewAuthentication - phpCAS::renewAuthentication();
  80. # 根据自己需要调用即可。
  81. $auth = \phpCAS::forceAuthentication();
  82. if ($auth) {
  83. var_dump($auth);
  84. return ;
  85. # 8 验证通过,或者说已经登陆系统,可进行已经登陆之后的逻辑处理...
  86. # 获得登陆CAS用户的名称
  87. $user_name = \phpCAS::getUser();
  88. echo $user_name . '已经成功登陆...<br>';
  89. # 9 你还可打印保存的phpCAS session信息
  90. print_r($_SESSION);
  91. # 10 还可获取有关已验证用户的属性,例如:$uid = phpCAS::getAttribute('id');
  92. $attr = \phpCAS::getAttributes();
  93. print_r($attr);
  94. # 11 进行退出的相关操作
  95. # 在你的PHP项目中处理完相应的退出逻辑之后,还需执行phpCAS::logout()进行CAS系统的退出
  96. # 当我们访问cas服务端的logout的时候,cas服务器会发送post请求到各个已经登录的客户端
  97. //phpCAS::logout();
  98. # 登出方法一:登出成功后跳转的地址
  99. //phpCAS::setServerLoginUrl("https://192.168.1.120:80/cas/logout?embed=true&service=http://localhost/phpCasClient/user.php?a=login");
  100. //phpCAS::logout();
  101. # 登出方法二:退出登录后返回地址
  102. //$param = array("service" => "http://cas.x.com");
  103. //phpCAS::logout($param);
  104. } else {
  105. # 12 验证未通过,说明未进行登陆
  106. # 将会跳转回你配置的CAS SSO SERVER服务的域名;
  107. # 在你输入正确的用户名和密码之后CAS会自动跳转回service=http%3A%2F%2Fcas.x.com%2F此地址
  108. # 在此你可以处理验证未通过的各种逻辑
  109. echo '还未登陆,跳转到CAS进行登陆...<br>';
  110. }
  111. }
  112. }