Skip to content
This repository has been archived by the owner on Dec 26, 2024. It is now read-only.

Commit

Permalink
feat(generate-token): optional API token length (#38)
Browse files Browse the repository at this point in the history
* feat(generate-token): optional token length

* deny negative values

* make --length work

* try again

* try again

* one more time

* we go again

* another attempt

* clean up code

* brought back zerolog

* updated README and help message

* go mod tidy

---------

Co-authored-by: ze0s <[email protected]>
  • Loading branch information
s0up4200 and zze0s authored Feb 23, 2023
1 parent f0e2f11 commit 35e6378
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 17 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ server:
```
Call with `omegabrr generate-token`
If you are using docker `docker exec omegabrr omegabrr generate-token`
Optionally call with `--length <number>`for a custom length.

### arr

Expand Down
25 changes: 15 additions & 10 deletions cmd/omegabrr/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,27 +30,29 @@ const usage = `omegabrr
Turn your monitored shows from your arrs into autobrr filters, automagically!
Usage:
omegabrr generate-token Generate API Token
omegabrr arr Run omegabrr once
omegabrr run Run omegabrr service
omegabrr version Print version info
omegabrr help Show this help message`
omegabrr generate-token Generate API Token Optionally call with --length <number>
omegabrr arr Run omegabrr once
omegabrr run Run omegabrr service
omegabrr version Print version info
omegabrr help Show this help message`

func init() {
pflag.Usage = func() {
fmt.Fprintf(flag.CommandLine.Output(), usage)
fmt.Fprint(flag.CommandLine.Output(), usage)
}
}

func main() {
var configPath string
var dryRun bool

pflag.StringVar(&configPath, "config", "", "path to configuration file")
pflag.BoolVar(&dryRun, "dry-run", false, "dry-run without inserting filters")

// Define and parse flags using pflag
length := pflag.Int("length", 16, "length of the generated API token")
pflag.Parse()

// setup logger
zerolog.TimeFieldFormat = time.RFC3339
zerolog.ErrorStackMarshaler = pkgerrors.MarshalStack
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr, TimeFormat: time.RFC3339})
Expand All @@ -59,9 +61,12 @@ func main() {
case "version":
fmt.Fprintf(flag.CommandLine.Output(), "Version: %v\nCommit: %v\n", version, commit)
case "generate-token":
key := apitoken.GenerateToken(16)
fmt.Fprintf(flag.CommandLine.Output(), "API Token: %v\nCopy and paste into your config file config.yaml\n", key)

key, err := apitoken.GenerateToken(*length)
if err != nil {
fmt.Fprintf(os.Stderr, "error generating API token: %v\n", err)
os.Exit(1)
}
fmt.Fprintf(os.Stdout, "API Token: %v\nCopy and paste into your config file config.yaml\n", key)
case "arr":
cfg := domain.NewConfig(configPath)

Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ require (
github.com/mitchellh/copystructure v1.2.0 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/mitchellh/reflectwalk v1.0.2 // indirect
github.com/stretchr/testify v1.8.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
golang.org/x/net v0.7.0 // indirect
golang.org/x/sys v0.5.0 // indirect
Expand Down
19 changes: 13 additions & 6 deletions internal/apitoken/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,19 @@ package apitoken
import (
"crypto/rand"
"encoding/hex"
"fmt"
)

func GenerateToken(length int) string {
b := make([]byte, length)
if _, err := rand.Read(b); err != nil {
return ""
}
return hex.EncodeToString(b)
func GenerateToken(length int) (string, error) {
if length <= 0 {
return "", fmt.Errorf("token length must be a positive integer")
}

b := make([]byte, length)
_, err := rand.Read(b)
if err != nil {
return "", err
}

return hex.EncodeToString(b), nil
}
5 changes: 4 additions & 1 deletion internal/domain/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,10 @@ func (c *Config) writeFile(configPath string) error {
return errors.Wrap(err, "could not create config template")
}

token := apitoken.GenerateToken(16)
token, err := apitoken.GenerateToken(16)
if err != nil {
return errors.Wrap(err, "Error generating token: %v")
}

tmplVars := map[string]string{
"host": host,
Expand Down

0 comments on commit 35e6378

Please sign in to comment.