2024-02-11 04:50:46 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"git.lumeweb.com/LumeWeb/gitea-github-proxy/config"
|
|
|
|
"git.lumeweb.com/LumeWeb/gitea-github-proxy/db/model"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
"gorm.io/gorm"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
)
|
|
|
|
|
|
|
|
func newSettingsApi(cfg *config.Config, db *gorm.DB, logger *zap.Logger) *settingsApi {
|
|
|
|
return &settingsApi{config: cfg, db: db, logger: logger}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s settingsApi) handlerNewApp(w http.ResponseWriter, r *http.Request) {
|
|
|
|
manifestData := r.FormValue("manifest")
|
|
|
|
|
|
|
|
var manifestObj manifest
|
|
|
|
|
|
|
|
err := json.Unmarshal([]byte(manifestData), &manifestObj)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, "Failed to parse manifest", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
appData := newApp()
|
|
|
|
|
|
|
|
appRecord := &model.Apps{
|
|
|
|
Name: manifestObj.Name,
|
|
|
|
Url: manifestObj.Url,
|
|
|
|
WebhookUrl: manifestObj.HookAttributes.URL,
|
|
|
|
Code: generateTempCode(),
|
|
|
|
WebhookSecret: generateTempCode(),
|
|
|
|
PrivateKey: appData.PrivateKey,
|
|
|
|
}
|
|
|
|
|
|
|
|
tx := s.db.Create(appRecord)
|
|
|
|
if tx.Error != nil {
|
|
|
|
s.logger.Error("Error creating app", zap.Error(tx.Error))
|
|
|
|
http.Error(w, "Error creating app", http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(manifestObj.RedirectURL) == 0 {
|
|
|
|
s.logger.Error("Redirect URL is required")
|
|
|
|
http.Error(w, "Redirect URL is required", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
redirectUrl, err := url.Parse(manifestObj.RedirectURL)
|
|
|
|
if err != nil {
|
|
|
|
s.logger.Error("Error parsing redirect URL", zap.Error(err))
|
|
|
|
http.Error(w, "Error parsing redirect URL", http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
query := redirectUrl.Query()
|
|
|
|
|
|
|
|
query.Add("code", appRecord.Code)
|
|
|
|
|
|
|
|
if r.URL.Query().Get("state") != "" {
|
|
|
|
query.Add("state", r.URL.Query().Get("state"))
|
|
|
|
}
|
|
|
|
|
|
|
|
redirectUrl.RawQuery = query.Encode()
|
|
|
|
|
|
|
|
http.Redirect(w, r, redirectUrl.String(), http.StatusFound)
|
|
|
|
|
|
|
|
}
|
2024-02-11 05:15:57 +00:00
|
|
|
|
|
|
|
func setupSettingsRoutes(params RouteParams) {
|
|
|
|
r := params.R
|
|
|
|
|
|
|
|
settingsRouter := r.PathPrefix("/settings").Subrouter()
|
|
|
|
settingsRouter.Use(giteaOauthVerifyMiddleware(params.Config))
|
|
|
|
settingsRouter.Use(requireAuthMiddleware(params.Config))
|
|
|
|
|
|
|
|
settingsApi := newSettingsApi(params.Config, params.Db, params.Logger)
|
|
|
|
|
|
|
|
settingsRouter.HandleFunc("/apps/new", settingsApi.handlerNewApp).Methods("POST")
|
|
|
|
}
|