first working version

This commit is contained in:
ston1th 2022-07-10 15:51:31 +02:00
commit c9dfecc519
21 changed files with 296 additions and 178 deletions

View file

@ -106,8 +106,8 @@ func indexHandler(ctx *Context) {
ctx.Redirect(login, http.StatusFound)
}
func profileHandler(ctx *Context) {
ctx.Redirect(uroot+"edit/"+ctx.User(), http.StatusFound)
func sessionsHandler(ctx *Context) {
ctx.Redirect(uroot+"sessions/"+ctx.User(), http.StatusFound)
}
func loginHandler(ctx *Context) {
@ -123,7 +123,7 @@ func loginHandler(ctx *Context) {
BodyTitle: "Login",
}
if ctx.LoggedOn() {
rdr := profile
rdr := sessions
if data.LoginChallenge != "" {
rdr, err = ctx.ApproveLogin(ctx.User(), data.LoginChallenge)
if err != nil {
@ -172,7 +172,7 @@ func loginHandler(ctx *Context) {
}
log.Info("user logged in", "user", data.User)
ctx.Login(u, remember)
rdr := profile
rdr := sessions
if data.LoginChallenge != "" {
rdr, err = ctx.ApproveLogin(data.User, data.LoginChallenge)
if err != nil {
@ -187,7 +187,7 @@ func loginHandler(ctx *Context) {
func loginTotpHandler(ctx *Context) {
if ctx.LoggedOn() {
ctx.Redirect(profile, http.StatusFound)
ctx.Redirect(sessions, http.StatusFound)
return
}
ctx.Template("loginTotpHandler")
@ -222,7 +222,7 @@ func loginTotpHandler(ctx *Context) {
}
log.Info("totp successful")
ctx.Login(u, ctx.Remember())
rdr := root
rdr := sessions
if data.LoginChallenge != "" {
rdr, err = ctx.ApproveLogin(user, data.LoginChallenge)
if err != nil {
@ -239,21 +239,30 @@ func loginTotpHandler(ctx *Context) {
func consentHandler(ctx *Context) {
ctx.Template("consentHandler")
ctx.Data = webData{
Title: "Consent",
BodyTitle: "Consent",
Title: "Authorize",
BodyTitle: "Authorize",
}
var data consentData
switch ctx.Method() {
case "GET":
data.ConsentChallenge = ctx.Query("consent_challenge")
ctx.Data.Data = data
ci, err := ctx.Consent(data.ConsentChallenge)
cr, err := ctx.Consent(data.ConsentChallenge)
if err != nil {
ctx.log.Error(err, "error getting consent info")
ctx.Error("error getting consent info")
return
}
data.Info = ci
if cr.GetSkip() {
rdr, err := ctx.ApproveConsent(data.ConsentChallenge)
if err != nil {
ctx.log.Error(err, "error approving consent")
ctx.Error("error approving consent")
return
}
ctx.Redirect(rdr, http.StatusFound)
}
data.Info = cr
ctx.Data.Data = data
ctx.Exec()
case "POST":
@ -321,11 +330,8 @@ func userNewHandler(ctx *Context) {
email := ctx.Form("email")
password := ctx.Form("password")
repeat := ctx.Form("repeat")
admin := ctx.Form("admin")
adm := false
if admin == "0" {
adm = true
}
admin := ctx.Form("admin") == "0"
disabled := ctx.Form("disabled") == "0"
if user == "" || email == "" || password == "" {
ctx.Error("empty user, email or password")
@ -335,11 +341,11 @@ func userNewHandler(ctx *Context) {
ctx.Error("passwords do not match")
return
}
if err := ctx.Srv.DB.CreateUser(user, email, password, adm); err != nil {
if err := ctx.Srv.DB.CreateUser(user, email, password, admin, disabled); err != nil {
ctx.Error(err)
return
}
ctx.log.Info("user created", "user", user, "actor", ctx.User())
ctx.log.Info("user created", "user", user, "actor", ctx.User(), "admin", admin, "disabled", disabled)
ctx.Redirect(users, http.StatusFound)
}
}
@ -364,25 +370,27 @@ func userEditHandler(ctx *Context) {
return
}
user := ctx.Var("user")
log := ctx.log.WithValues("user", user, "actor", ctx.User())
self := ctx.User()
log := ctx.log.WithValues("user", user, "actor", self)
password := ctx.Form("password")
repeat := ctx.Form("repeat")
admin := ctx.Form("admin")
adm := false
if admin == "0" {
adm = true
}
admin := ctx.Form("admin") == "0"
disabled := ctx.Form("disabled") == "0"
if password != repeat {
ctx.Error("passwords do not match")
return
}
if ctx.Admin() {
if err := ctx.Srv.DB.AdminUpdateUser(user, password, adm); err != nil {
if err := ctx.Srv.DB.AdminUpdateUser(user, password, admin, disabled); err != nil {
ctx.Error(err)
return
}
log.Info("user updated", "admin", adm)
log.Info("user updated", "admin", admin, "disabled", disabled)
if user == self {
logoutHandler(ctx)
return
}
ctx.Redirect(users, http.StatusFound)
return
}
@ -461,6 +469,40 @@ func userTotpHandler(ctx *Context) {
}
}
func userSessionsHandler(ctx *Context) {
ctx.Template("userSessionsHandler")
ctx.Data = webData{
Title: "Sessions",
BodyTitle: "Sessions",
}
user := ctx.Var("user")
log := ctx.log.WithValues("user", user)
switch ctx.Method() {
case "GET":
s, err := ctx.Sessions(user)
if err != nil {
log.Error(err, "error getting sessions")
ctx.Error("error getting sessions")
return
}
ctx.Data.Data = s
ctx.Exec()
case "POST":
if !ctx.CheckXsrf() {
return
}
client := ctx.Form("client")
all := ctx.Form("all") == "all"
err := ctx.RevokeSession(user, client, all)
if err != nil {
log.Error(err, "error revoking session", "client", client, "all", all)
ctx.Error("error revoking session")
return
}
sessionsHandler(ctx)
}
}
func userUnlockHandler(ctx *Context) {
user := ctx.Var("user")
log := ctx.log.WithValues("user", user, "actor", ctx.User())
@ -502,7 +544,7 @@ func userDelHandler(ctx *Context) {
log.Info("delete successful")
}
if user == self {
ctx.Redirect(logout, http.StatusFound)
logoutHandler(ctx)
return
}
ctx.Redirect(users, http.StatusFound)