2020-07-13 15:02:35 +00:00
|
|
|
// Copyright 2020 The Matrix.org Foundation C.I.C.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package api
|
|
|
|
|
2020-07-14 11:59:07 +00:00
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
2020-07-15 11:02:34 +00:00
|
|
|
"strings"
|
2020-07-15 17:40:41 +00:00
|
|
|
"time"
|
2020-07-14 11:59:07 +00:00
|
|
|
)
|
2020-07-13 15:02:35 +00:00
|
|
|
|
|
|
|
type KeyInternalAPI interface {
|
|
|
|
PerformUploadKeys(ctx context.Context, req *PerformUploadKeysRequest, res *PerformUploadKeysResponse)
|
2020-07-21 13:47:53 +00:00
|
|
|
// PerformClaimKeys claims one-time keys for use in pre-key messages
|
2020-07-13 15:02:35 +00:00
|
|
|
PerformClaimKeys(ctx context.Context, req *PerformClaimKeysRequest, res *PerformClaimKeysResponse)
|
|
|
|
QueryKeys(ctx context.Context, req *QueryKeysRequest, res *QueryKeysResponse)
|
2020-07-28 17:25:16 +00:00
|
|
|
QueryKeyChanges(ctx context.Context, req *QueryKeyChangesRequest, res *QueryKeyChangesResponse)
|
2020-07-13 15:02:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// KeyError is returned if there was a problem performing/querying the server
|
|
|
|
type KeyError struct {
|
2020-07-15 11:02:34 +00:00
|
|
|
Err string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (k *KeyError) Error() string {
|
|
|
|
return k.Err
|
2020-07-13 15:02:35 +00:00
|
|
|
}
|
|
|
|
|
2020-07-14 11:59:07 +00:00
|
|
|
// DeviceKeys represents a set of device keys for a single device
|
|
|
|
// https://matrix.org/docs/spec/client_server/r0.6.1#post-matrix-client-r0-keys-upload
|
|
|
|
type DeviceKeys struct {
|
|
|
|
// The user who owns this device
|
|
|
|
UserID string
|
|
|
|
// The device ID of this device
|
|
|
|
DeviceID string
|
|
|
|
// The raw device key JSON
|
|
|
|
KeyJSON []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
// OneTimeKeys represents a set of one-time keys for a single device
|
|
|
|
// https://matrix.org/docs/spec/client_server/r0.6.1#post-matrix-client-r0-keys-upload
|
|
|
|
type OneTimeKeys struct {
|
|
|
|
// The user who owns this device
|
|
|
|
UserID string
|
|
|
|
// The device ID of this device
|
|
|
|
DeviceID string
|
|
|
|
// A map of algorithm:key_id => key JSON
|
|
|
|
KeyJSON map[string]json.RawMessage
|
|
|
|
}
|
|
|
|
|
2020-07-15 11:02:34 +00:00
|
|
|
// Split a key in KeyJSON into algorithm and key ID
|
|
|
|
func (k *OneTimeKeys) Split(keyIDWithAlgo string) (algo string, keyID string) {
|
|
|
|
segments := strings.Split(keyIDWithAlgo, ":")
|
|
|
|
return segments[0], segments[1]
|
|
|
|
}
|
|
|
|
|
2020-07-14 11:59:07 +00:00
|
|
|
// OneTimeKeysCount represents the counts of one-time keys for a single device
|
|
|
|
type OneTimeKeysCount struct {
|
|
|
|
// The user who owns this device
|
|
|
|
UserID string
|
|
|
|
// The device ID of this device
|
|
|
|
DeviceID string
|
|
|
|
// algorithm to count e.g:
|
|
|
|
// {
|
|
|
|
// "curve25519": 10,
|
|
|
|
// "signed_curve25519": 20
|
|
|
|
// }
|
|
|
|
KeyCount map[string]int
|
|
|
|
}
|
|
|
|
|
|
|
|
// PerformUploadKeysRequest is the request to PerformUploadKeys
|
2020-07-13 15:02:35 +00:00
|
|
|
type PerformUploadKeysRequest struct {
|
2020-07-14 11:59:07 +00:00
|
|
|
DeviceKeys []DeviceKeys
|
|
|
|
OneTimeKeys []OneTimeKeys
|
2020-07-13 15:02:35 +00:00
|
|
|
}
|
|
|
|
|
2020-07-14 11:59:07 +00:00
|
|
|
// PerformUploadKeysResponse is the response to PerformUploadKeys
|
2020-07-13 15:02:35 +00:00
|
|
|
type PerformUploadKeysResponse struct {
|
2020-07-15 11:02:34 +00:00
|
|
|
// A fatal error when processing e.g database failures
|
2020-07-13 15:02:35 +00:00
|
|
|
Error *KeyError
|
2020-07-14 11:59:07 +00:00
|
|
|
// A map of user_id -> device_id -> Error for tracking failures.
|
|
|
|
KeyErrors map[string]map[string]*KeyError
|
|
|
|
OneTimeKeyCounts []OneTimeKeysCount
|
|
|
|
}
|
|
|
|
|
|
|
|
// KeyError sets a key error field on KeyErrors
|
|
|
|
func (r *PerformUploadKeysResponse) KeyError(userID, deviceID string, err *KeyError) {
|
|
|
|
if r.KeyErrors[userID] == nil {
|
|
|
|
r.KeyErrors[userID] = make(map[string]*KeyError)
|
|
|
|
}
|
|
|
|
r.KeyErrors[userID][deviceID] = err
|
2020-07-13 15:02:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type PerformClaimKeysRequest struct {
|
2020-07-21 13:47:53 +00:00
|
|
|
// Map of user_id to device_id to algorithm name
|
|
|
|
OneTimeKeys map[string]map[string]string
|
|
|
|
Timeout time.Duration
|
2020-07-13 15:02:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type PerformClaimKeysResponse struct {
|
2020-07-21 13:47:53 +00:00
|
|
|
// Map of user_id to device_id to algorithm:key_id to key JSON
|
|
|
|
OneTimeKeys map[string]map[string]map[string]json.RawMessage
|
|
|
|
// Map of remote server domain to error JSON
|
|
|
|
Failures map[string]interface{}
|
|
|
|
// Set if there was a fatal error processing this action
|
2020-07-13 15:02:35 +00:00
|
|
|
Error *KeyError
|
|
|
|
}
|
|
|
|
|
|
|
|
type QueryKeysRequest struct {
|
2020-07-15 17:40:41 +00:00
|
|
|
// Maps user IDs to a list of devices
|
|
|
|
UserToDevices map[string][]string
|
|
|
|
Timeout time.Duration
|
2020-07-13 15:02:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type QueryKeysResponse struct {
|
2020-07-15 17:40:41 +00:00
|
|
|
// Map of remote server domain to error JSON
|
|
|
|
Failures map[string]interface{}
|
|
|
|
// Map of user_id to device_id to device_key
|
|
|
|
DeviceKeys map[string]map[string]json.RawMessage
|
|
|
|
// Set if there was a fatal error processing this query
|
2020-07-13 15:02:35 +00:00
|
|
|
Error *KeyError
|
|
|
|
}
|
2020-07-28 17:25:16 +00:00
|
|
|
|
|
|
|
type QueryKeyChangesRequest struct {
|
|
|
|
// The partition which had key events sent to
|
|
|
|
Partition int32
|
|
|
|
// The offset of the last received key event, or sarama.OffsetOldest if this is from the beginning
|
|
|
|
Offset int64
|
|
|
|
}
|
|
|
|
|
|
|
|
type QueryKeyChangesResponse struct {
|
|
|
|
// The set of users who have had their keys change.
|
|
|
|
UserIDs []string
|
2020-07-30 10:15:46 +00:00
|
|
|
// The partition being served - useful if the partition is unknown at request time
|
|
|
|
Partition int32
|
2020-07-28 17:25:16 +00:00
|
|
|
// The latest offset represented in this response.
|
|
|
|
Offset int64
|
|
|
|
// Set if there was a problem handling the request.
|
|
|
|
Error *KeyError
|
|
|
|
}
|