Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b846135

Browse files
committed
adding more conditional statement, adding necessary userID strings
Signed-off-by: Philip-21 <[email protected]>
1 parent 6dc1e16 commit b846135

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed

cli/tokens.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,11 @@ func (r *RootCmd) createToken() *serpent.Command {
5959
r.InitClient(client),
6060
),
6161
Handler: func(inv *serpent.Invocation) error {
62+
userID := codersdk.Me
6263
if user != "" {
64+
userID = user
6365
adminID := codersdk.Me
64-
res, err := client.CreateTokenForUser(inv.Context(), adminID, user, codersdk.CreateTokenRequest{
66+
res, err := client.CreateTokenForUser(inv.Context(), adminID, userID, codersdk.CreateTokenRequest{
6567
Lifetime: tokenLifetime,
6668
TokenName: name,
6769
})
@@ -71,7 +73,7 @@ func (r *RootCmd) createToken() *serpent.Command {
7173
_, _ = fmt.Fprintln(inv.Stdout, res.Key) // Print the token to stdout
7274
} else {
7375
// Otherwise, create a token for the current user
74-
res, err := client.CreateToken(inv.Context(), codersdk.Me, codersdk.CreateTokenRequest{
76+
res, err := client.CreateToken(inv.Context(), userID, codersdk.CreateTokenRequest{
7577
Lifetime: tokenLifetime,
7678
TokenName: name,
7779
})

codersdk/apikey.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,9 @@ func (c *Client) CreateTokenForUser(ctx context.Context, adminID, targetUserID s
8888
if !isAdmin {
8989
return GenerateAPIKeyResponse{}, fmt.Errorf("user %s is not an admin", adminID)
9090
}
91+
if adminID == targetUserID {
92+
return GenerateAPIKeyResponse{}, fmt.Errorf("admin cannot create a token for themselves")
93+
}
9194
return c.CreateToken(ctx, targetUserID, req)
9295
}
9396

0 commit comments

Comments
 (0)