-
Notifications
You must be signed in to change notification settings - Fork 3
/
errors.go
362 lines (310 loc) · 11.4 KB
/
errors.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
// Copyright 2023 Cisco Systems, Inc. and its affiliates
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// SPDX-License-Identifier: Apache-2.0
package oauth2
import (
"bytes"
"encoding/gob"
"encoding/json"
"errors"
"github.com/cisco-open/go-lanai/pkg/security"
errorutils "github.com/cisco-open/go-lanai/pkg/utils/error"
"net/http"
)
// All "SubType" values are used as mask
// sub types of security.ErrorTypeCodeOAuth2
const (
_ = iota
ErrorSubTypeCodeOAuth2Internal = security.ErrorTypeCodeOAuth2 + iota<<errorutils.ErrorSubTypeOffset
ErrorSubTypeCodeOAuth2ClientAuth
ErrorSubTypeCodeOAuth2Authorize
ErrorSubTypeCodeOAuth2Grant
ErrorSubTypeCodeOAuth2Res
)
// ErrorSubTypeCodeOAuth2Internal
const (
_ = ErrorSubTypeCodeOAuth2Internal + iota
ErrorCodeOAuth2InternalGeneral
)
// ErrorSubTypeCodeOAuth2ClientAuth
const (
_ = ErrorSubTypeCodeOAuth2ClientAuth + iota
ErrorCodeClientNotFound
ErrorCodeInvalidClient
)
// ErrorSubTypeCodeOAuth2Authorize
const (
_ = ErrorSubTypeCodeOAuth2Authorize + iota
ErrorCodeInvalidAuthorizeRequest
ErrorCodeInvalidResponseType
ErrorCodeInvalidRedirectUri
ErrorCodeAccessRejected
ErrorCodeOpenIDExt
)
// ErrorSubTypeCodeOAuth2Grant
const (
_ = ErrorSubTypeCodeOAuth2Grant + iota
ErrorCodeGranterNotAvailable
ErrorCodeUnauthorizedClient // grant type is not allowed for client
ErrorCodeInvalidTokenRequest
ErrorCodeInvalidGrant
ErrorCodeInvalidScope
ErrorCodeUnsupportedTokenType
ErrorCodeGeneric
)
// ErrorSubTypeCodeOAuth2Res
const (
_ = ErrorSubTypeCodeOAuth2Res + iota
ErrorCodeInvalidAccessToken
ErrorCodeInsufficientScope
ErrorCodeResourceServerGeneral // this should only be used for error deserialization
)
// ErrorTypes, can be used in errors.Is
//goland:noinspection GoUnusedGlobalVariable
var (
ErrorTypeOAuth2 = security.NewErrorType(security.ErrorTypeCodeOAuth2, errors.New("error type: oauth2"))
ErrorSubTypeOAuth2Internal = security.NewErrorSubType(ErrorSubTypeCodeOAuth2Internal, errors.New("error sub-type: internal"))
ErrorSubTypeOAuth2ClientAuth = security.NewErrorSubType(ErrorSubTypeCodeOAuth2ClientAuth, errors.New("error sub-type: oauth2 client auth"))
ErrorSubTypeOAuth2Authorize = security.NewErrorSubType(ErrorSubTypeCodeOAuth2Authorize, errors.New("error sub-type: oauth2 auth"))
ErrorSubTypeOAuth2Grant = security.NewErrorSubType(ErrorSubTypeCodeOAuth2Grant, errors.New("error sub-type: oauth2 grant"))
ErrorSubTypeOAuth2Res = security.NewErrorSubType(ErrorSubTypeCodeOAuth2Res, errors.New("error sub-type: oauth2 resource"))
)
/************************
Error EC
*************************/
//goland:noinspection GoCommentStart
const (
// https://tools.ietf.org/html/rfc6749#section-4.1.2.1
ErrorTranslationInvalidRequest = "invalid_request"
ErrorTranslationUnauthorizedClient = "unauthorized_client"
ErrorTranslationAccessDenied = "access_denied"
ErrorTranslationInvalidResponseType = "unsupported_response_type"
ErrorTranslationInvalidScope = "invalid_scope"
ErrorTranslationInternal = "server_error"
ErrorTranslationInternalNA = "temporarily_unavailable"
// https://tools.ietf.org/html/rfc6749#section-5.2
ErrorTranslationInvalidClient = "invalid_client"
ErrorTranslationInvalidGrant = "invalid_grant"
ErrorTranslationGrantNotSupported = "unsupported_grant_type"
// commonly used (no RFC reference for now)
ErrorTranslationInsufficientScope = "insufficient_scope"
ErrorTranslationInvalidToken = "invalid_token"
ErrorTranslationRedirectMismatch = "redirect_uri_mismatch"
// https://tools.ietf.org/html/rfc7009#section-4.1.1
ErrorTranslationUnsupportedTokenType = "unsupported_token_type"
// https://openid.net/specs/openid-connect-core-1_0.html#AuthError
ErrorTranslationInteractionRequired = "interaction_required"
ErrorTranslationLoginRequired = "login_required"
ErrorTranslationAcctSelectRequired = "account_selection_required"
ErrorTranslationConsentRequired = "consent_required"
ErrorTranslationInvalidRequestURI = "invalid_request_uri"
ErrorTranslationInvalidRequestObj = "invalid_request_object"
ErrorTranslationRequestUnsupported = "request_not_supported"
ErrorTranslationRequestURIUnsupported = "request_uri_not_supported"
ErrorTranslationRegistrationUnsupported = "registration_not_supported"
//ErrorTranslation = ""
)
/************************
Extensions
*************************/
//goland:noinspection GoNameStartsWithPackageName
type OAuth2ErrorTranslator interface {
error
TranslateErrorCode() string
TranslateStatusCode() int
}
// OAuth2Error extends security.CodedError, and implements:
// - OAuth2ErrorTranslator
// - json.Marshaler
// - json.Unmarshaler
// - web.Headerer
// - web.StatusCoder
// - encoding.BinaryMarshaler
// - encoding.BinaryUnmarshaler
//goland:noinspection GoNameStartsWithPackageName
type OAuth2Error struct {
security.CodedError
EC string // oauth error code
SC int // status code
}
func (e *OAuth2Error) StatusCode() int {
return e.SC
}
func (e *OAuth2Error) Headers() http.Header {
header := http.Header{}
header.Add("Cache-Control", "no-store")
header.Add("Pragma", "no-cache")
return header
}
func (e *OAuth2Error) TranslateErrorCode() string {
return e.EC
}
func (e *OAuth2Error) TranslateStatusCode() int {
return e.SC
}
// MarshalJSON implements json.Marshaler
func (e *OAuth2Error) MarshalJSON() ([]byte, error) {
data := map[string]string{
ParameterError: e.EC,
ParameterErrorDescription: e.Error(),
}
return json.Marshal(data)
}
// UnmarshalJSON implements json.Unmarshaler
// Note: JSON doesn't include internal code error. So reconstruct error from JSON is not possible.
// Unmarshaler can only be used for opaque token checking HTTP call
func (e *OAuth2Error) UnmarshalJSON(data []byte) error {
values := map[string]string{}
if e := json.Unmarshal(data, &values); e != nil {
return e
}
e.EC = values[ParameterError]
desc := values[ParameterErrorDescription]
e.CodedError = *security.NewCodedError(ErrorCodeResourceServerGeneral, desc)
return nil
}
type oauth2ErrorCarrier struct {
CodedError security.CodedError
EC string // oauth error code
SC int // status code
}
// MarshalBinary implements encoding.BinaryMarshaler interface
func (e OAuth2Error) MarshalBinary() ([]byte, error) {
buffer := bytes.NewBuffer([]byte{})
encoder := gob.NewEncoder(buffer)
carrier := oauth2ErrorCarrier{
CodedError: e.CodedError,
EC: e.EC,
SC: e.SC,
}
if e := encoder.Encode(&carrier); e != nil {
return nil, e
}
return buffer.Bytes(), nil
}
// UnmarshalBinary implements encoding.BinaryUnmarshaler interface
func (e *OAuth2Error) UnmarshalBinary(data []byte) error {
buffer := bytes.NewBuffer(data)
decoder := gob.NewDecoder(buffer)
carrier := oauth2ErrorCarrier{}
if e := decoder.Decode(&carrier); e != nil {
return e
}
*e = OAuth2Error{
CodedError: carrier.CodedError,
EC: carrier.EC,
SC: carrier.SC,
}
return nil
}
/************************
Constructors
*************************/
func NewOAuth2Error(code int64, e interface{}, oauth2Code string, sc int, causes ...interface{}) *OAuth2Error {
embedded := security.NewCodedError(code, e, causes...)
return &OAuth2Error{
CodedError: *embedded,
EC: oauth2Code,
SC: sc,
}
}
/* OAuth2Internal family */
func NewInternalError(value interface{}, causes ...interface{}) error {
return NewOAuth2Error(ErrorCodeOAuth2InternalGeneral, value,
ErrorTranslationInternal, http.StatusBadRequest,
causes...)
}
func NewInternalUnavailableError(value interface{}, causes ...interface{}) error {
return NewOAuth2Error(ErrorCodeOAuth2InternalGeneral, value,
ErrorTranslationInternalNA, http.StatusBadRequest,
causes...)
}
/* OAuth2Auth family */
func NewGranterNotAvailableError(value interface{}, causes ...interface{}) error {
return NewOAuth2Error(ErrorCodeGranterNotAvailable, value,
ErrorTranslationGrantNotSupported, http.StatusBadRequest,
causes...)
}
func NewInvalidTokenRequestError(value interface{}, causes ...interface{}) error {
return NewOAuth2Error(ErrorCodeInvalidTokenRequest, value,
ErrorTranslationInvalidRequest, http.StatusBadRequest,
causes...)
}
func NewInvalidClientError(value interface{}, causes ...interface{}) error {
return NewOAuth2Error(ErrorCodeInvalidClient, value,
ErrorTranslationInvalidClient, http.StatusUnauthorized,
causes...)
}
func NewClientNotFoundError(value interface{}, causes ...interface{}) error {
return NewOAuth2Error(ErrorCodeClientNotFound, value,
ErrorTranslationInvalidClient, http.StatusUnauthorized,
causes...)
}
func NewUnauthorizedClientError(value interface{}, causes ...interface{}) error {
return NewOAuth2Error(ErrorCodeUnauthorizedClient, value,
ErrorTranslationUnauthorizedClient, http.StatusBadRequest,
causes...)
}
func NewInvalidGrantError(value interface{}, causes ...interface{}) error {
return NewOAuth2Error(ErrorCodeInvalidGrant, value,
ErrorTranslationInvalidGrant, http.StatusBadRequest,
causes...)
}
func NewInvalidScopeError(value interface{}, causes ...interface{}) error {
return NewOAuth2Error(ErrorCodeInvalidScope, value,
ErrorTranslationInvalidScope, http.StatusBadRequest,
causes...)
}
func NewUnsupportedTokenTypeError(value interface{}, causes ...interface{}) error {
return NewOAuth2Error(ErrorCodeUnsupportedTokenType, value,
ErrorTranslationUnsupportedTokenType, http.StatusBadRequest,
causes...)
}
func NewGenericError(value interface{}, causes ...interface{}) error {
return NewOAuth2Error(ErrorCodeGeneric, value,
ErrorTranslationInvalidRequest, http.StatusBadRequest,
causes...)
}
func NewInvalidAuthorizeRequestError(value interface{}, causes ...interface{}) error {
return NewOAuth2Error(ErrorCodeInvalidAuthorizeRequest, value,
ErrorTranslationInvalidRequest, http.StatusBadRequest,
causes...)
}
func NewInvalidRedirectUriError(value interface{}, causes ...interface{}) error {
return NewOAuth2Error(ErrorCodeInvalidRedirectUri, value,
ErrorTranslationRedirectMismatch, http.StatusBadRequest,
causes...)
}
func NewInvalidResponseTypeError(value interface{}, causes ...interface{}) error {
return NewOAuth2Error(ErrorCodeInvalidResponseType, value,
ErrorTranslationInvalidResponseType, http.StatusBadRequest,
causes...)
}
func NewAccessRejectedError(value interface{}, causes ...interface{}) error {
return NewOAuth2Error(ErrorCodeAccessRejected, value,
ErrorTranslationAccessDenied, http.StatusBadRequest,
causes...)
}
/* OAuth2Res family */
func NewInvalidAccessTokenError(value interface{}, causes ...interface{}) error {
return NewOAuth2Error(ErrorCodeInvalidAccessToken, value,
ErrorTranslationInvalidToken, http.StatusUnauthorized,
causes...)
}
func NewInsufficientScopeError(value interface{}, causes ...interface{}) error {
return NewOAuth2Error(ErrorCodeInsufficientScope, value,
ErrorTranslationInsufficientScope, http.StatusForbidden,
causes...)
}