MCPcopy Create free account
hub / github.com/GetStream/stream-cli / createTokenCmd

Function createTokenCmd

pkg/cmd/chat/user/user.go:35–90  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

33}
34
35func createTokenCmd() *cobra.Command {
36 cmd := &cobra.Command{
37 Use: "create-token --user [user-id] --expiration [epoch] --issued-at [epoch]",
38 Short: "Create a token",
39 Long: heredoc.Doc(`
40 Stream uses JWT (JSON Web Tokens) to authenticate chat users, enabling them to login.
41 Knowing whether a user is authorized to perform certain actions is
42 managed separately via a role based permissions system.
43
44 With this command you can generate token for a specific user that can be
45 used on the frontend.
46 `),
47 Example: heredoc.Doc(`
48 # Create a JWT token for a user with id '123'. This token has no expiration.
49 $ stream-cli chat create-token --user 123
50
51 # Create a JWT for user 'joe' with 'exp' and 'iat' claim
52 $ stream-cli chat create-token --user joe --expiration 1577880000 --issued-at 1577880000
53 `),
54 RunE: func(cmd *cobra.Command, args []string) error {
55 c, err := config.GetConfig(cmd).GetClient(cmd)
56 if err != nil {
57 return err
58 }
59
60 userID, _ := cmd.Flags().GetString("user")
61 exp, _ := cmd.Flags().GetInt("expiration")
62 iat, _ := cmd.Flags().GetInt("issued-at")
63
64 expDate := time.Time{}
65 iatDate := time.Time{}
66 if exp > 0 {
67 expDate = time.Unix(int64(exp), 0)
68 }
69 if iat > 0 {
70 iatDate = time.Unix(int64(iat), 0)
71 }
72
73 token, err := c.CreateToken(userID, expDate, iatDate)
74 if err != nil {
75 return err
76 }
77
78 cmd.Printf("Token for user [%s]:\n%s\n", userID, token)
79 return nil
80 },
81 }
82
83 fl := cmd.Flags()
84 fl.StringP("user", "u", "", "[required] ID of the user to create token for")
85 fl.IntP("expiration", "e", 0, "[optional] Expiration (exp) of the JWT in epoch timestamp")
86 fl.IntP("issued-at", "i", 0, "[optional] Issued at (iat) of the JWT in epoch timestamp")
87 _ = cmd.MarkFlagRequired("user")
88
89 return cmd
90}
91
92func upsertCmd() *cobra.Command {

Callers 1

NewCmdsFunction · 0.85

Calls 2

GetConfigFunction · 0.92
GetClientMethod · 0.80

Tested by

no test coverage detected