mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2025-12-08 06:29:47 +00:00
- Similair spirit of forgejo/forgejo!7453. - Refactor the code in such a way that it always succeeds. - To avoid doing mathematics if you use this function, define three security level (64, 128 and 256 bits) that correspond to a specific length which has that a security guarantee. I picked them as they fit the need for the existing usages of the code. Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/10110 Reviewed-by: Michael Kriese <michael.kriese@gmx.de> Reviewed-by: Lucas <sclu1034@noreply.codeberg.org> Co-authored-by: Gusted <postmaster@gusted.xyz> Co-committed-by: Gusted <postmaster@gusted.xyz>
106 lines
2.1 KiB
Go
106 lines
2.1 KiB
Go
// Copyright 2016 The Gogs Authors. All rights reserved.
|
|
// Copyright 2016 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package cmd
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"os"
|
|
|
|
"forgejo.org/modules/generate"
|
|
|
|
"github.com/mattn/go-isatty"
|
|
"github.com/urfave/cli/v3"
|
|
)
|
|
|
|
// CmdGenerate represents the available generate sub-command.
|
|
func cmdGenerate() *cli.Command {
|
|
return &cli.Command{
|
|
Name: "generate",
|
|
Usage: "Generate Forgejo's secrets/keys/tokens",
|
|
Commands: []*cli.Command{
|
|
subcmdSecret(),
|
|
},
|
|
}
|
|
}
|
|
|
|
func subcmdSecret() *cli.Command {
|
|
return &cli.Command{
|
|
Name: "secret",
|
|
Usage: "Generate a secret token",
|
|
Commands: []*cli.Command{
|
|
microcmdGenerateInternalToken(),
|
|
microcmdGenerateLfsJwtSecret(),
|
|
microcmdGenerateSecretKey(),
|
|
},
|
|
}
|
|
}
|
|
|
|
func microcmdGenerateInternalToken() *cli.Command {
|
|
return &cli.Command{
|
|
Name: "INTERNAL_TOKEN",
|
|
Usage: "Generate a new INTERNAL_TOKEN",
|
|
Before: noDanglingArgs,
|
|
Action: runGenerateInternalToken,
|
|
}
|
|
}
|
|
|
|
func microcmdGenerateLfsJwtSecret() *cli.Command {
|
|
return &cli.Command{
|
|
Name: "JWT_SECRET",
|
|
Aliases: []string{"LFS_JWT_SECRET"},
|
|
Usage: "Generate a new JWT_SECRET",
|
|
Before: noDanglingArgs,
|
|
Action: runGenerateLfsJwtSecret,
|
|
}
|
|
}
|
|
|
|
func microcmdGenerateSecretKey() *cli.Command {
|
|
return &cli.Command{
|
|
Name: "SECRET_KEY",
|
|
Usage: "Generate a new SECRET_KEY",
|
|
Before: noDanglingArgs,
|
|
Action: runGenerateSecretKey,
|
|
}
|
|
}
|
|
|
|
func runGenerateInternalToken(ctx context.Context, c *cli.Command) error {
|
|
internalToken, err := generate.NewInternalToken()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
fmt.Printf("%s", internalToken)
|
|
|
|
if isatty.IsTerminal(os.Stdout.Fd()) {
|
|
fmt.Println()
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func runGenerateLfsJwtSecret(ctx context.Context, c *cli.Command) error {
|
|
_, jwtSecretBase64 := generate.NewJwtSecret()
|
|
|
|
fmt.Printf("%s", jwtSecretBase64)
|
|
|
|
if isatty.IsTerminal(os.Stdout.Fd()) {
|
|
fmt.Print("\n")
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func runGenerateSecretKey(ctx context.Context, c *cli.Command) error {
|
|
secretKey := generate.NewSecretKey()
|
|
|
|
fmt.Printf("%s", secretKey)
|
|
|
|
if isatty.IsTerminal(os.Stdout.Fd()) {
|
|
fmt.Print("\n")
|
|
}
|
|
|
|
return nil
|
|
}
|