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");
// 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");
// you may not use this file except in compliance with the License.
@ -49,13 +49,22 @@ type KeyNotifier struct {
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
if reqErr := httputil.UnmarshalJSONRequest(req, &keybody); reqErr != nil {
if reqErr := httputil.UnmarshalJSONRequest(req, &keybody);
reqErr != nil {
return *reqErr
}
keySpecific := turnSpecific(keybody)
err := persistKeys(encryptionDB, req.Context(), &keySpecific, userID, deviceID)
err := persistKeys(
encryptionDB,
req.Context(),
&keySpecific,
userID, deviceID)
numMap := (QueryOneTimeKeys(
TYPESUM,
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
queryRp := types.QueryResponse{}
queryRp.Failure = make(map[string]interface{})
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
}
/*
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 +--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
claimRp := types.ClaimResponse{}
claimRp.Failures = make(map[string]interface{})
@ -354,7 +375,9 @@ func persistKeys(
return err
}
func turnSpecific(cont types.UploadEncrypt) (spec types.UploadEncryptSpecific) {
func turnSpecific(
cont types.UploadEncrypt,
) (spec types.UploadEncryptSpecific) {
// both device keys are coordinate
spec.DeviceKeys = cont.DeviceKeys
spec.OneTimeKey.KeyString = make(map[string]string)
@ -374,19 +397,32 @@ func turnSpecific(cont types.UploadEncrypt) (spec types.UploadEncryptSpecific) {
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)
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)
return
}
func pickOne(encryptDB storage.Database, ctx context.Context, uid, device, al string) (key types.KeyHolder, err error) {
key, err = encryptDB.SelectOneTimeKeySingle(ctx, uid, device, al)
return
func pickOne(
encryptDB storage.Database,
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) {

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");
// 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");
// 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
);
`
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 = `
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 {

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");
// 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
);
`
const insertkeySQL = `
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 = `
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 = `
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 = `
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 = `
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 = `
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 {

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");
// you may not use this file except in compliance with the License.
@ -86,7 +86,9 @@ func (d *Database) QueryInRange(
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) {
d.alStatements.insertAl(ctx, txn, uid, device, strings.Join(al, ","))
return
@ -94,7 +96,9 @@ func (d *Database) InsertAl(ctx context.Context, uid, device string, al []string
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) {
holder, err := d.alStatements.selectAl(ctx, txn, uid, device)
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");
// 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");
// 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");
// 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");
// you may not use this file except in compliance with the License.