Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [PHP] ograniczenie dostępu w google oauth
neurogen
post 10.09.2018, 21:32:42
Post #1





Grupa: Zarejestrowani
Postów: 58
Pomógł: 0
Dołączył: 25.03.2018

Ostrzeżenie: (0%)
-----


napisałem prosty skrypt w php do zarządzania listą zadań. jest online więc dodałem autoryzację google oauth, natomiast obecnie jest tak że wyświetla się okno wyboru użytkownika google którym chcę się zalogować ale każdy z nich może się zalogować. jak przyznać uprawnienia tylko wybranym? gdzieś w https://console.cloud.google.com/apis/credentials czy w skrypcie?

Google_API.php
  1. <?php
  2.  
  3. define('GOOGLE_CLIENT_ID', 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.apps.googleusercontent.com');
  4. define('GOOGLE_CLIENT_SECRET', 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx');
  5. define('CONFIG_CURRENT_DOMAIN', 'https://mojastrona.pl/tasks/index.php');
  6.  
  7. class Google_API
  8. {
  9. public function __construct($redirect_uri = null)
  10. {
  11. $this->redirect_uri = urlencode($redirect_uri);
  12. $this->client_id = GOOGLE_CLIENT_ID;
  13. $this->client_secret = GOOGLE_CLIENT_SECRET;
  14. }
  15.  
  16. public function Dialog($scope = null)
  17. {
  18. $state = uniqid();
  19. if (isset($_COOKIE['google_access_state'])) {
  20. setcookie('google_access_state', null, time() - 3600, '/', '.' . CONFIG_CURRENT_DOMAIN);
  21. }
  22. setcookie('google_access_state', $state, time() + 3600, '/', '.' . CONFIG_CURRENT_DOMAIN);
  23.  
  24. return sprintf("https://accounts.google.com/o/oauth2/auth?client_id=%s&scope=%s&redirect_uri=%s&state=%s&response_type=code",
  25. $this->client_id, $scope, $this->redirect_uri, $state
  26. );
  27. }
  28.  
  29. public function GetAccessToken($scope)
  30. {
  31. if ($this->IsAccessToken($scope) === true) {
  32. return $_COOKIE['GOOGLE_ACCESS_TOKEN_' . md5($scope)];
  33. }
  34.  
  35. $params = sprintf("code=%s&client_id=%s&client_secret=%s&redirect_uri=%s&grant_type=authorization_code",
  36. $_GET['code'], $this->client_id, $this->client_secret, $this->redirect_uri
  37. );
  38.  
  39. $oCURL = curl_init();
  40. curl_setopt($oCURL, CURLOPT_URL, 'https://accounts.google.com/o/oauth2/token');
  41. curl_setopt($oCURL, CURLOPT_POST, 1);
  42. curl_setopt($oCURL, CURLOPT_POSTFIELDS, $params);
  43. curl_setopt($oCURL, CURLOPT_RETURNTRANSFER, 1);
  44. $response = json_decode(curl_exec($oCURL), true);
  45. curl_close($oCURL);
  46.  
  47. if (!isset($response['access_token'])) {
  48. return 0;
  49. }
  50.  
  51. setcookie('GOOGLE_ACCESS_TOKEN_' . md5($scope), $response['access_token'], time() + $response['expires_in'], '/', $_SERVER['SERVER_NAME']);
  52. return $response['access_token'];
  53. }
  54.  
  55. public function Get($scope = null)
  56. {
  57. $access_token = $this->GetAccessToken($scope);
  58. if (empty($access_token)) {
  59. return array();
  60. }
  61.  
  62. $oCURL = curl_init();
  63. curl_setopt($oCURL, CURLOPT_URL, $scope);
  64. curl_setopt($oCURL, CURLOPT_HTTPHEADER, array('Authorization: OAuth ' . $access_token));
  65. curl_setopt($oCURL, CURLOPT_RETURNTRANSFER, true);
  66. $response = curl_exec($oCURL);
  67. curl_close($oCURL);
  68.  
  69. return $response;
  70. }
  71.  
  72. public function IsAccessToken($scope = null)
  73. {
  74. if (isset($_COOKIE['GOOGLE_ACCESS_TOKEN_' . md5($scope)])) {
  75. return true;
  76. }
  77.  
  78. return false;
  79. }
  80. }


kod dołączany do index.php
  1. <?php
  2.  
  3.  
  4. include_once('Google_API.php');
  5.  
  6. // Tutaj ustawiamy przekierowanie powrotne, na które Google zwróci dane
  7. $oGoogle_API = new Google_API('https://mojastrona.pl/tasks/index.php');
  8.  
  9. if (!isset($_GET['code']) OR !isset($_GET['state'])) {
  10. $dialog = $oGoogle_API->Dialog('email');
  11. Header('Location: ' . $dialog); exit;
  12. }
  13.  
  14. $google_access_state = empty($_COOKIE['google_access_state']) ? 0 : $_COOKIE['google_access_state'];
  15. if ($_GET['state'] !== $google_access_state) {
  16. // tutaj np. przekierowanie, jeżeli dane są błędne
  17. }
  18.  
  19. $graph = $oGoogle_API->Get('https://www.googleapis.com/oauth2/v3/userinfo');
  20. $graph = json_decode($graph, true);
  21.  
  22. if (!isset($graph['email'])) {
  23. // tutaj np. przekierowanie, jeżeli nie udało się pobrać danych konta Google
  24. }
  25.  
  26. // Wyświetlamy adres e-mail użytkownika, który dokonał autoryzacji.
  27. # echo $graph['email'];
  28. ?>
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Wersja Lo-Fi Aktualny czas: 30.03.2024 - 00:50