You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

error.go 8.5KB

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