1

網頁版 Google OAuth 的作法

 1 year ago
source link: https://blog.gslin.org/archives/2023/04/24/11152/%e7%b6%b2%e9%a0%81%e7%89%88-google-oauth-%e7%9a%84%e4%bd%9c%e6%b3%95/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

網頁版 Google OAuth 的作法

早期的作法是「Integrating Google Sign-In into your web app」這個,但官方已經標注 deprecated 了,在官方的文件上面可以看到對應的警告說明,現在雖然會動,但之後應該會關掉:

Warning: The support of Google Sign-In JavaScript platform library for Web is set to be deprecated after March 31, 2023. The solutions in this guide are based on this library and therefore also deprecated.

本來一開始是走「OAuth 2.0 for Client-side Web Applications」這個,這份文件會教你引入 Google 提供的 javascript library,也就是 https://apis.google.com/js/api.js 這份,但其實沒必要這樣用... 先不管會多吃多少資源,比較敏感的是隱私問題 (像是透過 3rd-party cookie 追蹤)。

後來研究出來比較好的方法是走「Using OAuth 2.0 for Web Server Applications」這邊提到的方式,就算是 javascript 也可以建立出來,像是這樣:

(() => {
  const el = document.getElementById('glogin');
  el.addEventListener('click', () => {
    document.location = 'https://accounts.google.com/o/oauth2/v2/auth?' +
      'client_id=x-x.apps.googleusercontent.com' +
      '&redirect_uri=https://test.example.com/google_redirect_uri.php' +
      '&response_type=code' +
      '&scope=profile+email';
  });
})();

然後這邊的接收端 (google_redirect_uri.php) 是讓 Google 授權後用 redirect 的方式把對應的認證變數 code 帶過來,這邊是用 PHP 實做,有兩個步驟:

  • 先用 POST 打 https://oauth2.googleapis.com/token 取得 (換得) access token,也就是 access_token
  • 再用 GET 打 https://www.googleapis.com/oauth2/v3/userinfo (帶上一個取得的 access token 進去) 取得使用者資料。

scope 裡如果沒有 email 的話,最後就不會拿到 email,但 SSO 應用應該會需要這個資訊,所以範例裡面還是放進去...

這邊是故意儘量用 PHP 內建的 library 實做,但應該不算複雜,換用 Guzzle 或是用其他語言應該算簡單:

    $qs = http_build_query([
        'code' => $_GET['code'],
        'client_id' => 'x-x.apps.googleusercontent.com',
        'client_secret' => 'x',
        'redirect_uri' => 'https://test.example.com/google_redirect_uri.php',
        'grant_type' => 'authorization_code',
    ]);

    $url = 'https://oauth2.googleapis.com/token';
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $qs);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $res = curl_exec($ch);
    curl_close($ch);

    $data = json_decode($res);

    $atoken = $data->access_token;

    $url = 'https://www.googleapis.com/oauth2/v3/userinfo';
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_HTTPHEADER, ["Authorization: Bearer ${atoken}"]);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $res = curl_exec($ch);
    curl_close($ch);

最後的資料就在 $res 裡面,想要看可以直接用 var_dump($res); 看。

這樣只有使用者真的要用 Google SSO 時才會有 request 進到 Google 伺服器。

Related

AWS 提供 Hybrid Cloud 環境下 DNS 管理的說明

不知道為什麼出現在 browser tab 上,不知道是哪邊看到的... AWS 放出了一份文件,在講 hybrid cloud 環境下當你同時有一般 IDC 機房,而且使用內部 domain 在管理時,網路與 AWS 打通後要怎麼解決 DNS resolver 的問題:「Hybrid Cloud DNS Solutions for Amazon VPC」。 有些東西在官方的說明文件內都寫過,但是是 AWS 的特殊設計,這邊就會重複說明 XDDD 像是這份文件裡提到 Amazon DNS Server 一定會在 VPC 的 base 位置加二 (舉例來說,10.0.0.0/16 的 VPC,Amazon DNS Server 會在 10.0.0.2): Amazon DNS Server The Amazon DNS…

November 19, 2017

In "AWS"

用 JavaScriptCore 實做的一站式方案 Bun

前幾天在 Hacker News 上討論得很熱烈的 Bun:「Bun: Fast JavaScript runtime, transpiler, and NPM client written in Zig (bun.sh)」。 從 Hacker News 上的標題上就可以看到 Bun 做了不少事情,看起來想要打造一個 all-in-one 環境,把所有開發與 server 端 JavaScript 所需要的東西就一次包進來,不需要在自己東挑西挑... 比較特別的是 Bun 在選 JavaScript Engine 的時候是選擇 Apple 家推出的 JSC (或者稱 JSCore,正式名稱是 JavaScriptCore),而不是現在主流的 V8 (Google 家),據說這樣比較省記憶體,但 server 端應用應該是不缺這個記憶體才對? JavaScript runtime with Web APIs…

July 11, 2022

In "Computer"

OAuth 2.0 Device Authorization Grant

看到「OAuth 2.0 Device Authorization Grant」這個變成 PROPOSED STANDARD 了,看了一下歷史是 2015 年年底的時候被提出來的,記得在前公司的時候有用這個 (當時還是 draft) 做智慧型電視上的 OAuth 認證: The OAuth 2.0 device authorization grant is designed for Internet-connected devices that either lack a browser to perform a user-agent-based authorization or are input constrained to the extent that requiring the user to input text…

August 19, 2019

In "Computer"

a611ee8db44c8d03a20edf0bf5a71d80?s=49&d=identicon&r=gAuthor Gea-Suan LinPosted on April 24, 2023April 24, 2023Categories Computer, Murmuring, Network, Programming, ServiceTags app, application, client, google, html, in, javascript, js, oauth, server, sign, signin, sso, web

Leave a Reply

Your email address will not be published. Required fields are marked *

Comment *

Name *

Email *

Website

Notify me of follow-up comments by email.

Notify me of new posts by email.

To respond on your own website, enter the URL of your response which should contain a link to this post's permalink URL. Your response will then appear (possibly after moderation) on this page. Want to update or remove your response? Update or delete your post and re-enter your post's URL again. (Learn More)

Post navigation


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK