Sync shared snapshot from df68167
This commit is contained in:
1 parent
588ef139a6
commit
06efc832ca
79 files changed
+10035
-3
No files matched your search
@@ -0,0 +1,25 @@
|
||||
package auth
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"crypto/sha256"
|
||||
"encoding/base64"
|
||||
)
|
||||
|
||||
// NewSessionToken creates a random bearer token and its SHA-256 storage hash.
|
||||
// Store only the hash; return the raw token to the caller that sets the cookie
|
||||
// or authorization header.
|
||||
func NewSessionToken() (raw string, hash string, err error) {
|
||||
bytes := make([]byte, 32)
|
||||
if _, err := rand.Read(bytes); err != nil {
|
||||
return "", "", err
|
||||
}
|
||||
raw = base64.RawURLEncoding.EncodeToString(bytes)
|
||||
return raw, HashToken(raw), nil
|
||||
}
|
||||
|
||||
// HashToken creates the stable database key for a raw session/API token.
|
||||
func HashToken(raw string) string {
|
||||
sum := sha256.Sum256([]byte(raw))
|
||||
return base64.RawURLEncoding.EncodeToString(sum[:])
|
||||
}
|
||||
Reference in new issue
Block a user