snapshot of https://github.com/mattermost/mattermost.git (master, 2026-09-16; blobs > 131072B omitted)
Some checks failed
Server CI / Postgres (shard 0) (push) Has been skipped
Server CI / Postgres (shard 3) (push) Has been skipped
BuildEnv Docker Image / build-image (push) Failing after 4s
BuildEnv Docker Image / build-image-fips (push) Failing after 4s
Migration Review & Release Notes / Generate migration review & release notes (push) Failing after 3s
Server CI / Build Environment (push) Failing after 3s
Server CI / Postgres (shard 1) (push) Has been skipped
Tools CI / check-style (mattermost-govet) (push) Failing after 4s
Server CI / Postgres (shard 2) (push) Has been skipped
Tools CI / check-style (mmgotool) (push) Failing after 3s
Server CI / Elasticsearch v8 Compatibility (push) Has been skipped
Server CI / OpenSearch v2 Compatibility (push) Has been skipped
Server CI / Postgres FIPS (shard 0) (push) Has been skipped
Tools CI / Test (mattermost-govet) (push) Failing after 3s
Server CI / Postgres FIPS (shard 1) (push) Has been skipped
Server CI / Postgres FIPS (shard 2) (push) Has been skipped
Tools CI / Test (mmgotool) (push) Failing after 3s
Server CI / Postgres FIPS (shard 3) (push) Has been skipped
Server CI / Run mmctl tests (push) Has been skipped
Server CI / Run mmctl tests (FIPS) (push) Has been skipped
Server CI / Merge Postgres Test Results (push) Has been skipped
Server CI / Merge Postgres FIPS Test Results (push) Has been skipped
Server CI / Check backport migrations (push) Has been skipped
Server CI / Check generated files (push) Has been skipped
Server CI / check-style (push) Has been skipped
Server CI / Vet API (push) Has been skipped
Server CI / Build mattermost server app (push) Has been skipped
Server CI / Server CI Complete (push) Failing after 2s
Server CI / Server CI Artifacts (push) Has been skipped
Server CI / Sentry Upload (push) Has been skipped
Server CI / Server CI Report (push) Failing after 5m1s
Server CI Nightly Race / Race Detector (push) Has been skipped
CodeQL / Analyze (go) (push) Has been cancelled
CodeQL / Analyze (javascript) (push) Has been cancelled
Webapp CI Cache Warm / Warm Webapp node_modules Cache (push) Has been cancelled
E2E CI Cache Warm / Warm E2E npm Registry Cache (push) Has been cancelled
API / build (push) Has been cancelled
Web App CI / check-types (push) Has been cancelled
Web App CI / check-lint (push) Has been cancelled
Web App CI / check-i18n (push) Has been cancelled
Web App CI / check-external-links (push) Has been cancelled
Web App CI / test (platform) (push) Has been cancelled
Web App CI / test (mattermost-redux) (push) Has been cancelled
Web App CI / test (channels shard 1/4) (push) Has been cancelled
Web App CI / test (channels shard 2/4) (push) Has been cancelled
Web App CI / test (channels shard 3/4) (push) Has been cancelled
Web App CI / test (channels shard 4/4) (push) Has been cancelled
Web App CI / upload-coverage (push) Has been cancelled
Web App CI / build (push) Has been cancelled
YAML Lint / yamllint (push) Has been cancelled
Push mirrored docker images / cd/Push mirrored docker images (push) Has been cancelled
docs-cd / Build and deploy docs (push) Has been cancelled
Docs CI / build (push) Has been cancelled
E2E Cache Warm: Cypress deps / Warm Cypress deps cache (push) Has been cancelled
Scorecards supply-chain security / Scorecard analysis (push) Has been cancelled
Server CI Weekly / Postgres FIPS (push) Has been skipped
Server CI Weekly / Run mmctl tests (FIPS) (push) Has been skipped
Server CI Weekly / Postgres with binary parameters (push) Has been skipped
Server CI Weekly / Build Environment (push) Failing after 1s
Server CI Cache Warm / Warm Go Module and Build Cache (push) Failing after 1s
Server CI Nightly Race / Build Environment (push) Failing after 1s
Some checks failed
Server CI / Postgres (shard 0) (push) Has been skipped
Server CI / Postgres (shard 3) (push) Has been skipped
BuildEnv Docker Image / build-image (push) Failing after 4s
BuildEnv Docker Image / build-image-fips (push) Failing after 4s
Migration Review & Release Notes / Generate migration review & release notes (push) Failing after 3s
Server CI / Build Environment (push) Failing after 3s
Server CI / Postgres (shard 1) (push) Has been skipped
Tools CI / check-style (mattermost-govet) (push) Failing after 4s
Server CI / Postgres (shard 2) (push) Has been skipped
Tools CI / check-style (mmgotool) (push) Failing after 3s
Server CI / Elasticsearch v8 Compatibility (push) Has been skipped
Server CI / OpenSearch v2 Compatibility (push) Has been skipped
Server CI / Postgres FIPS (shard 0) (push) Has been skipped
Tools CI / Test (mattermost-govet) (push) Failing after 3s
Server CI / Postgres FIPS (shard 1) (push) Has been skipped
Server CI / Postgres FIPS (shard 2) (push) Has been skipped
Tools CI / Test (mmgotool) (push) Failing after 3s
Server CI / Postgres FIPS (shard 3) (push) Has been skipped
Server CI / Run mmctl tests (push) Has been skipped
Server CI / Run mmctl tests (FIPS) (push) Has been skipped
Server CI / Merge Postgres Test Results (push) Has been skipped
Server CI / Merge Postgres FIPS Test Results (push) Has been skipped
Server CI / Check backport migrations (push) Has been skipped
Server CI / Check generated files (push) Has been skipped
Server CI / check-style (push) Has been skipped
Server CI / Vet API (push) Has been skipped
Server CI / Build mattermost server app (push) Has been skipped
Server CI / Server CI Complete (push) Failing after 2s
Server CI / Server CI Artifacts (push) Has been skipped
Server CI / Sentry Upload (push) Has been skipped
Server CI / Server CI Report (push) Failing after 5m1s
Server CI Nightly Race / Race Detector (push) Has been skipped
CodeQL / Analyze (go) (push) Has been cancelled
CodeQL / Analyze (javascript) (push) Has been cancelled
Webapp CI Cache Warm / Warm Webapp node_modules Cache (push) Has been cancelled
E2E CI Cache Warm / Warm E2E npm Registry Cache (push) Has been cancelled
API / build (push) Has been cancelled
Web App CI / check-types (push) Has been cancelled
Web App CI / check-lint (push) Has been cancelled
Web App CI / check-i18n (push) Has been cancelled
Web App CI / check-external-links (push) Has been cancelled
Web App CI / test (platform) (push) Has been cancelled
Web App CI / test (mattermost-redux) (push) Has been cancelled
Web App CI / test (channels shard 1/4) (push) Has been cancelled
Web App CI / test (channels shard 2/4) (push) Has been cancelled
Web App CI / test (channels shard 3/4) (push) Has been cancelled
Web App CI / test (channels shard 4/4) (push) Has been cancelled
Web App CI / upload-coverage (push) Has been cancelled
Web App CI / build (push) Has been cancelled
YAML Lint / yamllint (push) Has been cancelled
Push mirrored docker images / cd/Push mirrored docker images (push) Has been cancelled
docs-cd / Build and deploy docs (push) Has been cancelled
Docs CI / build (push) Has been cancelled
E2E Cache Warm: Cypress deps / Warm Cypress deps cache (push) Has been cancelled
Scorecards supply-chain security / Scorecard analysis (push) Has been cancelled
Server CI Weekly / Postgres FIPS (push) Has been skipped
Server CI Weekly / Run mmctl tests (FIPS) (push) Has been skipped
Server CI Weekly / Postgres with binary parameters (push) Has been skipped
Server CI Weekly / Build Environment (push) Failing after 1s
Server CI Cache Warm / Warm Go Module and Build Cache (push) Failing after 1s
Server CI Nightly Race / Build Environment (push) Failing after 1s
This commit is contained in:
215
server/cmd/mmctl/commands/token.go
Normal file
215
server/cmd/mmctl/commands/token.go
Normal file
@@ -0,0 +1,215 @@
|
||||
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
||||
// See LICENSE.txt for license information.
|
||||
|
||||
package commands
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/mattermost/mattermost/server/public/model"
|
||||
"github.com/mattermost/mattermost/server/v8/cmd/mmctl/client"
|
||||
"github.com/mattermost/mattermost/server/v8/cmd/mmctl/printer"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var TokenCmd = &cobra.Command{
|
||||
Use: "token",
|
||||
Short: "manage users' access tokens",
|
||||
}
|
||||
|
||||
var GenerateUserTokenCmd = &cobra.Command{
|
||||
Use: "generate [user] [description]",
|
||||
Short: "Generate token for a user",
|
||||
Long: "Generate token for a user. Use --expires-in to set an expiry, which may be required by " +
|
||||
"the server's MaximumPersonalAccessTokenLifetimeDays setting.",
|
||||
Example: ` generate testuser test-token
|
||||
generate testuser ci-token --expires-in 90d
|
||||
generate testuser short-lived --expires-in 12h`,
|
||||
RunE: withClient(generateTokenForAUserCmdF),
|
||||
Args: cobra.ExactArgs(2),
|
||||
}
|
||||
|
||||
var RevokeUserTokenCmd = &cobra.Command{
|
||||
Use: "revoke [token-ids]",
|
||||
Short: "Revoke tokens for a user",
|
||||
Long: "Revoke tokens for a user",
|
||||
Example: " revoke testuser test-token-id",
|
||||
RunE: withClient(revokeTokenForAUserCmdF),
|
||||
Args: cobra.MinimumNArgs(1),
|
||||
}
|
||||
|
||||
var RotateUserTokenCmd = &cobra.Command{
|
||||
Use: "rotate [token-id]",
|
||||
Short: "Rotate a personal access token",
|
||||
Long: "Generate a new secret for an existing personal access token, immediately invalidating the old secret. " +
|
||||
"Use --expires-in to set a new expiry.",
|
||||
Example: ` rotate xwt6numaubyj9mqjfkqjk5pfqr
|
||||
rotate xwt6numaubyj9mqjfkqjk5pfqr --expires-in 90d`,
|
||||
RunE: withClient(rotateTokenCmdF),
|
||||
Args: cobra.ExactArgs(1),
|
||||
}
|
||||
|
||||
var ListUserTokensCmd = &cobra.Command{
|
||||
Use: "list [user]",
|
||||
Short: "List users tokens",
|
||||
Long: "List the tokens of a user",
|
||||
Example: " user tokens testuser",
|
||||
RunE: withClient(listTokensOfAUserCmdF),
|
||||
Args: cobra.ExactArgs(1),
|
||||
}
|
||||
|
||||
func init() {
|
||||
GenerateUserTokenCmd.Flags().String("expires-in", "", "Duration after which the token expires (e.g. 90d, 12h, 30m). Accepts the standard Go duration syntax plus a 'd' (days) suffix. If empty, the token does not expire.")
|
||||
RotateUserTokenCmd.Flags().String("expires-in", "", "New expiry duration for the rotated token (e.g. 90d, 12h). If empty, the token does not expire (subject to server policy).")
|
||||
|
||||
ListUserTokensCmd.Flags().Int("page", 0, "Page number to fetch for the list of users")
|
||||
ListUserTokensCmd.Flags().Int("per-page", DefaultPageSize, "Number of users to be fetched")
|
||||
ListUserTokensCmd.Flags().Bool("all", false, "Fetch all tokens. --page flag will be ignore if provided")
|
||||
ListUserTokensCmd.Flags().Bool("active", true, "List only active tokens")
|
||||
ListUserTokensCmd.Flags().Bool("inactive", false, "List only inactive tokens")
|
||||
|
||||
TokenCmd.AddCommand(
|
||||
GenerateUserTokenCmd,
|
||||
RevokeUserTokenCmd,
|
||||
RotateUserTokenCmd,
|
||||
ListUserTokensCmd,
|
||||
)
|
||||
|
||||
RootCmd.AddCommand(
|
||||
TokenCmd,
|
||||
)
|
||||
}
|
||||
|
||||
func generateTokenForAUserCmdF(c client.Client, command *cobra.Command, args []string) error {
|
||||
expiresAt, err := resolveTokenExpiry(command)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
userArg := args[0]
|
||||
user := getUserFromUserArg(c, userArg)
|
||||
if user == nil {
|
||||
return errors.Errorf("could not retrieve user information of %q", userArg)
|
||||
}
|
||||
|
||||
token, _, err := c.CreateUserAccessToken(context.TODO(), user.Id, args[1], expiresAt)
|
||||
if err != nil {
|
||||
return errors.Errorf("could not create token for %q: %s", userArg, err.Error())
|
||||
}
|
||||
printer.PrintT("{{.Token}}: {{.Description}}", token)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// resolveTokenExpiry converts the --expires-in flag into a Unix-millis timestamp
|
||||
// suitable for UserAccessToken.ExpiresAt. Returns 0 when the flag is empty,
|
||||
// meaning the token does not expire (subject to server policy).
|
||||
func resolveTokenExpiry(command *cobra.Command) (int64, error) {
|
||||
raw, _ := command.Flags().GetString("expires-in")
|
||||
raw = strings.TrimSpace(raw)
|
||||
if raw == "" {
|
||||
return 0, nil
|
||||
}
|
||||
d, err := parseExpiresIn(raw)
|
||||
if err != nil {
|
||||
return 0, errors.Wrap(err, "invalid --expires-in value")
|
||||
}
|
||||
if d <= 0 {
|
||||
return 0, errors.Errorf("--expires-in must be positive, got %q", raw)
|
||||
}
|
||||
return time.Now().Add(d).UnixMilli(), nil
|
||||
}
|
||||
|
||||
// parseExpiresIn accepts a duration string. In addition to the standard Go
|
||||
// duration syntax (e.g. "12h", "30m"), a trailing "d" is interpreted as days
|
||||
// — the common case for token lifetimes which stdlib's time.ParseDuration
|
||||
// does not support.
|
||||
func parseExpiresIn(s string) (time.Duration, error) {
|
||||
if prefix, ok := strings.CutSuffix(s, "d"); ok {
|
||||
days, err := strconv.Atoi(prefix)
|
||||
if err != nil {
|
||||
return 0, errors.Errorf("%q is not a valid day count", s)
|
||||
}
|
||||
// time.Duration is int64 nanoseconds; days * 24h overflows past ~106751.
|
||||
// Cap at the server-side bound so the CLI rejects values that the server
|
||||
// would reject anyway, well below the int64-overflow point.
|
||||
if days > model.MaxPersonalAccessTokenLifetimeDays {
|
||||
return 0, errors.Errorf("%q exceeds maximum supported day count of %d", s, model.MaxPersonalAccessTokenLifetimeDays)
|
||||
}
|
||||
return time.Duration(days) * 24 * time.Hour, nil
|
||||
}
|
||||
return time.ParseDuration(s)
|
||||
}
|
||||
|
||||
func listTokensOfAUserCmdF(c client.Client, command *cobra.Command, args []string) error {
|
||||
page, _ := command.Flags().GetInt("page")
|
||||
perPage, _ := command.Flags().GetInt("per-page")
|
||||
showAll, _ := command.Flags().GetBool("all")
|
||||
active, _ := command.Flags().GetBool("active")
|
||||
inactive, _ := command.Flags().GetBool("inactive")
|
||||
|
||||
if showAll {
|
||||
page = 0
|
||||
perPage = 9999
|
||||
}
|
||||
|
||||
userArg := args[0]
|
||||
|
||||
user := getUserFromUserArg(c, userArg)
|
||||
if user == nil {
|
||||
return errors.Errorf("could not retrieve user information of %q", userArg)
|
||||
}
|
||||
|
||||
tokens, _, err := c.GetUserAccessTokensForUser(context.TODO(), user.Id, page, perPage)
|
||||
if err != nil {
|
||||
return errors.Errorf("could not retrieve tokens for user %q: %s", userArg, err.Error())
|
||||
}
|
||||
|
||||
if len(tokens) == 0 {
|
||||
return errors.Errorf("there are no tokens for the %q", userArg)
|
||||
}
|
||||
|
||||
for _, t := range tokens {
|
||||
if t.IsActive && !inactive {
|
||||
printer.PrintT("{{.Id}}: {{.Description}}", t)
|
||||
}
|
||||
if !t.IsActive && !active {
|
||||
printer.PrintT("{{.Id}}: {{.Description}}", t)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func rotateTokenCmdF(c client.Client, command *cobra.Command, args []string) error {
|
||||
expiresAt, err := resolveTokenExpiry(command)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
token, _, err := c.RotateUserAccessToken(context.TODO(), args[0], expiresAt)
|
||||
if err != nil {
|
||||
return errors.Errorf("could not rotate token %q: %s", args[0], err.Error())
|
||||
}
|
||||
printer.PrintT("{{.Token}}: {{.Description}}", token)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func revokeTokenForAUserCmdF(c client.Client, command *cobra.Command, args []string) error {
|
||||
for _, id := range args {
|
||||
res, err := c.RevokeUserAccessToken(context.TODO(), id)
|
||||
if err != nil {
|
||||
return errors.Errorf("could not revoke token %q: %s", id, err.Error())
|
||||
}
|
||||
if res.StatusCode != http.StatusOK {
|
||||
return errors.Errorf("could not revoke token %q", id)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user