mirror of
https://github.com/matrix-org/dendrite.git
synced 2025-12-12 09:23:09 -06:00
add blurbs and comment for nested code and each important function
This commit is contained in:
parent
e5effc328c
commit
40955a378a
|
|
@ -23,6 +23,10 @@ import (
|
|||
"fmt"
|
||||
)
|
||||
|
||||
// in order to gain key management capability
|
||||
// , CMD should involve this invoke into main function
|
||||
// , a setup need an assemble of i.e configs as base and
|
||||
// accountDB and deviceDB
|
||||
func SetupEcryptoapi(
|
||||
base *basecomponent.BaseDendrite,
|
||||
accountsDB *accounts.Database,
|
||||
|
|
|
|||
|
|
@ -49,6 +49,8 @@ type KeyNotifier struct {
|
|||
|
||||
var keyProducer = &KeyNotifier{}
|
||||
|
||||
// this function is for user upload his device key, and one-time-key
|
||||
// to a limit at 50 set as default
|
||||
func UploadPKeys(
|
||||
req *http.Request,
|
||||
encryptionDB *storage.Database,
|
||||
|
|
@ -60,11 +62,13 @@ func UploadPKeys(
|
|||
return *reqErr
|
||||
}
|
||||
keySpecific := turnSpecific(keybody)
|
||||
// persist keys into encryptionDB
|
||||
err := persistKeys(
|
||||
encryptionDB,
|
||||
req.Context(),
|
||||
&keySpecific,
|
||||
userID, deviceID)
|
||||
// numMap is algorithm-num map
|
||||
numMap := (QueryOneTimeKeys(
|
||||
TYPESUM,
|
||||
userID,
|
||||
|
|
@ -87,6 +91,7 @@ func UploadPKeys(
|
|||
}
|
||||
}
|
||||
|
||||
// this function is for user query other's device key
|
||||
func QueryPKeys(
|
||||
req *http.Request,
|
||||
encryptionDB *storage.Database,
|
||||
|
|
@ -127,15 +132,19 @@ func QueryPKeys(
|
|||
}
|
||||
}
|
||||
|
||||
// query one's device key from user corresponding to uid
|
||||
for uid, arr := range queryRq.DeviceKeys {
|
||||
queryRp.DeviceKeys[uid] = make(map[string]types.DeviceKeysQuery)
|
||||
deviceKeysQueryMap := queryRp.DeviceKeys[uid]
|
||||
// backward compatible to old interface
|
||||
midArr := []string{}
|
||||
// figure out device list from devices described as device which is actually deviceID
|
||||
for device, _ := range arr.(map[string]interface{}) {
|
||||
midArr = append(midArr, device)
|
||||
}
|
||||
// all device keys
|
||||
dkeys, _ := encryptionDB.QueryInRange(req.Context(), uid, midArr)
|
||||
// build response for them
|
||||
for _, key := range dkeys {
|
||||
// preset for complicated nested map struct
|
||||
if _, ok := deviceKeysQueryMap[key.Device_id]; !ok {
|
||||
|
|
@ -182,6 +191,7 @@ func QueryPKeys(
|
|||
}
|
||||
}
|
||||
|
||||
// claim for one time key that may be used in session exchange in olm encryption
|
||||
func ClaimOneTimeKeys(
|
||||
req *http.Request,
|
||||
encryptionDB *storage.Database,
|
||||
|
|
@ -258,7 +268,8 @@ func LookUpChangedPKeys() util.JSONResponse {
|
|||
}
|
||||
}
|
||||
|
||||
// todo: check through interface for duplicate
|
||||
// todo: check through interface for duplicate and what type of request should it be
|
||||
// whether device or one time or both of them
|
||||
func checkUpload(req *types.UploadEncryptSpecific, typ int) bool {
|
||||
if typ == BODYDEVICEKEY {
|
||||
devicekey := req.DeviceKeys
|
||||
|
|
@ -292,6 +303,7 @@ func QueryOneTimeKeys(
|
|||
// when web client sign out, a clean should be processed, cause all keys would never been used from then on.
|
||||
func ClearUnused() {}
|
||||
|
||||
// persist both device keys and one time keys
|
||||
func persistKeys(
|
||||
database *storage.Database,
|
||||
ctx context.Context,
|
||||
|
|
@ -300,6 +312,10 @@ func persistKeys(
|
|||
deviceID string,
|
||||
) (err error) {
|
||||
// in order to persist keys , a check filtering duplicate should be processed
|
||||
// true stands for counterparts are in request
|
||||
// situation 1: only device keys
|
||||
// situation 2: both device keys and one time keys
|
||||
// situation 3: only one time keys
|
||||
if checkUpload(body, BODYDEVICEKEY) {
|
||||
deviceKeys := body.DeviceKeys
|
||||
al := deviceKeys.Algorithm
|
||||
|
|
@ -375,6 +391,7 @@ func persistKeys(
|
|||
return err
|
||||
}
|
||||
|
||||
// make keys instantiated to specific struct from keybody interface{}
|
||||
func turnSpecific(
|
||||
cont types.UploadEncrypt,
|
||||
) (spec types.UploadEncryptSpecific) {
|
||||
|
|
|
|||
|
|
@ -58,6 +58,7 @@ func (s *alStatements) prepare(db *sql.DB) (err error) {
|
|||
return
|
||||
}
|
||||
|
||||
// persist algorithms
|
||||
func (ks *alStatements) insertAl(
|
||||
ctx context.Context, txn *sql.Tx,
|
||||
userID, deviceID, algorithms string,
|
||||
|
|
@ -67,6 +68,7 @@ func (ks *alStatements) insertAl(
|
|||
return err
|
||||
}
|
||||
|
||||
// select algorithms
|
||||
func (ks *alStatements) selectAl(
|
||||
ctx context.Context,
|
||||
txn *sql.Tx,
|
||||
|
|
|
|||
|
|
@ -95,6 +95,7 @@ func (s *keyStatements) prepare(db *sql.DB) (err error) {
|
|||
return
|
||||
}
|
||||
|
||||
// insert keys
|
||||
func (ks *keyStatements) insertKey(
|
||||
ctx context.Context, txn *sql.Tx,
|
||||
deviceID, userID, keyID, keyTyp, keyInfo, algorithm, signature string,
|
||||
|
|
@ -104,6 +105,7 @@ func (ks *keyStatements) insertKey(
|
|||
return err
|
||||
}
|
||||
|
||||
// select by user and device
|
||||
func (ks *keyStatements) selectKey(
|
||||
ctx context.Context,
|
||||
txn *sql.Tx,
|
||||
|
|
@ -131,6 +133,8 @@ func (ks *keyStatements) selectKey(
|
|||
}
|
||||
return holders, err
|
||||
}
|
||||
|
||||
// select single one for claim usage
|
||||
func (ks *keyStatements) selectSingleKey(
|
||||
ctx context.Context,
|
||||
userID, deviceID, algorithm string,
|
||||
|
|
@ -156,6 +160,7 @@ func (ks *keyStatements) selectSingleKey(
|
|||
return holder, err
|
||||
}
|
||||
|
||||
// select details by given an array of devices
|
||||
func (ks *keyStatements) selectInKeys(
|
||||
ctx context.Context,
|
||||
userID string,
|
||||
|
|
|
|||
|
|
@ -47,6 +47,7 @@ func NewDatabase(dataSourceName string) (*Database, error) {
|
|||
return &Database{db: db, keyStatements: keyStatement, alStatements: alStatement}, nil
|
||||
}
|
||||
|
||||
// insert device key
|
||||
func (d *Database) InsertKey(
|
||||
ctx context.Context,
|
||||
deviceID, userID, keyID, keyTyp, keyInfo, al, sig string,
|
||||
|
|
@ -57,6 +58,7 @@ func (d *Database) InsertKey(
|
|||
return
|
||||
}
|
||||
|
||||
// for key upload response usage a map from key algorithm to sum to counterpart
|
||||
func (d *Database) SelectOneTimeKeyCount(
|
||||
ctx context.Context,
|
||||
deviceID, userID string,
|
||||
|
|
@ -77,6 +79,7 @@ func (d *Database) SelectOneTimeKeyCount(
|
|||
return
|
||||
}
|
||||
|
||||
// query keys in a range of devices
|
||||
func (d *Database) QueryInRange(
|
||||
ctx context.Context,
|
||||
userID string,
|
||||
|
|
@ -86,6 +89,7 @@ func (d *Database) QueryInRange(
|
|||
return
|
||||
}
|
||||
|
||||
// persist algorithms
|
||||
func (d *Database) InsertAl(
|
||||
ctx context.Context, uid, device string, al []string,
|
||||
) (err error) {
|
||||
|
|
@ -96,6 +100,7 @@ func (d *Database) InsertAl(
|
|||
return
|
||||
}
|
||||
|
||||
// select algorithms
|
||||
func (d *Database) SelectAl(
|
||||
ctx context.Context, uid, device string,
|
||||
) (res []string, err error) {
|
||||
|
|
@ -107,6 +112,7 @@ func (d *Database) SelectAl(
|
|||
return
|
||||
}
|
||||
|
||||
// claim for one time key one for once
|
||||
func (d *Database) SelectOneTimeKeySingle(
|
||||
ctx context.Context,
|
||||
userID, deviceID, algorithm string,
|
||||
|
|
|
|||
Loading…
Reference in a new issue