fixed api calls

This commit is contained in:
ston1th 2022-07-10 01:51:33 +02:00
commit 69df101cb8
2 changed files with 28 additions and 14 deletions

View file

@ -6,6 +6,7 @@ import (
"context"
"encoding/json"
"errors"
"fmt"
"html/template"
"net/http"
"strconv"
@ -209,6 +210,12 @@ func (c *Context) Form(name string) string {
return c.Request.PostFormValue(name)
}
func (c *Context) Query(name string) string {
// does nothing if called twice
c.Request.ParseForm()
return c.Request.Form.Get(name)
}
func (c *Context) Var(name string) (ret string) {
ret = mux.Vars(c.Request)[name]
return
@ -324,9 +331,9 @@ func (c *Context) ApproveLogin(user, challenge string) (rdr string, err error) {
}
func (c *Context) ApproveConsent(challenge string) (rdr string, err error) {
req := c.Srv.API.AdminApi.AcceptConsentRequest(context.Background())
req.AcceptConsentRequest(*client.NewAcceptConsentRequest())
req.ConsentChallenge(challenge)
req := c.Srv.API.AdminApi.AcceptConsentRequest(context.Background()).
AcceptConsentRequest(*client.NewAcceptConsentRequest()).
ConsentChallenge(challenge)
_, resp, err := req.Execute()
if err != nil {
return
@ -346,9 +353,9 @@ func (c *Context) ApproveConsent(challenge string) (rdr string, err error) {
}
func (c *Context) RejectConsent(challenge string) (rdr string, err error) {
req := c.Srv.API.AdminApi.RejectConsentRequest(context.Background())
req.RejectRequest(*client.NewRejectRequest())
req.ConsentChallenge(challenge)
req := c.Srv.API.AdminApi.RejectConsentRequest(context.Background()).
RejectRequest(*client.NewRejectRequest()).
ConsentChallenge(challenge)
_, resp, err := req.Execute()
if err != nil {
return
@ -368,10 +375,12 @@ func (c *Context) RejectConsent(challenge string) (rdr string, err error) {
}
func (c *Context) Consent(challenge string) (ci *consentInfo, err error) {
req := c.Srv.API.AdminApi.GetConsentRequest(context.Background())
req.ConsentChallenge(challenge)
fmt.Println(challenge)
req := c.Srv.API.AdminApi.GetConsentRequest(context.Background()).
ConsentChallenge(challenge)
_, resp, err := req.Execute()
if err != nil {
fmt.Println(err)
return
}
defer resp.Body.Close()

View file

@ -116,7 +116,7 @@ func loginHandler(ctx *Context) {
data loginData
err error
)
data.LoginChallenge = ctx.Var("login_challenge")
data.LoginChallenge = ctx.Query("login_challenge")
ctx.Template("loginHandler")
ctx.Data = webData{
Title: "Login",
@ -198,7 +198,7 @@ func loginTotpHandler(ctx *Context) {
var data loginData
switch ctx.Method() {
case "GET":
data.LoginChallenge = ctx.Var("login_challenge")
data.LoginChallenge = ctx.Query("login_challenge")
ctx.Data.Data = data
ctx.Exec()
case "POST":
@ -227,6 +227,8 @@ func loginTotpHandler(ctx *Context) {
rdr, err = ctx.ApproveLogin(user, data.LoginChallenge)
if err != nil {
ctx.Error(err)
ctx.log.Error(err, "error approving login")
ctx.Error("error approving login")
return
}
}
@ -243,11 +245,12 @@ func consentHandler(ctx *Context) {
var data consentData
switch ctx.Method() {
case "GET":
data.ConsentChallenge = ctx.Var("consent_challenge")
data.ConsentChallenge = ctx.Query("consent_challenge")
ctx.Data.Data = data
ci, err := ctx.Consent(data.ConsentChallenge)
if err != nil {
ctx.Error(err)
ctx.log.Error(err, "error getting consent info")
ctx.Error("error getting consent info")
return
}
data.Info = ci
@ -266,13 +269,15 @@ func consentHandler(ctx *Context) {
if consent == "approve" {
rdr, err = ctx.ApproveConsent(data.ConsentChallenge)
if err != nil {
ctx.Error(err)
ctx.log.Error(err, "error approving consent")
ctx.Error("error approving consent")
return
}
} else {
rdr, err = ctx.RejectConsent(data.ConsentChallenge)
if err != nil {
ctx.Error(err)
ctx.log.Error(err, "error rejecting consent")
ctx.Error("error rejecting consent")
return
}
}