-
Notifications
You must be signed in to change notification settings - Fork 359
/
Copy pathauthenticator.go
78 lines (63 loc) · 2.24 KB
/
authenticator.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
// Copyright © 2023 Ory Corp
// SPDX-License-Identifier: Apache-2.0
package authn
import (
"encoding/json"
"net/http"
"net/url"
"github.com/pkg/errors"
"github.com/ory/herodot"
"github.com/mitchellh/copystructure"
"github.com/ory/oathkeeper/pipeline"
)
var ErrAuthenticatorNotResponsible = errors.New("Authenticator not responsible")
var ErrAuthenticatorNotEnabled = herodot.DefaultError{
ErrorField: "authenticator matching this route is misconfigured or disabled",
CodeField: http.StatusInternalServerError,
StatusField: http.StatusText(http.StatusInternalServerError),
}
type Authenticator interface {
Authenticate(r *http.Request, session *AuthenticationSession, config json.RawMessage, rule pipeline.Rule) error
GetID() string
Validate(config json.RawMessage) error
}
func NewErrAuthenticatorNotEnabled(a Authenticator) *herodot.DefaultError {
return ErrAuthenticatorNotEnabled.WithTrace(errors.New("")).WithReasonf(`Authenticator "%s" is disabled per configuration.`, a.GetID())
}
func NewErrAuthenticatorMisconfigured(a Authenticator, err error) *herodot.DefaultError {
return ErrAuthenticatorNotEnabled.WithTrace(err).WithReasonf(
`Configuration for authenticator "%s" could not be validated: %s`,
a.GetID(),
err,
)
}
type AuthenticationSession struct {
Subject string `json:"subject"`
Extra map[string]interface{} `json:"extra"`
Header http.Header `json:"header"`
MatchContext MatchContext `json:"match_context"`
}
type MatchContext struct {
RegexpCaptureGroups []string `json:"regexp_capture_groups"`
URL *url.URL `json:"url"`
Method string `json:"method"`
Header http.Header `json:"header"`
}
type AuthenticatorForwardConfig interface {
GetCheckSessionURL() string
GetPreserveQuery() bool
GetPreservePath() bool
GetPreserveHost() bool
GetForwardHTTPHeaders() []string
GetSetHeaders() map[string]string
GetForceMethod() string
}
func (a *AuthenticationSession) SetHeader(key, val string) {
if a.Header == nil {
a.Header = map[string][]string{}
}
a.Header.Set(key, val)
}
func (a *AuthenticationSession) Copy() *AuthenticationSession {
return copystructure.Must(copystructure.Copy(a)).(*AuthenticationSession)
}