-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathidentifier_helpers.go
More file actions
79 lines (69 loc) · 2.38 KB
/
identifier_helpers.go
File metadata and controls
79 lines (69 loc) · 2.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package agentremote
import (
"fmt"
"net/http"
"net/url"
"strings"
"time"
"github.com/google/uuid"
"maunium.net/go/mautrix/bridgev2"
"maunium.net/go/mautrix/bridgev2/networkid"
"maunium.net/go/mautrix/id"
)
func MatrixMessageID(eventID id.EventID) networkid.MessageID {
return networkid.MessageID("mx:" + string(eventID))
}
// NewEventID generates a unique Matrix-style event ID with the given prefix.
func NewEventID(prefix string) id.EventID {
return id.EventID(fmt.Sprintf("$%s-%s", prefix, uuid.NewString()))
}
func HumanUserID(prefix string, loginID networkid.UserLoginID) networkid.UserID {
return networkid.UserID(prefix + ":" + string(loginID))
}
// MakeUserLoginID creates a login ID in the format "prefix:escaped-mxid[:ordinal]".
func MakeUserLoginID(prefix string, mxid id.UserID, ordinal int) networkid.UserLoginID {
escaped := url.PathEscape(string(mxid))
base := networkid.UserLoginID(fmt.Sprintf("%s:%s", prefix, escaped))
if ordinal <= 1 {
return base
}
return networkid.UserLoginID(fmt.Sprintf("%s:%d", base, ordinal))
}
// NextUserLoginID finds the next available ordinal for a login ID with the given prefix.
func NextUserLoginID(user *bridgev2.User, prefix string) networkid.UserLoginID {
used := map[string]struct{}{}
for _, existing := range user.GetUserLogins() {
if existing == nil {
continue
}
used[string(existing.ID)] = struct{}{}
}
for ordinal := 1; ordinal <= len(used)+1; ordinal++ {
loginID := MakeUserLoginID(prefix, user.MXID, ordinal)
if _, ok := used[string(loginID)]; !ok {
return loginID
}
}
// Should be unreachable: there are at most len(used) occupied ordinals,
// so ordinal len(used)+1 must be free. Fall back to a safe default.
return MakeUserLoginID(prefix, user.MXID, len(used)+1)
}
// NewTurnID generates a new unique, sortable turn ID using a timestamp-based format.
func NewTurnID() string {
return "turn_" + strings.ReplaceAll(time.Now().UTC().Format("20060102T150405.000000000"), ".", "")
}
func SingleLoginFlow(enabled bool, flow bridgev2.LoginFlow) []bridgev2.LoginFlow {
if !enabled {
return nil
}
return []bridgev2.LoginFlow{flow}
}
func ValidateSingleLoginFlow(flowID, expectedFlowID string, enabled bool) error {
if flowID != expectedFlowID {
return bridgev2.ErrInvalidLoginFlowID
}
if !enabled {
return NewLoginRespError(http.StatusForbidden, "This login flow is disabled.", "LOGIN", "DISABLED")
}
return nil
}