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
880
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
770
Swagger × Spring Cloud
int128
0
100
The Evolution of System Architecture
int128
0
180
Other Decks in Technology
See All in Technology
25分で解説する「最小権限の原則」を実現するための AWS「ポリシー」大全 / 20250625-aws-summit-aws-policy
opelab
8
810
「Chatwork」の認証基盤の移行とログ活用によるプロダクト改善
kubell_hr
1
100
フィンテック養成勉強会#54
finengine
0
120
本当に使える?AutoUpgrade の新機能を実践検証してみた
oracle4engineer
PRO
1
130
ObsidianをMCP連携させてみる
ttnyt8701
2
140
標準技術と独自システムで作る「つらくない」SaaS アカウント管理 / Effortless SaaS Account Management with Standard Technologies & Custom Systems
yuyatakeyama
2
1.1k
Prox Industries株式会社 会社紹介資料
proxindustries
0
210
AI技術トレンド勉強会 #1MCPの基礎と実務での応用
nisei_k
1
250
ハノーバーメッセ2025座談会.pdf
iotcomjpadmin
0
150
Observability infrastructure behind the trillion-messages scale Kafka platform
lycorptech_jp
PRO
0
130
LinkX_GitHubを基点にした_AI時代のプロジェクトマネジメント.pdf
iotcomjpadmin
0
160
米国国防総省のDevSecOpsライフサイクルをAWSのセキュリティサービスとOSSで実現
syoshie
2
830
Featured
See All Featured
Writing Fast Ruby
sferik
628
61k
A Modern Web Designer's Workflow
chriscoyier
693
190k
Unsuck your backbone
ammeep
671
58k
Testing 201, or: Great Expectations
jmmastey
42
7.5k
Docker and Python
trallard
44
3.4k
Principles of Awesome APIs and How to Build Them.
keavy
126
17k
Intergalactic Javascript Robots from Outer Space
tanoku
271
27k
Faster Mobile Websites
deanohume
307
31k
GraphQLとの向き合い方2022年版
quramy
46
14k
Optimizing for Happiness
mojombo
379
70k
Building a Modern Day E-commerce SEO Strategy
aleyda
41
7.3k
Balancing Empowerment & Direction
lara
1
350
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サーバ、チャネルやゴルーチンを組み合わせた実装を紹介した