owncast/utils/accessTokens.go
Gabe Kangas c6c6f0233d
Expanded linting + fix warnings (#1396)
* Expand the linters and types of warnings to improve consistency and safety

* Fail lint workflow if there are errors

* golint has been replaced by revive

* Hand-pick some of the default exclude list

* Ignore error when trying to delete preview gif

* Ignore linter warning opening playlist path

* Rename user field Id -> ID

* A bunch of renames to address linter warnings

* Rename ChatClient -> Client per linter suggestion best practice

* Rename ChatServer -> Server per linter suggestion best practice

* More linter warning fixes

* Add missing comments to all exported functions and properties
2021-09-12 00:18:15 -07:00

41 lines
1.1 KiB
Go

package utils
import (
"encoding/base64"
"math/rand"
"time"
)
const tokenLength = 32
// GenerateAccessToken will generate and return an access token.
func GenerateAccessToken() (string, error) {
return generateRandomString(tokenLength)
}
// generateRandomBytes returns securely generated random bytes.
// It will return an error if the system's secure random
// number generator fails to function correctly, in which
// case the caller should not continue.
func generateRandomBytes(n int) ([]byte, error) {
b := make([]byte, n)
rand.Seed(time.Now().UTC().UnixNano())
_, err := rand.Read(b) //nolint
// Note that err == nil only if we read len(b) bytes.
if err != nil {
return nil, err
}
return b, nil
}
// generateRandomString returns a URL-safe, base64 encoded
// securely generated random string.
// It will return an error if the system's secure random
// number generator fails to function correctly, in which
// case the caller should not continue.
func generateRandomString(n int) (string, error) {
b, err := generateRandomBytes(n)
return base64.URLEncoding.EncodeToString(b), err
}