2022-04-26 22:30:39 +00:00
|
|
|
// Copyright 2022 the Pinniped contributors. All Rights Reserved.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
|
|
|
package login
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"go.pinniped.dev/internal/oidc"
|
2022-05-05 20:12:06 +00:00
|
|
|
"go.pinniped.dev/internal/oidc/login/loginhtml"
|
2022-04-26 22:30:39 +00:00
|
|
|
)
|
|
|
|
|
2022-05-05 20:12:06 +00:00
|
|
|
const (
|
|
|
|
internalErrorMessage = "An internal error occurred. Please contact your administrator for help."
|
|
|
|
incorrectUsernameOrPasswordErrorMessage = "Incorrect username or password."
|
2022-04-28 16:11:51 +00:00
|
|
|
)
|
|
|
|
|
2022-05-05 20:12:06 +00:00
|
|
|
func NewGetHandler() HandlerFunc {
|
2022-04-26 22:30:39 +00:00
|
|
|
return func(w http.ResponseWriter, r *http.Request, encodedState string, decodedState *oidc.UpstreamStateParamData) error {
|
2022-05-05 20:12:06 +00:00
|
|
|
alertMessage, hasAlert := getAlert(r)
|
|
|
|
|
|
|
|
pageInputs := &loginhtml.PageData{
|
|
|
|
PostPath: r.URL.Path, // the path for POST is the same as for GET
|
2022-04-29 17:36:13 +00:00
|
|
|
State: encodedState,
|
|
|
|
IDPName: decodedState.UpstreamName,
|
2022-05-05 20:12:06 +00:00
|
|
|
HasAlertError: hasAlert,
|
|
|
|
AlertMessage: alertMessage,
|
2022-04-28 16:11:51 +00:00
|
|
|
}
|
2022-05-05 20:12:06 +00:00
|
|
|
return loginhtml.Template().Execute(w, pageInputs)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func getAlert(r *http.Request) (string, bool) {
|
|
|
|
errorParamValue := r.URL.Query().Get(errParamName)
|
2022-04-28 16:11:51 +00:00
|
|
|
|
2022-05-05 20:12:06 +00:00
|
|
|
message := internalErrorMessage
|
|
|
|
if errorParamValue == string(ShowBadUserPassErr) {
|
|
|
|
message = incorrectUsernameOrPasswordErrorMessage
|
2022-04-26 22:30:39 +00:00
|
|
|
}
|
2022-05-05 20:12:06 +00:00
|
|
|
|
|
|
|
return message, errorParamValue != ""
|
2022-04-26 22:30:39 +00:00
|
|
|
}
|