Debian Codegraph @

server/backend/types.go

raw
1 package backend
2
3 import (
4 "encoding/base64"
5 "math/big"
6 "time"
7
8 "github.com/ProtonMail/gopenpgp/v2/crypto"
9 "github.com/google/uuid"
10 "github.com/henrybear327/go-proton-api"
11 )
12
13 type ID uint64
14
15 func (v ID) String() string {
16 return base64.URLEncoding.EncodeToString(v.Bytes())
17 }
18
19 func (v ID) Bytes() []byte {
20 if v == 0 {
21 return []byte{0}
22 }
23
24 return new(big.Int).SetUint64(uint64(v)).Bytes()
25 }
26
27 func (v *ID) FromString(s string) error {
28 b, err := base64.URLEncoding.DecodeString(s)
29 if err != nil {
30 return err
31 }
32
33 *v = ID(new(big.Int).SetBytes(b).Uint64())
34
35 return nil
36 }
37
38 type auth struct {
39 acc string
40 ref string
41
42 creation time.Time
43 }
44
45 func newAuth(authLife time.Duration) auth {
46 return auth{
47 acc: uuid.NewString(),
48 ref: uuid.NewString(),
49
50 creation: time.Now(),
51 }
52 }
53
54 func (auth *auth) toAuth(userID, authUID string, proof []byte) proton.Auth {
55 return proton.Auth{
56 UserID: userID,
57
58 UID: authUID,
59 AccessToken: auth.acc,
60 RefreshToken: auth.ref,
61 ServerProof: base64.StdEncoding.EncodeToString(proof),
62
63 PasswordMode: proton.OnePasswordMode,
64 }
65 }
66
67 func (auth *auth) toAuthSession(authUID string) proton.AuthSession {
68 return proton.AuthSession{
69 UID: authUID,
70 CreateTime: auth.creation.Unix(),
71 Revocable: true,
72 }
73 }
74
75 type key struct {
76 keyID string
77 key string
78 tok string
79 sig string
80 }
81
82 func (key key) unlock(passphrase []byte) (*crypto.KeyRing, error) {
83 lockedKey, err := crypto.NewKeyFromArmored(key.key)
84 if err != nil {
85 return nil, err
86 }
87
88 unlockedKey, err := lockedKey.Unlock(passphrase)
89 if err != nil {
90 return nil, err
91 }
92
93 return crypto.NewKeyRing(unlockedKey)
94 }
95
96 func (key key) getPubKey() (*crypto.Key, error) {
97 privKey, err := crypto.NewKeyFromArmored(key.key)
98 if err != nil {
99 return nil, err
100 }
101
102 pubKeyBin, err := privKey.GetPublicKey()
103 if err != nil {
104 return nil, err
105 }
106
107 return crypto.NewKey(pubKeyBin)
108 }