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
900
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
190
Other Decks in Technology
See All in Technology
OPENLOGI Company Profile for engineer
hr01
1
38k
Claude CodeでKiroの仕様駆動開発を実現させるには...
gotalab555
3
1.1k
React Server ComponentsでAPI不要の開発体験
polidog
PRO
0
260
Lambda management with ecspresso and Terraform
ijin
2
160
AI時代の経営、Bet AI Vision #BetAIDay
layerx
PRO
1
2k
AIのグローバルトレンド 2025 / ai global trend 2025
kyonmm
PRO
1
140
2025新卒研修・HTML/CSS #弁護士ドットコム
bengo4com
3
13k
Infrastructure as Prompt実装記 〜Bedrock AgentCoreで作る自然言語インフラエージェント〜
yusukeshimizu
1
120
Instant Apps Eulogy
cyrilmottier
1
110
リリース2ヶ月で収益化した話
kent_code3
1
290
生成AI導入の効果を最大化する データ活用戦略
ham0215
0
160
AI時代の大規模データ活用とセキュリティ戦略
ken5scal
0
130
Featured
See All Featured
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
110
19k
A Modern Web Designer's Workflow
chriscoyier
695
190k
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
34
3.1k
What’s in a name? Adding method to the madness
productmarketing
PRO
23
3.6k
How to Ace a Technical Interview
jacobian
278
23k
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
29
2.8k
The Language of Interfaces
destraynor
158
25k
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
26
3k
A better future with KSS
kneath
239
17k
4 Signs Your Business is Dying
shpigford
184
22k
Dealing with People You Can't Stand - Big Design 2015
cassininazir
367
26k
Building Adaptive Systems
keathley
43
2.7k
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サーバ、チャネルやゴルーチンを組み合わせた実装を紹介した