|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "flag" |
| 6 | + "fmt" |
| 7 | + "log" |
| 8 | + "os" |
| 9 | + "strings" |
| 10 | + |
| 11 | + "github.com/int128/oauth2cli" |
| 12 | + "github.com/int128/oauth2cli/oauth2params" |
| 13 | + "github.com/pkg/browser" |
| 14 | + "golang.org/x/oauth2" |
| 15 | + "golang.org/x/oauth2/google" |
| 16 | + "golang.org/x/sync/errgroup" |
| 17 | + |
| 18 | + "github.com/int128/kubelogin/pkg/jwt" |
| 19 | +) |
| 20 | + |
| 21 | +func init() { |
| 22 | + log.SetFlags(log.Lshortfile | log.Lmicroseconds) |
| 23 | +} |
| 24 | + |
| 25 | +type cmdOptions struct { |
| 26 | + authURL string |
| 27 | + tokenURL string |
| 28 | + clientID string |
| 29 | + clientSecret string |
| 30 | + scopes string |
| 31 | + localServerPort string |
| 32 | + debug bool |
| 33 | +} |
| 34 | + |
| 35 | +func main() { |
| 36 | + var o cmdOptions |
| 37 | + flag.StringVar(&o.authURL, "auth-url", google.Endpoint.AuthURL, "Authorization URL of the endpoint") |
| 38 | + flag.StringVar(&o.tokenURL, "token-url", google.Endpoint.TokenURL, "Authorization URL of the endpoint") |
| 39 | + flag.StringVar(&o.clientID, "client-id", "", "OAuth Client ID") |
| 40 | + flag.StringVar(&o.clientSecret, "client-secret", "", "OAuth Client Secret (optional)") |
| 41 | + flag.StringVar(&o.scopes, "scopes", "email", "Scopes to request, comma separated") |
| 42 | + flag.StringVar(&o.localServerPort, "local-server-port", "0", "Port number of the local server (optional)") |
| 43 | + flag.BoolVar(&o.debug, "debug", false, "Enable debug logging") |
| 44 | + flag.Parse() |
| 45 | + |
| 46 | + log.SetOutput(os.Stderr) |
| 47 | + |
| 48 | + if o.clientID == "" { |
| 49 | + log.Printf(`You need to set oauth2 credentials. |
| 50 | +Open https://console.cloud.google.com/apis/credentials and create a client. |
| 51 | +Then set the following options:`) |
| 52 | + flag.PrintDefaults() |
| 53 | + os.Exit(1) |
| 54 | + return |
| 55 | + } |
| 56 | + |
| 57 | + pkce, err := oauth2params.NewPKCE() |
| 58 | + if err != nil { |
| 59 | + log.Fatalf("error: %s", err) |
| 60 | + } |
| 61 | + ready := make(chan string, 1) |
| 62 | + defer close(ready) |
| 63 | + cfg := oauth2cli.Config{ |
| 64 | + OAuth2Config: oauth2.Config{ |
| 65 | + ClientID: o.clientID, |
| 66 | + ClientSecret: o.clientSecret, |
| 67 | + Endpoint: oauth2.Endpoint{ |
| 68 | + AuthURL: o.authURL, |
| 69 | + TokenURL: o.tokenURL, |
| 70 | + }, |
| 71 | + Scopes: strings.Split(o.scopes, ","), |
| 72 | + }, |
| 73 | + AuthCodeOptions: pkce.AuthCodeOptions(), |
| 74 | + TokenRequestOptions: pkce.TokenRequestOptions(), |
| 75 | + LocalServerReadyChan: ready, |
| 76 | + LocalServerBindAddress: []string{ |
| 77 | + fmt.Sprintf("127.0.0.1:%s", o.localServerPort), |
| 78 | + "127.0.0.1:0", |
| 79 | + }, |
| 80 | + } |
| 81 | + if o.debug { |
| 82 | + cfg.Logf = log.Printf |
| 83 | + } |
| 84 | + |
| 85 | + ctx := context.Background() |
| 86 | + eg, ctx := errgroup.WithContext(ctx) |
| 87 | + eg.Go(func() error { |
| 88 | + select { |
| 89 | + case url := <-ready: |
| 90 | + if err := browser.OpenURL(url); err != nil { |
| 91 | + log.Printf("could not open the browser: %s", err) |
| 92 | + } |
| 93 | + return nil |
| 94 | + case <-ctx.Done(): |
| 95 | + return fmt.Errorf("context done while waiting for authorization: %w", ctx.Err()) |
| 96 | + } |
| 97 | + }) |
| 98 | + eg.Go(func() error { |
| 99 | + token, err := oauth2cli.GetToken(ctx, cfg) |
| 100 | + if err != nil { |
| 101 | + return fmt.Errorf("could not get a token: %w", err) |
| 102 | + } |
| 103 | + claims, err := jwt.DecodeWithoutVerify(token.Extra("id_token").(string)) |
| 104 | + if err != nil { |
| 105 | + return fmt.Errorf("could not decode the token: %w", err) |
| 106 | + } |
| 107 | + fmt.Printf("%s\n", claims.Pretty) |
| 108 | + return nil |
| 109 | + }) |
| 110 | + if err := eg.Wait(); err != nil { |
| 111 | + log.Fatalf("authorization error: %s", err) |
| 112 | + } |
| 113 | +} |
0 commit comments