goweb-oidc/internal/route/login.go

38 lines
907 B
Go
Raw Normal View History

2020-05-20 10:43:12 +02:00
package route
import (
"net/http"
oidc "forge.cadoles.com/wpetit/goweb-oidc"
"forge.cadoles.com/wpetit/goweb-oidc/internal/config"
"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 {
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
}
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))
http.Redirect(w, r, conf.HTTP.PublicBaseURL+"/", http.StatusSeeOther)
2020-05-20 10:43:12 +02:00
}