/
error_handling.go
140 lines (124 loc) · 4.35 KB
/
error_handling.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
// 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 samlidp
import (
"context"
"errors"
"fmt"
"github.com/cisco-open/go-lanai/pkg/security"
errorutils "github.com/cisco-open/go-lanai/pkg/utils/error"
"github.com/crewjam/saml"
"net/http"
)
const CtxKeySamlAuthnRequest = "kSamlAuthnRequest"
type SamlErrorHandler struct {}
func NewSamlErrorHandler() *SamlErrorHandler {
return &SamlErrorHandler{}
}
// HandleError
/**
Handles error as saml response when possible.
Otherwise let the error handling handle it
See http://docs.oasis-open.org/security/saml/v2.0/saml-profiles-2.0-os.pdf 4.1.3.5
*/
//nolint:errorlint
func (h *SamlErrorHandler) HandleError(c context.Context, r *http.Request, rw http.ResponseWriter, err error) {
err = h.findCause(err)
if !errors.Is(err, security.ErrorTypeSaml) {
return
}
switch {
case errors.Is(err, ErrorSubTypeSamlInternal):
writeErrorAsHtml(c, r, rw, err)
case errors.Is(err, ErrorSubTypeSamlSso):
h.handleSsoError(c, r, rw, err)
case errors.Is(err, ErrorSubTypeSamlSlo):
h.handleSloError(c, r, rw, err)
}
}
// findCause returns nested error if it's caused by SAML error, otherwise return error itself
//nolint:errorlint
func (h *SamlErrorHandler) findCause(err error) error {
e := err
for ;!errors.Is(e, security.ErrorTypeSaml); {
nested, ok := e.(errorutils.NestedError)
if !ok {
return err
}
e = nested.Cause()
}
return e
}
//nolint:errorlint
func (h *SamlErrorHandler) handleSsoError(c context.Context, r *http.Request, rw http.ResponseWriter, err error) {
authRequest, ok := c.Value(CtxKeySamlAuthnRequest).(*saml.IdpAuthnRequest)
if !ok {
writeErrorAsHtml(c, r, rw, err)
}
code := saml.StatusResponder
message := ""
if translator, ok := err.(SamlSsoErrorTranslator); ok { //all the saml sub types should implement the translator API
code = translator.TranslateErrorCode()
message = translator.TranslateErrorMessage()
}
respErr := MakeErrorResponse(authRequest, code, message)
if respErr != nil {
writeErrorAsHtml(c, r, rw, NewSamlInternalError("cannot create response", respErr))
}
writeErr := authRequest.WriteResponse(rw)
if writeErr != nil {
writeErrorAsHtml(c, r, rw, NewSamlInternalError("cannot write response", writeErr))
}
}
//nolint:errorlint
func (h *SamlErrorHandler) handleSloError(c context.Context, r *http.Request, rw http.ResponseWriter, err error) {
sloRequest, ok := c.Value(ctxKeySloRequest).(*SamlLogoutRequest)
if !ok {
writeErrorAsHtml(c, r, rw, err)
return
}
code := saml.StatusAuthnFailed
message := err.Error()
if translator, ok := err.(SamlSsoErrorTranslator); ok { //all the saml sub types should implement the translator API
code = translator.TranslateErrorCode()
message = translator.TranslateErrorMessage()
}
switch {
case errors.Is(err, ErrorSamlSloRequester):
// requester error, means requester is not validated, we display errors as HTML
writeErrorAsHtml(c, r, rw, err)
return
}
resp, e := MakeLogoutResponse(sloRequest, code, message)
if e != nil {
msg := fmt.Sprintf("unable to create logout error response with code [%s]: %s. Reason: %v", code, message, e)
writeErrorAsHtml(c, r, rw, NewSamlInternalError(msg, e))
return
}
sloRequest.Response = resp
if e := sloRequest.WriteResponse(rw); e != nil {
msg := fmt.Sprintf("unable to send logout error response with code [%s]: %s. Reason: %v", code, message, e)
writeErrorAsHtml(c, r, rw, NewSamlInternalError(msg, e))
}
}
func writeErrorAsHtml(c context.Context, _ *http.Request, rw http.ResponseWriter, err error) {
code := http.StatusInternalServerError
//nolint:errorlint
if translator, ok := err.(SamlSsoErrorTranslator); ok { //all the saml errors should implement this interface
code = translator.TranslateHttpStatusCode()
}
security.WriteErrorAsHtml(c, rw, code, err)
}