response.go (3922B)
1 package errors 2 3 import ( 4 "errors" 5 "net/http" 6 ) 7 8 // Response error response 9 type Response struct { 10 Error error 11 ErrorCode int 12 Description string 13 URI string 14 StatusCode int 15 Header http.Header 16 } 17 18 // NewResponse create the response pointer 19 func NewResponse(err error, statusCode int) *Response { 20 return &Response{ 21 Error: err, 22 StatusCode: statusCode, 23 } 24 } 25 26 // SetHeader sets the header entries associated with key to 27 // the single element value. 28 func (r *Response) SetHeader(key, value string) { 29 if r.Header == nil { 30 r.Header = make(http.Header) 31 } 32 r.Header.Set(key, value) 33 } 34 35 // https://tools.ietf.org/html/rfc6749#section-5.2 36 var ( 37 ErrInvalidRequest = errors.New("invalid_request") 38 ErrUnauthorizedClient = errors.New("unauthorized_client") 39 ErrAccessDenied = errors.New("access_denied") 40 ErrUnsupportedResponseType = errors.New("unsupported_response_type") 41 ErrInvalidScope = errors.New("invalid_scope") 42 ErrServerError = errors.New("server_error") 43 ErrTemporarilyUnavailable = errors.New("temporarily_unavailable") 44 ErrInvalidClient = errors.New("invalid_client") 45 ErrInvalidGrant = errors.New("invalid_grant") 46 ErrUnsupportedGrantType = errors.New("unsupported_grant_type") 47 ErrCodeChallengeRquired = errors.New("invalid_request") 48 ErrUnsupportedCodeChallengeMethod = errors.New("invalid_request") 49 ErrInvalidCodeChallengeLen = errors.New("invalid_request") 50 ) 51 52 // Descriptions error description 53 var Descriptions = map[error]string{ 54 ErrInvalidRequest: "The request is missing a required parameter, includes an invalid parameter value, includes a parameter more than once, or is otherwise malformed", 55 ErrUnauthorizedClient: "The client is not authorized to request an authorization code using this method", 56 ErrAccessDenied: "The resource owner or authorization server denied the request", 57 ErrUnsupportedResponseType: "The authorization server does not support obtaining an authorization code using this method", 58 ErrInvalidScope: "The requested scope is invalid, unknown, or malformed", 59 ErrServerError: "The authorization server encountered an unexpected condition that prevented it from fulfilling the request", 60 ErrTemporarilyUnavailable: "The authorization server is currently unable to handle the request due to a temporary overloading or maintenance of the server", 61 ErrInvalidClient: "Client authentication failed", 62 ErrInvalidGrant: "The provided authorization grant (e.g., authorization code, resource owner credentials) or refresh token is invalid, expired, revoked, does not match the redirection URI used in the authorization request, or was issued to another client", 63 ErrUnsupportedGrantType: "The authorization grant type is not supported by the authorization server", 64 ErrCodeChallengeRquired: "PKCE is required. code_challenge is missing", 65 ErrUnsupportedCodeChallengeMethod: "Selected code_challenge_method not supported", 66 ErrInvalidCodeChallengeLen: "Code challenge length must be between 43 and 128 charachters long", 67 } 68 69 // StatusCodes response error HTTP status code 70 var StatusCodes = map[error]int{ 71 ErrInvalidRequest: 400, 72 ErrUnauthorizedClient: 401, 73 ErrAccessDenied: 403, 74 ErrUnsupportedResponseType: 401, 75 ErrInvalidScope: 400, 76 ErrServerError: 500, 77 ErrTemporarilyUnavailable: 503, 78 ErrInvalidClient: 401, 79 ErrInvalidGrant: 401, 80 ErrUnsupportedGrantType: 401, 81 ErrCodeChallengeRquired: 400, 82 ErrUnsupportedCodeChallengeMethod: 400, 83 ErrInvalidCodeChallengeLen: 400, 84 }