Debian Codegraph @

server/backend/account.go

raw
1 package backend
2
3 import (
4 "sync"
5
6 "github.com/ProtonMail/gopenpgp/v2/crypto"
7 "github.com/bradenaw/juniper/xslices"
8 "github.com/google/uuid"
9 "github.com/henrybear327/go-proton-api"
10 )
11
12 type account struct {
13 userID string
14 username string
15 addresses map[string]*address
16 mailSettings *mailSettings
17 userSettings proton.UserSettings
18
19 auth map[string]auth
20 authLock sync.RWMutex
21
22 keys []key
23 salt []byte
24 verifier []byte
25
26 labelIDs []string
27 messageIDs []string
28 updateIDs []ID
29 }
30
31 func newAccount(userID, username string, armKey string, salt, verifier []byte) *account {
32 return &account{
33 userID: userID,
34 username: username,
35 addresses: make(map[string]*address),
36 mailSettings: newMailSettings(username),
37 userSettings: newUserSettings(),
38
39 auth: make(map[string]auth),
40 keys: []key{{keyID: uuid.NewString(), key: armKey}},
41 salt: salt,
42 verifier: verifier,
43 }
44 }
45
46 func (acc *account) toUser() proton.User {
47 return proton.User{
48 ID: acc.userID,
49 Name: acc.username,
50 DisplayName: acc.username,
51 Email: acc.primary().email,
52 Keys: xslices.Map(acc.keys, func(key key) proton.Key {
53 privKey, err := crypto.NewKeyFromArmored(key.key)
54 if err != nil {
55 panic(err)
56 }
57
58 rawKey, err := privKey.Serialize()
59 if err != nil {
60 panic(err)
61 }
62
63 return proton.Key{
64 ID: key.keyID,
65 PrivateKey: rawKey,
66 Primary: key == acc.keys[0],
67 Active: true,
68 }
69 }),
70 }
71 }
72
73 func (acc *account) primary() *address {
74 for _, addr := range acc.addresses {
75 if addr.order == 1 {
76 return addr
77 }
78 }
79
80 panic("no primary address")
81 }
82
83 func (acc *account) getAddr(email string) (*address, bool) {
84 for _, addr := range acc.addresses {
85 if addr.email == email {
86 return addr, true
87 }
88 }
89
90 return nil, false
91 }