2021-07-17 20:24:11 +01:00
|
|
|
package outpost
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2021-07-19 12:17:13 +01:00
|
|
|
"net"
|
2021-07-17 20:24:11 +01:00
|
|
|
"net/http"
|
|
|
|
"net/http/cookiejar"
|
|
|
|
"net/url"
|
|
|
|
"strconv"
|
2021-07-19 12:17:13 +01:00
|
|
|
"strings"
|
2021-07-17 20:24:11 +01:00
|
|
|
|
2021-07-22 18:17:34 +01:00
|
|
|
"github.com/getsentry/sentry-go"
|
2021-07-17 20:24:11 +01:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"goauthentik.io/api"
|
|
|
|
"goauthentik.io/internal/constants"
|
|
|
|
"goauthentik.io/internal/outpost/ak"
|
|
|
|
)
|
|
|
|
|
|
|
|
type StageComponent string
|
|
|
|
|
|
|
|
const (
|
|
|
|
StageIdentification = StageComponent("ak-stage-identification")
|
|
|
|
StagePassword = StageComponent("ak-stage-password")
|
|
|
|
StageAuthenticatorValidate = StageComponent("ak-stage-authenticator-validate")
|
|
|
|
StageAccessDenied = StageComponent("ak-stage-access-denied")
|
|
|
|
)
|
|
|
|
|
2021-07-19 12:17:13 +01:00
|
|
|
const (
|
|
|
|
HeaderAuthentikRemoteIP = "X-authentik-remote-ip"
|
|
|
|
HeaderAuthentikOutpostToken = "X-authentik-outpost-token"
|
|
|
|
)
|
|
|
|
|
2021-07-17 20:24:11 +01:00
|
|
|
type FlowExecutor struct {
|
|
|
|
Params url.Values
|
|
|
|
Answers map[StageComponent]string
|
2021-07-22 18:17:34 +01:00
|
|
|
Context context.Context
|
2021-07-17 20:24:11 +01:00
|
|
|
|
|
|
|
api *api.APIClient
|
|
|
|
flowSlug string
|
|
|
|
log *log.Entry
|
2021-07-19 12:17:13 +01:00
|
|
|
token string
|
2021-07-22 18:17:34 +01:00
|
|
|
|
|
|
|
sp *sentry.Span
|
2021-07-17 20:24:11 +01:00
|
|
|
}
|
|
|
|
|
2021-07-22 18:17:34 +01:00
|
|
|
func NewFlowExecutor(ctx context.Context, flowSlug string, refConfig *api.Configuration, logFields log.Fields) *FlowExecutor {
|
|
|
|
rsp := sentry.StartSpan(ctx, "authentik.outposts.flow_executor")
|
|
|
|
|
2021-07-19 12:41:29 +01:00
|
|
|
l := log.WithField("flow", flowSlug).WithFields(logFields)
|
2021-07-17 20:24:11 +01:00
|
|
|
jar, err := cookiejar.New(nil)
|
|
|
|
if err != nil {
|
|
|
|
l.WithError(err).Warning("Failed to create cookiejar")
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
// Create new http client that also sets the correct ip
|
|
|
|
config := api.NewConfiguration()
|
|
|
|
config.Host = refConfig.Host
|
|
|
|
config.Scheme = refConfig.Scheme
|
|
|
|
config.UserAgent = constants.OutpostUserAgent()
|
|
|
|
config.HTTPClient = &http.Client{
|
|
|
|
Jar: jar,
|
2021-07-23 16:37:06 +01:00
|
|
|
Transport: ak.NewTracingTransport(ak.GetTLSTransport()),
|
2021-07-17 20:24:11 +01:00
|
|
|
}
|
|
|
|
apiClient := api.NewAPIClient(config)
|
|
|
|
return &FlowExecutor{
|
|
|
|
Params: url.Values{},
|
|
|
|
Answers: make(map[StageComponent]string),
|
2021-07-22 18:17:34 +01:00
|
|
|
Context: rsp.Context(),
|
2021-07-17 20:24:11 +01:00
|
|
|
api: apiClient,
|
|
|
|
flowSlug: flowSlug,
|
|
|
|
log: l,
|
2021-07-19 12:17:13 +01:00
|
|
|
token: strings.Split(refConfig.DefaultHeader["Authorization"], " ")[1],
|
2021-07-22 18:17:34 +01:00
|
|
|
sp: rsp,
|
2021-07-17 20:24:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fe *FlowExecutor) ApiClient() *api.APIClient {
|
|
|
|
return fe.api
|
|
|
|
}
|
|
|
|
|
|
|
|
type ChallengeInt interface {
|
|
|
|
GetComponent() string
|
|
|
|
GetType() api.ChallengeChoices
|
|
|
|
GetResponseErrors() map[string][]api.ErrorDetail
|
|
|
|
}
|
|
|
|
|
2021-07-19 12:17:13 +01:00
|
|
|
func (fe *FlowExecutor) DelegateClientIP(a net.Addr) {
|
|
|
|
host, _, err := net.SplitHostPort(a.String())
|
|
|
|
if err != nil {
|
|
|
|
fe.log.WithError(err).Warning("Failed to get remote IP")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
fe.api.GetConfig().AddDefaultHeader(HeaderAuthentikRemoteIP, host)
|
|
|
|
fe.api.GetConfig().AddDefaultHeader(HeaderAuthentikOutpostToken, fe.token)
|
|
|
|
}
|
|
|
|
|
2021-07-17 20:24:11 +01:00
|
|
|
func (fe *FlowExecutor) CheckApplicationAccess(appSlug string) (bool, error) {
|
2021-07-22 18:17:34 +01:00
|
|
|
acsp := sentry.StartSpan(fe.Context, "authentik.outposts.flow_executor.check_access")
|
|
|
|
defer acsp.Finish()
|
2021-07-22 19:38:30 +01:00
|
|
|
p, _, err := fe.api.CoreApi.CoreApplicationsCheckAccessRetrieve(acsp.Context(), appSlug).Execute()
|
2021-07-17 20:24:11 +01:00
|
|
|
if !p.Passing {
|
|
|
|
fe.log.Info("Access denied for user")
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return false, fmt.Errorf("failed to check access: %w", err)
|
|
|
|
}
|
2021-07-19 12:41:29 +01:00
|
|
|
fe.log.Debug("User has access")
|
2021-07-17 20:24:11 +01:00
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fe *FlowExecutor) getAnswer(stage StageComponent) string {
|
|
|
|
if v, o := fe.Answers[stage]; o {
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fe *FlowExecutor) Execute() (bool, error) {
|
|
|
|
return fe.solveFlowChallenge(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fe *FlowExecutor) solveFlowChallenge(depth int) (bool, error) {
|
2021-07-22 18:17:34 +01:00
|
|
|
defer fe.sp.Finish()
|
|
|
|
|
2021-07-22 19:38:30 +01:00
|
|
|
// Get challenge
|
2021-07-22 18:17:34 +01:00
|
|
|
gcsp := sentry.StartSpan(fe.Context, "authentik.outposts.flow_executor.get_challenge")
|
2021-07-22 19:38:30 +01:00
|
|
|
req := fe.api.FlowsApi.FlowsExecutorGet(gcsp.Context(), fe.flowSlug).Query(fe.Params.Encode())
|
2021-07-17 20:24:11 +01:00
|
|
|
challenge, _, err := req.Execute()
|
|
|
|
if err != nil {
|
|
|
|
return false, errors.New("failed to get challenge")
|
|
|
|
}
|
|
|
|
ch := challenge.GetActualInstance().(ChallengeInt)
|
|
|
|
fe.log.WithField("component", ch.GetComponent()).WithField("type", ch.GetType()).Debug("Got challenge")
|
2021-07-22 18:17:34 +01:00
|
|
|
gcsp.SetTag("ak_challenge", string(ch.GetType()))
|
|
|
|
gcsp.SetTag("ak_component", ch.GetComponent())
|
|
|
|
gcsp.Finish()
|
|
|
|
|
2021-07-22 19:38:30 +01: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())
|
2021-07-17 20:24:11 +01:00
|
|
|
switch ch.GetComponent() {
|
|
|
|
case string(StageIdentification):
|
|
|
|
responseReq = responseReq.FlowChallengeResponseRequest(api.IdentificationChallengeResponseRequestAsFlowChallengeResponseRequest(api.NewIdentificationChallengeResponseRequest(fe.getAnswer(StageIdentification))))
|
|
|
|
case string(StagePassword):
|
|
|
|
responseReq = responseReq.FlowChallengeResponseRequest(api.PasswordChallengeResponseRequestAsFlowChallengeResponseRequest(api.NewPasswordChallengeResponseRequest(fe.getAnswer(StagePassword))))
|
|
|
|
case string(StageAuthenticatorValidate):
|
|
|
|
// We only support duo as authenticator, check if that's allowed
|
|
|
|
var deviceChallenge *api.DeviceChallenge
|
|
|
|
for _, devCh := range challenge.AuthenticatorValidationChallenge.DeviceChallenges {
|
|
|
|
if devCh.DeviceClass == string(api.DEVICECLASSESENUM_DUO) {
|
|
|
|
deviceChallenge = &devCh
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if deviceChallenge == nil {
|
|
|
|
return false, errors.New("got ak-stage-authenticator-validate without duo")
|
|
|
|
}
|
|
|
|
devId, err := strconv.Atoi(deviceChallenge.DeviceUid)
|
|
|
|
if err != nil {
|
|
|
|
return false, errors.New("failed to convert duo device id to int")
|
|
|
|
}
|
|
|
|
devId32 := int32(devId)
|
|
|
|
inner := api.NewAuthenticatorValidationChallengeResponseRequest()
|
|
|
|
inner.Duo = &devId32
|
|
|
|
responseReq = responseReq.FlowChallengeResponseRequest(api.AuthenticatorValidationChallengeResponseRequestAsFlowChallengeResponseRequest(inner))
|
|
|
|
case string(StageAccessDenied):
|
|
|
|
return false, errors.New("got ak-stage-access-denied")
|
|
|
|
default:
|
|
|
|
return false, fmt.Errorf("unsupported challenge type %s", ch.GetComponent())
|
|
|
|
}
|
2021-07-22 18:17:34 +01:00
|
|
|
|
2021-07-17 20:24:11 +01:00
|
|
|
response, _, err := responseReq.Execute()
|
|
|
|
ch = response.GetActualInstance().(ChallengeInt)
|
|
|
|
fe.log.WithField("component", ch.GetComponent()).WithField("type", ch.GetType()).Debug("Got response")
|
2021-07-22 18:17:34 +01:00
|
|
|
scsp.SetTag("ak_challenge", string(ch.GetType()))
|
|
|
|
scsp.SetTag("ak_component", ch.GetComponent())
|
|
|
|
scsp.Finish()
|
|
|
|
|
2021-07-17 20:24:11 +01:00
|
|
|
switch ch.GetComponent() {
|
|
|
|
case string(StageAccessDenied):
|
|
|
|
return false, errors.New("got ak-stage-access-denied")
|
|
|
|
}
|
|
|
|
if ch.GetType() == "redirect" {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return false, fmt.Errorf("failed to submit challenge %w", err)
|
|
|
|
}
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if depth >= 10 {
|
|
|
|
return false, errors.New("exceeded stage recursion depth")
|
|
|
|
}
|
|
|
|
return fe.solveFlowChallenge(depth + 1)
|
|
|
|
}
|