ValidatePrincipalName performs the same checks as IsValidPrincipalName, but adds length check and returns a more verbose error message. This function is slower than IsValidPrincipalName, and should be used only for user and role creation. Names should have a max length of 239 chars, to account for
(name string)
| 164 | // and role creation. Names should have a max length of 239 chars, to account for SG prefixes. All validation |
| 165 | // errors are concatenated and returned as one error message. |
| 166 | func ValidatePrincipalName(name string) error { |
| 167 | namelen := len(name) |
| 168 | if namelen == 0 { |
| 169 | return nil // guest user |
| 170 | } |
| 171 | |
| 172 | const validationMsg = "invalid name: " |
| 173 | msgs := make([]string, 0, 4) |
| 174 | |
| 175 | if namelen > base.MaxPrincipalNameLen { |
| 176 | const msg = "length exceeds 239" // leaving as const to avoid fmt performance (21% slower) |
| 177 | msgs = append(msgs, msg) |
| 178 | } |
| 179 | |
| 180 | if !utf8.ValidString(name) { |
| 181 | const msg = "non UTF-8 encoding" |
| 182 | msgs = append(msgs, msg) |
| 183 | } |
| 184 | |
| 185 | seenAnInvalid := false |
| 186 | seenAnAlphanum := false |
| 187 | for _, char := range name { |
| 188 | // Reasons for forbidding each of these: |
| 189 | // colons: basic authentication uses them to separate usernames from passwords |
| 190 | // commas: fails channels.IsValidChannel, which channels.compileAccessMap uses via SetFromArray |
| 191 | // slashes: would need to make many (possibly breaking) changes to routing |
| 192 | // backticks: MB-50619 |
| 193 | if (char == '/' || char == ':' || char == ',' || char == '`') && !seenAnInvalid { |
| 194 | seenAnInvalid = true |
| 195 | const msg = "contains '/', ':', ',', or '`'" |
| 196 | msgs = append(msgs, msg) |
| 197 | if seenAnAlphanum { |
| 198 | break |
| 199 | } |
| 200 | } |
| 201 | if !seenAnAlphanum && (unicode.IsLetter(char) || unicode.IsNumber(char)) { |
| 202 | seenAnAlphanum = true |
| 203 | if seenAnInvalid { |
| 204 | break |
| 205 | } |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | if !seenAnAlphanum { |
| 210 | const msg = "must contain alphanumeric" |
| 211 | msgs = append(msgs, msg) |
| 212 | } |
| 213 | |
| 214 | if len(msgs) > 0 { |
| 215 | return errors.New(validationMsg + strings.Join(msgs, "; ")) |
| 216 | } |
| 217 | return nil |
| 218 | } |
| 219 | |
| 220 | // Creates a new Role object. |
| 221 | func (auth *Authenticator) NewRole(name string, channels base.Set) (Role, error) { |
no outgoing calls