2018-02-18 12:14:37 -06:00
|
|
|
// Copyright 2016 The Gogs Authors. All rights reserved.
|
|
|
|
// Copyright 2016 The Gitea Authors. All rights reserved.
|
2022-11-27 12:20:29 -06:00
|
|
|
// SPDX-License-Identifier: MIT
|
2018-02-18 12:14:37 -06:00
|
|
|
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2020-05-20 11:16:06 -05:00
|
|
|
"os"
|
2018-02-18 12:14:37 -06:00
|
|
|
|
|
|
|
"code.gitea.io/gitea/modules/generate"
|
|
|
|
|
2020-05-20 11:16:06 -05:00
|
|
|
"github.com/mattn/go-isatty"
|
2018-02-18 12:14:37 -06:00
|
|
|
"github.com/urfave/cli"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// CmdGenerate represents the available generate sub-command.
|
|
|
|
CmdGenerate = cli.Command{
|
|
|
|
Name: "generate",
|
|
|
|
Usage: "Command line interface for running generators",
|
|
|
|
Subcommands: []cli.Command{
|
|
|
|
subcmdSecret,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
subcmdSecret = cli.Command{
|
|
|
|
Name: "secret",
|
|
|
|
Usage: "Generate a secret token",
|
|
|
|
Subcommands: []cli.Command{
|
|
|
|
microcmdGenerateInternalToken,
|
|
|
|
microcmdGenerateLfsJwtSecret,
|
|
|
|
microcmdGenerateSecretKey,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
microcmdGenerateInternalToken = cli.Command{
|
|
|
|
Name: "INTERNAL_TOKEN",
|
|
|
|
Usage: "Generate a new INTERNAL_TOKEN",
|
|
|
|
Action: runGenerateInternalToken,
|
|
|
|
}
|
|
|
|
|
|
|
|
microcmdGenerateLfsJwtSecret = cli.Command{
|
2019-05-01 21:32:06 -05:00
|
|
|
Name: "JWT_SECRET",
|
|
|
|
Aliases: []string{"LFS_JWT_SECRET"},
|
|
|
|
Usage: "Generate a new JWT_SECRET",
|
|
|
|
Action: runGenerateLfsJwtSecret,
|
2018-02-18 12:14:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
microcmdGenerateSecretKey = cli.Command{
|
|
|
|
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 11:16:06 -05:00
|
|
|
fmt.Printf("%s", internalToken)
|
|
|
|
|
|
|
|
if isatty.IsTerminal(os.Stdout.Fd()) {
|
|
|
|
fmt.Printf("\n")
|
|
|
|
}
|
|
|
|
|
2018-02-18 12:14:37 -06:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func runGenerateLfsJwtSecret(c *cli.Context) error {
|
2021-06-17 16:56:46 -05:00
|
|
|
JWTSecretBase64, err := generate.NewJwtSecretBase64()
|
2018-02-18 12:14:37 -06:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-05-20 11:16:06 -05:00
|
|
|
fmt.Printf("%s", JWTSecretBase64)
|
|
|
|
|
|
|
|
if isatty.IsTerminal(os.Stdout.Fd()) {
|
|
|
|
fmt.Printf("\n")
|
|
|
|
}
|
|
|
|
|
2018-02-18 12:14:37 -06:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func runGenerateSecretKey(c *cli.Context) error {
|
|
|
|
secretKey, err := generate.NewSecretKey()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-05-20 11:16:06 -05:00
|
|
|
fmt.Printf("%s", secretKey)
|
|
|
|
|
|
|
|
if isatty.IsTerminal(os.Stdout.Fd()) {
|
|
|
|
fmt.Printf("\n")
|
|
|
|
}
|
|
|
|
|
2018-02-18 12:14:37 -06:00
|
|
|
return nil
|
|
|
|
}
|