Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Twitter OAuth2.0 Beta

kg0r0
October 27, 2021

Twitter OAuth2.0 Beta

iddance Lesson3 TwitterのOAuth 2.0とかCIBAの使い所を知ろうの会の資料です。
https://idance.connpass.com/event/226073/

kg0r0

October 27, 2021
Tweet

More Decks by kg0r0

Other Decks in Technology

Transcript

  1. Twitter OAuth2.0 Beta
    iddance Lesson3 #iddance

    2021年10⽉時点の実装をもとに説明しています。
    実装は今後変わることが想定されるため適宜最新の情報を参照ください。 1

    View Slide

  2. もくじ
    ● はじめに
    ● OAuth2.0おさらい
    ● Twitter OAuth2.0 Beta解説
    ● Twiiter OAuth2.0 Betaのココ
    が気になる
    ● おわりに
    2

    View Slide

  3. はじめに
    3

    View Slide

  4. ⾃⼰紹介
    ● 合路 健⼈ (@kg0r0)
    ● アクセス制御関連の技術を中⼼に⾊々やっ
    てます
    ● 技術同⼈サークル「Secure旅団 (第陸拾
    参連隊)」で不定期活動
    https://techbookfest.org/organization/
    5667545562480640
    4

    View Slide

  5. Twitter OAuth2.0 Beta
    ● 2021年9⽉にTwitterがOAuth2.0 Beta
    をリリース
    ※これまではOAuth1.0とOAuth2.0
    Client Credentials Grantに対応
    ● TwitterのDeveloper Accountをもって
    いればBetaプログラムへの応募が可能
    出所) https://twitter.com/TwitterDev/status/1436020870875656196
    5

    View Slide

  6. OAuth2.0 Client ID
    ● Developer Portalに
    OAuth2.0のClient IDが
    追加される
    ● リダイレクトURIの登録
    はOAuth1.0と共通
    6

    View Slide

  7. OAuth2.0おさらい
    7

    View Slide

  8. OAuth2.0
    ● サードパーティーアプリケーショ
    ンによるHTTPサービスへの限定的
    なアクセスを可能にする認可フレ
    ームワーク
    ● RFC 5849に記載されている
    OAuth 1.0 プロトコルを廃⽌し,
    その代替となるもの
    出所) https://openid-foundation-japan.github.io/rfc6749.ja.html
    8

    View Slide

  9. OAuth2.0 Client Type
    ● Confidential Client
    クライアントシークレットの機密性を維持することができるクライアント
    (Webアプリケーションなど)
    ● Public Client
    クライアントシークレットの機密性を維持することができないクライアント
    (JavaScriptアプリやNativeアプリなど)
    9

    View Slide

  10. OAuth2.0 Grant Type
    ● Authorization Code Grant
    ※RFC 7636 PKCEによりPublic Clientでも安全に利⽤可能
    ● Implicit Grant
    ● Client Credentials Grant
    ● Resource Owner Password Credentials Grant
    ※ Proof Key for Code Exchange by OAuth Public Clients
    https://datatracker.ietf.org/doc/html/rfc7636
    10

    View Slide

  11. OAuth2.0 Grant Type
    ● Authorization Code Grant
    ※RFC 7636 PKCEによりPublic Clientでも安全に利⽤可能
    ● Implicit Grant
    ● Client Credentials Grant
    ● Resource Owner Password Credentials Grant
    サポートされた
    ※元々サポート
    ※ Proof Key for Code Exchange by OAuth Public Clients
    https://datatracker.ietf.org/doc/html/rfc7636
    11

    View Slide

  12. Twitter OAuth2.0 Beta解説
    12

    View Slide

  13. Authorization Code Flow with PKCE
    [1] Init
    Resource Owner Client Authorization Server Resource Server
    [2] Redirect to Authorization Server
    [3]Authorization Request
    (code_challenge,code_challenge_method)
    [5] Redirect back to Client
    [6] Authorization Response
    w/ Authorization Code
    [7] Token Request
    (code_verifier)
    [8] Token Response
    [9] API Request
    [10] API Response
    [4] End-User Authentication
    & Consent
    13

    View Slide

  14. Authorization Code Flow with PKCE
    [1] Init
    Resource Owner Client Authorization Server Resource Server
    [2] Redirect to Authorization Server
    [3]Authorization Request
    (code_challenge,code_challenge_method)
    [5] Redirect back to Client
    [6] Authorization Response
    w/ Authorization Code
    [7] Token Request
    (code_verifier)
    [8] Token Response
    [9] API Request
    [10] API Response
    [4] End-User Authentication
    & Consent
    14

    View Slide

  15. Authorization Request
    GET /i/api/2/oauth2/authorize?
    response_type=code
    &client_id=
    &redirect_uri=https://www.example.com
    &scope=tweet.read%20users.read%20offline.access
    &state=abc
    &code_challenge=E9Melhoa2OwvFrEMTJguCHaoeK1t8URWbuGJSstw-cM
    &code_challenge_method=S256 HTTP/2
    Host: twitter.com
    15
    サンプル

    View Slide

  16. End-User Authentication & Consent
    Twitter OAuth1.0 Twitter OAuth2.0 16

    View Slide

  17. Authorization Code Flow with PKCE
    [1] Init
    Resource Owner Client Authorization Server Resource Server
    [2] Redirect to Authorization Server
    [3]Authorization Request
    (code_challenge,code_challenge_method)
    [5] Redirect back to Client
    [6] Authorization Response
    w/ Authorization Code
    [7] Token Request
    (code_verifier)
    [8] Token Response
    [9] API Request
    [10] API Response
    [4] End-User Authentication
    & Consent
    17

    View Slide

  18. Authorization Response
    GET /?
    state=abc
    &code= HTTP/1.1
    Host: www.example.com
    ※ヘッダは⼀部省略
    18
    サンプル

    View Slide

  19. Authorization Code Flow with PKCE
    [1] Init
    Resource Owner Client Authorization Server Resource Server
    [2] Redirect to Authorization Server
    [3]Authorization Request
    (code_challenge,code_challenge_method)
    [5] Redirect back to Client
    [6] Authorization Response
    w/ Authorization Code
    [7] Token Request
    (code_verifier)
    [8] Token Response
    [9] API Request
    [10] API Response
    [4] End-User Authentication
    & Consent
    19

    View Slide

  20. Token Request
    POST /2/oauth2/token HTTP/2
    Host: api.twitter.com
    Content-Type: application/x-www-form-urlencoded
    code=
    &grant_type=authorization_code
    &client_id=
    &redirect_uri=https://www.example.com
    &code_verifier=dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk
    20
    サンプル

    View Slide

  21. Authorization Code Flow with PKCE
    [1] Init
    Resource Owner Client Authorization Server Resource Server
    [2] Redirect to Authorization Server
    [3]Authorization Request
    (code_challenge,code_challenge_method)
    [5] Redirect back to Client
    [6] Authorization Response
    w/ Authorization Code
    [7] Token Request
    (code_verifier)
    [8] Token Response
    [9] API Request
    [10] API Response
    [4] End-User Authentication
    & Consent
    21

    View Slide

  22. Token Response
    HTTP/2 200 OK
    {
    "token_type": "bearer",
    "expires_in": 7200,
    "access_token": "",
    "scope": "offline.access users.read tweet.read",
    "refresh_token": "”
    }
    ※レスポンスヘッダは⼀部省略
    22
    サンプル

    View Slide

  23. Authorization Code Flow with PKCE
    [1] Init
    Resource Owner Client Authorization Server Resource Server
    [2] Redirect to Authorization Server
    [3]Authorization Request
    (code_challenge,code_challenge_method)
    [5] Redirect back to Client
    [6] Authorization Response
    w/ Authorization Code
    [7] Token Request
    (code_verifier)
    [8] Token Response
    [9] API Request
    [10] API Response
    [4] End-User Authentication
    & Consent
    23

    View Slide

  24. API Request
    GET /2/users/:id HTTP/2
    Host: api.twitter.com
    Authorization: Bearer
    24
    サンプル

    View Slide

  25. Refresh Token - Request
    POST /2/oauth2/token HTTP/2
    Host: api.twitter.com
    Content-Type: application/x-www-form-urlencoded
    refresh_token=
    &grant_type=refresh_token
    &client_id=
    25
    サンプル

    View Slide

  26. Refresh Token - Response
    HTTP/2 200 OK
    {
    "token_type":"bearer",
    "expires_in":7200,
    "access_token":”",
    "scope":"offline.access users.read tweet.read",
    "refresh_token":””
    }
    26
    サンプル
    ※レスポンスヘッダは⼀部省略

    View Slide

  27. Revoke Token - Request
    POST /2/oauth2/revoke HTTP/2
    Host: api.twitter.com
    Content-Type: application/x-www-form-urlencoded
    token=
    &token_type_hint=access_token
    &client_id=
    27
    サンプル

    View Slide

  28. Revoke Token - Response
    HTTP/2 200 OK
    Content-Type: application/json; charset=utf-8
    {
    "revoked":true
    }
    28
    サンプル
    ※レスポンスヘッダは⼀部省略

    View Slide

  29. Twitter OAuth2.0 Beta
    ココが気になる
    29

    View Slide

  30. ココが気になる
    ● OAuth1.0実装との違い
    ● Twitter OAuth2.0 Beta実装注意点
    ● OAuth1.0からOAuth2.0への移⾏
    ● 今後対応して欲しい機能
    30

    View Slide

  31. ココが気になる
    ● OAuth1.0実装との違い
    ● Twitter OAuth2.0 Beta実装注意点
    ● OAuth1.0からOAuth2.0への移⾏
    ● 今後対応して欲しい機能
    31

    View Slide

  32. OAuth Core 1.0 Revision A
    ● Security Considerationsの中
    でも実装依存で不備を作り込
    みやすい項⽬に注⽬
    ○ Secrecy of the Client Credentials
    ○ Scoping of Access Requests
    ○ Cross-Site Request Forgery
    (CSRF)
    出所) https://openid-foundation-japan.github.io/rfc5849.ja.html
    32

    View Slide

  33. Secrecy of the Client Credentials
    ● クライアントクレデンシャルの検
    証について (OAuth2.0のPublic
    Client相当に関係する事項)
    ● OAuth2.0 Security Best Current
    PracticeやRFC 8252 OAuth2.0
    for Native Appsなどに準拠するこ
    とでリスクを低減することが可能
    33
    出所) https://openid-foundation-japan.github.io/rfc5849.ja.html

    View Slide

  34. Scoping of Access Requests (1/3)
    ● OAuth1.0ではクライアントに許
    可する権限のスコープを定める⽅
    法が提供されておらず実装依存
    ● OAuth2.0ではscopeパラメーター
    を⽤いて要求するアクセス範囲を
    明⽰
    34
    出所) https://openid-foundation-japan.github.io/rfc5849.ja.html

    View Slide

  35. Scoping of Access Requests (2/3)
    Twitter OAuth1.0 Twitter OAuth2.0 35
    従来よりも細かく権限が指定できそう

    View Slide

  36. Scoping of Access Requests (3/3)
    36
    ● 各Endpointと要求するスコープには
    以下のような特徴がある
    ○ 1つのEndpointに対して複数の
    scopeを組み合わせるケースが
    多い
    ○ Endpointによっては要求する
    scopeが共通している
    出所) https://developer.twitter.com/en/docs/twitter-api/oauth2

    View Slide

  37. Cross-Site Request Forgery (CSRF)
    ● CSRF 攻撃の防御策はOAuth1.0仕様の
    範囲外
    ● Twitter OAuth2.0 BetaではPKCEの利
    ⽤が必須(stateパラメーターにも対応)
    ● Clientが脆弱なcode_verifierや⾮推奨の
    code_challenge_methodを利⽤しなけ
    れば対策可能
    出所) https://oauth.jp/blog/2014/06/23/csrf-on-twitter-login/
    37

    View Slide

  38. ココが気になる
    ● OAuth1.0実装との違い
    ● Twitter OAuth2.0 Beta実装注意点
    ● OAuth1.0からOAuth2.0への移⾏
    ● 今後対応して欲しい機能
    38

    View Slide

  39. Twitter OAuth2.0 Beta実装注意点 (1/2)
    2021年10⽉時点でClient Typeの指定が無い (すべてPublic Client相当)
    39
    Google OAuth2.0
    (アプリケーションの種類が指定可能)
    Twitter OAuth2.0

    View Slide

  40. Twitter OAuth2.0 Beta実装注意点 (2/2)
    クライアント認証が無いことにより実装不備などのリスクが⼤きくなるケースがある
    例)
    ● Refresh Tokenが漏洩した場合
    Refresh Tokenが漏洩した際にクライアント認証無しでAccess Tokenが更新できてしまうため
    ⻑期間アクセス権限を侵害される可能性がある
    ※Twitter OAuth2.0ではトークンを更新した場合、旧トークンは利⽤できなかった
    ● redirect_uriの設定に不備があった場合
    攻撃者が⽤意したAuthorization Requestを被害者に踏ませることで攻撃者がそのままAccess
    Tokenまで取得できてしまう可能性がある
    40
    ※2021年10⽉時点の実装をもとに説明

    View Slide

  41. ココが気になる
    ● OAuth1.0実装との違い
    ● Twitter OAuth2.0 Beta実装注意点
    ● OAuth1.0からOAuth2.0への移⾏
    ● 今後対応して欲しい機能
    41

    View Slide

  42. OAuth1.0からOAuth2.0への移⾏
    ● 現状TwitterのOAuth1.0は広く利⽤さ
    れており、サポート終了した場合の影
    響がかなり⼤きいと想定
    ● 過去にOAuth1.0のサポートを終了し
    た事例はある
    ● しばらくはTwitterの動向をウォッチし
    つつ来たるべきときに備える感じ︖
    42
    出所) https://developer.yahoo.co.jp/changelog/2019-11-27-yconnect.html

    View Slide

  43. ココが気になる
    ● OAuth1.0実装との違い
    ● Twitter OAuth2.0 Beta実装注意点
    ● OAuth1.0からOAuth2.0への移⾏
    ● 今後対応して欲しい機能
    43

    View Slide

  44. 今後対応して欲しい機能 ※個⼈的
    ● Client Typeの指定(Confidential Clientのサポート)
    ● OpenID Connect(従来のTwitterログインの代替)
    ● その他OpenID Connect Discoveryなど拡張機能
    ※フィードバックしたら諸々対応していきたいとの返信があったので期待し
    ても良いかも︖
    44

    View Slide

  45. おわりに
    45

    View Slide

  46. まとめ
    ● Twitterが待望のOAuth2.0に対応
    ● 現状はPublic Clientを想定した利⽤のみ
    ● 今後まだまだ改善されていくそうです
    ● いつかは来る移⾏タイミングに備えてウォッチしておいた⽅が良いかも
    46

    View Slide

  47. おまけ
    ⼀応⾊々つくって試してみました (不具合などあればIssueやPRもらえると嬉しいです)
    ● Twitter OAuth2.0 Beta Authorization Code Grant w/ PKCE
    https://github.com/kg0r0/twitter-oauth2-client
    ● Twitter OAuth2.0 Client Credentials Grant
    https://github.com/kg0r0/twitter-client-credentials
    ● Twitter OAuth1.0
    https://github.com/kg0r0/twitter-login-example
    47

    View Slide

  48. 参考
    • Twitter Developer Platform – OAuth2.0
    https://developer.twitter.com/en/docs/twitter-api/oauth2
    • Developer Platform - Application-only authentication and OAuth 2.0 Bearer Token
    https://developer.twitter.com/en/docs/authentication/oauth-2-0/application-only
    • The OAuth 1.0 Protocol
    https://openid-foundation-japan.github.io/rfc5849.ja.html
    • The OAuth 2.0 Authorization Framework
    https://openid-foundation-japan.github.io/rfc6749.ja.html
    • Proof Key for Code Exchange by OAuth Public Clients
    https://datatracker.ietf.org/doc/html/rfc7636
    • TwitterのOAuth2.0 Betaためしてみた!
    https://zenn.dev/kg0r0/articles/8d787860e9b2e1
    48

    View Slide