|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "flag" |
| 7 | + "fmt" |
| 8 | + "io" |
| 9 | + "io/ioutil" |
| 10 | + "log" |
| 11 | + "net/url" |
| 12 | + "os" |
| 13 | + "os/user" |
| 14 | + "path/filepath" |
| 15 | + |
| 16 | + "github.com/sirupsen/logrus" |
| 17 | + "golang.org/x/oauth2" |
| 18 | + "golang.org/x/oauth2/google" |
| 19 | +) |
| 20 | + |
| 21 | +// Exit codes are int values that represent an exit code for a particular error. |
| 22 | +const ( |
| 23 | + ExitCodeOK int = 0 |
| 24 | + ExitCodeError int = 1 + iota |
| 25 | +) |
| 26 | + |
| 27 | +// CLI is the command line object |
| 28 | +type CLI struct { |
| 29 | + // outStream and errStream are the stdout and stderr |
| 30 | + // to write message from the CLI. |
| 31 | + outStream, errStream io.Writer |
| 32 | +} |
| 33 | + |
| 34 | +// Run invokes the CLI with the given arguments. |
| 35 | +func (cli *CLI) Run(args []string) int { |
| 36 | + var ( |
| 37 | + config string |
| 38 | + version bool |
| 39 | + ) |
| 40 | + |
| 41 | + flags := flag.NewFlagSet(Name, flag.ContinueOnError) |
| 42 | + flags.SetOutput(cli.errStream) |
| 43 | + |
| 44 | + flags.StringVar(&config, "config", "/etc/pam-google-web-oauth/client_secret.json", "Config file path") |
| 45 | + flags.StringVar(&config, "c", "/etc/pam-google-web-oauth/client_secret.json", "Config file path(Short)") |
| 46 | + |
| 47 | + flags.BoolVar(&version, "version", false, "Print version information and quit.") |
| 48 | + |
| 49 | + // Parse commandline flag |
| 50 | + if err := flags.Parse(args[1:]); err != nil { |
| 51 | + return ExitCodeError |
| 52 | + } |
| 53 | + |
| 54 | + // Show version |
| 55 | + if version { |
| 56 | + fmt.Fprintf(cli.errStream, "%s version %s\n", Name, Version) |
| 57 | + return ExitCodeOK |
| 58 | + } |
| 59 | + if err := cli.run(config); err != nil { |
| 60 | + logrus.Error(err) |
| 61 | + return ExitCodeError |
| 62 | + } |
| 63 | + return ExitCodeOK |
| 64 | + |
| 65 | +} |
| 66 | +func (cli *CLI) run(config string) error { |
| 67 | + ctx := context.Background() |
| 68 | + b, err := ioutil.ReadFile(config) |
| 69 | + if err != nil { |
| 70 | + log.Fatalf("Unable to read client secret file: %s", config) |
| 71 | + } |
| 72 | + |
| 73 | + gconfig, err := google.ConfigFromJSON(b, "profile") |
| 74 | + if err != nil { |
| 75 | + fmt.Errorf("Unable to parse client secret file to config: %v", err) |
| 76 | + } |
| 77 | + err = auth(ctx, gconfig) |
| 78 | + if err != nil { |
| 79 | + return err |
| 80 | + } |
| 81 | + |
| 82 | + return nil |
| 83 | + |
| 84 | +} |
| 85 | + |
| 86 | +func auth(ctx context.Context, c *oauth2.Config) error { |
| 87 | + cacheFile, err := tokenCacheFile() |
| 88 | + if err != nil { |
| 89 | + return err |
| 90 | + } |
| 91 | + tok, err := tokenFromFile(cacheFile) |
| 92 | + if err != nil || !tok.Valid() { |
| 93 | + tok, err := getTokenFromWeb(c) |
| 94 | + if err != nil { |
| 95 | + return err |
| 96 | + } |
| 97 | + return saveToken(cacheFile, tok) |
| 98 | + } |
| 99 | + return nil |
| 100 | +} |
| 101 | + |
| 102 | +func getTokenFromWeb(c *oauth2.Config) (*oauth2.Token, error) { |
| 103 | + authURL := c.AuthCodeURL("state-token", oauth2.AccessTypeOffline) |
| 104 | + fmt.Printf("Go to the following link in your browser then type the "+ |
| 105 | + "authorization code: \n\n%v\n\nPlease type code:", authURL) |
| 106 | + |
| 107 | + var code string |
| 108 | + if _, err := fmt.Scan(&code); err != nil { |
| 109 | + return nil, fmt.Errorf("Unable to read authorization code %v", err) |
| 110 | + } |
| 111 | + |
| 112 | + tok, err := c.Exchange(oauth2.NoContext, code) |
| 113 | + if err != nil { |
| 114 | + return nil, fmt.Errorf("Unable to retrieve token from web %v", err) |
| 115 | + } |
| 116 | + return tok, nil |
| 117 | +} |
| 118 | + |
| 119 | +func tokenCacheFile() (string, error) { |
| 120 | + userInfo, err := user.Lookup(os.Getenv("PAM_USER")) |
| 121 | + if err != nil { |
| 122 | + return "", err |
| 123 | + } |
| 124 | + tokenCacheDir := filepath.Join(userInfo.HomeDir, ".credentials") |
| 125 | + err = os.MkdirAll(tokenCacheDir, 0700) |
| 126 | + if err != nil { |
| 127 | + return "", err |
| 128 | + } |
| 129 | + return filepath.Join(tokenCacheDir, url.QueryEscape("google_oauth.json")), nil |
| 130 | +} |
| 131 | + |
| 132 | +func tokenFromFile(file string) (*oauth2.Token, error) { |
| 133 | + f, err := os.Open(file) |
| 134 | + if err != nil { |
| 135 | + return nil, err |
| 136 | + } |
| 137 | + t := &oauth2.Token{} |
| 138 | + err = json.NewDecoder(f).Decode(t) |
| 139 | + defer f.Close() |
| 140 | + return t, err |
| 141 | +} |
| 142 | + |
| 143 | +func saveToken(file string, token *oauth2.Token) error { |
| 144 | + fmt.Printf("Saving credential file to: %s\n", file) |
| 145 | + f, err := os.OpenFile(file, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600) |
| 146 | + if err != nil { |
| 147 | + log.Fatalf("Unable to cache oauth token: %v", err) |
| 148 | + } |
| 149 | + defer f.Close() |
| 150 | + return json.NewEncoder(f).Encode(token) |
| 151 | +} |
0 commit comments