photoprism/pkg/authn/username.go
Michael Mayer 713593da4e Auth: Add CLI command to create access tokens for apps #782 #808 #3943
You can now run "photoprism auth add" to create new client access tokens
that allow external applications to use the built-in REST API.

Signed-off-by: Michael Mayer <michael@photoprism.app>
2024-01-05 16:31:07 +01:00

37 lines
762 B
Go

package authn
import (
"errors"
"strings"
"github.com/photoprism/photoprism/pkg/clean"
"github.com/photoprism/photoprism/pkg/rnd"
"github.com/photoprism/photoprism/pkg/txt"
)
var (
ErrEmpty = errors.New("empty")
ErrTooLong = errors.New("too long")
ErrInvalid = errors.New("invalid")
ErrReserved = errors.New("reserved")
)
// Username checks if the name provided is invalid or reserved.
func Username(name string) (sanitized string, err error) {
if name == "" {
return "", ErrEmpty
} else if len(name) > txt.ClipEmail {
return "", ErrTooLong
}
s := clean.Username(name)
switch {
case s == "" || !strings.EqualFold(s, name):
return s, ErrInvalid
case rnd.IsUID(s, 'c'), s == "visitor":
return s, ErrReserved
}
return s, err
}