Skip to content

armgit / posta

Public Imported from GitHub
0

Self-hosted email delivery platform that enables applications to send emails via HTTP APIs while handling SMTP delivery, templates, storage, security, and analytics. Fully self-hostable alternative to services like SendGrid or Mailgun.

3.7 KB
Raw Blame
// Copyright (c) 2026 Jonas Kaninda.
//
// This file is part of Posta.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//
// SPDX-License-Identifier: AGPL-3.0-or-later

package routes

import (
	"net/http"

	"github.com/goposta/posta/internal/dto"
	"github.com/goposta/posta/internal/handlers"
	"github.com/jkaninda/okapi"
)

// licenseRoutes manage the Posta Enterprise license. Installing and removing
// it is platform administration, so it takes an admin session and never an
// API key. Any signed-in caller may ask whether Enterprise features are on.
func (r *Router) licenseRoutes() []okapi.RouteDefinition {
	admin := r.v1.Group("/admin/license", r.mw.jwtAdminAuth.Middleware).WithTagInfo(okapi.GroupTag{
		Name:        tagAdmin,
		Description: descAdmin,
	})
	admin.WithBearerAuth()

	return []okapi.RouteDefinition{
		{
			Method:      http.MethodGet,
			Path:        "",
			Handler:     r.h.license.Get,
			Group:       admin,
			Summary:     "Get the Enterprise license",
			Description: "The installed Posta Enterprise license, its state (none, valid, grace, degraded, binding_mismatch, unknown_signing_key), this installation's Install ID and any warnings.",
			Response:    &dto.Response[handlers.LicenseView]{},
		},
		{
			Method:      http.MethodPost,
			Path:        "",
			Handler:     okapi.H(r.h.license.Install),
			Group:       admin,
			Summary:     "Install an Enterprise license",
			Description: "Verifies a license token and installs it, replacing any other. Enterprise features unlock at once, with no restart. A license bound to another installation is refused with 402 LICENSE_BINDING_MISMATCH.",
			Request:     &handlers.InstallLicenseRequest{},
			Response:    &dto.Response[handlers.LicenseView]{},
			Options: []okapi.RouteOption{
				okapi.DocResponse(400, &dto.ErrorResponseBody{}),
				okapi.DocResponse(402, &dto.ErrorResponseBody{}),
			},
		},
		{
			Method:      http.MethodDelete,
			Path:        "",
			Handler:     r.h.license.Remove,
			Group:       admin,
			Summary:     "Remove the Enterprise license",
			Description: "Reverts to Community. Enterprise features stop; their data and settings are kept.",
			Response:    &dto.Response[handlers.LicenseView]{},
		},
		{
			Method:      http.MethodGet,
			Path:        "/system/license/entitlements",
			Handler:     r.h.license.Entitlements,
			Group:       r.v1,
			Middlewares: []okapi.Middleware{r.mw.auth},
			Tags:        []string{"Info"},
			Summary:     "Whether Enterprise features are on",
			Description: "licensed: Enterprise features run. mutable: their settings may change (false once a license has expired past its grace period).",
			Response:    &dto.Response[handlers.Entitlements]{},
		},
	}
}

// licensed refuses a route with 402 unless Enterprise features may run.
func (r *Router) licensed() []okapi.Middleware {
	return []okapi.Middleware{handlers.RequireLicense(r.ee)}
}

// licensedToChange refuses a route with 402 unless Enterprise settings may
// change, which an expired license no longer allows.
func (r *Router) licensedToChange() []okapi.Middleware {
	return []okapi.Middleware{handlers.RequireLicenseToChange(r.ee)}
}