2020-05-20 10:43:12 +02:00
|
|
|
package route
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
oidc "forge.cadoles.com/wpetit/goweb-oidc"
|
2022-08-02 11:48:42 +02:00
|
|
|
"forge.cadoles.com/wpetit/goweb-oidc/internal/config"
|
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
|
|
|
|
}
|
|
|
|
|
2022-08-02 11:48:42 +02:00
|
|
|
ctn := container.Must(ctx)
|
|
|
|
conf := config.Must(ctn)
|
|
|
|
|
2020-05-20 10:43:12 +02:00
|
|
|
logger.Info(ctx, "user logged in", logger.F("sub", idToken.Subject))
|
|
|
|
|
2022-08-02 11:48:42 +02:00
|
|
|
http.Redirect(w, r, conf.HTTP.PublicBaseURL+"/", http.StatusSeeOther)
|
2020-05-20 10:43:12 +02:00
|
|
|
}
|