1// Copyright 2021 The Gitea Authors. All rights reserved.
2// Use of this source code is governed by a MIT-style
3// license that can be found in the LICENSE file.
4
5package asymkey
6
7import "fmt"
8
9// ErrKeyUnableVerify represents a "KeyUnableVerify" kind of error.
10type ErrKeyUnableVerify struct {
11	Result string
12}
13
14// IsErrKeyUnableVerify checks if an error is a ErrKeyUnableVerify.
15func IsErrKeyUnableVerify(err error) bool {
16	_, ok := err.(ErrKeyUnableVerify)
17	return ok
18}
19
20func (err ErrKeyUnableVerify) Error() string {
21	return fmt.Sprintf("Unable to verify key content [result: %s]", err.Result)
22}
23
24// ErrKeyNotExist represents a "KeyNotExist" kind of error.
25type ErrKeyNotExist struct {
26	ID int64
27}
28
29// IsErrKeyNotExist checks if an error is a ErrKeyNotExist.
30func IsErrKeyNotExist(err error) bool {
31	_, ok := err.(ErrKeyNotExist)
32	return ok
33}
34
35func (err ErrKeyNotExist) Error() string {
36	return fmt.Sprintf("public key does not exist [id: %d]", err.ID)
37}
38
39// ErrKeyAlreadyExist represents a "KeyAlreadyExist" kind of error.
40type ErrKeyAlreadyExist struct {
41	OwnerID     int64
42	Fingerprint string
43	Content     string
44}
45
46// IsErrKeyAlreadyExist checks if an error is a ErrKeyAlreadyExist.
47func IsErrKeyAlreadyExist(err error) bool {
48	_, ok := err.(ErrKeyAlreadyExist)
49	return ok
50}
51
52func (err ErrKeyAlreadyExist) Error() string {
53	return fmt.Sprintf("public key already exists [owner_id: %d, finger_print: %s, content: %s]",
54		err.OwnerID, err.Fingerprint, err.Content)
55}
56
57// ErrKeyNameAlreadyUsed represents a "KeyNameAlreadyUsed" kind of error.
58type ErrKeyNameAlreadyUsed struct {
59	OwnerID int64
60	Name    string
61}
62
63// IsErrKeyNameAlreadyUsed checks if an error is a ErrKeyNameAlreadyUsed.
64func IsErrKeyNameAlreadyUsed(err error) bool {
65	_, ok := err.(ErrKeyNameAlreadyUsed)
66	return ok
67}
68
69func (err ErrKeyNameAlreadyUsed) Error() string {
70	return fmt.Sprintf("public key already exists [owner_id: %d, name: %s]", err.OwnerID, err.Name)
71}
72
73// ErrGPGNoEmailFound represents a "ErrGPGNoEmailFound" kind of error.
74type ErrGPGNoEmailFound struct {
75	FailedEmails []string
76	ID           string
77}
78
79// IsErrGPGNoEmailFound checks if an error is a ErrGPGNoEmailFound.
80func IsErrGPGNoEmailFound(err error) bool {
81	_, ok := err.(ErrGPGNoEmailFound)
82	return ok
83}
84
85func (err ErrGPGNoEmailFound) Error() string {
86	return fmt.Sprintf("none of the emails attached to the GPG key could be found: %v", err.FailedEmails)
87}
88
89// ErrGPGInvalidTokenSignature represents a "ErrGPGInvalidTokenSignature" kind of error.
90type ErrGPGInvalidTokenSignature struct {
91	Wrapped error
92	ID      string
93}
94
95// IsErrGPGInvalidTokenSignature checks if an error is a ErrGPGInvalidTokenSignature.
96func IsErrGPGInvalidTokenSignature(err error) bool {
97	_, ok := err.(ErrGPGInvalidTokenSignature)
98	return ok
99}
100
101func (err ErrGPGInvalidTokenSignature) Error() string {
102	return "the provided signature does not sign the token with the provided key"
103}
104
105// ErrGPGKeyParsing represents a "ErrGPGKeyParsing" kind of error.
106type ErrGPGKeyParsing struct {
107	ParseError error
108}
109
110// IsErrGPGKeyParsing checks if an error is a ErrGPGKeyParsing.
111func IsErrGPGKeyParsing(err error) bool {
112	_, ok := err.(ErrGPGKeyParsing)
113	return ok
114}
115
116func (err ErrGPGKeyParsing) Error() string {
117	return fmt.Sprintf("failed to parse gpg key %s", err.ParseError.Error())
118}
119
120// ErrGPGKeyNotExist represents a "GPGKeyNotExist" kind of error.
121type ErrGPGKeyNotExist struct {
122	ID int64
123}
124
125// IsErrGPGKeyNotExist checks if an error is a ErrGPGKeyNotExist.
126func IsErrGPGKeyNotExist(err error) bool {
127	_, ok := err.(ErrGPGKeyNotExist)
128	return ok
129}
130
131func (err ErrGPGKeyNotExist) Error() string {
132	return fmt.Sprintf("public gpg key does not exist [id: %d]", err.ID)
133}
134
135// ErrGPGKeyImportNotExist represents a "GPGKeyImportNotExist" kind of error.
136type ErrGPGKeyImportNotExist struct {
137	ID string
138}
139
140// IsErrGPGKeyImportNotExist checks if an error is a ErrGPGKeyImportNotExist.
141func IsErrGPGKeyImportNotExist(err error) bool {
142	_, ok := err.(ErrGPGKeyImportNotExist)
143	return ok
144}
145
146func (err ErrGPGKeyImportNotExist) Error() string {
147	return fmt.Sprintf("public gpg key import does not exist [id: %s]", err.ID)
148}
149
150// ErrGPGKeyIDAlreadyUsed represents a "GPGKeyIDAlreadyUsed" kind of error.
151type ErrGPGKeyIDAlreadyUsed struct {
152	KeyID string
153}
154
155// IsErrGPGKeyIDAlreadyUsed checks if an error is a ErrKeyNameAlreadyUsed.
156func IsErrGPGKeyIDAlreadyUsed(err error) bool {
157	_, ok := err.(ErrGPGKeyIDAlreadyUsed)
158	return ok
159}
160
161func (err ErrGPGKeyIDAlreadyUsed) Error() string {
162	return fmt.Sprintf("public key already exists [key_id: %s]", err.KeyID)
163}
164
165// ErrGPGKeyAccessDenied represents a "GPGKeyAccessDenied" kind of Error.
166type ErrGPGKeyAccessDenied struct {
167	UserID int64
168	KeyID  int64
169}
170
171// IsErrGPGKeyAccessDenied checks if an error is a ErrGPGKeyAccessDenied.
172func IsErrGPGKeyAccessDenied(err error) bool {
173	_, ok := err.(ErrGPGKeyAccessDenied)
174	return ok
175}
176
177// Error pretty-prints an error of type ErrGPGKeyAccessDenied.
178func (err ErrGPGKeyAccessDenied) Error() string {
179	return fmt.Sprintf("user does not have access to the key [user_id: %d, key_id: %d]",
180		err.UserID, err.KeyID)
181}
182
183// ErrKeyAccessDenied represents a "KeyAccessDenied" kind of error.
184type ErrKeyAccessDenied struct {
185	UserID int64
186	KeyID  int64
187	Note   string
188}
189
190// IsErrKeyAccessDenied checks if an error is a ErrKeyAccessDenied.
191func IsErrKeyAccessDenied(err error) bool {
192	_, ok := err.(ErrKeyAccessDenied)
193	return ok
194}
195
196func (err ErrKeyAccessDenied) Error() string {
197	return fmt.Sprintf("user does not have access to the key [user_id: %d, key_id: %d, note: %s]",
198		err.UserID, err.KeyID, err.Note)
199}
200
201// ErrDeployKeyNotExist represents a "DeployKeyNotExist" kind of error.
202type ErrDeployKeyNotExist struct {
203	ID     int64
204	KeyID  int64
205	RepoID int64
206}
207
208// IsErrDeployKeyNotExist checks if an error is a ErrDeployKeyNotExist.
209func IsErrDeployKeyNotExist(err error) bool {
210	_, ok := err.(ErrDeployKeyNotExist)
211	return ok
212}
213
214func (err ErrDeployKeyNotExist) Error() string {
215	return fmt.Sprintf("Deploy key does not exist [id: %d, key_id: %d, repo_id: %d]", err.ID, err.KeyID, err.RepoID)
216}
217
218// ErrDeployKeyAlreadyExist represents a "DeployKeyAlreadyExist" kind of error.
219type ErrDeployKeyAlreadyExist struct {
220	KeyID  int64
221	RepoID int64
222}
223
224// IsErrDeployKeyAlreadyExist checks if an error is a ErrDeployKeyAlreadyExist.
225func IsErrDeployKeyAlreadyExist(err error) bool {
226	_, ok := err.(ErrDeployKeyAlreadyExist)
227	return ok
228}
229
230func (err ErrDeployKeyAlreadyExist) Error() string {
231	return fmt.Sprintf("public key already exists [key_id: %d, repo_id: %d]", err.KeyID, err.RepoID)
232}
233
234// ErrDeployKeyNameAlreadyUsed represents a "DeployKeyNameAlreadyUsed" kind of error.
235type ErrDeployKeyNameAlreadyUsed struct {
236	RepoID int64
237	Name   string
238}
239
240// IsErrDeployKeyNameAlreadyUsed checks if an error is a ErrDeployKeyNameAlreadyUsed.
241func IsErrDeployKeyNameAlreadyUsed(err error) bool {
242	_, ok := err.(ErrDeployKeyNameAlreadyUsed)
243	return ok
244}
245
246func (err ErrDeployKeyNameAlreadyUsed) Error() string {
247	return fmt.Sprintf("public key with name already exists [repo_id: %d, name: %s]", err.RepoID, err.Name)
248}
249
250// ErrSSHInvalidTokenSignature represents a "ErrSSHInvalidTokenSignature" kind of error.
251type ErrSSHInvalidTokenSignature struct {
252	Wrapped     error
253	Fingerprint string
254}
255
256// IsErrSSHInvalidTokenSignature checks if an error is a ErrSSHInvalidTokenSignature.
257func IsErrSSHInvalidTokenSignature(err error) bool {
258	_, ok := err.(ErrSSHInvalidTokenSignature)
259	return ok
260}
261
262func (err ErrSSHInvalidTokenSignature) Error() string {
263	return "the provided signature does not sign the token with the provided key"
264}
265