-
Notifications
You must be signed in to change notification settings - Fork 5
/
token.go
65 lines (53 loc) · 1.54 KB
/
token.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package cmd
import (
"encoding/json"
"fmt"
"os"
"time"
"github.com/chanzuckerberg/go-misc/oidc_cli/oidc_impl"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)
func init() {
tokenCmd.Flags().StringVar(&clientID, "client-id", "", "client_id generated from the OIDC application")
tokenCmd.Flags().StringVar(&issuerURL, "issuer-url", "", "The URL that hosts the OIDC identity provider")
tokenCmd.MarkFlagRequired("client-id") // nolint:errcheck
tokenCmd.MarkFlagRequired("issuer-url") // nolint:errcheck
rootCmd.AddCommand(tokenCmd)
}
const (
stdoutTokenVersion = 1
)
type stdoutToken struct {
Version int `json:"version,omitempty"`
IDToken string `json:"id_token,omitempty"`
AccessToken string `json:"access_token,omitempty"`
Expiry time.Time `json:"expiry,omitempty"`
}
var tokenCmd = &cobra.Command{
Use: "token",
Short: "token prints the oidc tokens to stdout in json format",
SilenceErrors: true,
RunE: func(cmd *cobra.Command, args []string) error {
stdoutToken := &stdoutToken{
Version: stdoutTokenVersion,
}
token, err := oidc_impl.GetToken(
cmd.Context(),
clientID,
issuerURL,
)
if err != nil {
return err
}
stdoutToken.AccessToken = token.AccessToken
stdoutToken.IDToken = token.IDToken
stdoutToken.Expiry = token.Expiry
data, err := json.Marshal(stdoutToken)
if err != nil {
return errors.Wrap(err, "could not json marshal oidc token")
}
_, err = fmt.Fprintln(os.Stdout, string(data))
return errors.Wrap(err, "could not print token to stdout")
},
}