/
authenticator_cookie_session.go
255 lines (202 loc) · 6.85 KB
/
authenticator_cookie_session.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
// Copyright © 2023 Ory Corp
// SPDX-License-Identifier: Apache-2.0
package authn
import (
"encoding/json"
"io"
"net/http"
"net/url"
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
"go.opentelemetry.io/otel/trace"
"github.com/pkg/errors"
"github.com/tidwall/gjson"
"github.com/ory/oathkeeper/x/header"
"github.com/ory/x/otelx"
"github.com/ory/x/stringsx"
"github.com/ory/herodot"
"github.com/ory/oathkeeper/driver/configuration"
"github.com/ory/oathkeeper/helper"
"github.com/ory/oathkeeper/pipeline"
)
func init() {
gjson.AddModifier("this", func(json, arg string) string {
return json
})
}
type AuthenticatorCookieSessionFilter struct {
}
type AuthenticatorCookieSessionConfiguration struct {
Only []string `json:"only"`
CheckSessionURL string `json:"check_session_url"`
PreserveQuery bool `json:"preserve_query"`
PreservePath bool `json:"preserve_path"`
ExtraFrom string `json:"extra_from"`
SubjectFrom string `json:"subject_from"`
PreserveHost bool `json:"preserve_host"`
ForwardHTTPHeaders []string `json:"forward_http_headers"`
SetHeaders map[string]string `json:"additional_headers"`
ForceMethod string `json:"force_method"`
}
func (a *AuthenticatorCookieSessionConfiguration) GetCheckSessionURL() string {
return a.CheckSessionURL
}
func (a *AuthenticatorCookieSessionConfiguration) GetPreserveQuery() bool {
return a.PreserveQuery
}
func (a *AuthenticatorCookieSessionConfiguration) GetPreservePath() bool {
return a.PreservePath
}
func (a *AuthenticatorCookieSessionConfiguration) GetPreserveHost() bool {
return a.PreserveHost
}
func (a *AuthenticatorCookieSessionConfiguration) GetForwardHTTPHeaders() []string {
return a.ForwardHTTPHeaders
}
func (a *AuthenticatorCookieSessionConfiguration) GetSetHeaders() map[string]string {
return a.SetHeaders
}
func (a *AuthenticatorCookieSessionConfiguration) GetForceMethod() string {
return a.ForceMethod
}
type AuthenticatorCookieSession struct {
c configuration.Provider
client *http.Client
tracer trace.Tracer
}
var _ AuthenticatorForwardConfig = new(AuthenticatorCookieSessionConfiguration)
func NewAuthenticatorCookieSession(c configuration.Provider, provider trace.TracerProvider) *AuthenticatorCookieSession {
return &AuthenticatorCookieSession{
c: c,
client: &http.Client{
Transport: otelhttp.NewTransport(
http.DefaultTransport,
otelhttp.WithTracerProvider(provider),
),
},
tracer: provider.Tracer("oauthkeeper/pipeline/authn"),
}
}
func (a *AuthenticatorCookieSession) GetID() string {
return "cookie_session"
}
func (a *AuthenticatorCookieSession) Validate(config json.RawMessage) error {
if !a.c.AuthenticatorIsEnabled(a.GetID()) {
return NewErrAuthenticatorNotEnabled(a)
}
_, err := a.Config(config)
return err
}
func (a *AuthenticatorCookieSession) Config(config json.RawMessage) (*AuthenticatorCookieSessionConfiguration, error) {
var c AuthenticatorCookieSessionConfiguration
if err := a.c.AuthenticatorConfig(a.GetID(), config, &c); err != nil {
return nil, NewErrAuthenticatorMisconfigured(a, err)
}
if len(c.ExtraFrom) == 0 {
c.ExtraFrom = "extra"
}
if len(c.SubjectFrom) == 0 {
c.SubjectFrom = "subject"
}
// Add Authorization and Cookie headers for backward compatibility
c.ForwardHTTPHeaders = append(c.ForwardHTTPHeaders, []string{header.Cookie}...)
return &c, nil
}
func (a *AuthenticatorCookieSession) Authenticate(r *http.Request, session *AuthenticationSession, config json.RawMessage, _ pipeline.Rule) (err error) {
ctx, span := a.tracer.Start(r.Context(), "pipeline.authn.AuthenticatorCookieSession.Authenticate")
defer otelx.End(span, &err)
r = r.WithContext(ctx)
cf, err := a.Config(config)
if err != nil {
return err
}
if !cookieSessionResponsible(r, cf.Only) {
return errors.WithStack(ErrAuthenticatorNotResponsible)
}
body, err := forwardRequestToSessionStore(a.client, r, cf)
if err != nil {
return err
}
var (
subject string
extra map[string]interface{}
subjectRaw = []byte(stringsx.Coalesce(gjson.GetBytes(body, cf.SubjectFrom).Raw, "null"))
extraRaw = []byte(stringsx.Coalesce(gjson.GetBytes(body, cf.ExtraFrom).Raw, "null"))
)
if err = json.Unmarshal(subjectRaw, &subject); err != nil {
return helper.ErrForbidden.WithReasonf("The configured subject_from GJSON path returned an error on JSON output: %s", err.Error()).WithDebugf("GJSON path: %s\nBody: %s\nResult: %s", cf.SubjectFrom, body, subjectRaw).WithTrace(err)
}
if err = json.Unmarshal(extraRaw, &extra); err != nil {
return helper.ErrForbidden.WithReasonf("The configured extra_from GJSON path returned an error on JSON output: %s", err.Error()).WithDebugf("GJSON path: %s\nBody: %s\nResult: %s", cf.ExtraFrom, body, extraRaw).WithTrace(err)
}
session.Subject = subject
session.Extra = extra
return nil
}
func cookieSessionResponsible(r *http.Request, only []string) bool {
if len(only) == 0 && len(r.Cookies()) > 0 {
return true
}
for _, cookieName := range only {
if _, err := r.Cookie(cookieName); err == nil {
return true
}
}
return false
}
func forwardRequestToSessionStore(client *http.Client, r *http.Request, cf AuthenticatorForwardConfig) (json.RawMessage, error) {
req, err := PrepareRequest(r, cf)
if err != nil {
return nil, err
}
res, err := client.Do(req.WithContext(r.Context()))
if err != nil {
return nil, helper.ErrForbidden.WithReason(err.Error()).WithTrace(err)
}
defer res.Body.Close()
if res.StatusCode == http.StatusOK {
body, err := io.ReadAll(res.Body)
if err != nil {
return json.RawMessage{}, errors.WithStack(herodot.ErrInternalServerError.WithReasonf("Unable to fetch cookie session context from remote: %+v", err))
}
return body, nil
} else {
return json.RawMessage{}, errors.WithStack(helper.ErrUnauthorized)
}
}
func PrepareRequest(r *http.Request, cf AuthenticatorForwardConfig) (http.Request, error) {
reqURL, err := url.Parse(cf.GetCheckSessionURL())
if err != nil {
return http.Request{}, errors.WithStack(herodot.ErrInternalServerError.WithReasonf("Unable to parse session check URL: %s", err))
}
if !cf.GetPreservePath() {
reqURL.Path = r.URL.Path
}
if !cf.GetPreserveQuery() {
reqURL.RawQuery = r.URL.RawQuery
}
m := cf.GetForceMethod()
if m == "" {
m = r.Method
}
req := http.Request{
Method: m,
URL: reqURL,
Header: http.Header{},
}
// We need to copy only essential and configurable headers
for requested, v := range r.Header {
for _, allowed := range cf.GetForwardHTTPHeaders() {
// Check against canonical names of header
if requested == header.Canonical(allowed) {
req.Header[requested] = v
}
}
}
for k, v := range cf.GetSetHeaders() {
req.Header.Set(k, v)
}
if cf.GetPreserveHost() {
req.Header.Set(header.XForwardedHost, r.Host)
}
return req, nil
}