mirror of
https://github.com/ProtonMail/proton-bridge.git
synced 2025-12-17 23:56:56 +00:00
Store factory to make store optional
This commit is contained in:
@ -30,12 +30,17 @@ import (
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
const sep = "\x00"
|
||||
const (
|
||||
sep = "\x00"
|
||||
|
||||
itemLengthBridge = 9
|
||||
itemLengthImportExport = 6 // Old format for Import/Export.
|
||||
)
|
||||
|
||||
var (
|
||||
log = logrus.WithField("pkg", "credentials") //nolint[gochecknoglobals]
|
||||
|
||||
ErrWrongFormat = errors.New("backend/creds: malformed password")
|
||||
ErrWrongFormat = errors.New("malformed credentials")
|
||||
)
|
||||
|
||||
type Credentials struct {
|
||||
@ -85,7 +90,7 @@ func (s *Credentials) Unmarshal(secret string) error {
|
||||
}
|
||||
items := strings.Split(string(b), sep)
|
||||
|
||||
if len(items) != 9 {
|
||||
if len(items) != itemLengthBridge && len(items) != itemLengthImportExport {
|
||||
return ErrWrongFormat
|
||||
}
|
||||
|
||||
@ -93,16 +98,26 @@ func (s *Credentials) Unmarshal(secret string) error {
|
||||
s.Emails = items[1]
|
||||
s.APIToken = items[2]
|
||||
s.MailboxPassword = items[3]
|
||||
s.BridgePassword = items[4]
|
||||
s.Version = items[5]
|
||||
if _, err = fmt.Sscan(items[6], &s.Timestamp); err != nil {
|
||||
s.Timestamp = 0
|
||||
}
|
||||
if s.IsHidden = false; items[7] == "1" {
|
||||
s.IsHidden = true
|
||||
}
|
||||
if s.IsCombinedAddressMode = false; items[8] == "1" {
|
||||
s.IsCombinedAddressMode = true
|
||||
|
||||
switch len(items) {
|
||||
case itemLengthBridge:
|
||||
s.BridgePassword = items[4]
|
||||
s.Version = items[5]
|
||||
if _, err = fmt.Sscan(items[6], &s.Timestamp); err != nil {
|
||||
s.Timestamp = 0
|
||||
}
|
||||
if s.IsHidden = false; items[7] == "1" {
|
||||
s.IsHidden = true
|
||||
}
|
||||
if s.IsCombinedAddressMode = false; items[8] == "1" {
|
||||
s.IsCombinedAddressMode = true
|
||||
}
|
||||
|
||||
case itemLengthImportExport:
|
||||
s.Version = items[4]
|
||||
if _, err = fmt.Sscan(items[5], &s.Timestamp); err != nil {
|
||||
s.Timestamp = 0
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
67
internal/users/credentials/credentials_test.go
Normal file
67
internal/users/credentials/credentials_test.go
Normal file
@ -0,0 +1,67 @@
|
||||
// Copyright (c) 2020 Proton Technologies AG
|
||||
//
|
||||
// This file is part of ProtonMail Bridge.
|
||||
//
|
||||
// ProtonMail Bridge is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// ProtonMail Bridge is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with ProtonMail Bridge. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
package credentials
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
r "github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
var wantCredentials = Credentials{
|
||||
UserID: "1",
|
||||
Name: "name",
|
||||
Emails: "email1;email2",
|
||||
APIToken: "token",
|
||||
MailboxPassword: "mailbox pass",
|
||||
BridgePassword: "bridge pass",
|
||||
Version: "k11",
|
||||
Timestamp: time.Now().Unix(),
|
||||
IsHidden: false,
|
||||
IsCombinedAddressMode: false,
|
||||
}
|
||||
|
||||
func TestUnmarshallBridge(t *testing.T) {
|
||||
encoded := wantCredentials.Marshal()
|
||||
haveCredentials := Credentials{UserID: "1"}
|
||||
r.NoError(t, haveCredentials.Unmarshal(encoded))
|
||||
r.Equal(t, wantCredentials, haveCredentials)
|
||||
}
|
||||
|
||||
func TestUnmarshallImportExport(t *testing.T) {
|
||||
items := []string{
|
||||
wantCredentials.Name,
|
||||
wantCredentials.Emails,
|
||||
wantCredentials.APIToken,
|
||||
wantCredentials.MailboxPassword,
|
||||
"k11",
|
||||
fmt.Sprint(wantCredentials.Timestamp),
|
||||
}
|
||||
|
||||
str := strings.Join(items, sep)
|
||||
encoded := base64.StdEncoding.EncodeToString([]byte(str))
|
||||
|
||||
haveCredentials := Credentials{UserID: "1"}
|
||||
haveCredentials.BridgePassword = wantCredentials.BridgePassword // This one is not used.
|
||||
r.NoError(t, haveCredentials.Unmarshal(encoded))
|
||||
r.Equal(t, wantCredentials, haveCredentials)
|
||||
}
|
||||
Reference in New Issue
Block a user