2023-09-15 13:06:31 +01:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/1f349/tulip/database"
|
2024-03-12 21:04:25 +00:00
|
|
|
"github.com/1f349/tulip/database/types"
|
2023-09-15 13:06:31 +01:00
|
|
|
"github.com/1f349/tulip/pages"
|
2024-03-12 21:04:25 +00:00
|
|
|
"github.com/1f349/tulip/password"
|
|
|
|
"github.com/google/uuid"
|
2023-09-15 13:06:31 +01:00
|
|
|
"github.com/julienschmidt/httprouter"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"strconv"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (h *HttpServer) ManageAppsGet(rw http.ResponseWriter, req *http.Request, _ httprouter.Params, auth UserAuth) {
|
|
|
|
offset := 0
|
|
|
|
q := req.URL.Query()
|
|
|
|
if q.Has("offset") {
|
|
|
|
var err error
|
|
|
|
offset, err = strconv.Atoi(q.Get("offset"))
|
|
|
|
if err != nil {
|
|
|
|
http.Error(rw, "400 Bad Request: Invalid offset", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-11 12:39:52 +00:00
|
|
|
var role types.UserRole
|
2024-03-12 21:04:25 +00:00
|
|
|
var appList []database.GetAppListRow
|
2024-03-11 12:39:52 +00:00
|
|
|
if h.DbTx(rw, func(tx *database.Queries) (err error) {
|
2024-03-12 21:04:25 +00:00
|
|
|
role, err = tx.GetUserRole(req.Context(), auth.ID)
|
2023-09-15 13:06:31 +01:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2024-03-12 21:04:25 +00:00
|
|
|
appList, err = tx.GetAppList(req.Context(), database.GetAppListParams{
|
|
|
|
Owner: auth.ID,
|
|
|
|
Column2: role == types.RoleAdmin,
|
|
|
|
Offset: int64(offset),
|
|
|
|
})
|
2023-09-15 13:06:31 +01:00
|
|
|
return
|
|
|
|
}) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
m := map[string]any{
|
2023-10-10 18:06:43 +01:00
|
|
|
"ServiceName": h.conf.ServiceName,
|
|
|
|
"Apps": appList,
|
|
|
|
"Offset": offset,
|
2024-03-11 12:39:52 +00:00
|
|
|
"IsAdmin": role == types.RoleAdmin,
|
2023-10-10 18:06:43 +01:00
|
|
|
"NewAppName": q.Get("NewAppName"),
|
|
|
|
"NewAppSecret": q.Get("NewAppSecret"),
|
2023-09-15 13:06:31 +01:00
|
|
|
}
|
|
|
|
if q.Has("edit") {
|
|
|
|
for _, i := range appList {
|
2024-03-12 21:04:25 +00:00
|
|
|
if i.Subject == q.Get("edit") {
|
2023-09-15 13:06:31 +01:00
|
|
|
m["Edit"] = i
|
|
|
|
goto validEdit
|
|
|
|
}
|
|
|
|
}
|
|
|
|
http.Error(rw, "400 Bad Request: Invalid client app to edit", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
validEdit:
|
|
|
|
rw.Header().Set("Content-Type", "text/html")
|
|
|
|
rw.WriteHeader(http.StatusOK)
|
|
|
|
pages.RenderPageTemplate(rw, "manage-apps", m)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *HttpServer) ManageAppsPost(rw http.ResponseWriter, req *http.Request, _ httprouter.Params, auth UserAuth) {
|
|
|
|
err := req.ParseForm()
|
|
|
|
if err != nil {
|
|
|
|
http.Error(rw, "400 Bad Request: Failed to parse form", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
offset := req.Form.Get("offset")
|
|
|
|
action := req.Form.Get("action")
|
|
|
|
name := req.Form.Get("name")
|
|
|
|
domain := req.Form.Get("domain")
|
2024-02-08 01:16:46 +00:00
|
|
|
public := req.Form.Has("public")
|
2023-09-15 13:06:31 +01:00
|
|
|
sso := req.Form.Has("sso")
|
|
|
|
active := req.Form.Has("active")
|
|
|
|
|
|
|
|
if sso {
|
2024-03-11 12:39:52 +00:00
|
|
|
var role types.UserRole
|
|
|
|
if h.DbTx(rw, func(tx *database.Queries) (err error) {
|
2024-03-12 21:04:25 +00:00
|
|
|
role, err = tx.GetUserRole(req.Context(), auth.ID)
|
2023-09-15 13:06:31 +01:00
|
|
|
return
|
|
|
|
}) {
|
|
|
|
return
|
|
|
|
}
|
2024-03-11 12:39:52 +00:00
|
|
|
if role != types.RoleAdmin {
|
2023-09-15 13:06:31 +01:00
|
|
|
http.Error(rw, "400 Bad Request: Only admin users can create SSO client applications", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch action {
|
|
|
|
case "create":
|
2024-03-11 12:39:52 +00:00
|
|
|
if h.DbTx(rw, func(tx *database.Queries) error {
|
2024-03-12 21:04:25 +00:00
|
|
|
secret, err := password.GenerateApiSecret(70)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return tx.InsertClientApp(req.Context(), database.InsertClientAppParams{
|
|
|
|
Subject: uuid.NewString(),
|
|
|
|
Name: name,
|
|
|
|
Secret: secret,
|
|
|
|
Domain: domain,
|
|
|
|
Owner: auth.ID,
|
|
|
|
Public: public,
|
|
|
|
Sso: sso,
|
|
|
|
Active: active,
|
|
|
|
})
|
2023-09-15 13:06:31 +01:00
|
|
|
}) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
case "edit":
|
2024-03-11 12:39:52 +00:00
|
|
|
if h.DbTx(rw, func(tx *database.Queries) error {
|
2024-03-12 21:04:25 +00:00
|
|
|
return tx.UpdateClientApp(req.Context(), database.UpdateClientAppParams{
|
|
|
|
Name: name,
|
|
|
|
Domain: domain,
|
|
|
|
Public: public,
|
|
|
|
Sso: sso,
|
|
|
|
Active: active,
|
|
|
|
Subject: req.FormValue("subject"),
|
|
|
|
Owner: auth.ID,
|
|
|
|
})
|
2023-09-15 13:06:31 +01:00
|
|
|
}) {
|
|
|
|
return
|
|
|
|
}
|
2023-10-10 18:06:43 +01:00
|
|
|
case "secret":
|
2024-03-12 21:04:25 +00:00
|
|
|
var info database.ClientStore
|
2023-10-10 18:06:43 +01:00
|
|
|
var secret string
|
2024-03-11 12:39:52 +00:00
|
|
|
if h.DbTx(rw, func(tx *database.Queries) error {
|
2024-02-09 15:24:40 +00:00
|
|
|
sub := req.Form.Get("subject")
|
2024-03-12 21:04:25 +00:00
|
|
|
info, err = tx.GetClientInfo(req.Context(), sub)
|
2023-10-10 18:06:43 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-03-12 21:04:25 +00:00
|
|
|
secret, err := password.GenerateApiSecret(70)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = tx.ResetClientAppSecret(req.Context(), database.ResetClientAppSecretParams{
|
|
|
|
Secret: secret,
|
|
|
|
Subject: sub,
|
|
|
|
Owner: auth.ID,
|
|
|
|
})
|
2023-10-10 18:06:43 +01:00
|
|
|
return err
|
|
|
|
}) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-03-12 21:04:25 +00:00
|
|
|
appName := info.GetName()
|
2023-10-10 18:06:43 +01:00
|
|
|
|
|
|
|
h.ManageAppsGet(rw, &http.Request{
|
|
|
|
URL: &url.URL{
|
|
|
|
RawQuery: url.Values{
|
|
|
|
"offset": []string{offset},
|
|
|
|
"NewAppName": []string{appName},
|
|
|
|
"NewAppSecret": []string{secret},
|
|
|
|
}.Encode(),
|
|
|
|
},
|
|
|
|
}, httprouter.Params{}, auth)
|
|
|
|
return
|
2023-09-15 13:06:31 +01:00
|
|
|
default:
|
|
|
|
http.Error(rw, "400 Bad Request: Invalid action", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
redirectUrl := url.URL{Path: "/manage/apps", RawQuery: url.Values{"offset": []string{offset}}.Encode()}
|
|
|
|
http.Redirect(rw, req, redirectUrl.String(), http.StatusFound)
|
|
|
|
}
|