Skip to content

Commit fbba7ad

Browse files
authored
Better encryption handling (#132)
Closes #131 * encryption and decryption for login model * encrption and decryption * update * update
1 parent 3ef8b00 commit fbba7ad

2 files changed

Lines changed: 57 additions & 13 deletions

File tree

internal/app/login.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,51 @@ package app
22

33
import (
44
"encoding/base64"
5+
"reflect"
56

67
"github.com/passwall/passwall-server/internal/storage"
78
"github.com/passwall/passwall-server/model"
89
"github.com/spf13/viper"
910
)
1011

12+
// Model encryption
13+
func EncryptLogin(loginDTO model.LoginDTO) model.LoginDTO {
14+
num := reflect.TypeOf(loginDTO).NumField()
15+
16+
var tagVal string
17+
18+
for i := 0; i < num; i++ {
19+
tagVal = reflect.TypeOf(loginDTO).Field(i).Tag.Get("encrypt")
20+
value := reflect.ValueOf(loginDTO).Field(i).String()
21+
22+
if tagVal == "true" {
23+
value = base64.StdEncoding.EncodeToString(Encrypt(value, viper.GetString("server.passphrase")))
24+
reflect.ValueOf(&loginDTO).Elem().Field(i).SetString(value)
25+
}
26+
}
27+
28+
return loginDTO
29+
}
30+
31+
// Model decryption
32+
func DecryptLogin(login model.Login) model.Login {
33+
num := reflect.TypeOf(login).NumField()
34+
35+
var tagVal string
36+
37+
for i := 0; i < num; i++ {
38+
tagVal = reflect.TypeOf(login).Field(i).Tag.Get("encrypt")
39+
value := reflect.ValueOf(login).Field(i).String()
40+
41+
if tagVal == "true" {
42+
valueByte, _ := base64.StdEncoding.DecodeString(value)
43+
value = string(Decrypt(string(valueByte[:]), viper.GetString("server.passphrase")))
44+
}
45+
}
46+
47+
return login
48+
}
49+
1150
// CreateLogin creates a login and saves it to the store
1251
func CreateLogin(s storage.Store, dto *model.LoginDTO, schema string) (*model.Login, error) {
1352

model/login.go

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,29 @@ import (
66

77
// Login ...
88
type Login struct {
9-
ID uint `gorm:"primary_key" json:"id"`
10-
CreatedAt time.Time `json:"created_at"`
11-
UpdatedAt time.Time `json:"updated_at"`
12-
DeletedAt *time.Time `json:"deleted_at"`
13-
Title string `json:"title"`
14-
URL string `json:"url"`
15-
Username string `json:"username"`
16-
Password string `json:"password"`
9+
ID uint `gorm:"primary_key" json:"id" encrypt:"false"`
10+
CreatedAt time.Time `json:"created_at" encrypt:"true"`
11+
UpdatedAt time.Time `json:"updated_at" encrypt:"true"`
12+
DeletedAt *time.Time `json:"deleted_at" encrypt:"true"`
13+
Title string `json:"title" encrypt:"false"`
14+
URL string `json:"url" encrypt:"true"`
15+
Username string `json:"username" encrypt:"true"`
16+
Password string `json:"password" encrypt:"true"`
1717
}
1818

1919
type LoginDTO struct {
20-
ID uint `json:"id"`
21-
Title string `json:"title"`
22-
URL string `json:"url"`
23-
Username string `json:"username"`
24-
Password string `json:"password"`
20+
ID uint `json:"id" encrypt:"false"`
21+
Title string `json:"title" encrypt:"false"`
22+
URL string `json:"url" encrypt:"true"`
23+
Username string `json:"username" encrypt:"true"`
24+
Password string `json:"password" encrypt:"true"`
2525
}
2626

2727
// ToLogin ...
2828
func ToLogin(loginDTO *LoginDTO) *Login {
29+
30+
//*loginDTO = app.EncryptLogin(*loginDTO)
31+
2932
return &Login{
3033
Title: loginDTO.Title,
3134
URL: loginDTO.URL,
@@ -42,6 +45,8 @@ func ToLoginDTO(login *Login) *LoginDTO {
4245
// login.URL = strings.TrimPrefix(login.URL, trims[i])
4346
// }
4447

48+
//*login = app.DecryptLogin(*login)
49+
4550
return &LoginDTO{
4651
ID: login.ID,
4752
Title: login.Title,

0 commit comments

Comments
 (0)