standardize format and spacing and codes length

This commit is contained in:
terrill 2018-07-03 10:00:52 +08:00
parent 084ad07d16
commit e5effc328c
10 changed files with 80 additions and 40 deletions

View file

@ -1,4 +1,4 @@
// Copyright 2017 Vector Creations Ltd // Copyright 2018 Vector Creations Ltd
// //
// 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.

View file

@ -1,4 +1,4 @@
// Copyright 2017 Vector Creations Ltd // Copyright 2018 Vector Creations Ltd
// //
// 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.
@ -49,13 +49,22 @@ type KeyNotifier struct {
var keyProducer = &KeyNotifier{} var keyProducer = &KeyNotifier{}
func UploadPKeys(req *http.Request, encryptionDB *storage.Database, userID, deviceID string) util.JSONResponse { func UploadPKeys(
req *http.Request,
encryptionDB *storage.Database,
userID, deviceID string,
) util.JSONResponse {
var keybody types.UploadEncrypt var keybody types.UploadEncrypt
if reqErr := httputil.UnmarshalJSONRequest(req, &keybody); reqErr != nil { if reqErr := httputil.UnmarshalJSONRequest(req, &keybody);
reqErr != nil {
return *reqErr return *reqErr
} }
keySpecific := turnSpecific(keybody) keySpecific := turnSpecific(keybody)
err := persistKeys(encryptionDB, req.Context(), &keySpecific, userID, deviceID) err := persistKeys(
encryptionDB,
req.Context(),
&keySpecific,
userID, deviceID)
numMap := (QueryOneTimeKeys( numMap := (QueryOneTimeKeys(
TYPESUM, TYPESUM,
userID, userID,
@ -78,17 +87,24 @@ func UploadPKeys(req *http.Request, encryptionDB *storage.Database, userID, devi
} }
} }
func QueryPKeys(req *http.Request, encryptionDB *storage.Database, userID, deviceID string, deviceDB *devices.Database) util.JSONResponse { func QueryPKeys(
req *http.Request,
encryptionDB *storage.Database,
userID, deviceID string,
deviceDB *devices.Database,
) util.JSONResponse {
var queryRq types.QueryRequest var queryRq types.QueryRequest
queryRp := types.QueryResponse{} queryRp := types.QueryResponse{}
queryRp.Failure = make(map[string]interface{}) queryRp.Failure = make(map[string]interface{})
queryRp.DeviceKeys = make(map[string]map[string]types.DeviceKeysQuery) queryRp.DeviceKeys = make(map[string]map[string]types.DeviceKeysQuery)
if reqErr := httputil.UnmarshalJSONRequest(req, &queryRq); reqErr != nil { if reqErr := httputil.UnmarshalJSONRequest(req, &queryRq);
reqErr != nil {
return *reqErr return *reqErr
} }
/* /*
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.
domain --------+ fed (keys) domain --------+ fed (keys)
domain +--tout-- timer domain +--tout-- timer
*/ */
@ -166,7 +182,12 @@ func QueryPKeys(req *http.Request, encryptionDB *storage.Database, userID, devic
} }
} }
func ClaimOneTimeKeys(req *http.Request, encryptionDB *storage.Database, userID, deviceID string, deviceDB *devices.Database) util.JSONResponse { func ClaimOneTimeKeys(
req *http.Request,
encryptionDB *storage.Database,
userID, deviceID string,
deviceDB *devices.Database,
) util.JSONResponse {
var claimRq types.ClaimRequest var claimRq types.ClaimRequest
claimRp := types.ClaimResponse{} claimRp := types.ClaimResponse{}
claimRp.Failures = make(map[string]interface{}) claimRp.Failures = make(map[string]interface{})
@ -354,7 +375,9 @@ func persistKeys(
return err return err
} }
func turnSpecific(cont types.UploadEncrypt) (spec types.UploadEncryptSpecific) { func turnSpecific(
cont types.UploadEncrypt,
) (spec types.UploadEncryptSpecific) {
// both device keys are coordinate // both device keys are coordinate
spec.DeviceKeys = cont.DeviceKeys spec.DeviceKeys = cont.DeviceKeys
spec.OneTimeKey.KeyString = make(map[string]string) spec.OneTimeKey.KeyString = make(map[string]string)
@ -374,19 +397,32 @@ func turnSpecific(cont types.UploadEncrypt) (spec types.UploadEncryptSpecific) {
return return
} }
func persistAl(encryptDB storage.Database, ctx context.Context, uid, device string, al []string) (err error) { func persistAl(
encryptDB storage.Database,
ctx context.Context,
uid, device string,
al []string,
) (err error) {
err = encryptDB.InsertAl(ctx, uid, device, al) err = encryptDB.InsertAl(ctx, uid, device, al)
return return
} }
func takeAL(encryptDB storage.Database, ctx context.Context, uid, device string) (al []string, err error) { func takeAL(
encryptDB storage.Database,
ctx context.Context,
uid, device string,
) (al []string, err error) {
al, err = encryptDB.SelectAl(ctx, uid, device) al, err = encryptDB.SelectAl(ctx, uid, device)
return return
} }
func pickOne(encryptDB storage.Database, ctx context.Context, uid, device, al string) (key types.KeyHolder, err error) { func pickOne(
key, err = encryptDB.SelectOneTimeKeySingle(ctx, uid, device, al) encryptDB storage.Database,
return ctx context.Context,
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) {

View file

@ -1,4 +1,4 @@
// Copyright 2017 Vector Creations Ltd // Copyright 2018 Vector Creations Ltd
// //
// 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.

View file

@ -1,4 +1,4 @@
// Copyright 2017 Vector Creations Ltd // Copyright 2018 Vector Creations Ltd
// //
// 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.
@ -30,13 +30,13 @@ CREATE TABLE IF NOT EXISTS encrypt_algorithm (
algorithms TEXT NOT NULL algorithms TEXT NOT NULL
); );
` `
const insertalSQL = ` const insertalSQL = `
INSERT INTO encrypt_algorithm (device_id, user_id, algorithms) VALUES ($1, $2, $3) INSERT INTO encrypt_algorithm (device_id, user_id, algorithms)
VALUES ($1, $2, $3)
` `
const selectalSQL = ` const selectalSQL = `
SELECT user_id, device_id, algorithms FROM encrypt_algorithm WHERE user_id = $1 AND device_id = $2 SELECT user_id, device_id, algorithms FROM encrypt_algorithm
WHERE user_id = $1 AND device_id = $2
` `
type alStatements struct { type alStatements struct {

View file

@ -1,4 +1,4 @@
// Copyright 2017 Vector Creations Ltd // Copyright 2018 Vector Creations Ltd
// //
// 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.
@ -35,29 +35,29 @@ CREATE TABLE IF NOT EXISTS encrypt_keys (
signature TEXT NOT NULL signature TEXT NOT NULL
); );
` `
const insertkeySQL = ` const insertkeySQL = `
INSERT INTO encrypt_keys (device_id, user_id, key_id, key_type, key_info, algorithm, signature) INSERT INTO encrypt_keys (device_id, user_id, key_id, key_type, key_info, algorithm, signature)
VALUES ($1, $2, $3, $4, $5, $6, $7) VALUES ($1, $2, $3, $4, $5, $6, $7)
` `
const selectkeySQL = ` const selectkeySQL = `
SELECT user_id, device_id, key_id, key_type, key_info, algorithm, signature FROM encrypt_keys WHERE user_id = $1 AND device_id = $2 SELECT user_id, device_id, key_id, key_type, key_info, algorithm, signature FROM encrypt_keys
WHERE user_id = $1 AND device_id = $2
` `
const deleteSinglekeySQL = ` const deleteSinglekeySQL = `
SELECT user_id, device_id, key_id, key_type, key_info, algorithm, signature FROM encrypt_keys WHERE user_id = $1 AND device_id = $2 AND algorithm = $3 SELECT user_id, device_id, key_id, key_type, key_info, algorithm, signature FROM encrypt_keys
WHERE user_id = $1 AND device_id = $2 AND algorithm = $3
` `
const selectSinglekeySQL = ` const selectSinglekeySQL = `
DELETE FROM encrypt_keys WHERE user_id = $1 AND device_id = $2 AND algorithm = $3 AND key_id = $4 DELETE FROM encrypt_keys
WHERE user_id = $1 AND device_id = $2 AND algorithm = $3 AND key_id = $4
` `
const selectInkeysSQL = ` const selectInkeysSQL = `
SELECT user_id, device_id, key_id, key_type, key_info, algorithm, signature FROM encrypt_keys WHERE user_id = $1 AND key_type = 'device_key' AND device_id = ANY($2) SELECT user_id, device_id, key_id, key_type, key_info, algorithm, signature FROM encrypt_keys
WHERE user_id = $1 AND key_type = 'device_key' AND device_id = ANY($2)
` `
const selectAllkeysSQL = ` const selectAllkeysSQL = `
SELECT user_id, device_id, key_id, key_type, key_info, algorithm, signature FROM encrypt_keys WHERE user_id = $1 AND key_type = $2 SELECT user_id, device_id, key_id, key_type, key_info, algorithm, signature FROM encrypt_keys
WHERE user_id = $1 AND key_type = $2
` `
type keyStatements struct { type keyStatements struct {

View file

@ -1,4 +1,4 @@
// Copyright 2017 Vector Creations Ltd // Copyright 2018 Vector Creations Ltd
// //
// 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.
@ -86,7 +86,9 @@ func (d *Database) QueryInRange(
return return
} }
func (d *Database) InsertAl(ctx context.Context, uid, device string, al []string) (err error) { func (d *Database) InsertAl(
ctx context.Context, uid, device string, al []string,
) (err error) {
err = common.WithTransaction(d.db, func(txn *sql.Tx) (err error) { err = common.WithTransaction(d.db, func(txn *sql.Tx) (err error) {
d.alStatements.insertAl(ctx, txn, uid, device, strings.Join(al, ",")) d.alStatements.insertAl(ctx, txn, uid, device, strings.Join(al, ","))
return return
@ -94,7 +96,9 @@ func (d *Database) InsertAl(ctx context.Context, uid, device string, al []string
return return
} }
func (d *Database) SelectAl(ctx context.Context, uid, device string) (res []string, err error) { func (d *Database) SelectAl(
ctx context.Context, uid, device string,
) (res []string, err error) {
err = common.WithTransaction(d.db, func(txn *sql.Tx) (err error) { err = common.WithTransaction(d.db, func(txn *sql.Tx) (err error) {
holder, err := d.alStatements.selectAl(ctx, txn, uid, device) holder, err := d.alStatements.selectAl(ctx, txn, uid, device)
res = strings.Split(holder.Supported_algorithm, ",") res = strings.Split(holder.Supported_algorithm, ",")

View file

@ -1,4 +1,4 @@
// Copyright 2017 Vector Creations Ltd // Copyright 2018 Vector Creations Ltd
// //
// 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.

View file

@ -1,4 +1,4 @@
// Copyright 2017 Vector Creations Ltd // Copyright 2018 Vector Creations Ltd
// //
// 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.

View file

@ -1,4 +1,4 @@
// Copyright 2017 Vector Creations Ltd // Copyright 2018 Vector Creations Ltd
// //
// 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.

View file

@ -1,4 +1,4 @@
// Copyright 2017 Vector Creations Ltd // Copyright 2018 Vector Creations Ltd
// //
// 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.