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
680
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.2k
Cluster AutoscalerをTerraformとHelmfileでデプロイしてPrometheusでモニタリングする / Deploy the Cluster Autoscaler with Terraform and Helmfile, Monitor with Prometheus
int128
3
1.6k
認証の仕組みとclient-go credential plugin / authentication and client-go credential plugin
int128
7
7.1k
AppEngine × Spring Boot × Kotlin
int128
0
79
いつものJIRA設定
int128
1
150
Swaggerのテンプレートを魔改造した話 / Customize Swagger Templates
int128
1
4.6k
本番環境のリリースを自動化した話
int128
0
670
Swagger × Spring Cloud
int128
0
80
The Evolution of System Architecture
int128
0
150
Other Decks in Technology
See All in Technology
Cloud Run と GitHub Template Repository による軽量なアプリケーションプラットフォーム/ #nikkei_tech_talk
nikkei_engineer_recruiting
0
110
可視化により内部品質をあげるAIドキュメントリバース/20240910 Hiromitsu Akiba
shift_evolve
0
220
あなたの知らないiOS開発の世界
recruitengineers
PRO
3
180
The XZ Backdoor Story
fr0gger
0
3.6k
タイミーのレコメンドにおける ABテストの運用
ozeshun
1
150
Swift Testingのconfirmationを コードリーディング/Dive into Swift Testing confirmation
laprasdrum
2
260
チームビルディングは"感性"で向き合おう / Team Building with Awareness
kohzas
0
260
忙しい人のためのLangGraph概要まとめ
__ymgc__
1
190
Google CloudのLLM活用の選択肢を広げるVertex AIのパートナーモデル
nayuts
0
130
Developer Experienceを向上させる基盤づくりの取り組み事例集
coconala_engineer
0
150
DroidKaigi 2024 たすけて!ViewModel
mhidaka
5
950
『GRANBLUE FANTASY Relink』ソフトウェアラスタライザによる実践的なオクルージョンカリング
cygames
0
170
Featured
See All Featured
Rebuilding a faster, lazier Slack
samanthasiow
78
8.6k
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
PRO
5
480
The Illustrated Children's Guide to Kubernetes
chrisshort
47
48k
Design by the Numbers
sachag
277
19k
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
28
8.9k
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
23
1.7k
Code Review Best Practice
trishagee
62
16k
Bootstrapping a Software Product
garrettdimon
PRO
304
110k
Unsuck your backbone
ammeep
667
57k
Producing Creativity
orderedlist
PRO
340
39k
Thoughts on Productivity
jonyablonski
66
4.2k
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
103
48k
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サーバ、チャネルやゴルーチンを組み合わせた実装を紹介した