encrypt_algorithm.go 6.3 KB
Newer Older
T
tanggen 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
// 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.
// 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.
//
//
// Modifications copyright (C) 2020 Finogeeks Co., Ltd

package encryptoapi

import (
	"context"
	"database/sql"

	"github.com/finogeeks/ligase/common"
	"github.com/finogeeks/ligase/model/dbtypes"
	log "github.com/finogeeks/ligase/skunkworks/log"
)

const algorithmSchema = `
-- The media_repository table holds metadata for each media file stored and accessible to the local server,
-- the actual file is stored separately.
CREATE TABLE IF NOT EXISTS encrypt_algorithm (
    device_id TEXT 				NOT NULL,
    user_id TEXT 				NOT NULL,
	algorithms TEXT 			NOT NULL,
	identifier TEXT NOT NULL DEFAULT '',
	CONSTRAINT encrypt_algorithm_unique UNIQUE (device_id, user_id),
    PRIMARY KEY(device_id, user_id)
);

CREATE INDEX IF NOT EXISTS encrypt_algorithm_user_id ON encrypt_algorithm(user_id);
CREATE INDEX IF NOT EXISTS encrypt_algorithm_device_id ON encrypt_algorithm(device_id);
`
const insertAlSQL = `
INSERT INTO encrypt_algorithm (device_id, user_id, algorithms, identifier)
VALUES ($1, $2, $3, $4) on conflict (device_id, user_id) do UPDATE SET algorithms = EXCLUDED.algorithms, identifier = EXCLUDED.identifier
`

const recoverAlsSQL = `
SELECT device_id, user_id, algorithms, identifier FROM encrypt_algorithm limit $1 offset $2
`

const deleteAlSQL = `
DELETE FROM encrypt_algorithm WHERE device_id = $1 AND user_id = $2
`

const deleteMacAlSQL = `
DELETE FROM encrypt_algorithm WHERE user_id = $1 AND identifier = $2 AND device_id != $3
`

type alStatements struct {
	db              *Database
	insertAlStmt    *sql.Stmt
	recoverAlsStmt  *sql.Stmt
	deleteAlStmt    *sql.Stmt
	deleteMacAlStmt *sql.Stmt
}

func (s *alStatements) prepare(d *Database) (err error) {
	s.db = d
	_, err = d.db.Exec(algorithmSchema)
	if err != nil {
		return
	}
	if s.insertAlStmt, err = d.db.Prepare(insertAlSQL); err != nil {
		return
	}
	if s.recoverAlsStmt, err = d.db.Prepare(recoverAlsSQL); err != nil {
		return
	}
	if s.deleteAlStmt, err = d.db.Prepare(deleteAlSQL); err != nil {
		return
	}
	if s.deleteMacAlStmt, err = d.db.Prepare(deleteMacAlSQL); err != nil {
		return
	}
	return
}

func (s *alStatements) recoverAls(ctx context.Context) error {
	limit := 1000
	offset := 0
	exists := true
	for exists {
		exists = false
		rows, err := s.recoverAlsStmt.QueryContext(ctx, limit, offset)
		if err != nil {
			return err
		}
		offset = offset + limit
		exists, err = s.processRecover(ctx, rows)
		if err != nil {
			return err
		}
	}

	return nil
}

func (s *alStatements) processRecover(ctx context.Context, rows *sql.Rows) (exists bool, err error) {
	defer rows.Close()
	for rows.Next() {
		exists = true
		var alsInsert dbtypes.AlInsert
		if err1 := rows.Scan(&alsInsert.DeviceID, &alsInsert.UserID, &alsInsert.Algorithm, &alsInsert.Identifier); err1 != nil {
			log.Errorf("load algorithm error: %v", err1)
			if err == nil {
				err = err1
			}
			continue
		}

		var update dbtypes.DBEvent
		update.Category = dbtypes.CATEGORY_E2E_DB_EVENT
		update.Key = dbtypes.AlInsertKey
		update.IsRecovery = true
		update.E2EDBEvents.AlInsert = &alsInsert
		update.SetUid(int64(common.CalcStringHashCode64(alsInsert.UserID)))
		err2 := s.db.WriteDBEventWithTbl(ctx, &update, "encrypt_algorithm")
		if err2 != nil {
			log.Errorf("update algorithm cache error: %v", err2)
			if err == nil {
				err = err2
			}
			continue
		}
	}
	return
}

// persist algorithms
func (s *alStatements) insertAl(
	ctx context.Context,
	userID, deviceID, algorithms, identifier string,
) error {
	if s.db.AsyncSave == true {
		var update dbtypes.DBEvent
		update.Category = dbtypes.CATEGORY_E2E_DB_EVENT
		update.Key = dbtypes.AlInsertKey
		update.E2EDBEvents.AlInsert = &dbtypes.AlInsert{
			DeviceID:   deviceID,
			UserID:     userID,
			Algorithm:  algorithms,
			Identifier: identifier,
		}
		update.SetUid(int64(common.CalcStringHashCode64(userID)))
		return s.db.WriteDBEventWithTbl(ctx, &update, "encrypt_algorithm")
	} else {
		stmt := s.insertAlStmt
		_, err := stmt.ExecContext(ctx, deviceID, userID, algorithms, identifier)
		return err
	}
}

func (s *alStatements) onInsertAl(
	ctx context.Context,
	userID, deviceID, algorithms, identifier string,
) error {
	stmt := s.insertAlStmt
	_, err := stmt.ExecContext(ctx, deviceID, userID, algorithms, identifier)
	return err
}

func (s *alStatements) deleteAl(
	ctx context.Context,
	userID, deviceID string,
) error {
	if s.db.AsyncSave == true {
		var update dbtypes.DBEvent
		update.Category = dbtypes.CATEGORY_E2E_DB_EVENT
		update.Key = dbtypes.DeviceAlDeleteKey
		update.E2EDBEvents.DeviceKeyDelete = &dbtypes.DeviceKeyDelete{
			DeviceID: deviceID,
			UserID:   userID,
		}
		update.SetUid(int64(common.CalcStringHashCode64(userID)))
		return s.db.WriteDBEventWithTbl(ctx, &update, "encrypt_algorithm")
	} else {
		stmt := s.deleteAlStmt
		_, err := stmt.ExecContext(ctx, deviceID, userID)
		return err
	}
}

func (s *alStatements) onDeleteAl(
	ctx context.Context,
	userID, deviceID string,
) error {
	stmt := s.deleteAlStmt
	_, err := stmt.ExecContext(ctx, deviceID, userID)
	return err
}

func (s *alStatements) deleteMacAl(
	ctx context.Context,
	userID, deviceID, identifier string,
) error {
	if s.db.AsyncSave == true {
		var update dbtypes.DBEvent
		update.Category = dbtypes.CATEGORY_E2E_DB_EVENT
		update.Key = dbtypes.MacDeviceAlDeleteKey
		update.E2EDBEvents.MacKeyDelete = &dbtypes.MacKeyDelete{
			DeviceID:   deviceID,
			UserID:     userID,
			Identifier: identifier,
		}
		update.SetUid(int64(common.CalcStringHashCode64(userID)))
		return s.db.WriteDBEventWithTbl(ctx, &update, "encrypt_algorithm")
	} else {
		stmt := s.deleteMacAlStmt
		_, err := stmt.ExecContext(ctx, deviceID, userID, identifier)
		return err
	}
}

func (s *alStatements) onDeleteMacAl(
	ctx context.Context,
	userID, deviceID, identifier string,
) error {
	stmt := s.deleteMacAlStmt
	_, err := stmt.ExecContext(ctx, deviceID, userID, identifier)
	return err
}