File: account.go

package info (click to toggle)
golang-github-henrybear327-go-proton-api 1.0.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,088 kB
  • sloc: sh: 55; makefile: 26
file content (91 lines) | stat: -rw-r--r-- 1,826 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package backend

import (
	"sync"

	"github.com/ProtonMail/gopenpgp/v2/crypto"
	"github.com/bradenaw/juniper/xslices"
	"github.com/google/uuid"
	"github.com/henrybear327/go-proton-api"
)

type account struct {
	userID       string
	username     string
	addresses    map[string]*address
	mailSettings *mailSettings
	userSettings proton.UserSettings

	auth     map[string]auth
	authLock sync.RWMutex

	keys     []key
	salt     []byte
	verifier []byte

	labelIDs   []string
	messageIDs []string
	updateIDs  []ID
}

func newAccount(userID, username string, armKey string, salt, verifier []byte) *account {
	return &account{
		userID:       userID,
		username:     username,
		addresses:    make(map[string]*address),
		mailSettings: newMailSettings(username),
		userSettings: newUserSettings(),

		auth:     make(map[string]auth),
		keys:     []key{{keyID: uuid.NewString(), key: armKey}},
		salt:     salt,
		verifier: verifier,
	}
}

func (acc *account) toUser() proton.User {
	return proton.User{
		ID:          acc.userID,
		Name:        acc.username,
		DisplayName: acc.username,
		Email:       acc.primary().email,
		Keys: xslices.Map(acc.keys, func(key key) proton.Key {
			privKey, err := crypto.NewKeyFromArmored(key.key)
			if err != nil {
				panic(err)
			}

			rawKey, err := privKey.Serialize()
			if err != nil {
				panic(err)
			}

			return proton.Key{
				ID:         key.keyID,
				PrivateKey: rawKey,
				Primary:    key == acc.keys[0],
				Active:     true,
			}
		}),
	}
}

func (acc *account) primary() *address {
	for _, addr := range acc.addresses {
		if addr.order == 1 {
			return addr
		}
	}

	panic("no primary address")
}

func (acc *account) getAddr(email string) (*address, bool) {
	for _, addr := range acc.addresses {
		if addr.email == email {
			return addr, true
		}
	}

	return nil, false
}