mirror of
				https://codeberg.org/forgejo/forgejo.git
				synced 2025-10-31 06:21:11 +00:00 
			
		
		
		
	- Unify the usage of [`crypto/rand.Read`](https://pkg.go.dev/crypto/rand#Read) to `util.CryptoRandomBytes`. - Refactor `util.CryptoRandomBytes` to never return an error. It is documented by Go, https://go.dev/issue/66821, to always succeed. So if we still receive a error or if the returned bytes read is not equal to the expected bytes to be read we panic (just to be on the safe side). - This simplifies a lot of code to no longer care about error handling. Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/7453 Reviewed-by: Earl Warren <earl-warren@noreply.codeberg.org> Co-authored-by: Gusted <postmaster@gusted.xyz> Co-committed-by: Gusted <postmaster@gusted.xyz>
		
			
				
	
	
		
			97 lines
		
	
	
	
		
			1.9 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			97 lines
		
	
	
	
		
			1.9 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 (
 | |
| 	"fmt"
 | |
| 	"os"
 | |
| 
 | |
| 	"forgejo.org/modules/generate"
 | |
| 
 | |
| 	"github.com/mattn/go-isatty"
 | |
| 	"github.com/urfave/cli/v2"
 | |
| )
 | |
| 
 | |
| var (
 | |
| 	// CmdGenerate represents the available generate sub-command.
 | |
| 	CmdGenerate = &cli.Command{
 | |
| 		Name:  "generate",
 | |
| 		Usage: "Generate Gitea's secrets/keys/tokens",
 | |
| 		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{
 | |
| 		Name:    "JWT_SECRET",
 | |
| 		Aliases: []string{"LFS_JWT_SECRET"},
 | |
| 		Usage:   "Generate a new JWT_SECRET",
 | |
| 		Action:  runGenerateLfsJwtSecret,
 | |
| 	}
 | |
| 
 | |
| 	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
 | |
| 	}
 | |
| 
 | |
| 	fmt.Printf("%s", internalToken)
 | |
| 
 | |
| 	if isatty.IsTerminal(os.Stdout.Fd()) {
 | |
| 		fmt.Printf("\n")
 | |
| 	}
 | |
| 
 | |
| 	return nil
 | |
| }
 | |
| 
 | |
| func runGenerateLfsJwtSecret(c *cli.Context) error {
 | |
| 	_, jwtSecretBase64 := generate.NewJwtSecret()
 | |
| 
 | |
| 	fmt.Printf("%s", jwtSecretBase64)
 | |
| 
 | |
| 	if isatty.IsTerminal(os.Stdout.Fd()) {
 | |
| 		fmt.Printf("\n")
 | |
| 	}
 | |
| 
 | |
| 	return nil
 | |
| }
 | |
| 
 | |
| func runGenerateSecretKey(c *cli.Context) error {
 | |
| 	secretKey, err := generate.NewSecretKey()
 | |
| 	if err != nil {
 | |
| 		return err
 | |
| 	}
 | |
| 
 | |
| 	fmt.Printf("%s", secretKey)
 | |
| 
 | |
| 	if isatty.IsTerminal(os.Stdout.Fd()) {
 | |
| 		fmt.Printf("\n")
 | |
| 	}
 | |
| 
 | |
| 	return nil
 | |
| }
 |