iTranslated by AI
Explaining OAuth 2.0 Authorization through Zenn's Google Login for My Own Understanding
Characters
Provider
A website that holds information about users, such as Google or Twitter.
Consumer
A site that does various things using information from the Provider.
When you want to create a website using OAuth authentication/authorization, the site you create becomes the Consumer. Since zenn.dev supports Google login, it is a Consumer.
User
A person who has already registered their information with the Provider and is about to start using the Consumer's site.
Client
The program on the User's side of the operation.
OAuth 2.0
The steps are as follows:
-
The Client tells the Consumer (Zenn), "I want to use OAuth" (for example, by clicking a button like Login with Google).
-
The Consumer (Zenn) decides on
scopesthat represent how much user information is needed (how much permission is required). At the same time, it creates a random token calledstate. Then, it provides a URL to the Provider (Google) to the Client, with thescopes,state, and aclient idrepresenting who the Consumer is embedded in it. -
The Client accesses the Provider (Google) using the provided URL (for example, the URL of the Google screen that appears after clicking Login with Google is the URL provided by the Consumer in the previous step).
By looking at theclient idembedded in this URL, the Provider (Google) identifies the Consumer (Zenn). When creating an app using OAuth, the Consumer (Zenn) side registers with the Provider (Google) in advance to use OAuth authentication, so it can be identified (for example, in the case of Google, you register at the Google Developer Console).
Also, by looking at thescopesembedded in the URL, it confirms what kind of permissions the Consumer (Zenn) is requesting. Based on that, the Provider (Google) displays a confirmation screen saying, "This site called Consumer (Zenn) is requesting the following permissions." -
Once the User agrees by logging in or clicking the OK button, the Provider (Google) issues an
authorization code. Then, it provides a URL to the Consumer (Zenn) to the Client, with theauthorization codeandstateembedded in it. -
The Client accesses the Consumer (Zenn) using the provided URL. The Consumer (Zenn) first looks at the
statetoken embedded in this URL to confirm if it matches the one it issued previously. This prevents a malicious third party from impersonating the original User.
Then, the Consumer (Zenn) sends theauthorization codeembedded in the URL along with its ownclient idto the Provider (Google) via a POST request. The Provider (Google) side confirms that theauthorization codeis indeed the one it issued previously. Finally, it returns anaccess tokento the Consumer (Zenn). With this, the entire process is completed.
As long as the Consumer (Zenn) has theaccess token, it can determine that the User is "logged in" and process returning the page after login.
Summary of Terms
-
client id: Used by the Consumer (Zenn) to tell the Provider (Google) who they are -
scope: Something the Consumer (Zenn) sets by considering how much user information is needed for their app -
state: Prepared by the Consumer (Zenn) to prevent account hijacking by malicious users (CSRF attacks) -
authorization code: Issued by the Provider (Google) to confirm the legitimacy of the Consumer (Zenn) -
access token: Used by the Consumer (Zenn) to confirm that the user has been authorized (issued by the Provider (Google))
Difference Between Authentication and Authorization
Authentication is confirming "who someone is," while authorization is granting permissions.
Logging in with OAuth means that the authentication of a user is also performed based on the existence of that user's authorized access token. For this reason, it is sometimes referred to collectively as authentication/authorization.
However, since OAuth itself was originally a protocol created for authorization, using it for authentication as well is potentially dangerous. In fact, if a malicious site leaks a user's access token, it would mean that the user could be authenticated as themselves on other sites that use that OAuth for authentication.
Reference
Discussion
ちょっと気になったんですが
と、読者が混乱しそうな内容になっているように思えます。
アクセストークン取得までの流れを説明したい場合は、Googleログインの例を使ったとしても「この時点でアクセストークンを取得できます」「アプリケーションはユーザーの代わりにAPIアクセスできます」までで留めておくのが良さそうです。
その上で、認証用途に利用する場合は
という流れにすると、うまく機能を整理しながら説明できるかもしれません。
はい、おっしゃる通り、体系だった説明にはなっておりません。あくまで自分の理解のためにまとめたものです。
記事タイトルが啓蒙っぽい感じでミスリーディングだったので、変更しておきました。
(この記事を書いた時には存在に気づかなかったのですが、スクラップを使った方が良かったなぁと思っています。)