41 lines
900 B
Go
41 lines
900 B
Go
package models
|
||
|
||
import (
|
||
"regexp"
|
||
"strings"
|
||
)
|
||
|
||
const (
|
||
MaxAuthClientIDLen = 64
|
||
MaxAuthClientNameLen = 128
|
||
)
|
||
|
||
var authClientIDRe = regexp.MustCompile(`^[a-zA-Z0-9][a-zA-Z0-9_.:-]{0,63}$`)
|
||
|
||
// AuthClientEntry 记录某第三方应用曾用本账号完成认证(登录 / 校验令牌 / 拉取 me)。
|
||
type AuthClientEntry struct {
|
||
ClientID string `json:"clientId"`
|
||
DisplayName string `json:"displayName,omitempty"`
|
||
FirstSeenAt string `json:"firstSeenAt"`
|
||
LastSeenAt string `json:"lastSeenAt"`
|
||
}
|
||
|
||
func NormalizeAuthClientID(raw string) (string, bool) {
|
||
s := strings.TrimSpace(raw)
|
||
if s == "" || len(s) > MaxAuthClientIDLen {
|
||
return "", false
|
||
}
|
||
if !authClientIDRe.MatchString(s) {
|
||
return "", false
|
||
}
|
||
return s, true
|
||
}
|
||
|
||
func ClampAuthClientName(raw string) string {
|
||
s := strings.TrimSpace(raw)
|
||
if len(s) > MaxAuthClientNameLen {
|
||
return s[:MaxAuthClientNameLen]
|
||
}
|
||
return s
|
||
}
|