diff --git a/internal/admin/apiservices.go b/internal/admin/apiservices.go index 7c7f658..8b7bda1 100644 --- a/internal/admin/apiservices.go +++ b/internal/admin/apiservices.go @@ -29,6 +29,6 @@ func NewApiServiceDTO(service repository.ApiService) ApiServiceDTO { GrantTypes: service.GrantTypes, CreatedAt: service.CreatedAt, UpdatedAt: service.UpdatedAt, - IsActive: false, + IsActive: service.IsActive, } } diff --git a/internal/admin/routes.go b/internal/admin/routes.go index fc2e117..b2f153d 100644 --- a/internal/admin/routes.go +++ b/internal/admin/routes.go @@ -11,6 +11,7 @@ import ( "gitea.local/admin/hspguard/internal/util" "gitea.local/admin/hspguard/internal/web" "github.com/go-chi/chi/v5" + "github.com/jackc/pgx/v5/pgtype" ) type AdminHandler struct { @@ -107,14 +108,24 @@ func (h *AdminHandler) AddApiService(w http.ResponseWriter, r *http.Request) { return } - service, err := h.repo.CreateApiService(r.Context(), repository.CreateApiServiceParams{ + params := repository.CreateApiServiceParams{ ClientID: clientId, ClientSecret: hashSecret, Name: req.Name, RedirectUris: req.RedirectUris, Scopes: req.Scopes, GrantTypes: req.GrantTypes, - }) + IsActive: req.IsActive, + } + + if req.Description != "" { + params.Description = pgtype.Text{ + String: req.Description, + Valid: true, + } + } + + service, err := h.repo.CreateApiService(r.Context(), params) if err != nil { web.Error(w, "failed to create new api service", http.StatusInternalServerError) return @@ -124,8 +135,24 @@ func (h *AdminHandler) AddApiService(w http.ResponseWriter, r *http.Request) { service.ClientSecret = clientSecret + type ApiServiceCredentials struct { + ClientId string `json:"client_id"` + ClientSecret string `json:"client_secret"` + } + + type Response struct { + Service ApiServiceDTO `json:"service"` + Credentials ApiServiceCredentials `json:"credentials"` + } + encoder := json.NewEncoder(w) - if err := encoder.Encode(NewApiServiceDTO(service)); err != nil { + if err := encoder.Encode(Response{ + Service: NewApiServiceDTO(service), + Credentials: ApiServiceCredentials{ + ClientId: service.ClientID, + ClientSecret: service.ClientSecret, + }, + }); err != nil { web.Error(w, "failed to encode response", http.StatusInternalServerError) } }