Skip to content

Commit

Permalink
add generic enc to email
Browse files Browse the repository at this point in the history
  • Loading branch information
yakuter committed Jul 30, 2020
1 parent 0329ad4 commit 0627c96
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 78 deletions.
9 changes: 4 additions & 5 deletions internal/api/email.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ func FindAllEmails(s storage.Store) http.HandlerFunc {
return
}

emails = app.DecryptEmailPasswords(emails)

// Encrypt payload
var payload model.Payload
key := r.Context().Value("transmissionKey").(string)
Expand All @@ -55,19 +53,20 @@ func FindEmailByID(s storage.Store) http.HandlerFunc {
}

schema := r.Context().Value("schema").(string)
account, err := s.Emails().FindByID(uint(id), schema)
email, err := s.Emails().FindByID(uint(id), schema)
if err != nil {
RespondWithError(w, http.StatusNotFound, err.Error())
return
}

email, err := app.DecryptEmailPassword(s, account)
// Decrypt server side encrypted fields
decEmail, err := app.DecryptModel(email)
if err != nil {
RespondWithError(w, http.StatusInternalServerError, err.Error())
return
}

emailDTO := model.ToEmailDTO(email)
emailDTO := model.ToEmailDTO(decEmail.(*model.Email))

// Encrypt payload
var payload model.Payload
Expand Down
49 changes: 9 additions & 40 deletions internal/app/email.go
Original file line number Diff line number Diff line change
@@ -1,68 +1,37 @@
package app

import (
"encoding/base64"

"github.com/passwall/passwall-server/internal/storage"
"github.com/passwall/passwall-server/model"
"github.com/spf13/viper"
)

// CreateEmail creates a new bank account and saves it to the store
func CreateEmail(s storage.Store, dto *model.EmailDTO, schema string) (*model.Email, error) {
rawModel := model.ToEmail(dto)
encModel := EncryptModel(rawModel)

rawPass := dto.Password
dto.Password = base64.StdEncoding.EncodeToString(Encrypt(dto.Password, viper.GetString("server.passphrase")))

createdEmail, err := s.Emails().Save(model.ToEmail(dto), schema)
createdEmail, err := s.Emails().Save(encModel.(*model.Email), schema)
if err != nil {
return nil, err
}

createdEmail.Password = rawPass

return createdEmail, nil
}

// UpdateEmail updates the account with the dto and applies the changes in the store
func UpdateEmail(s storage.Store, email *model.Email, emailDTO *model.EmailDTO, schema string) (*model.Email, error) {
func UpdateEmail(s storage.Store, email *model.Email, dto *model.EmailDTO, schema string) (*model.Email, error) {
rawModel := model.ToEmail(dto)
encModel := EncryptModel(rawModel).(*model.Email)

rawPass := emailDTO.Password
emailDTO.Password = base64.StdEncoding.EncodeToString(Encrypt(emailDTO.Password, viper.GetString("server.passphrase")))

email.ID = uint(email.ID)
email.Title = emailDTO.Title
email.Email = emailDTO.Email
email.Password = emailDTO.Password
email.Title = encModel.Title
email.Email = encModel.Email
email.Password = encModel.Password

updatedEmail, err := s.Emails().Save(email, schema)
if err != nil {

return nil, err
}

updatedEmail.Password = rawPass
return updatedEmail, nil
}

// DecryptEmailPassword decrypts password
func DecryptEmailPassword(s storage.Store, account *model.Email) (*model.Email, error) {
passByte, _ := base64.StdEncoding.DecodeString(account.Password)
account.Password = string(Decrypt(string(passByte[:]), viper.GetString("server.passphrase")))

return account, nil
}

// DecryptEmailPasswords ...
// TODO: convert to pointers
func DecryptEmailPasswords(emails []model.Email) []model.Email {
for i := range emails {
if emails[i].Password == "" {
continue
}
passByte, _ := base64.StdEncoding.DecodeString(emails[i].Password)
passB64 := string(Decrypt(string(passByte[:]), viper.GetString("server.passphrase")))
emails[i].Password = passB64
}
return emails
}
15 changes: 8 additions & 7 deletions internal/app/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,18 @@ func CreateLogin(s storage.Store, dto *model.LoginDTO, schema string) (*model.Lo

// UpdateLogin updates the login with the dto and applies the changes in the store
func UpdateLogin(s storage.Store, login *model.Login, dto *model.LoginDTO, schema string) (*model.Login, error) {
rawLogin := model.ToLogin(dto)
encLogin := EncryptModel(rawLogin).(*model.Login)
rawModel := model.ToLogin(dto)
encModel := EncryptModel(rawModel).(*model.Login)

login.Title = encModel.Title
login.URL = encModel.URL
login.Username = encModel.Username
login.Password = encModel.Password

login.Title = encLogin.Title
login.URL = encLogin.URL
login.Username = encLogin.Username
login.Password = encLogin.Password
updatedLogin, err := s.Logins().Save(login, schema)
if err != nil {
return nil, err
}
updatedLogin.Password = rawLogin.Password

return updatedLogin, nil
}
10 changes: 5 additions & 5 deletions model/bank_accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ type BankAccount struct {
DeletedAt *time.Time `json:"deleted_at"`
BankName string `json:"bank_name"`
BankCode string `json:"bank_code"`
AccountName string `json:"account_name"`
AccountNumber string `json:"account_number"`
IBAN string `json:"iban"`
Currency string `json:"currency"`
Password string `json:"password"`
AccountName string `json:"account_name" encrypt:"true"`
AccountNumber string `json:"account_number" encrypt:"true"`
IBAN string `json:"iban" encrypt:"true"`
Currency string `json:"currency" encrypt:"true"`
Password string `json:"password" encrypt:"true"`
}

type BankAccountDTO struct {
Expand Down
10 changes: 5 additions & 5 deletions model/credit_cards.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ type CreditCard struct {
UpdatedAt time.Time `json:"updated_at"`
DeletedAt *time.Time `json:"deleted_at"`
CardName string `json:"card_name"`
CardholderName string `json:"cardholder_name"`
Type string `json:"type"`
Number string `json:"number"`
VerificationNumber string `json:"verification_number"`
ExpiryDate string `json:"expiry_date"`
CardholderName string `json:"cardholder_name" encrypt:"true"`
Type string `json:"type" encrypt:"true"`
Number string `json:"number" encrypt:"true"`
VerificationNumber string `json:"verification_number" encrypt:"true"`
ExpiryDate string `json:"expiry_date" encrypt:"true"`
}

type CreditCardDTO struct {
Expand Down
4 changes: 2 additions & 2 deletions model/emails.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ type Email struct {
UpdatedAt time.Time `json:"updated_at"`
DeletedAt *time.Time `json:"deleted_at"`
Title string `json:"title"`
Email string `json:"email"`
Password string `json:"password"`
Email string `json:"email" encrypt:"true"`
Password string `json:"password" encrypt:"true"`
}

// EmailDTO ...
Expand Down
12 changes: 6 additions & 6 deletions model/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ import (

// Login ...
type Login struct {
ID uint `gorm:"primary_key" json:"id" encrypt:"false"`
CreatedAt time.Time `json:"created_at" encrypt:"false"`
UpdatedAt time.Time `json:"updated_at" encrypt:"false"`
DeletedAt *time.Time `json:"deleted_at" encrypt:"false"`
Title string `json:"title" encrypt:"false"`
URL string `json:"url" encrypt:"false"`
ID uint `gorm:"primary_key" json:"id"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
DeletedAt *time.Time `json:"deleted_at"`
Title string `json:"title"`
URL string `json:"url"`
Username string `json:"username" encrypt:"true"`
Password string `json:"password" encrypt:"true"`
}
Expand Down
2 changes: 1 addition & 1 deletion model/notes.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ type Note struct {
UpdatedAt time.Time `json:"updated_at"`
DeletedAt *time.Time `json:"deleted_at"`
Title string `json:"title"`
Note string `json:"note"`
Note string `json:"note" encrypt:"true"`
}

// NoteDTO ...
Expand Down
14 changes: 7 additions & 7 deletions model/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ type Server struct {
UpdatedAt time.Time `json:"updated_at"`
DeletedAt *time.Time `json:"deleted_at"`
Title string `json:"title"`
IP string `json:"ip"`
Username string `json:"username"`
Password string `json:"password"`
IP string `json:"ip" encrypt:"true"`
Username string `json:"username" encrypt:"true"`
Password string `json:"password" encrypt:"true"`
URL string `json:"url"`
HostingUsername string `json:"hosting_username"`
HostingPassword string `json:"hosting_password"`
AdminUsername string `json:"admin_username"`
AdminPassword string `json:"admin_password"`
HostingUsername string `json:"hosting_username" encrypt:"true"`
HostingPassword string `json:"hosting_password" encrypt:"true"`
AdminUsername string `json:"admin_username" encrypt:"true"`
AdminPassword string `json:"admin_password" encrypt:"true"`
}

type ServerDTO struct {
Expand Down

0 comments on commit 0627c96

Please sign in to comment.