2018-02-18 23:44:37 +05:30
|
|
|
// Copyright 2016 The Gogs Authors. All rights reserved.
|
|
|
|
// Copyright 2016 The Gitea Authors. All rights reserved.
|
2022-11-27 23:50:29 +05:30
|
|
|
// SPDX-License-Identifier: MIT
|
2018-02-18 23:44:37 +05:30
|
|
|
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2020-05-20 21:46:06 +05:30
|
|
|
"os"
|
2018-02-18 23:44:37 +05:30
|
|
|
|
|
|
|
"code.gitea.io/gitea/modules/generate"
|
|
|
|
|
2020-05-20 21:46:06 +05:30
|
|
|
"github.com/mattn/go-isatty"
|
2023-07-21 14:58:19 +05:30
|
|
|
"github.com/urfave/cli/v2"
|
2018-02-18 23:44:37 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// CmdGenerate represents the available generate sub-command.
|
2023-07-21 14:58:19 +05:30
|
|
|
CmdGenerate = &cli.Command{
|
2018-02-18 23:44:37 +05:30
|
|
|
Name: "generate",
|
2023-12-15 21:19:01 +05:30
|
|
|
Usage: "Generate Gitea's secrets/keys/tokens",
|
2023-07-21 14:58:19 +05:30
|
|
|
Subcommands: []*cli.Command{
|
2018-02-18 23:44:37 +05:30
|
|
|
subcmdSecret,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2023-07-21 14:58:19 +05:30
|
|
|
subcmdSecret = &cli.Command{
|
2018-02-18 23:44:37 +05:30
|
|
|
Name: "secret",
|
|
|
|
Usage: "Generate a secret token",
|
2023-07-21 14:58:19 +05:30
|
|
|
Subcommands: []*cli.Command{
|
2018-02-18 23:44:37 +05:30
|
|
|
microcmdGenerateInternalToken,
|
|
|
|
microcmdGenerateLfsJwtSecret,
|
|
|
|
microcmdGenerateSecretKey,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2023-07-21 14:58:19 +05:30
|
|
|
microcmdGenerateInternalToken = &cli.Command{
|
2018-02-18 23:44:37 +05:30
|
|
|
Name: "INTERNAL_TOKEN",
|
|
|
|
Usage: "Generate a new INTERNAL_TOKEN",
|
|
|
|
Action: runGenerateInternalToken,
|
|
|
|
}
|
|
|
|
|
2023-07-21 14:58:19 +05:30
|
|
|
microcmdGenerateLfsJwtSecret = &cli.Command{
|
2019-05-02 08:02:06 +05:30
|
|
|
Name: "JWT_SECRET",
|
|
|
|
Aliases: []string{"LFS_JWT_SECRET"},
|
|
|
|
Usage: "Generate a new JWT_SECRET",
|
|
|
|
Action: runGenerateLfsJwtSecret,
|
2018-02-18 23:44:37 +05:30
|
|
|
}
|
|
|
|
|
2023-07-21 14:58:19 +05:30
|
|
|
microcmdGenerateSecretKey = &cli.Command{
|
2018-02-18 23:44:37 +05:30
|
|
|
Name: "SECRET_KEY",
|
|
|
|
Usage: "Generate a new SECRET_KEY",
|
|
|
|
Action: runGenerateSecretKey,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
func runGenerateInternalToken(c *cli.Context) error {
|
|
|
|
internalToken, err := generate.NewInternalToken()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-05-20 21:46:06 +05:30
|
|
|
fmt.Printf("%s", internalToken)
|
|
|
|
|
|
|
|
if isatty.IsTerminal(os.Stdout.Fd()) {
|
|
|
|
fmt.Printf("\n")
|
|
|
|
}
|
|
|
|
|
2018-02-18 23:44:37 +05:30
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func runGenerateLfsJwtSecret(c *cli.Context) error {
|
2024-01-24 20:55:06 +05:30
|
|
|
_, jwtSecretBase64, err := generate.NewJwtSecret()
|
2018-02-18 23:44:37 +05:30
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-08-14 16:00:16 +05:30
|
|
|
fmt.Printf("%s", jwtSecretBase64)
|
2020-05-20 21:46:06 +05:30
|
|
|
|
|
|
|
if isatty.IsTerminal(os.Stdout.Fd()) {
|
|
|
|
fmt.Printf("\n")
|
|
|
|
}
|
|
|
|
|
2018-02-18 23:44:37 +05:30
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func runGenerateSecretKey(c *cli.Context) error {
|
|
|
|
secretKey, err := generate.NewSecretKey()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-05-20 21:46:06 +05:30
|
|
|
fmt.Printf("%s", secretKey)
|
|
|
|
|
|
|
|
if isatty.IsTerminal(os.Stdout.Fd()) {
|
|
|
|
fmt.Printf("\n")
|
|
|
|
}
|
|
|
|
|
2018-02-18 23:44:37 +05:30
|
|
|
return nil
|
|
|
|
}
|