github.com/nats-io/nsc@v0.0.0-20221206222106-35db9400b257/cmd/revoke_listusers.go (about) 1 /* 2 * Copyright 2018-2020 The NATS Authors 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 package cmd 17 18 import ( 19 "fmt" 20 "time" 21 22 "github.com/nats-io/jwt/v2" 23 "github.com/nats-io/nsc/cmd/store" 24 "github.com/spf13/cobra" 25 "github.com/xlab/tablewriter" 26 ) 27 28 func createRevokeListUsersCmd() *cobra.Command { 29 var params RevokeListUserParams 30 cmd := &cobra.Command{ 31 Use: "list-users", 32 Aliases: []string{"list_users"}, 33 Short: "List users revoked in an account", 34 Args: MaxArgs(0), 35 SilenceUsage: true, 36 RunE: func(cmd *cobra.Command, args []string) error { 37 return RunAction(cmd, args, ¶ms) 38 }, 39 } 40 params.AccountContextParams.BindFlags(cmd) 41 42 return cmd 43 } 44 45 func init() { 46 revokeCmd.AddCommand(createRevokeListUsersCmd()) 47 } 48 49 // RevokeListUserParams hold the info necessary to add a user to the revocation list in an account 50 type RevokeListUserParams struct { 51 AccountContextParams 52 claim *jwt.AccountClaims 53 } 54 55 func (p *RevokeListUserParams) SetDefaults(ctx ActionCtx) error { 56 return p.AccountContextParams.SetDefaults(ctx) 57 } 58 59 func (p *RevokeListUserParams) PreInteractive(ctx ActionCtx) error { 60 var err error 61 62 if err = p.AccountContextParams.Edit(ctx); err != nil { 63 return err 64 } 65 return nil 66 } 67 68 func (p *RevokeListUserParams) Load(ctx ActionCtx) error { 69 var err error 70 71 if err = p.AccountContextParams.Validate(ctx); err != nil { 72 return err 73 } 74 75 p.claim, err = ctx.StoreCtx().Store.ReadAccountClaim(p.AccountContextParams.Name) 76 if err != nil { 77 return err 78 } 79 80 return nil 81 } 82 83 func (p *RevokeListUserParams) Validate(ctx ActionCtx) error { 84 return nil 85 } 86 87 func (p *RevokeListUserParams) PostInteractive(ctx ActionCtx) error { 88 return nil 89 } 90 91 func (p *RevokeListUserParams) Run(ctx ActionCtx) (store.Status, error) { 92 name := p.claim.Name 93 if name == "" { 94 name = p.claim.Subject 95 } 96 97 if len(p.claim.Revocations) == 0 { 98 return nil, fmt.Errorf("account %s does not have revoked users", name) 99 } 100 101 table := tablewriter.CreateTable() 102 table.AddTitle(fmt.Sprintf("Revoked Users for %s", name)) 103 table.AddHeaders("Public Key", "Revoke Credentials Before") 104 105 for pubKey, at := range p.claim.Revocations { 106 if pubKey == jwt.All { 107 pubKey = fmt.Sprintf("%s [All Users]", pubKey) 108 } 109 t := time.Unix(at, 0) 110 formatted := t.Format(time.RFC1123) 111 table.AddRow(pubKey, formatted) 112 } 113 return nil, Write("--", []byte(table.Render())) 114 }