goweb-oidc/internal/route/login.go

33 lines
730 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"
"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(err))
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)
}