photoprism/pkg/clean/token.go
Michael Mayer f5a8c5a45d Auth: Session and ACL enhancements #98 #1746
Signed-off-by: Michael Mayer <michael@photoprism.app>
2022-09-28 09:01:17 +02:00

41 lines
812 B
Go

package clean
import (
"strings"
)
// Token returns the sanitized token string with a length of up to 4096 characters.
func Token(s string) string {
if s == "" || reject(s, MaxLength) {
return ""
}
// Remove all invalid characters.
s = strings.Map(func(r rune) rune {
if (r < '0' || r > '9') && (r < 'a' || r > 'z') && (r < 'A' || r > 'Z') && r != '-' && r != '_' && r != ':' {
return -1
}
return r
}, s)
return s
}
// UrlToken returns the sanitized URL token with a length of up to 42 characters.
func UrlToken(s string) string {
if s == "" || len(s) > 64 {
return ""
}
return Token(s)
}
// ShareToken returns the sanitized link share token with a length of up to 160 characters.
func ShareToken(s string) string {
if s == "" || len(s) > 160 {
return ""
}
return Token(s)
}