mirror of
https://github.com/moul/sshportal.git
synced 2024-12-26 09:42:29 +08:00
fix: change rand lib(math/rand => crypto/rand)
Signed-off-by: ismael FALL <ismael.fall@epitech.eu>
This commit is contained in:
parent
73926212d5
commit
a30952348b
2 changed files with 18 additions and 6 deletions
|
@ -1,10 +1,11 @@
|
||||||
package bastion // import "moul.io/sshportal/pkg/bastion"
|
package bastion // import "moul.io/sshportal/pkg/bastion"
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"crypto/rand"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
"math/rand"
|
"math/big"
|
||||||
"os"
|
"os"
|
||||||
"os/user"
|
"os/user"
|
||||||
"strings"
|
"strings"
|
||||||
|
@ -617,7 +618,10 @@ func DBInit(db *gorm.DB) error {
|
||||||
}
|
}
|
||||||
if count == 0 {
|
if count == 0 {
|
||||||
// if no admin, create an account for the first connection
|
// if no admin, create an account for the first connection
|
||||||
inviteToken := randStringBytes(16)
|
inviteToken, err := randStringBytes(16)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
if os.Getenv("SSHPORTAL_DEFAULT_ADMIN_INVITE_TOKEN") != "" {
|
if os.Getenv("SSHPORTAL_DEFAULT_ADMIN_INVITE_TOKEN") != "" {
|
||||||
inviteToken = os.Getenv("SSHPORTAL_DEFAULT_ADMIN_INVITE_TOKEN")
|
inviteToken = os.Getenv("SSHPORTAL_DEFAULT_ADMIN_INVITE_TOKEN")
|
||||||
}
|
}
|
||||||
|
@ -673,12 +677,16 @@ func DBInit(db *gorm.DB) error {
|
||||||
}).Error
|
}).Error
|
||||||
}
|
}
|
||||||
|
|
||||||
func randStringBytes(n int) string {
|
func randStringBytes(n int) (string, error) {
|
||||||
const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
|
const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
|
||||||
|
|
||||||
b := make([]byte, n)
|
b := make([]byte, n)
|
||||||
for i := range b {
|
for i := range b {
|
||||||
b[i] = letterBytes[rand.Intn(len(letterBytes))]
|
r, err := rand.Int(rand.Reader, big.NewInt(int64(len(letterBytes))))
|
||||||
|
if err != nil {
|
||||||
|
return "", fmt.Errorf("failed to generate random string: %s", err)
|
||||||
|
}
|
||||||
|
b[i] = letterBytes[r.Int64()]
|
||||||
}
|
}
|
||||||
return string(b)
|
return string(b), nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -1640,11 +1640,15 @@ GLOBAL OPTIONS:
|
||||||
name = c.String("name")
|
name = c.String("name")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
r, err := randStringBytes(16)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
user := dbmodels.User{
|
user := dbmodels.User{
|
||||||
Name: name,
|
Name: name,
|
||||||
Email: email,
|
Email: email,
|
||||||
Comment: c.String("comment"),
|
Comment: c.String("comment"),
|
||||||
InviteToken: randStringBytes(16),
|
InviteToken: r,
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, err := govalidator.ValidateStruct(user); err != nil {
|
if _, err := govalidator.ValidateStruct(user); err != nil {
|
||||||
|
|
Loading…
Reference in a new issue