2019-05-23 08:47:20 +00:00
|
|
|
package sharedsecret
|
2019-05-17 11:06:56 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Response struct {
|
|
|
|
secret []byte
|
|
|
|
installationIDs map[string]bool
|
|
|
|
}
|
|
|
|
|
2019-07-17 22:25:42 +00:00
|
|
|
type sqlitePersistence struct {
|
2019-05-17 11:06:56 +00:00
|
|
|
db *sql.DB
|
|
|
|
}
|
|
|
|
|
2019-07-17 22:25:42 +00:00
|
|
|
func newSQLitePersistence(db *sql.DB) *sqlitePersistence {
|
|
|
|
return &sqlitePersistence{db: db}
|
2019-05-17 11:06:56 +00:00
|
|
|
}
|
|
|
|
|
2019-07-17 22:25:42 +00:00
|
|
|
func (s *sqlitePersistence) Add(identity []byte, secret []byte, installationID string) error {
|
2019-05-17 11:06:56 +00:00
|
|
|
tx, err := s.db.Begin()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-05-23 08:47:20 +00:00
|
|
|
insertSecretStmt, err := tx.Prepare("INSERT INTO secrets(identity, secret) VALUES (?, ?)")
|
2019-05-17 11:06:56 +00:00
|
|
|
if err != nil {
|
|
|
|
_ = tx.Rollback()
|
|
|
|
return err
|
|
|
|
}
|
2019-05-23 08:47:20 +00:00
|
|
|
defer insertSecretStmt.Close()
|
2019-05-17 11:06:56 +00:00
|
|
|
|
2019-05-23 08:47:20 +00:00
|
|
|
_, err = insertSecretStmt.Exec(identity, secret)
|
2019-05-17 11:06:56 +00:00
|
|
|
if err != nil {
|
|
|
|
_ = tx.Rollback()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-05-23 08:47:20 +00:00
|
|
|
insertInstallationIDStmt, err := tx.Prepare("INSERT INTO secret_installation_ids(id, identity_id) VALUES (?, ?)")
|
2019-05-17 11:06:56 +00:00
|
|
|
if err != nil {
|
|
|
|
_ = tx.Rollback()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer insertInstallationIDStmt.Close()
|
|
|
|
|
|
|
|
_, err = insertInstallationIDStmt.Exec(installationID, identity)
|
|
|
|
if err != nil {
|
|
|
|
_ = tx.Rollback()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return tx.Commit()
|
|
|
|
}
|
|
|
|
|
2019-07-17 22:25:42 +00:00
|
|
|
func (s *sqlitePersistence) Get(identity []byte, installationIDs []string) (*Response, error) {
|
2019-05-17 11:06:56 +00:00
|
|
|
response := &Response{
|
|
|
|
installationIDs: make(map[string]bool),
|
|
|
|
}
|
|
|
|
args := make([]interface{}, len(installationIDs)+1)
|
|
|
|
args[0] = identity
|
|
|
|
for i, installationID := range installationIDs {
|
|
|
|
args[i+1] = installationID
|
|
|
|
}
|
|
|
|
|
|
|
|
/* #nosec */
|
|
|
|
query := `SELECT secret, id
|
2019-07-17 22:25:42 +00:00
|
|
|
FROM secrets t
|
|
|
|
JOIN
|
|
|
|
secret_installation_ids tid
|
|
|
|
ON t.identity = tid.identity_id
|
|
|
|
WHERE
|
|
|
|
t.identity = ?
|
|
|
|
AND
|
|
|
|
tid.id IN (?` + strings.Repeat(",?", len(installationIDs)-1) + `)`
|
2019-05-17 11:06:56 +00:00
|
|
|
|
|
|
|
rows, err := s.db.Query(query, args...)
|
|
|
|
if err != nil && err != sql.ErrNoRows {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for rows.Next() {
|
|
|
|
var installationID string
|
|
|
|
var secret []byte
|
|
|
|
err = rows.Scan(&secret, &installationID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
response.secret = secret
|
|
|
|
response.installationIDs[installationID] = true
|
|
|
|
}
|
|
|
|
|
|
|
|
return response, nil
|
|
|
|
}
|
|
|
|
|
2019-07-17 22:25:42 +00:00
|
|
|
func (s *sqlitePersistence) All() ([][][]byte, error) {
|
|
|
|
query := "SELECT identity, secret FROM secrets"
|
2019-05-17 11:06:56 +00:00
|
|
|
|
|
|
|
var secrets [][][]byte
|
|
|
|
|
|
|
|
rows, err := s.db.Query(query)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for rows.Next() {
|
|
|
|
var secret []byte
|
|
|
|
var identity []byte
|
|
|
|
err = rows.Scan(&identity, &secret)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
secrets = append(secrets, [][]byte{identity, secret})
|
|
|
|
}
|
|
|
|
|
|
|
|
return secrets, nil
|
|
|
|
}
|