2021-12-24 18:52:19 +00:00
|
|
|
package flow
|
2021-07-17 19:24:11 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"net/http/cookiejar"
|
|
|
|
"net/url"
|
2021-07-19 11:17:13 +00:00
|
|
|
"strings"
|
2023-07-27 16:51:08 +00:00
|
|
|
"time"
|
2021-07-17 19:24:11 +00:00
|
|
|
|
2021-07-22 17:17:34 +00:00
|
|
|
"github.com/getsentry/sentry-go"
|
2021-09-09 13:52:24 +00:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
|
|
"github.com/prometheus/client_golang/prometheus/promauto"
|
2021-07-17 19:24:11 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
2022-03-03 09:40:07 +00:00
|
|
|
"goauthentik.io/api/v3"
|
2021-07-17 19:24:11 +00:00
|
|
|
"goauthentik.io/internal/constants"
|
|
|
|
"goauthentik.io/internal/outpost/ak"
|
2022-06-20 09:54:10 +00:00
|
|
|
"goauthentik.io/internal/utils/web"
|
2021-07-17 19:24:11 +00:00
|
|
|
)
|
|
|
|
|
2021-09-09 13:52:24 +00:00
|
|
|
var (
|
|
|
|
FlowTimingGet = promauto.NewHistogramVec(prometheus.HistogramOpts{
|
2023-07-27 16:51:08 +00:00
|
|
|
Name: "authentik_outpost_flow_timing_get_seconds",
|
|
|
|
Help: "Duration it took to get a challenge in seconds",
|
|
|
|
}, []string{"stage", "flow"})
|
|
|
|
FlowTimingPost = promauto.NewHistogramVec(prometheus.HistogramOpts{
|
|
|
|
Name: "authentik_outpost_flow_timing_post_seconds",
|
|
|
|
Help: "Duration it took to send a challenge in seconds",
|
|
|
|
}, []string{"stage", "flow"})
|
|
|
|
|
|
|
|
// NOTE: the following metrics are kept for compatibility purpose
|
|
|
|
FlowTimingGetLegacy = promauto.NewHistogramVec(prometheus.HistogramOpts{
|
2021-09-09 13:52:24 +00:00
|
|
|
Name: "authentik_outpost_flow_timing_get",
|
|
|
|
Help: "Duration it took to get a challenge",
|
2022-05-29 19:45:25 +00:00
|
|
|
}, []string{"stage", "flow"})
|
2023-07-27 16:51:08 +00:00
|
|
|
FlowTimingPostLegacy = promauto.NewHistogramVec(prometheus.HistogramOpts{
|
2021-09-09 13:52:24 +00:00
|
|
|
Name: "authentik_outpost_flow_timing_post",
|
|
|
|
Help: "Duration it took to send a challenge",
|
2022-05-29 19:45:25 +00:00
|
|
|
}, []string{"stage", "flow"})
|
2021-09-09 13:52:24 +00:00
|
|
|
)
|
|
|
|
|
2022-09-03 19:29:58 +00:00
|
|
|
type SolverFunction func(*api.ChallengeTypes, api.ApiFlowsExecutorSolveRequest) (api.FlowChallengeResponseRequest, error)
|
|
|
|
|
2021-07-17 19:24:11 +00:00
|
|
|
type FlowExecutor struct {
|
|
|
|
Params url.Values
|
|
|
|
Answers map[StageComponent]string
|
2021-07-22 17:17:34 +00:00
|
|
|
Context context.Context
|
2021-07-17 19:24:11 +00:00
|
|
|
|
2022-09-03 19:29:58 +00:00
|
|
|
solvers map[StageComponent]SolverFunction
|
|
|
|
|
2022-05-08 14:48:53 +00:00
|
|
|
cip string
|
|
|
|
api *api.APIClient
|
|
|
|
flowSlug string
|
|
|
|
log *log.Entry
|
|
|
|
token string
|
|
|
|
session *http.Cookie
|
|
|
|
transport http.RoundTripper
|
2021-07-22 17:17:34 +00:00
|
|
|
|
|
|
|
sp *sentry.Span
|
2021-07-17 19:24:11 +00:00
|
|
|
}
|
|
|
|
|
2021-07-22 17:17:34 +00:00
|
|
|
func NewFlowExecutor(ctx context.Context, flowSlug string, refConfig *api.Configuration, logFields log.Fields) *FlowExecutor {
|
|
|
|
rsp := sentry.StartSpan(ctx, "authentik.outposts.flow_executor")
|
2021-12-16 10:00:19 +00:00
|
|
|
rsp.Description = flowSlug
|
2021-07-22 17:17:34 +00:00
|
|
|
|
2021-07-19 11:41:29 +00:00
|
|
|
l := log.WithField("flow", flowSlug).WithFields(logFields)
|
2021-07-17 19:24:11 +00:00
|
|
|
jar, err := cookiejar.New(nil)
|
|
|
|
if err != nil {
|
|
|
|
l.WithError(err).Warning("Failed to create cookiejar")
|
|
|
|
panic(err)
|
|
|
|
}
|
2022-06-20 09:54:10 +00:00
|
|
|
transport := web.NewUserAgentTransport(constants.OutpostUserAgent(), web.NewTracingTransport(rsp.Context(), ak.GetTLSTransport()))
|
2022-05-08 14:48:53 +00:00
|
|
|
fe := &FlowExecutor{
|
|
|
|
Params: url.Values{},
|
|
|
|
Answers: make(map[StageComponent]string),
|
|
|
|
Context: rsp.Context(),
|
|
|
|
flowSlug: flowSlug,
|
|
|
|
log: l,
|
|
|
|
sp: rsp,
|
|
|
|
cip: "",
|
|
|
|
transport: transport,
|
|
|
|
}
|
2022-09-03 19:29:58 +00:00
|
|
|
fe.solvers = map[StageComponent]SolverFunction{
|
|
|
|
StageIdentification: fe.solveChallenge_Identification,
|
|
|
|
StagePassword: fe.solveChallenge_Password,
|
|
|
|
StageAuthenticatorValidate: fe.solveChallenge_AuthenticatorValidate,
|
2023-03-15 19:21:05 +00:00
|
|
|
StageUserLogin: fe.solveChallenge_UserLogin,
|
2022-09-03 19:29:58 +00:00
|
|
|
}
|
2021-07-17 19:24:11 +00:00
|
|
|
// Create new http client that also sets the correct ip
|
|
|
|
config := api.NewConfiguration()
|
|
|
|
config.Host = refConfig.Host
|
|
|
|
config.Scheme = refConfig.Scheme
|
|
|
|
config.HTTPClient = &http.Client{
|
|
|
|
Jar: jar,
|
2022-05-08 14:48:53 +00:00
|
|
|
Transport: fe,
|
2021-07-17 19:24:11 +00:00
|
|
|
}
|
2022-05-08 14:48:53 +00:00
|
|
|
fe.token = strings.Split(refConfig.DefaultHeader["Authorization"], " ")[1]
|
|
|
|
config.AddDefaultHeader(HeaderAuthentikOutpostToken, fe.token)
|
|
|
|
fe.api = api.NewAPIClient(config)
|
|
|
|
return fe
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fe *FlowExecutor) RoundTrip(req *http.Request) (*http.Response, error) {
|
|
|
|
res, err := fe.transport.RoundTrip(req)
|
|
|
|
if res != nil {
|
|
|
|
for _, cookie := range res.Cookies() {
|
|
|
|
if cookie.Name == "authentik_session" {
|
|
|
|
fe.session = cookie
|
|
|
|
}
|
|
|
|
}
|
2021-07-17 19:24:11 +00:00
|
|
|
}
|
2022-05-08 14:48:53 +00:00
|
|
|
return res, err
|
2021-07-17 19:24:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (fe *FlowExecutor) ApiClient() *api.APIClient {
|
|
|
|
return fe.api
|
|
|
|
}
|
|
|
|
|
2022-09-03 19:29:58 +00:00
|
|
|
type challengeInt interface {
|
2021-07-17 19:24:11 +00:00
|
|
|
GetComponent() string
|
|
|
|
GetType() api.ChallengeChoices
|
|
|
|
GetResponseErrors() map[string][]api.ErrorDetail
|
|
|
|
}
|
|
|
|
|
2021-11-05 09:37:30 +00:00
|
|
|
func (fe *FlowExecutor) DelegateClientIP(a string) {
|
|
|
|
fe.cip = a
|
2021-09-09 13:52:24 +00:00
|
|
|
fe.api.GetConfig().AddDefaultHeader(HeaderAuthentikRemoteIP, fe.cip)
|
2021-07-19 11:17:13 +00:00
|
|
|
}
|
|
|
|
|
2021-07-17 19:24:11 +00:00
|
|
|
func (fe *FlowExecutor) CheckApplicationAccess(appSlug string) (bool, error) {
|
2021-07-22 17:17:34 +00:00
|
|
|
acsp := sentry.StartSpan(fe.Context, "authentik.outposts.flow_executor.check_access")
|
|
|
|
defer acsp.Finish()
|
2021-07-22 18:38:30 +00:00
|
|
|
p, _, err := fe.api.CoreApi.CoreApplicationsCheckAccessRetrieve(acsp.Context(), appSlug).Execute()
|
2022-07-01 15:02:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, fmt.Errorf("failed to check access: %w", err)
|
|
|
|
}
|
2021-07-17 19:24:11 +00:00
|
|
|
if !p.Passing {
|
|
|
|
fe.log.Info("Access denied for user")
|
|
|
|
return false, nil
|
|
|
|
}
|
2021-07-19 11:41:29 +00:00
|
|
|
fe.log.Debug("User has access")
|
2021-07-17 19:24:11 +00:00
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fe *FlowExecutor) getAnswer(stage StageComponent) string {
|
|
|
|
if v, o := fe.Answers[stage]; o {
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2022-05-08 14:48:53 +00:00
|
|
|
func (fe *FlowExecutor) GetSession() *http.Cookie {
|
|
|
|
return fe.session
|
|
|
|
}
|
|
|
|
|
2023-01-23 19:36:30 +00:00
|
|
|
func (fe *FlowExecutor) SetSession(s *http.Cookie) {
|
|
|
|
fe.session = s
|
|
|
|
}
|
|
|
|
|
2021-08-21 14:17:30 +00:00
|
|
|
// WarmUp Ensure authentik's flow cache is warmed up
|
|
|
|
func (fe *FlowExecutor) WarmUp() error {
|
|
|
|
gcsp := sentry.StartSpan(fe.Context, "authentik.outposts.flow_executor.get_challenge")
|
2021-11-11 20:27:06 +00:00
|
|
|
defer gcsp.Finish()
|
2021-08-21 14:17:30 +00:00
|
|
|
req := fe.api.FlowsApi.FlowsExecutorGet(gcsp.Context(), fe.flowSlug).Query(fe.Params.Encode())
|
|
|
|
_, _, err := req.Execute()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-07-17 19:24:11 +00:00
|
|
|
func (fe *FlowExecutor) Execute() (bool, error) {
|
2022-09-03 19:29:58 +00:00
|
|
|
initial, err := fe.getInitialChallenge()
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
2021-07-22 17:17:34 +00:00
|
|
|
defer fe.sp.Finish()
|
2022-09-03 19:29:58 +00:00
|
|
|
return fe.solveFlowChallenge(initial, 1)
|
|
|
|
}
|
2021-07-22 17:17:34 +00:00
|
|
|
|
2022-09-03 19:29:58 +00:00
|
|
|
func (fe *FlowExecutor) getInitialChallenge() (*api.ChallengeTypes, error) {
|
2021-07-22 18:38:30 +00:00
|
|
|
// Get challenge
|
2021-07-22 17:17:34 +00:00
|
|
|
gcsp := sentry.StartSpan(fe.Context, "authentik.outposts.flow_executor.get_challenge")
|
2021-07-22 18:38:30 +00:00
|
|
|
req := fe.api.FlowsApi.FlowsExecutorGet(gcsp.Context(), fe.flowSlug).Query(fe.Params.Encode())
|
2021-07-17 19:24:11 +00:00
|
|
|
challenge, _, err := req.Execute()
|
|
|
|
if err != nil {
|
2022-09-03 19:29:58 +00:00
|
|
|
return nil, err
|
2021-07-17 19:24:11 +00:00
|
|
|
}
|
2022-09-23 20:11:47 +00:00
|
|
|
i := challenge.GetActualInstance()
|
|
|
|
if i == nil {
|
|
|
|
return nil, errors.New("response instance was null")
|
|
|
|
}
|
|
|
|
ch := i.(challengeInt)
|
2021-07-17 19:24:11 +00:00
|
|
|
fe.log.WithField("component", ch.GetComponent()).WithField("type", ch.GetType()).Debug("Got challenge")
|
2021-12-16 10:00:19 +00:00
|
|
|
gcsp.SetTag("authentik.flow.challenge", string(ch.GetType()))
|
|
|
|
gcsp.SetTag("authentik.flow.component", ch.GetComponent())
|
2021-07-22 17:17:34 +00:00
|
|
|
gcsp.Finish()
|
2021-09-09 13:52:24 +00:00
|
|
|
FlowTimingGet.With(prometheus.Labels{
|
2022-05-29 19:45:25 +00:00
|
|
|
"stage": ch.GetComponent(),
|
|
|
|
"flow": fe.flowSlug,
|
2023-07-27 16:51:08 +00:00
|
|
|
}).Observe(float64(gcsp.EndTime.Sub(gcsp.StartTime)) / float64(time.Second))
|
|
|
|
FlowTimingGetLegacy.With(prometheus.Labels{
|
|
|
|
"stage": ch.GetComponent(),
|
|
|
|
"flow": fe.flowSlug,
|
2021-09-09 13:52:24 +00:00
|
|
|
}).Observe(float64(gcsp.EndTime.Sub(gcsp.StartTime)))
|
2022-09-03 19:29:58 +00:00
|
|
|
return challenge, nil
|
|
|
|
}
|
2021-07-22 17:17:34 +00:00
|
|
|
|
2022-09-03 19:29:58 +00:00
|
|
|
func (fe *FlowExecutor) solveFlowChallenge(challenge *api.ChallengeTypes, depth int) (bool, error) {
|
2021-07-22 18:38:30 +00:00
|
|
|
// Resole challenge
|
|
|
|
scsp := sentry.StartSpan(fe.Context, "authentik.outposts.flow_executor.solve_challenge")
|
|
|
|
responseReq := fe.api.FlowsApi.FlowsExecutorSolve(scsp.Context(), fe.flowSlug).Query(fe.Params.Encode())
|
2022-09-23 20:11:47 +00:00
|
|
|
i := challenge.GetActualInstance()
|
|
|
|
if i == nil {
|
|
|
|
return false, errors.New("response request instance was null")
|
|
|
|
}
|
|
|
|
ch := i.(challengeInt)
|
2022-09-03 19:29:58 +00:00
|
|
|
|
|
|
|
// Check for any validation errors that we might've gotten
|
|
|
|
if len(ch.GetResponseErrors()) > 0 {
|
|
|
|
for key, errs := range ch.GetResponseErrors() {
|
|
|
|
for _, err := range errs {
|
|
|
|
return false, fmt.Errorf("flow error %s: %s", key, err.String)
|
2021-07-17 19:24:11 +00:00
|
|
|
}
|
|
|
|
}
|
2022-09-03 19:29:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
switch ch.GetType() {
|
|
|
|
case api.CHALLENGECHOICES_REDIRECT:
|
|
|
|
return true, nil
|
|
|
|
case api.CHALLENGECHOICES_NATIVE:
|
|
|
|
if ch.GetComponent() == string(StageAccessDenied) {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
solver, ok := fe.solvers[StageComponent(ch.GetComponent())]
|
|
|
|
if !ok {
|
|
|
|
return false, fmt.Errorf("unsupported challenge type %s", ch.GetComponent())
|
2021-07-17 19:24:11 +00:00
|
|
|
}
|
2022-09-03 19:29:58 +00:00
|
|
|
rr, err := solver(challenge, responseReq)
|
2021-07-17 19:24:11 +00:00
|
|
|
if err != nil {
|
2022-09-03 19:29:58 +00:00
|
|
|
return false, err
|
2021-07-17 19:24:11 +00:00
|
|
|
}
|
2022-09-03 19:29:58 +00:00
|
|
|
responseReq = responseReq.FlowChallengeResponseRequest(rr)
|
2021-07-17 19:24:11 +00:00
|
|
|
}
|
2021-07-22 17:17:34 +00:00
|
|
|
|
2021-07-17 19:24:11 +00:00
|
|
|
response, _, err := responseReq.Execute()
|
2022-09-03 19:29:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, fmt.Errorf("failed to submit challenge %w", err)
|
|
|
|
}
|
2022-09-23 20:11:47 +00:00
|
|
|
i = response.GetActualInstance()
|
|
|
|
if i == nil {
|
|
|
|
return false, errors.New("response instance was null")
|
|
|
|
}
|
|
|
|
ch = i.(challengeInt)
|
2021-07-17 19:24:11 +00:00
|
|
|
fe.log.WithField("component", ch.GetComponent()).WithField("type", ch.GetType()).Debug("Got response")
|
2021-12-16 10:00:19 +00:00
|
|
|
scsp.SetTag("authentik.flow.challenge", string(ch.GetType()))
|
|
|
|
scsp.SetTag("authentik.flow.component", ch.GetComponent())
|
2021-07-22 17:17:34 +00:00
|
|
|
scsp.Finish()
|
2021-09-09 13:52:24 +00:00
|
|
|
FlowTimingPost.With(prometheus.Labels{
|
2022-05-29 19:45:25 +00:00
|
|
|
"stage": ch.GetComponent(),
|
|
|
|
"flow": fe.flowSlug,
|
2023-07-27 16:51:08 +00:00
|
|
|
}).Observe(float64(scsp.EndTime.Sub(scsp.StartTime)) / float64(time.Second))
|
|
|
|
FlowTimingPostLegacy.With(prometheus.Labels{
|
|
|
|
"stage": ch.GetComponent(),
|
|
|
|
"flow": fe.flowSlug,
|
2021-09-09 13:52:24 +00:00
|
|
|
}).Observe(float64(scsp.EndTime.Sub(scsp.StartTime)))
|
|
|
|
|
2021-07-17 19:24:11 +00:00
|
|
|
if depth >= 10 {
|
|
|
|
return false, errors.New("exceeded stage recursion depth")
|
|
|
|
}
|
2022-09-03 19:29:58 +00:00
|
|
|
return fe.solveFlowChallenge(response, depth+1)
|
2021-07-17 19:24:11 +00:00
|
|
|
}
|