| 1 | package httperror |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "fmt" |
| 6 | "net/http" |
| 7 | "strings" |
| 8 | |
| 9 | "github.com/rs/zerolog" |
| 10 | "github.com/sassoftware/relic/v7/internal/zhttp" |
| 11 | ) |
| 12 | |
| 13 | // Problem implements a RFC 7807 HTTP "problem" response |
| 14 | type Problem struct { |
| 15 | Status int `json:"status"` |
| 16 | Type string `json:"type"` |
| 17 | |
| 18 | Title string `json:"title,omitempty"` |
| 19 | Detail string `json:"detail,omitempty"` |
| 20 | Instance string `json:"instance,omitempty"` |
| 21 | |
| 22 | // error-specific |
| 23 | Param string `json:"param,omitempty"` |
| 24 | Errors []string `json:"errors,omitempty"` |
| 25 | } |
| 26 | |
| 27 | func (e Problem) Error() string { |
| 28 | title := e.Title |
| 29 | if title == "" { |
| 30 | title = "[" + e.Type + "]" |
| 31 | } |
| 32 | m := fmt.Sprintf("HTTP %d %s", e.Status, title) |
| 33 | if e.Detail != "" { |
| 34 | m += ": " + e.Detail |
| 35 | } |
| 36 | return m |
| 37 | } |
| 38 | |
| 39 | func (e Problem) ServeHTTP(rw http.ResponseWriter, req *http.Request) { |
| 40 | if e.Type != "" { |
| 41 | zhttp.AppendAccessLog(req, func(ev *zerolog.Event) { |
| 42 | ev.Str("problem", e.Type) |
| 43 | }) |
| 44 | } |
| 45 | blob, _ := json.MarshalIndent(e, "", " ") |
| 46 | rw.Header().Set("Content-Type", "application/problem+json") |
| 47 | rw.WriteHeader(e.Status) |
| 48 | _, _ = rw.Write(blob) |
| 49 | } |
| 50 | |
| 51 | func (e Problem) Temporary() bool { |
| 52 | return statusIsTemporary(e.Status) |
| 53 | } |
| 54 | |
| 55 | const ( |
| 56 | ProblemBase = "https://relic.sas.com/" |
| 57 | ProblemKeyUsage = ProblemBase + "key-usage" |
| 58 | ) |
| 59 | |
| 60 | var ( |
| 61 | ErrForbidden = &Problem{ |
| 62 | Status: http.StatusForbidden, |
| 63 | Type: ProblemBase + "forbidden", |
| 64 | } |
| 65 | ErrCertificateRequired = &Problem{ |
| 66 | Status: http.StatusUnauthorized, |
| 67 | Type: ProblemBase + "certificate-required", |
| 68 | Detail: "A client certificate must be provided to use this service", |
| 69 | } |
| 70 | ErrCertificateNotRecognized = &Problem{ |
| 71 | Status: http.StatusUnauthorized, |
| 72 | Type: ProblemBase + "certificate-not-recognized", |
| 73 | Detail: "The provided client certificate was not recognized or does not grant access to any resources", |
| 74 | } |
| 75 | ErrTokenRequired = &Problem{ |
| 76 | Status: http.StatusUnauthorized, |
| 77 | Type: ProblemBase + "token-required", |
| 78 | Detail: "A bearer token or client certificate must be provided to use this service", |
| 79 | } |
| 80 | ErrUnknownSignatureType = &Problem{ |
| 81 | Status: http.StatusBadRequest, |
| 82 | Type: ProblemBase + "unknown-signature-type", |
| 83 | Detail: "Unknown signature type specified", |
| 84 | } |
| 85 | ErrUnknownDigest = &Problem{ |
| 86 | Status: http.StatusBadRequest, |
| 87 | Type: ProblemBase + "unknown-digest-algorithm", |
| 88 | Detail: "Unknown digest algorithm specified", |
| 89 | } |
| 90 | ) |
| 91 | |
| 92 | func MissingParameterError(param string) Problem { |
| 93 | return Problem{ |
| 94 | Status: http.StatusBadRequest, |
| 95 | Type: ProblemBase + "missing-parameter", |
| 96 | Detail: "Parameter " + param + " is required", |
| 97 | Param: param, |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | func BadParameterError(err error) Problem { |
| 102 | return Problem{ |
| 103 | Status: http.StatusBadRequest, |
| 104 | Type: ProblemBase + "bad-parameter", |
| 105 | Detail: "Failed to parse signer parameters: " + err.Error(), |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | func TokenAuthorizationError(code int, errors []string) Problem { |
| 110 | p := Problem{ |
| 111 | Status: code, |
| 112 | Type: ProblemBase + "token-authorization-failed", |
| 113 | Errors: errors, |
| 114 | } |
| 115 | if len(p.Errors) == 0 { |
| 116 | p.Detail = "denied by policy" |
| 117 | } else { |
| 118 | p.Detail = strings.Join(errors, ", ") |
| 119 | } |
| 120 | return p |
| 121 | } |
| 122 | |
| 123 | func NoCertificateError(certType string) Problem { |
| 124 | return Problem{ |
| 125 | Status: http.StatusBadRequest, |
| 126 | Type: ProblemBase + "certificate-not-defined", |
| 127 | Detail: "No certificate of type \"" + certType + "\" is defined for this key", |
| 128 | } |
| 129 | } |