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

Commit

Permalink
Update token generation with crypto/rand (#40)
Browse files Browse the repository at this point in the history
Co-authored-by: Frank Chiarulli Jr <[email protected]>
svix-frank and fcjr authored Jun 28, 2021
1 parent 2425061 commit 5f4dee1
Showing 4 changed files with 19 additions and 7 deletions.
3 changes: 2 additions & 1 deletion cmd/listen.go
Original file line number Diff line number Diff line change
@@ -45,7 +45,8 @@ to http://localhost:8000/webhook/`,
if viper.IsSet("relay_token") {
token = viper.GetString("relay_token")
} else {
token = relay.GenerateToken()
token, err = relay.GenerateToken()
printer.CheckErr(err)
viper.Set("relay_token", token)
err := config.Write(viper.AllSettings())
printer.CheckErr(err)
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -8,7 +8,6 @@ require (
github.com/gorilla/websocket v1.4.2
github.com/manifoldco/promptui v0.8.0
github.com/mitchellh/go-homedir v1.1.0
github.com/segmentio/ksuid v1.0.3
github.com/skratchdot/open-golang v0.0.0-20200116055534-eef842397966
github.com/spf13/cobra v1.1.3
github.com/spf13/viper v1.7.1
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -231,8 +231,6 @@ github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFR
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/ryanuber/columnize v0.0.0-20160712163229-9b3edd62028f/go.mod h1:sm1tb6uqfes/u+d4ooFouqFdy9/2g9QGwK3SQygK0Ts=
github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529/go.mod h1:DxrIzT+xaE7yg65j358z/aeFdxmN0P9QXhEzd20vsDc=
github.com/segmentio/ksuid v1.0.3 h1:FoResxvleQwYiPAVKe1tMUlEirodZqlqglIuFsdDntY=
github.com/segmentio/ksuid v1.0.3/go.mod h1:/XUiZBD3kVx5SmUOl55voK5yeAbBNNIed+2O73XgrPE=
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
github.com/skratchdot/open-golang v0.0.0-20200116055534-eef842397966 h1:JIAuq3EEf9cgbU6AtGPK4CTG3Zf6CKMNqf0MHTggAUA=
20 changes: 17 additions & 3 deletions relay/token.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,21 @@
package relay

import "github.com/segmentio/ksuid"
import (
"crypto/rand"
"math/big"
)

func GenerateToken() string {
return ksuid.New().String()
const base62 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"

func GenerateToken() (string, error) {
length := 27
ret := make([]byte, length)
for i := 0; i < length; i++ {
num, err := rand.Int(rand.Reader, big.NewInt(int64(len(base62))))
if err != nil {
return "", err
}
ret[i] = base62[num.Int64()]
}
return string(ret), nil
}

0 comments on commit 5f4dee1

Please sign in to comment.