File: calendar_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 (140 lines) | stat: -rw-r--r-- 2,766 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package proton

import (
	"errors"

	"github.com/ProtonMail/gopenpgp/v2/crypto"
)

type Calendar struct {
	ID          string
	Name        string
	Description string
	Color       string
	Display     Bool

	Type  CalendarType
	Flags CalendarFlag
}

type CalendarFlag int64

const (
	CalendarFlagActive CalendarFlag = 1 << iota
	CalendarFlagUpdatePassphrase
	CalendarFlagResetNeeded
	CalendarFlagIncompleteSetup
	CalendarFlagLostAccess
)

type CalendarType int

const (
	CalendarTypeNormal CalendarType = iota
	CalendarTypeSubscribed
)

type CalendarKey struct {
	ID           string
	CalendarID   string
	PassphraseID string
	PrivateKey   string
	Flags        CalendarKeyFlag
}

func (key CalendarKey) Unlock(passphrase []byte) (*crypto.Key, error) {
	lockedKey, err := crypto.NewKeyFromArmored(key.PrivateKey)
	if err != nil {
		return nil, err
	}

	return lockedKey.Unlock(passphrase)
}

type CalendarKeys []CalendarKey

func (keys CalendarKeys) Unlock(passphrase []byte) (*crypto.KeyRing, error) {
	kr, err := crypto.NewKeyRing(nil)
	if err != nil {
		return nil, err
	}

	for _, key := range keys {
		if k, err := key.Unlock(passphrase); err != nil {
			continue
		} else if err := kr.AddKey(k); err != nil {
			return nil, err
		}
	}

	return kr, nil
}

// TODO: What is this?
type CalendarKeyFlag int64

const (
	CalendarKeyFlagActive CalendarKeyFlag = 1 << iota
	CalendarKeyFlagPrimary
)

type CalendarMember struct {
	ID          string
	Permissions CalendarPermissions
	Email       string
	Color       string
	Display     Bool
	CalendarID  string
}

// TODO: What is this?
type CalendarPermissions int

// TODO: Support invitations.
type CalendarPassphrase struct {
	ID                string
	Flags             CalendarPassphraseFlag
	MemberPassphrases []MemberPassphrase
}

func (passphrase CalendarPassphrase) Decrypt(memberID string, addrKR *crypto.KeyRing) ([]byte, error) {
	for _, passphrase := range passphrase.MemberPassphrases {
		if passphrase.MemberID == memberID {
			return passphrase.decrypt(addrKR)
		}
	}

	return nil, errors.New("no such member passphrase")
}

// TODO: What is this?
type CalendarPassphraseFlag int64

type MemberPassphrase struct {
	MemberID   string
	Passphrase string
	Signature  string
}

func (passphrase MemberPassphrase) decrypt(addrKR *crypto.KeyRing) ([]byte, error) {
	msg, err := crypto.NewPGPMessageFromArmored(passphrase.Passphrase)
	if err != nil {
		return nil, err
	}

	sig, err := crypto.NewPGPSignatureFromArmored(passphrase.Signature)
	if err != nil {
		return nil, err
	}

	dec, err := addrKR.Decrypt(msg, nil, crypto.GetUnixTime())
	if err != nil {
		return nil, err
	}

	if err := addrKR.VerifyDetached(dec, sig, crypto.GetUnixTime()); err != nil {
		return nil, err
	}

	return dec.GetBinary(), nil
}