2022-04-15 09:42:07 +02:00
|
|
|
package clean
|
2021-12-14 18:34:52 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
// Token returns the sanitized token string with a length of up to 4096 characters.
|
2021-12-14 18:34:52 +01:00
|
|
|
func Token(s string) string {
|
2022-09-28 09:01:17 +02:00
|
|
|
if s == "" || reject(s, MaxLength) {
|
2021-12-15 12:24:05 +01:00
|
|
|
return ""
|
2021-12-14 18:34:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
2022-09-28 09:01:17 +02:00
|
|
|
|
|
|
|
// 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)
|
|
|
|
}
|