errors.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. package email
  2. /*
  3. errors.go
  4. Sentinel errors the AGI layer maps onto flags the Mail front-end acts on
  5. (authFailed opens the sign-in dialog, blocked explains the admin policy).
  6. */
  7. import (
  8. "errors"
  9. "strings"
  10. )
  11. var (
  12. ErrAccountNotFound = errors.New("mail account not found")
  13. ErrAuthFailed = errors.New("authentication failed")
  14. ErrBlockedAddress = errors.New("connections to local network addresses are disabled by the administrator")
  15. ErrInsecureBlocked = errors.New("unencrypted mail connections are disabled by the administrator")
  16. ErrFolderNotFound = errors.New("folder not found")
  17. ErrMessageNotFound = errors.New("message not found, it may have been moved or deleted")
  18. ErrTooLarge = errors.New("message is larger than the allowed size")
  19. ErrOAuthDisabled = errors.New("this sign-in method has not been configured by the administrator")
  20. )
  21. // AuthError wraps a server's rejection of our credentials. Hint carries a
  22. // provider specific suggestion (e.g. "use an app password").
  23. type AuthError struct {
  24. Server string
  25. Detail string
  26. Hint string
  27. }
  28. func (e *AuthError) Error() string {
  29. message := "authentication failed"
  30. if e.Server != "" {
  31. message = e.Server + " " + message
  32. }
  33. if e.Detail != "" {
  34. message += ": " + e.Detail
  35. }
  36. return message
  37. }
  38. func (e *AuthError) Unwrap() error {
  39. return ErrAuthFailed
  40. }
  41. // IsAuthError reports whether err means the stored credential is wrong or
  42. // expired, so the UI should ask the user to sign in again.
  43. func IsAuthError(err error) bool {
  44. return errors.Is(err, ErrAuthFailed)
  45. }
  46. // AuthHint returns the provider hint carried by an authentication error.
  47. func AuthHint(err error) string {
  48. var authErr *AuthError
  49. if errors.As(err, &authErr) {
  50. return authErr.Hint
  51. }
  52. return ""
  53. }
  54. // looksLikeAuthFailure recognises the many ways servers word a rejected login
  55. // when they do not use the AUTHENTICATIONFAILED response code.
  56. func looksLikeAuthFailure(message string) bool {
  57. lower := strings.ToLower(message)
  58. for _, marker := range []string{
  59. "authenticationfailed", "authentication failed", "invalid credentials",
  60. "login failed", "logon failure", "authenticate failed", "auth failed",
  61. "username and password not accepted", "incorrect password",
  62. "invalid user", "authorizationfailed", "535", "534",
  63. "application-specific password required", "web login required",
  64. "unsafe login", "not authorized", "invalid login",
  65. } {
  66. if strings.Contains(lower, marker) {
  67. return true
  68. }
  69. }
  70. return false
  71. }