iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
🤖

Explaining OAuth 2.0 Authorization through Zenn's Google Login for My Own Understanding

に公開
2021/01/31
2件

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:

  1. The Client tells the Consumer (Zenn), "I want to use OAuth" (for example, by clicking a button like Login with Google).

  2. The Consumer (Zenn) decides on scopes that represent how much user information is needed (how much permission is required). At the same time, it creates a random token called state. Then, it provides a URL to the Provider (Google) to the Client, with the scopes, state, and a client id representing who the Consumer is embedded in it.

  3. 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 the client id embedded 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 the scopes embedded 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."

  4. 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 the authorization code and state embedded in it.

  5. The Client accesses the Consumer (Zenn) using the provided URL. The Consumer (Zenn) first looks at the state token 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 the authorization code embedded in the URL along with its own client id to the Provider (Google) via a POST request. The Provider (Google) side confirms that the authorization code is indeed the one it issued previously. Finally, it returns an access token to the Consumer (Zenn). With this, the entire process is completed.
    As long as the Consumer (Zenn) has the access 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

https://flask-dance.readthedocs.io/en/latest/how-oauth-works.html
https://murashun.jp/article/programming/oauth2.html
https://qiita.com/sgmryk/items/0f5734a2a6991a6a1873

Discussion

ritouritou

ちょっと気になったんですが

  • 説明したいこと : OAuth 2.0での認可の流れ
  • 例 : Google でログイン
  • 認証と認可は違う

と、読者が混乱しそうな内容になっているように思えます。

アクセストークン取得までの流れを説明したい場合は、Googleログインの例を使ったとしても「この時点でアクセストークンを取得できます」「アプリケーションはユーザーの代わりにAPIアクセスできます」までで留めておくのが良さそうです。

その上で、認証用途に利用する場合は

  • アクセストークンからユーザー情報を引いてログインさせるユーザーを判別 : トークン管理によっては危険
  • GoogleはOpenID Connectにも対応しており、IDTokenという認証イベントの結果を用いてログインさせるユーザーを判別できる

という流れにすると、うまく機能を整理しながら説明できるかもしれません。

1
Kaito SugimotoKaito Sugimoto

読者が混乱しそうな内容になっているように思えます。

はい、おっしゃる通り、体系だった説明にはなっておりません。あくまで自分の理解のためにまとめたものです。
記事タイトルが啓蒙っぽい感じでミスリーディングだったので、変更しておきました。
(この記事を書いた時には存在に気づかなかったのですが、スクラップを使った方が良かったなぁと思っています。)