You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
38 lines
1.0 KiB
38 lines
1.0 KiB
package utils |
|
|
|
import ( |
|
"crypto/rand" |
|
"encoding/base64" |
|
) |
|
|
|
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) |
|
_, err := rand.Read(b) |
|
// 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 |
|
}
|
|
|