File: types.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 (108 lines) | stat: -rw-r--r-- 1,893 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package backend

import (
	"encoding/base64"
	"math/big"
	"time"

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

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 auth struct {
	acc string
	ref string

	creation time.Time
}

func newAuth(authLife time.Duration) auth {
	return auth{
		acc: uuid.NewString(),
		ref: uuid.NewString(),

		creation: time.Now(),
	}
}

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)
}