Skip to content

Commit

Permalink
Output-sso-expiry-in-JSON (#550)
Browse files Browse the repository at this point in the history
* WIP

* printing to stdout

* added --json flag
  • Loading branch information
shwethaumashanker authored Nov 23, 2023
1 parent 6190595 commit fafdde5
Showing 1 changed file with 31 additions and 2 deletions.
33 changes: 31 additions & 2 deletions pkg/granted/tokens.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package granted

import (
"context"
"encoding/json"
"errors"
"fmt"
"os"
Expand Down Expand Up @@ -65,7 +66,8 @@ var ListSSOTokensCommand = cli.Command{
var TokenExpiryCommand = cli.Command{
Name: "expiry",
Usage: "Lists expiry status for all access tokens saved in the keyring",
Flags: []cli.Flag{&cli.StringFlag{Name: "url", Usage: "If provided, prints the expiry of the token for the specific SSO URL"}},
Flags: []cli.Flag{&cli.StringFlag{Name: "url", Usage: "If provided, prints the expiry of the token for the specific SSO URL"},
&cli.BoolFlag{Name: "json", Usage: "If provided, prints the expiry of the tokens in JSON"}},
Action: func(ctx *cli.Context) error {
url := ctx.String("url")

Expand Down Expand Up @@ -101,6 +103,16 @@ var TokenExpiryCommand = cli.Command{
return err
}

jsonflag := ctx.Bool("json")

type sso_expiry struct {
StartURLs string `json:"start_urls"`
ExpiresAt string `json:"expires_at"`
IsExpired bool `json:"is_expired"`
}

var jsonDataArray []sso_expiry

for _, key := range keys {
token := secureSSOTokenStorage.GetValidSSOToken(key)

Expand All @@ -110,9 +122,26 @@ var TokenExpiryCommand = cli.Command{
} else {
expiry = token.Expiry.Local().Format(time.RFC3339)
}
if jsonflag {
sso_expiry_data := sso_expiry{
StartURLs: key,
ExpiresAt: expiry,
IsExpired: expiry == "EXPIRED",
}
jsonDataArray = append(jsonDataArray, sso_expiry_data)
} else {
clio.Logf("%-*s (%s) expires at: %s", max, key, strings.Join(startUrlMap[key], ", "), expiry)
}
}

clio.Logf("%-*s (%s) expires at: %s", max, key, strings.Join(startUrlMap[key], ", "), expiry)
if jsonflag {
jsonData, err := json.Marshal(jsonDataArray)
if err != nil {
return err
}
fmt.Println(string(jsonData))
}

return nil
},
}
Expand Down

0 comments on commit fafdde5

Please sign in to comment.