mirror of
https://github.com/matrix-org/dendrite.git
synced 2025-12-16 11:23:11 -06:00
Progress in /query, /claim FED,CS
This commit is contained in:
parent
f928f570c0
commit
d3a099f22b
|
|
@ -183,13 +183,17 @@ func ClaimOneTimeKeys(
|
||||||
encryptionDB *storage.Database,
|
encryptionDB *storage.Database,
|
||||||
) util.JSONResponse {
|
) util.JSONResponse {
|
||||||
var claimRq types.ClaimRequest
|
var claimRq types.ClaimRequest
|
||||||
claimRp := types.ClaimResponse{}
|
claimRes := types.ClaimResponse{}
|
||||||
claimRp.Failures = make(map[string]interface{})
|
claimRes.Failures = make(map[string]interface{})
|
||||||
claimRp.ClaimBody = make(map[string]map[string]map[string]interface{})
|
claimRes.OneTimeKeys = make(map[string]map[string]map[string]interface{})
|
||||||
if reqErr := httputil.UnmarshalJSONRequest(req, &claimRq); reqErr != nil {
|
if reqErr := httputil.UnmarshalJSONRequest(req, &claimRq); reqErr != nil {
|
||||||
return *reqErr
|
return *reqErr
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var obtainedFromFed types.QueryResponse
|
||||||
|
obtainedKeysFromFed := obtainedFromFed.DeviceKeys
|
||||||
|
claimRes.OneTimeKeys = obtainedKeysFromFed
|
||||||
|
|
||||||
// not sure what FED should return here
|
// not sure what FED should return here
|
||||||
/*
|
/*
|
||||||
federation consideration: when user id is in federation, a query is needed to ask fed for keys
|
federation consideration: when user id is in federation, a query is needed to ask fed for keys
|
||||||
|
|
@ -206,9 +210,9 @@ func ClaimOneTimeKeys(
|
||||||
}()
|
}()
|
||||||
select {
|
select {
|
||||||
case <-stimuCh:
|
case <-stimuCh:
|
||||||
claimRp.Failures = make(map[string]interface{})
|
claimRes.Failures = make(map[string]interface{})
|
||||||
// todo: key in this map is restricted to username at the end, yet a mocked one.
|
// todo: key in this map is restricted to username at the end, yet a mocked one.
|
||||||
claimRp.Failures["@alice:localhost"] = "ran out of offered time"
|
claimRes.Failures["@alice:localhost"] = "ran out of offered time"
|
||||||
case <-make(chan interface{}):
|
case <-make(chan interface{}):
|
||||||
// todo : here goes federation chan , still a mocked one
|
// todo : here goes federation chan , still a mocked one
|
||||||
}
|
}
|
||||||
|
|
@ -219,40 +223,9 @@ func ClaimOneTimeKeys(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
content := claimRq.ClaimDetail
|
|
||||||
for uid, detail := range content {
|
|
||||||
for deviceID, alg := range detail {
|
|
||||||
var algTyp int
|
|
||||||
if strings.Contains(alg, "signed") {
|
|
||||||
algTyp = ONETIMEKEYOBJECT
|
|
||||||
} else {
|
|
||||||
algTyp = ONETIMEKEYSTRING
|
|
||||||
}
|
|
||||||
key, err := pickOne(req.Context(), *encryptionDB, uid, deviceID, alg)
|
|
||||||
if err != nil {
|
|
||||||
claimRp.Failures[uid] = fmt.Sprintf("%s: %s", "failed to get keys for device", deviceID)
|
|
||||||
}
|
|
||||||
claimRp.ClaimBody[uid] = make(map[string]map[string]interface{})
|
|
||||||
keyPreMap := claimRp.ClaimBody[uid]
|
|
||||||
keymap := keyPreMap[deviceID]
|
|
||||||
if keymap == nil {
|
|
||||||
keymap = make(map[string]interface{})
|
|
||||||
}
|
|
||||||
switch algTyp {
|
|
||||||
case ONETIMEKEYSTRING:
|
|
||||||
keymap[fmt.Sprintf("%s:%s", alg, key.KeyID)] = key.Key
|
|
||||||
case ONETIMEKEYOBJECT:
|
|
||||||
sig := make(map[string]map[string]string)
|
|
||||||
sig[uid] = make(map[string]string)
|
|
||||||
sig[uid][fmt.Sprintf("%s:%s", "ed25519", deviceID)] = key.Signature
|
|
||||||
keymap[fmt.Sprintf("%s:%s", alg, key.KeyID)] = types.KeyObject{Key: key.Key, Signature: sig}
|
|
||||||
}
|
|
||||||
claimRp.ClaimBody[uid][deviceID] = keymap
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return util.JSONResponse{
|
return util.JSONResponse{
|
||||||
Code: http.StatusOK,
|
Code: http.StatusOK,
|
||||||
JSON: claimRp,
|
JSON: claimRes,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -403,15 +376,6 @@ func persistAl(
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func pickOne(
|
|
||||||
ctx context.Context,
|
|
||||||
encryptDB storage.Database,
|
|
||||||
uid, device, al string,
|
|
||||||
) (key types.KeyHolder, err error) {
|
|
||||||
key, err = encryptDB.SelectOneTimeKeySingle(ctx, uid, device, al)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
func upnotify(userID string) {
|
func upnotify(userID string) {
|
||||||
m := sarama.ProducerMessage{
|
m := sarama.ProducerMessage{
|
||||||
Topic: "keyUpdate",
|
Topic: "keyUpdate",
|
||||||
|
|
|
||||||
|
|
@ -17,11 +17,11 @@ package types
|
||||||
// ClaimRequest structure
|
// ClaimRequest structure
|
||||||
type ClaimRequest struct {
|
type ClaimRequest struct {
|
||||||
Timeout int64 `json:"timeout"`
|
Timeout int64 `json:"timeout"`
|
||||||
ClaimDetail map[string]map[string]string `json:"one_time_keys"`
|
OneTimeKeys map[string]map[string]string `json:"one_time_keys"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// ClaimResponse structure
|
// ClaimResponse structure
|
||||||
type ClaimResponse struct {
|
type ClaimResponse struct {
|
||||||
Failures map[string]interface{} `json:"failures"`
|
Failures map[string]interface{} `json:"failures"`
|
||||||
ClaimBody map[string]map[string]map[string]interface{} `json:"one_time_keys"`
|
OneTimeKeys map[string]map[string]map[string]interface{} `json:"one_time_keys"`
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
// Copyright Sumukha PK 2019
|
// Copyright 2019 Sumukha PK
|
||||||
//
|
//
|
||||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
// you may not use this file except in compliance with the License.
|
// you may not use this file except in compliance with the License.
|
||||||
|
|
@ -18,6 +18,7 @@ import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/matrix-org/dendrite/clientapi/auth/storage/devices"
|
"github.com/matrix-org/dendrite/clientapi/auth/storage/devices"
|
||||||
|
|
||||||
|
|
@ -29,16 +30,72 @@ import (
|
||||||
"github.com/matrix-org/util"
|
"github.com/matrix-org/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
// ONETIMEKEYSTRING key string
|
||||||
|
ONETIMEKEYSTRING = iota
|
||||||
|
// ONETIMEKEYOBJECT key object
|
||||||
|
ONETIMEKEYOBJECT
|
||||||
|
)
|
||||||
|
|
||||||
|
// ONETIMEKEYSTR stands for storage string property
|
||||||
|
const ONETIMEKEYSTR = "one_time_key"
|
||||||
|
|
||||||
|
// DEVICEKEYSTR stands for storage string property
|
||||||
|
const DEVICEKEYSTR = "device_key"
|
||||||
|
|
||||||
// ClaimKeys provides the e2ee keys of the user
|
// ClaimKeys provides the e2ee keys of the user
|
||||||
func ClaimKeys(
|
func ClaimKeys(
|
||||||
httpReq *http.Request,
|
httpReq *http.Request,
|
||||||
request *gomatrixserverlib.FederationRequest,
|
request *gomatrixserverlib.FederationRequest,
|
||||||
encryptionDB *storage.Database,
|
encryptionDB *storage.Database,
|
||||||
) util.JSONResponse {
|
) util.JSONResponse {
|
||||||
|
var claimReq types.ClaimRequest
|
||||||
|
claimRes := types.ClaimResponse{}
|
||||||
|
claimRes.OneTimeKeys = make(map[string]map[string]map[string]interface{})
|
||||||
|
if reqErr := httputil.UnmarshalJSONRequest(httpReq, &claimReq); reqErr != nil {
|
||||||
|
return *reqErr
|
||||||
|
}
|
||||||
|
|
||||||
|
content := claimReq.OneTimeKeys
|
||||||
|
for uid, detail := range content {
|
||||||
|
for deviceID, alg := range detail {
|
||||||
|
var algTyp int
|
||||||
|
if strings.Contains(alg, "signed") {
|
||||||
|
algTyp = ONETIMEKEYOBJECT
|
||||||
|
} else {
|
||||||
|
algTyp = ONETIMEKEYSTRING
|
||||||
|
}
|
||||||
|
key, err := pickOne(httpReq.Context(), *encryptionDB, uid, deviceID, alg)
|
||||||
|
if err != nil {
|
||||||
|
// send a better response in order to capture failures on the other part
|
||||||
|
return util.JSONResponse{
|
||||||
|
Code: http.StatusInternalServerError,
|
||||||
|
JSON: struct{}{},
|
||||||
|
}
|
||||||
|
// claimRes.Failures[uid] = fmt.Sprintf("%s: %s", "failed to get keys for device", deviceID)
|
||||||
|
}
|
||||||
|
claimRes.OneTimeKeys[uid] = make(map[string]map[string]interface{})
|
||||||
|
keyPreMap := claimRes.OneTimeKeys[uid]
|
||||||
|
keymap := keyPreMap[deviceID]
|
||||||
|
if keymap == nil {
|
||||||
|
keymap = make(map[string]interface{})
|
||||||
|
}
|
||||||
|
switch algTyp {
|
||||||
|
case ONETIMEKEYSTRING:
|
||||||
|
keymap[fmt.Sprintf("%s:%s", alg, key.KeyID)] = key.Key
|
||||||
|
case ONETIMEKEYOBJECT:
|
||||||
|
sig := make(map[string]map[string]string)
|
||||||
|
sig[uid] = make(map[string]string)
|
||||||
|
sig[uid][fmt.Sprintf("%s:%s", "ed25519", deviceID)] = key.Signature
|
||||||
|
keymap[fmt.Sprintf("%s:%s", alg, key.KeyID)] = types.KeyObject{Key: key.Key, Signatures: sig}
|
||||||
|
}
|
||||||
|
claimRes.OneTimeKeys[uid][deviceID] = keymap
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return util.JSONResponse{
|
return util.JSONResponse{
|
||||||
Code: http.StatusOK,
|
Code: http.StatusOK,
|
||||||
JSON: struct{}{},
|
JSON: claimRes,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -143,3 +200,12 @@ func takeAlgo(
|
||||||
al, err = encryptDB.SelectAlgo(ctx, uid, device)
|
al, err = encryptDB.SelectAlgo(ctx, uid, device)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func pickOne(
|
||||||
|
ctx context.Context,
|
||||||
|
encryptDB storage.Database,
|
||||||
|
uid, device, al string,
|
||||||
|
) (key types.KeyHolder, err error) {
|
||||||
|
key, err = encryptDB.SelectOneTimeKeySingle(ctx, uid, device, al)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
|
||||||
31
federationapi/types/claim.go
Normal file
31
federationapi/types/claim.go
Normal file
|
|
@ -0,0 +1,31 @@
|
||||||
|
// Copyright 2019 Sumukha PK
|
||||||
|
//
|
||||||
|
// 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 types
|
||||||
|
|
||||||
|
// ClaimRequest structure
|
||||||
|
type ClaimRequest struct {
|
||||||
|
OneTimeKeys map[string]map[string]string `json:"one_time_keys"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// ClaimResponse structure
|
||||||
|
type ClaimResponse struct {
|
||||||
|
OneTimeKeys map[string]map[string]map[string]interface{}
|
||||||
|
}
|
||||||
|
|
||||||
|
// KeyObject structure
|
||||||
|
type KeyObject struct {
|
||||||
|
Key string `json:"key"`
|
||||||
|
Signatures map[string]string `json:"signatures"`
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue