photoprism/pkg/rnd/session.go
Michael Mayer 0d2f8be522 Auth: Use hashed auth tokens for enhanced security #3943 #808 #782
Signed-off-by: Michael Mayer <michael@photoprism.app>
2024-01-06 17:35:19 +01:00

41 lines
647 B
Go

package rnd
import (
"crypto/rand"
"fmt"
"log"
)
// AuthToken returns a new session id.
func AuthToken() string {
b := make([]byte, 24)
if _, err := rand.Read(b); err != nil {
log.Fatal(err)
}
return fmt.Sprintf("%x", b)
}
// IsAuthToken checks if the string is a session id.
func IsAuthToken(s string) bool {
if len(s) != 48 {
return false
}
return IsHex(s)
}
// SessionID returns the hashed session id string.
func SessionID(s string) string {
return Sha256([]byte(s))
}
// IsSessionID checks if the string is a session id string.
func IsSessionID(s string) bool {
if len(s) != 64 {
return false
}
return IsHex(s)
}