-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathtypes.go
More file actions
128 lines (101 loc) · 2.12 KB
/
types.go
File metadata and controls
128 lines (101 loc) · 2.12 KB
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package backend
import (
"encoding/base64"
"math/big"
"time"
"github.com/ProtonMail/go-proton-api"
"github.com/ProtonMail/gopenpgp/v2/crypto"
"github.com/google/uuid"
)
type ID uint64
func (v ID) String() string {
return base64.URLEncoding.EncodeToString(v.Bytes())
}
func (v ID) Bytes() []byte {
if v == 0 {
return []byte{0}
}
return new(big.Int).SetUint64(uint64(v)).Bytes()
}
func (v *ID) FromString(s string) error {
b, err := base64.URLEncoding.DecodeString(s)
if err != nil {
return err
}
*v = ID(new(big.Int).SetBytes(b).Uint64())
return nil
}
type Scope int
// TODO: Add more scopes?
const (
ScopeNone Scope = iota
ScopeTOTP
ScopeFull
)
type auth struct {
acc string
ref string
scope Scope
creation time.Time
}
func newAuth(scope Scope) auth {
return auth{
acc: uuid.NewString(),
ref: uuid.NewString(),
scope: scope,
creation: time.Now(),
}
}
func newAuthFromExpired(old auth) auth {
return auth{
acc: "",
ref: old.ref,
scope: old.scope,
creation: old.creation,
}
}
func (auth *auth) toAuth(userID, authUID string, proof []byte) proton.Auth {
return proton.Auth{
UserID: userID,
UID: authUID,
AccessToken: auth.acc,
RefreshToken: auth.ref,
ServerProof: base64.StdEncoding.EncodeToString(proof),
PasswordMode: proton.OnePasswordMode,
}
}
func (auth *auth) toAuthSession(authUID string) proton.AuthSession {
return proton.AuthSession{
UID: authUID,
CreateTime: auth.creation.Unix(),
Revocable: true,
}
}
type key struct {
keyID string
key string
tok string
sig string
}
func (key key) unlock(passphrase []byte) (*crypto.KeyRing, error) {
lockedKey, err := crypto.NewKeyFromArmored(key.key)
if err != nil {
return nil, err
}
unlockedKey, err := lockedKey.Unlock(passphrase)
if err != nil {
return nil, err
}
return crypto.NewKeyRing(unlockedKey)
}
func (key key) getPubKey() (*crypto.Key, error) {
privKey, err := crypto.NewKeyFromArmored(key.key)
if err != nil {
return nil, err
}
pubKeyBin, err := privKey.GetPublicKey()
if err != nil {
return nil, err
}
return crypto.NewKey(pubKeyBin)
}