Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
CLIでOAuth/OIDCを快適に利用する
Search
Hidetake Iwata
July 09, 2018
Technology
0
890
CLIでOAuth/OIDCを快適に利用する
Gopher Dojo #2
2018.07.09
Hidetake Iwata
July 09, 2018
Tweet
Share
More Decks by Hidetake Iwata
See All by Hidetake Iwata
Rewrite Go error handling using AST transformation
int128
1
1.3k
Cluster AutoscalerをTerraformとHelmfileでデプロイしてPrometheusでモニタリングする / Deploy the Cluster Autoscaler with Terraform and Helmfile, Monitor with Prometheus
int128
3
1.7k
認証の仕組みとclient-go credential plugin / authentication and client-go credential plugin
int128
7
7.5k
AppEngine × Spring Boot × Kotlin
int128
0
120
いつものJIRA設定
int128
1
190
Swaggerのテンプレートを魔改造した話 / Customize Swagger Templates
int128
1
4.8k
本番環境のリリースを自動化した話
int128
0
790
Swagger × Spring Cloud
int128
0
110
The Evolution of System Architecture
int128
0
180
Other Decks in Technology
See All in Technology
組織内、組織間の資産保護に必要なアイデンティティ基盤と関連技術の最新動向
fujie
0
500
地図と生成AI
nakasho
0
670
Bliki (ja), and the Cathedral, and the Bazaar
koic
8
1.3k
AI駆動開発 with MixLeap Study【大阪支部 #3】
lycorptech_jp
PRO
0
150
Jitera Company Deck / JP
jitera
0
140
名刺メーカーDevグループ 紹介資料
sansan33
PRO
0
840
なぜAI時代に 「イベント」を中心に考えるのか? / Why focus on "events" in the age of AI?
ytake
2
310
An introduction to Claude Code SDK
choplin
3
3.2k
OpenTelemetry の Log を使いこなそう
biwashi
4
960
OTel 公式ドキュメント翻訳 PJ から始めるコミュニティ活動/Community activities starting with the OTel official document translation project
msksgm
0
200
AI Ready API ─ AI時代に求められるAPI設計とは?/ AI-Ready API - Designing MCP and APIs in the AI Era
yokawasa
20
5.7k
Talk to Someone At Delta Airlines™️ USA Contact Numbers
travelcarecenter
0
170
Featured
See All Featured
Code Review Best Practice
trishagee
69
19k
Agile that works and the tools we love
rasmusluckow
329
21k
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
31
1.3k
Why Our Code Smells
bkeepers
PRO
337
57k
The Language of Interfaces
destraynor
158
25k
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
130
19k
Product Roadmaps are Hard
iamctodd
PRO
54
11k
Improving Core Web Vitals using Speculation Rules API
sergeychernyshev
18
1k
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
10
990
Building Better People: How to give real-time feedback that sticks.
wjessup
367
19k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
31
2.4k
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
234
17k
Transcript
CLIでOAuth/OIDCを 快適に利用する Hidetake Iwata (@int128)
TL;DR CLI(コマンドラインツール)でOAuthやOpenID Connectを利用する場合、 ユーザは複雑なオペレーションが必要になる 一時的にHTTPサーバを立ち上げて認可コードを受け取ることで、 ユーザのオペレーションを軽減できる
Hidetake Iwata https://github.com/int128 Software Engineer at Tokyo ❤ DevOps, Agile,
CI/CD, Kubernetes, App Engine, Golang, Gradle, Groovy
CLIでOAuthやOpenID Connectを利用する場合、ユーザは複雑なオペレーションが必 要になる 1. ターミナルでコマンドを実行する 2. ターミナルに表示されたURLをブラウザで開く 3. Googleアカウントでログインする 4.
ブラウザに表示された認可コードをコピーする 5. ターミナルに認可コードをペーストする 6. コマンドの実行結果を見る OAuth/OIDC CLIの課題 https://developers.google.com/identity/protocols/OAuth2#installed
OAuth/OIDC CLIの例 $ ./upload_google_drive Open https://accounts.google.com/… Enter code: XXXXXXXXXXX... OK.
You are logged in as int128. ① ② ③ ④ ⑤ ⑥
Package golang.org/x/oauth2 config := &oauth2.Config{ ClientID: clientID, ClientSecret: clientSecret, Endpoint:
google.Endpoint, RedirectURL: "urn:ietf:wg:oauth:2.0:oob", } authCodeURL := config.AuthCodeURL(state) fmt.Printf("Open %s\nEnter code: ", authCodeURL) var code string _, err := fmt.Scanln(&code) token, err := config.Exchange(ctx, code) ② ⑤
OAuth/OIDC CLIのUX改善 ユーザのオペレーションを以下のように軽減したい 1. ターミナルでコマンドを実行する 2. ターミナルに表示されたURLをブラウザで開く 3. Googleアカウントでログインする 4.
ブラウザに表示された認可コードをコピーする 5. ターミナルに認可コードをペーストする 6. コマンドの実行結果を見る
OAuth/OIDC CLIのUX改善 ユーザのオペレーションを以下のように軽減したい 1. ターミナルでコマンドを実行する 2. (外部コマンドで自動的にブラウザを開く) 3. Googleアカウントでログインする 4.
(ローカルHTTPサーバにリダイレクトされる) 5. (ローカルHTTPサーバが認可コードを受け取る) 6. コマンドの実行結果を見る
ブラウザベースの認可コードフロー [1] https://developers.google.com/identity/protocols/OAuth2#webserver 一般的なWebアプリケーションと同様にリダイレクト で認可コードを受け取る 1. HTTPサーバを実行する 2. ブラウザで認可要求URLを開く 3.
http://localhost へのリダイレクトで認可コード を受け取る 4. HTTPサーバを終了する 5. 認可コードとトークンを交換する
codeCh := make(chan string) errCh := make(chan error) server :=
http.Server{":8080", &AuthCodeGrantHandler{ Callback: func(code string, err error) { switch { case err != nil: errCh <- err default: codeCh <- code } } }} go func() { if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed { errCh <- err } }() select { case err := <-errCh: server.Shutdown(ctx) return "", err case code := <-codeCh: server.Shutdown(ctx) return code, nil } HTTPサーバが認可コードを受け取っ たらチャネルに送信 ゴルーチンでHTTPサーバを実行 認可コードを受け取ったらHTTPサー バをシャットダウン
See Also 特定URLへのアクセスを契機としたHTTPサーバのGraceful Shutdown https://int128.hatenablog.com/entry/2018/03/28/232810 gpup: Upload files to your
Google Photos with the new Photos Library API https://github.com/int128/gpup kubelogin: kubectl with OpenID Connect authentication https://github.com/int128/kubelogin
まとめ コマンドラインツールでOAuthやOpenID Connectを利用する場合、 ユーザは認可コードの貼り付けなどの複雑なオペレーションが必要になる 一時的にHTTPサーバを立ち上げてリダイレクトで認可コードを受け取ることで、ユーザ のオペレーションを軽減できる GoのHTTPサーバ、チャネルやゴルーチンを組み合わせた実装を紹介した