MCPcopy Create free account
hub / github.com/daodst/chat / ParseUsernameParam

Function ParseUsernameParam

clientapi/userutil/userutil.go:33–50  ·  view source on GitHub ↗

ParseUsernameParam extracts localpart from usernameParam. usernameParam can either be a user ID or just the localpart/username. If serverName is passed, it is verified against the domain obtained from usernameParam (if present) Returns error in case of invalid usernameParam.

(usernameParam string, expectedServerName *gomatrixserverlib.ServerName)

Source from the content-addressed store, hash-verified

31// If serverName is passed, it is verified against the domain obtained from usernameParam (if present)
32// Returns error in case of invalid usernameParam.
33func ParseUsernameParam(usernameParam string, expectedServerName *gomatrixserverlib.ServerName) (string, error) {
34 localpart := usernameParam
35
36 if strings.HasPrefix(usernameParam, "@") {
37 lp, domain, err := gomatrixserverlib.SplitID('@', usernameParam)
38
39 if err != nil {
40 return "", errors.New("invalid username")
41 }
42
43 if expectedServerName != nil && domain != *expectedServerName {
44 return "", errors.New("user ID does not belong to this server")
45 }
46
47 localpart = lp
48 }
49 return localpart, nil
50}
51
52// MakeUserID generates user ID from localpart & server name
53func MakeUserID(localpart string, server gomatrixserverlib.ServerName) string {

Callers 4

TestGoodUserIDFunction · 0.85
TestWithLocalpartOnlyFunction · 0.85
TestIncorrectDomainFunction · 0.85
TestBadUserIDFunction · 0.85

Calls

no outgoing calls

Tested by 4

TestGoodUserIDFunction · 0.68
TestWithLocalpartOnlyFunction · 0.68
TestIncorrectDomainFunction · 0.68
TestBadUserIDFunction · 0.68