2022-02-14 17:22:30 +01:00
|
|
|
package login
|
2021-06-10 14:18:07 +02:00
|
|
|
|
|
|
|
import (
|
2023-04-11 17:07:32 +02:00
|
|
|
"context"
|
2021-06-10 14:18:07 +02:00
|
|
|
"net/http"
|
|
|
|
|
2022-04-27 01:01:45 +02:00
|
|
|
"github.com/zitadel/zitadel/internal/domain"
|
2023-12-08 16:30:55 +02:00
|
|
|
"github.com/zitadel/zitadel/internal/zerrors"
|
2021-06-10 14:18:07 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
tmplLoginSuccess = "login_success"
|
|
|
|
)
|
|
|
|
|
|
|
|
type loginSuccessData struct {
|
|
|
|
userData
|
|
|
|
RedirectURI string `schema:"redirect-uri"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *Login) redirectToLoginSuccess(w http.ResponseWriter, r *http.Request, id string) {
|
|
|
|
http.Redirect(w, r, l.renderer.pathPrefix+EndpointLoginSuccess+"?authRequestID="+id, http.StatusFound)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *Login) handleLoginSuccess(w http.ResponseWriter, r *http.Request) {
|
|
|
|
authRequest, _ := l.getAuthRequest(r)
|
2021-12-14 09:47:49 +01:00
|
|
|
if authRequest == nil {
|
|
|
|
l.renderSuccessAndCallback(w, r, nil, nil)
|
2022-01-12 15:31:11 +01:00
|
|
|
return
|
2021-12-14 09:47:49 +01:00
|
|
|
}
|
|
|
|
for _, step := range authRequest.PossibleSteps {
|
|
|
|
if step.Type() != domain.NextStepLoginSucceeded && step.Type() != domain.NextStepRedirectToCallback {
|
2021-06-10 14:18:07 +02:00
|
|
|
l.renderNextStep(w, r, authRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
l.renderSuccessAndCallback(w, r, authRequest, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *Login) renderSuccessAndCallback(w http.ResponseWriter, r *http.Request, authReq *domain.AuthRequest, err error) {
|
2021-06-18 10:31:53 +02:00
|
|
|
var errID, errMessage string
|
2021-06-10 14:18:07 +02:00
|
|
|
if err != nil {
|
2021-06-18 10:31:53 +02:00
|
|
|
errID, errMessage = l.getErrorMessage(r, err)
|
2021-06-10 14:18:07 +02:00
|
|
|
}
|
2023-12-05 12:12:01 +01:00
|
|
|
translator := l.getTranslator(r.Context(), authReq)
|
2021-06-10 14:18:07 +02:00
|
|
|
data := loginSuccessData{
|
2023-12-05 12:12:01 +01:00
|
|
|
userData: l.getUserData(r, authReq, translator, "LoginSuccess.Title", "", errID, errMessage),
|
2021-06-10 14:18:07 +02:00
|
|
|
}
|
|
|
|
if authReq != nil {
|
2023-04-11 17:07:32 +02:00
|
|
|
data.RedirectURI, err = l.authRequestCallback(r.Context(), authReq)
|
|
|
|
if err != nil {
|
|
|
|
l.renderInternalError(w, r, authReq, err)
|
|
|
|
return
|
2022-09-12 17:18:08 +01:00
|
|
|
}
|
2021-06-10 14:18:07 +02:00
|
|
|
}
|
2023-12-05 12:12:01 +01:00
|
|
|
l.renderer.RenderTemplate(w, r, translator, l.renderer.Templates[tmplLoginSuccess], data, nil)
|
2021-06-10 14:18:07 +02:00
|
|
|
}
|
2021-12-14 09:47:49 +01:00
|
|
|
|
|
|
|
func (l *Login) redirectToCallback(w http.ResponseWriter, r *http.Request, authReq *domain.AuthRequest) {
|
2023-04-11 17:07:32 +02:00
|
|
|
callback, err := l.authRequestCallback(r.Context(), authReq)
|
|
|
|
if err != nil {
|
|
|
|
l.renderInternalError(w, r, authReq, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
http.Redirect(w, r, callback, http.StatusFound)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *Login) authRequestCallback(ctx context.Context, authReq *domain.AuthRequest) (string, error) {
|
2022-09-12 17:18:08 +01:00
|
|
|
switch authReq.Request.(type) {
|
|
|
|
case *domain.AuthRequestOIDC:
|
2023-04-11 17:07:32 +02:00
|
|
|
return l.oidcAuthCallbackURL(ctx, authReq.ID), nil
|
2022-09-12 17:18:08 +01:00
|
|
|
case *domain.AuthRequestSAML:
|
2023-04-11 17:07:32 +02:00
|
|
|
return l.samlAuthCallbackURL(ctx, authReq.ID), nil
|
2023-04-19 11:46:02 +03:00
|
|
|
case *domain.AuthRequestDevice:
|
|
|
|
return l.deviceAuthCallbackURL(authReq.ID), nil
|
2022-09-12 17:18:08 +01:00
|
|
|
default:
|
2023-12-08 16:30:55 +02:00
|
|
|
return "", zerrors.ThrowInternal(nil, "LOGIN-rhjQF", "Errors.AuthRequest.RequestTypeNotSupported")
|
2022-09-12 17:18:08 +01:00
|
|
|
}
|
2021-12-14 09:47:49 +01:00
|
|
|
}
|