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

Method handleLocalKeys

federationapi/internal/keys.go:96–143  ·  view source on GitHub ↗

handleLocalKeys handles cases where the key request contains a request for our own server keys, either current or old.

(
	_ context.Context,
	requests map[gomatrixserverlib.PublicKeyLookupRequest]gomatrixserverlib.Timestamp,
	results map[gomatrixserverlib.PublicKeyLookupRequest]gomatrixserverlib.PublicKeyLookupResult,
)

Source from the content-addressed store, hash-verified

94// handleLocalKeys handles cases where the key request contains
95// a request for our own server keys, either current or old.
96func (s *FederationInternalAPI) handleLocalKeys(
97 _ context.Context,
98 requests map[gomatrixserverlib.PublicKeyLookupRequest]gomatrixserverlib.Timestamp,
99 results map[gomatrixserverlib.PublicKeyLookupRequest]gomatrixserverlib.PublicKeyLookupResult,
100) {
101 for req := range requests {
102 if req.ServerName != s.cfg.Matrix.ServerName {
103 continue
104 }
105 if req.KeyID == s.cfg.Matrix.KeyID {
106 // We found a key request that is supposed to be for our own
107 // keys. Remove it from the request list so we don't hit the
108 // database or the fetchers for it.
109 delete(requests, req)
110
111 // Insert our own key into the response.
112 results[req] = gomatrixserverlib.PublicKeyLookupResult{
113 VerifyKey: gomatrixserverlib.VerifyKey{
114 Key: gomatrixserverlib.Base64Bytes(s.cfg.Matrix.PrivateKey.Public().(ed25519.PublicKey)),
115 },
116 ExpiredTS: gomatrixserverlib.PublicKeyNotExpired,
117 ValidUntilTS: gomatrixserverlib.AsTimestamp(time.Now().Add(s.cfg.Matrix.KeyValidityPeriod)),
118 }
119 } else {
120 // The key request doesn't match our current key. Let's see
121 // if it matches any of our old verify keys.
122 for _, oldVerifyKey := range s.cfg.Matrix.OldVerifyKeys {
123 if req.KeyID == oldVerifyKey.KeyID {
124 // We found a key request that is supposed to be an expired
125 // key.
126 delete(requests, req)
127
128 // Insert our own key into the response.
129 results[req] = gomatrixserverlib.PublicKeyLookupResult{
130 VerifyKey: gomatrixserverlib.VerifyKey{
131 Key: gomatrixserverlib.Base64Bytes(oldVerifyKey.PrivateKey.Public().(ed25519.PublicKey)),
132 },
133 ExpiredTS: oldVerifyKey.ExpiredAt,
134 ValidUntilTS: gomatrixserverlib.PublicKeyNotValid,
135 }
136
137 // No need to look at the other keys.
138 break
139 }
140 }
141 }
142 }
143}
144
145// handleDatabaseKeys handles cases where the key requests can be
146// satisfied from our local database/cache.

Callers 1

FetchKeysMethod · 0.95

Calls 1

AddMethod · 0.80

Tested by

no test coverage detected