added user.NewAuthToken

This commit is contained in:
2025-10-03 23:29:04 -06:00
parent 1952b34cce
commit f728eb21b0
3 changed files with 55 additions and 1 deletions
+35
View File
@@ -0,0 +1,35 @@
/*
* Amsterdam Web Communities System
* Copyright (c) 2025 Erbosoft Metaverse Design Solutions, All Rights Reserved
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
// Package util contains utility definitions.
package util
import (
"crypto/rand"
"io"
)
// authAlphabet is the set of characters from which we generate auth strings.
const authAlphabet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz./"
// authStringLen is the standard lengtth of authentication strings.
const authStringLen = 32
// GenerateRandomAuthString generates a random authentication string.
func GenerateRandomAuthString() string {
b := make([]byte, authStringLen)
if _, err := io.ReadFull(rand.Reader, b); err != nil {
// can't happen (at least on a modern OS)
panic("failed to read random: " + err.Error())
}
for i := 0; i < authStringLen; i++ {
b[i] = authAlphabet[int(b[i])%len(authAlphabet)]
}
return string(b)
}