Loosen restrictions for IAM user names

The only restriction for an IAM username is that it cannot start with
a '$'. An username that contains a ':' must escape it with another ':'
for use in a token for RTMP or SRT.
This commit is contained in:
Ingo Oppermann
2023-06-26 13:49:53 +02:00
parent 6f47f96f6e
commit abfe4918b4
8 changed files with 124 additions and 36 deletions

45
encoding/token/token.go Normal file
View File

@@ -0,0 +1,45 @@
package token
import "strings"
func Marshal(username, token string) string {
username = strings.ReplaceAll(username, ":", "::")
return username + ":" + token
}
// Unmarshal splits a username/token combination into a username and
// token. If the input doesn't contain a username, the whole input
// is returned as token.
func Unmarshal(usertoken string) (string, string) {
r := []rune(usertoken)
count := 0
index := -1
for i, ru := range r {
if ru == ':' {
count++
continue
}
if count > 0 && count%2 != 0 {
index = i - 1
break
}
count = 0
}
if index == -1 {
return "", usertoken
}
before, after := r[:index], r[index+1:]
username := string(before)
token := string(after)
username = strings.ReplaceAll(username, "::", ":")
return username, token
}