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
800
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.3k
AppEngine × Spring Boot × Kotlin
int128
0
94
いつものJIRA設定
int128
1
170
Swaggerのテンプレートを魔改造した話 / Customize Swagger Templates
int128
1
4.7k
本番環境のリリースを自動化した話
int128
0
730
Swagger × Spring Cloud
int128
0
86
The Evolution of System Architecture
int128
0
160
Other Decks in Technology
See All in Technology
株式会社Awarefy(アウェアファイ)会社説明資料 / Awarefy-Company-Deck
awarefy
3
11k
【内製開発Summit 2025】イオンスマートテクノロジーの内製化組織の作り方/In-house-development-summit-AST
aeonpeople
2
600
What's new in Go 1.24?
ciarana
1
110
データエンジニアリング領域におけるDuckDBのユースケース
chanyou0311
8
2.1k
IAMポリシーのAllow/Denyについて、改めて理解する
smt7174
2
200
クラウドサービス事業者におけるOSS
tagomoris
4
1k
(機械学習システムでも) SLO から始める信頼性構築 - ゆる SRE#9 2025/02/21
daigo0927
0
260
AIエージェント元年
shukob
0
150
組織におけるCCoEの役割とAWS活用事例
nrinetcom
PRO
4
120
RayでPHPのデバッグをちょっと快適にする
muno92
PRO
0
190
実は強い 非ViTな画像認識モデル
tattaka
2
1.2k
アジャイルな開発チームでテスト戦略の話は誰がする? / Who Talks About Test Strategy?
ak1210
1
470
Featured
See All Featured
What's in a price? How to price your products and services
michaelherold
244
12k
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
47
5.2k
BBQ
matthewcrist
87
9.5k
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
30
4.6k
Keith and Marios Guide to Fast Websites
keithpitt
411
22k
The Language of Interfaces
destraynor
156
24k
Designing for humans not robots
tammielis
250
25k
10 Git Anti Patterns You Should be Aware of
lemiorhan
PRO
656
59k
How GitHub (no longer) Works
holman
314
140k
Gamification - CAS2011
davidbonilla
80
5.2k
The Cult of Friendly URLs
andyhume
78
6.2k
The Straight Up "How To Draw Better" Workshop
denniskardys
232
140k
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サーバ、チャネルやゴルーチンを組み合わせた実装を紹介した