From 26822b6b492228060906ffbdfcc1d7899ee83db1 Mon Sep 17 00:00:00 2001 From: Raja Subramanian Date: Sat, 22 Mar 2025 13:16:13 +0530 Subject: [PATCH] ParseUsername utility for TURN user name. (#3547) * ParseUsername utility for TURN user name. NOTE: There is no state, so no need for struct method, but just doing it similar to CreateUsername which also does not have state, but uses struct method. * missed base62 decode --- pkg/service/turn.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/pkg/service/turn.go b/pkg/service/turn.go index dc2d21b7a..f37ff3e6b 100644 --- a/pkg/service/turn.go +++ b/pkg/service/turn.go @@ -164,6 +164,19 @@ func (h *TURNAuthHandler) CreateUsername(apiKey string, pID livekit.ParticipantI return base62.EncodeToString([]byte(fmt.Sprintf("%s|%s", apiKey, pID))) } +func (h *TURNAuthHandler) ParseUsername(username string) (apiKey string, pID livekit.ParticipantID, err error) { + decoded, err := base62.DecodeString(username) + if err != nil { + return "", "", err + } + parts := strings.Split(string(decoded), "|") + if len(parts) != 2 { + return "", "", errors.New("invalid username") + } + + return parts[0], livekit.ParticipantID(parts[1]), nil +} + func (h *TURNAuthHandler) CreatePassword(apiKey string, pID livekit.ParticipantID) (string, error) { secret := h.keyProvider.GetSecret(apiKey) if secret == "" {