Upstream sync
This commit is contained in:
@@ -11,36 +11,32 @@ import (
|
||||
_ "github.com/jinzhu/gorm/dialects/postgres"
|
||||
)
|
||||
|
||||
|
||||
var db *gorm.DB
|
||||
var err error
|
||||
|
||||
|
||||
func Init() error{
|
||||
host := config.AppConfig.Database.HostName
|
||||
user := config.AppConfig.Database.UserName
|
||||
func Init() error {
|
||||
host := config.AppConfig.Database.HostName
|
||||
user := config.AppConfig.Database.UserName
|
||||
// port := config.AppConfig.Database.Port
|
||||
dbName := config.AppConfig.Database.DatabaseName
|
||||
password := config.AppConfig.Database.Password
|
||||
dbName := config.AppConfig.Database.DatabaseName
|
||||
password := config.AppConfig.Database.Password
|
||||
|
||||
dbString:= fmt.Sprintf("host=%s user=%s dbname=%s sslmode=disable password=%s",host,user,dbName,password)
|
||||
dbString := fmt.Sprintf("host=%s user=%s dbname=%s sslmode=disable password=%s", host, user, dbName, password)
|
||||
// db, err = gorm.Open("postgres", "host=localhost user=postgres dbname=postgres sslmode=disable password=root")
|
||||
var err error
|
||||
// //PostgreSQL
|
||||
db, err = gorm.Open("postgres",dbString)
|
||||
db, err = gorm.Open("postgres", dbString)
|
||||
|
||||
if err != nil {
|
||||
log.Println("Error initializing the database: ", err)
|
||||
return err
|
||||
}
|
||||
//TODO AUTOMIGRATE models once we have them
|
||||
db.AutoMigrate(&models.User{}, &models.Company{}, &models.Device{}, &models.DeviceInfo{})
|
||||
db.AutoMigrate(&models.User{}, &models.Company{}, &models.Device{}, &models.DeviceInfo{}, &models.Contract{}, &models.ContractInfo{})
|
||||
|
||||
return nil
|
||||
|
||||
}
|
||||
|
||||
|
||||
func GetDb() *gorm.DB {
|
||||
return db
|
||||
}
|
||||
|
||||
88
shared/encryption.go
Normal file
88
shared/encryption.go
Normal file
@@ -0,0 +1,88 @@
|
||||
package shared
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"compress/flate"
|
||||
"crypto/aes"
|
||||
"crypto/cipher"
|
||||
"crypto/rand"
|
||||
"encoding/base64"
|
||||
"fmt"
|
||||
"io"
|
||||
)
|
||||
|
||||
type EncryptionClient interface {
|
||||
Encrypt(string) (string, error)
|
||||
Decrypt(string) (string, error)
|
||||
}
|
||||
|
||||
type encryptionClient struct {
|
||||
Secret string
|
||||
}
|
||||
|
||||
func NewEncryptionClient(secret string) EncryptionClient {
|
||||
return &encryptionClient{Secret: secret}
|
||||
}
|
||||
|
||||
func compress(text string) ([]byte, error) {
|
||||
var compressedBytes bytes.Buffer
|
||||
w, err := flate.NewWriter(&compressedBytes, flate.BestCompression)
|
||||
w.Write([]byte(text))
|
||||
w.Close()
|
||||
return compressedBytes.Bytes(), err
|
||||
}
|
||||
|
||||
func decompress(compressedBytes []byte) (string, error) {
|
||||
reader := flate.NewReader(bytes.NewReader(compressedBytes))
|
||||
defer reader.Close()
|
||||
textBytes := new(bytes.Buffer)
|
||||
_, err := textBytes.ReadFrom(reader)
|
||||
return textBytes.String(), err
|
||||
}
|
||||
|
||||
// Encrypt method is to encrypt or hide any classified text
|
||||
func (e encryptionClient) Encrypt(text string) (string, error) {
|
||||
block, err := aes.NewCipher([]byte(e.Secret))
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
compressedText, err := compress(text)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
gcm, err := cipher.NewGCM(block)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
bytes := make([]byte, gcm.NonceSize())
|
||||
if _, err = io.ReadFull(rand.Reader, bytes); err != nil {
|
||||
return "", err
|
||||
}
|
||||
encodedText := gcm.Seal(bytes, bytes, compressedText, nil)
|
||||
return base64.StdEncoding.EncodeToString(encodedText), nil
|
||||
}
|
||||
|
||||
// Decrypt method is to extract back the encrypted text
|
||||
func (e encryptionClient) Decrypt(text string) (string, error) {
|
||||
block, err := aes.NewCipher([]byte(e.Secret))
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
cipherText, err := base64.StdEncoding.DecodeString(text)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
gcm, err := cipher.NewGCM(block)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if len(cipherText) < gcm.NonceSize() {
|
||||
return "", fmt.Errorf("text is too small")
|
||||
}
|
||||
bytes, cipherText := cipherText[:gcm.NonceSize()], cipherText[gcm.NonceSize():]
|
||||
compressedTextBytes, err := gcm.Open(nil, bytes, cipherText, nil)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return decompress(compressedTextBytes)
|
||||
}
|
||||
22
shared/encryption_test.go
Normal file
22
shared/encryption_test.go
Normal file
@@ -0,0 +1,22 @@
|
||||
package shared
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestEncryptionClient(t *testing.T) {
|
||||
|
||||
client := encryptionClient{Secret: `abc&1*~#^2^#s0^=)^^7%b34`}
|
||||
|
||||
t.Run("encrypt/ decyrpt works", func(t *testing.T) {
|
||||
text := "sample text to test encryption"
|
||||
encodedText, err := client.Encrypt(text)
|
||||
assert.NoError(t, err)
|
||||
assert.NotEqual(t, text, encodedText)
|
||||
decodedText, err := client.Decrypt(encodedText)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, text, decodedText)
|
||||
})
|
||||
}
|
||||
@@ -5,6 +5,6 @@
|
||||
package shared
|
||||
|
||||
const (
|
||||
RoleAdmin string = "admin"
|
||||
RoleAdmin string = "admin"
|
||||
RoleProUser string = "pro-user"
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user