github.com/qri-io/qri@v0.10.1-0.20220104210721-c771715036cb/cmd/access.go (about) 1 package cmd 2 3 import ( 4 "context" 5 6 "github.com/qri-io/ioes" 7 "github.com/qri-io/qri/lib" 8 "github.com/spf13/cobra" 9 ) 10 11 // NewAccessCommand creates a new `qri access` cobra command for managing 12 // permissions 13 func NewAccessCommand(f Factory, ioStreams ioes.IOStreams) *cobra.Command { 14 o := &AccessOptions{IOStreams: ioStreams} 15 cmd := &cobra.Command{ 16 Use: "access", 17 Short: "manage user permissions", 18 Long: ``, 19 Example: ``, 20 Annotations: map[string]string{ 21 "group": "other", 22 }, 23 } 24 25 tokenCmd := &cobra.Command{ 26 Use: "token", 27 Short: "create an access token", 28 Long: ` 29 token creates a JSON Web Token (JWT) that authenticates the given user. 30 Constructing an access token requires a private key that backs the given user. 31 32 In the course of normal operation you shouldn't need this command, It's mainly 33 here for crafting API requests in external progrmas`[1:], 34 Example: ` 35 # create an access token to authenticate yourself else where: 36 $ qri access token --for me 37 `[1:], 38 RunE: func(cmd *cobra.Command, args []string) error { 39 if err := o.Complete(f, args); err != nil { 40 return err 41 } 42 ctx := context.TODO() 43 return o.CreateAccessToken(ctx) 44 }, 45 } 46 tokenCmd.Flags().StringVar(&o.GranteeUsername, "for", "", "user to create access token for") 47 tokenCmd.MarkFlagRequired("for") 48 49 cmd.AddCommand(tokenCmd) 50 return cmd 51 } 52 53 // AccessOptions encapsulates state for the apply command 54 type AccessOptions struct { 55 ioes.IOStreams 56 Instance *lib.Instance 57 58 GranteeUsername string 59 } 60 61 // Complete adds any missing configuration that can only be added just before calling Run 62 func (o *AccessOptions) Complete(f Factory, args []string) (err error) { 63 o.Instance, err = f.Instance() 64 return err 65 } 66 67 // CreateAccessToken constructs an access token suitable for making 68 // authenticated requests 69 func (o *AccessOptions) CreateAccessToken(ctx context.Context) error { 70 p := &lib.CreateAuthTokenParams{ 71 GranteeUsername: o.GranteeUsername, 72 } 73 token, err := o.Instance.Access().CreateAuthToken(ctx, p) 74 if err != nil { 75 return err 76 } 77 78 printInfo(o.Out, token) 79 return nil 80 }