2020-05-20 10:43:12 +02:00
|
|
|
package route
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
oidc "forge.cadoles.com/wpetit/goweb-oidc"
|
2022-07-19 10:24:49 +02:00
|
|
|
"github.com/pkg/errors"
|
2020-05-20 10:43:12 +02:00
|
|
|
"gitlab.com/wpetit/goweb/logger"
|
|
|
|
"gitlab.com/wpetit/goweb/middleware/container"
|
|
|
|
)
|
|
|
|
|
2020-05-20 13:06:04 +02:00
|
|
|
func handleLogin(w http.ResponseWriter, r *http.Request) {
|
2020-05-20 10:43:12 +02:00
|
|
|
ctn := container.Must(r.Context())
|
2020-05-20 13:06:04 +02:00
|
|
|
client := oidc.Must(ctn)
|
|
|
|
client.Login(w, r)
|
2020-05-20 10:43:12 +02:00
|
|
|
}
|
|
|
|
|
2020-05-20 13:06:04 +02:00
|
|
|
func handleLoginCallback(w http.ResponseWriter, r *http.Request) {
|
2020-05-20 10:43:12 +02:00
|
|
|
ctx := r.Context()
|
|
|
|
|
|
|
|
idToken, err := oidc.IDToken(w, r)
|
|
|
|
if err != nil {
|
2022-07-19 10:24:49 +02:00
|
|
|
logger.Error(ctx, "could not retrieve idToken", logger.E(errors.WithStack(err)))
|
2020-05-20 10:43:12 +02:00
|
|
|
|
|
|
|
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.Info(ctx, "user logged in", logger.F("sub", idToken.Subject))
|
|
|
|
|
|
|
|
http.Redirect(w, r, "/", http.StatusSeeOther)
|
|
|
|
}
|