API Layer — Routes, Middleware, Authentication
The HTTP layer lives in internal/web (routes) and internal/middleware. One Gin engine serves the panel API, the sign-in endpoints, OAuth2, the daemon API and the web client on web.host. The endpoint-by-endpoint list is in the API reference; scopes are listed in the scopes reference.
Engine set-up (cmd/panel/run.go)
Section titled “Engine set-up (cmd/panel/run.go)”Before any route is registered:
gin.Recovery()and an access log whose lines leave out secrets (middleware.RedactedLogFormatter: the token in/api/map-view/<server>/<token>/...and thetoken,code,access_tokenandrefresh_tokenquery values are replaced with***).- Trusted proxies: this machine, private networks and Cloudflare’s edge always;
security.trustedProxiesadds more andsecurity.trustedProxyHeadernames a platform header (see Configuration). - When the panel is enabled, the
sessioncookie store (panel.web.cookies.*) is added.
The HTTP server sets a 10-second ReadHeaderTimeout and, on shutdown, lets requests in progress finish for up to 10 seconds.
Main router (internal/web/loader.go)
Section titled “Main router (internal/web/loader.go)”RegisterRoutes(e *gin.Engine) registers, in this order:
1. CORS (every route)2. SecurityHeaders (every route)3. SameOriginGuard (every route)4. Recover (every route)5. /daemon/* if daemon.enable6. if panel.enable: /swagger/*any signed-in administrators only (scope admin) /api/* panel API /oauth2/* OAuth2 token endpoint /auth/* sign-in, sign-up, password reset, SSO, public key static web client embedded build, overlaid by panel.web.files /favicon.ico, /manifest.json, /favicon.png NoRoute SPA fallback (index.html)The web client is the Astro build embedded in the binary (client/frontend/dist). A directory is redirected to its URL with a trailing slash and served from its index.html. Unknown paths under /api/, /oauth2/ and /daemon/ answer 404; a missing build asset (/_astro/..., .js, .css, fonts…) also answers 404 with Cache-Control: no-store, so a page opened before an update reloads instead of running HTML as a script. Everything else gets index.html with caching disabled.
corsConfig := cors.DefaultConfig()corsConfig.AllowOriginFunc = func(string) bool { return true }corsConfig.AllowCredentials = falsecorsConfig.AddAllowHeaders("Authorization", "Content-Type", "Accept", "Origin")corsConfig.AddAllowMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")Any site may call the API with a bearer token, but never with the user’s cookies: credentials are not allowed cross-origin. The panel itself is same-origin and does not need CORS. The console websocket routes add their own CORS handler.
SecurityHeaders
Section titled “SecurityHeaders”Every answer carries:
| Header | Value |
|---|---|
Content-Security-Policy |
frame-ancestors 'self'; object-src 'none'; base-uri 'self' |
X-Frame-Options |
SAMEORIGIN |
X-Content-Type-Options |
nosniff |
Referrer-Policy |
same-origin |
Strict-Transport-Security |
max-age=31536000, only when the request came over HTTPS (TLS or X-Forwarded-Proto: https) |
Handlers that serve untrusted pages (extension files, world maps) replace the policy with their own sandbox.
SameOriginGuard
Section titled “SameOriginGuard”Refuses (403 {"error": "cross-origin request refused"}) a request that:
- carries a panel cookie (
skypanel_authorsession), and - changes state (any method other than
GET,HEADandOPTIONS, or a websocket upgrade), and - was not started by the panel’s own pages.
Origin is decided with Sec-Fetch-Site when the browser sends it (same-origin or none pass). Otherwise the Origin header must match the request host, X-Forwarded-Host or the host of panel.settings.masterUrl; a request with neither header did not come from a browser page and passes.
It exists because SameSite=Lax is not enough here: a “site” is the host without the port, so a game server’s web page on http://IP:25566 is the same site as the panel on http://IP:8080 and could otherwise post forms or open a console websocket with the user’s cookies. Requests without cookies (bearer tokens, nodes, OAuth2 clients) are not affected.
Middleware
Section titled “Middleware”| Middleware | File | Role |
|---|---|---|
SecurityHeaders, SameOriginGuard |
security.go |
See above. |
Recover, ResponseAndRecover |
middleware.go |
Turn a panic into a 500 (the second one also writes the error response). |
NeedsDatabase, HasTransaction |
database.go |
Put the GORM connection (or a transaction) in the context. |
AuthMiddleware |
auth.go |
Session authentication (cookie or bearer token). |
RequiresPermission, RequiresAnyPermission |
middleware.go |
Scope checks. |
ResolveServerPanel / ResolveServerNode |
middleware.go |
Load the server from the panel database / from the daemon cache, or answer 404. |
APIKeyAuthMiddleware, RequiresAPIKeyPermission |
api_key_auth.go |
API keys of the billing API. |
ValidateJWT |
decrypt.go |
Checks the panel-signed token on /daemon routes. |
AddVersionHeader |
version.go |
Adds X-API-Version: <panel version>. |
RecordActivity |
activity.go |
Writes sign-in events to the activity feed (best effort). |
/api/* chain (internal/web/api/loader.go)
Section titled “/api/* chain (internal/web/api/loader.go)”rg.Use(func(c *gin.Context) { c.Header("Cache-Control", "no-store"); c.Next() })rg.Use(middleware.ResponseAndRecover)rg.Use(middleware.NeedsDatabase)publicRg := rg.Group("") // no authentication below this grouprg.Use(middleware.AuthMiddleware)rg.Use(middleware.AddVersionHeader)Public groups (no session): GET /api/config, /api/extransfer/* (federated transfers, authenticated with their own transfer tokens), /api/map-view/* (world map behind a signed link), /api/extension-files/* (extension pages, framed sandboxed) and /api/v1/* (billing API, API keys).
Route groups
Section titled “Route groups”| Prefix | File | Main scopes |
|---|---|---|
/api/servers |
servers.go and server_*.go, databases.go |
server.* per server; create needs server.create |
/api/nodes |
nodes.go, node_ports.go, node_network.go |
nodes.view, nodes.create, nodes.edit, nodes.delete, nodes.deploy |
/api/users |
users.go |
users.info.*, users.perms.* |
/api/self |
self.go |
login, self.edit, self.clients (profile, 2FA, personal OAuth2 clients, SFTP keys) |
/api/roles |
roles.go |
admin (listing also with user scopes) |
/api/templates |
templates.go |
login to read, templates.* to change |
/api/game-versions |
game_versions.go |
login |
/api/settings |
settings.go |
settings.edit |
/api/settings/apikeys |
apikeys.go |
admin |
/api/userSettings |
usersettings.go |
login |
/api/activity |
activity.go |
users.info.search |
/api/uptime |
uptime.go |
admin or uptime.view; per server server.view |
/api/databasehosts |
databasehosts.go |
admin |
/api/provision/products |
products.go |
admin |
/api/extensions |
extensions.go |
admin (/active with login) |
/api/v1 |
provision.go |
API key (see below) |
Most /api/servers/{id}/... routes are a scope check plus proxyServerRequest, which forwards the request to the node (see Panel to node requests).
/auth/* (internal/web/auth/loader.go)
Section titled “/auth/* (internal/web/auth/loader.go)”| Route | Middleware | Purpose |
|---|---|---|
POST /auth/login |
NeedsDatabase |
Password step. Opens the session, or answers {"otpNeeded": true} and keeps the pending sign-in in the session cookie for 5 minutes. |
POST /auth/otp |
NeedsDatabase |
Two-factor step (TOTP or recovery code). |
POST /auth/register |
NeedsDatabase |
Public sign-up, if panel.registrationEnabled. |
POST /auth/forgot-password |
NeedsDatabase |
Sends a reset link. |
POST /auth/reset-password |
NeedsDatabase |
Consumes the reset token and sets the new password. |
POST /auth/reauth |
AuthMiddleware, NeedsDatabase |
Refreshes the current session. |
POST /auth/logout |
NeedsDatabase |
Ends the current session. |
GET /auth/sso |
NeedsDatabase |
Consumes a one-time link created by the billing API (POST /api/v1/sso) and signs the user in. |
GET /auth/challenge |
— | Proof-of-work challenge of the built-in captcha. |
GET /auth/publickey |
— | The panel’s public key set (JWKS) used by nodes. |
POST /auth/sftp-key |
NeedsDatabase |
For remote nodes: checks an SFTP login with an SSH key (internal/web/auth/sftp_key.go). Authenticated with the node secret (Authorization: Bearer <secret>), body {"username": "email#serverId", "publicKey": "<authorized_keys line>"}. Answers 200 {"scope": "<serverId>:server.sftp"} (never a session), 401 invalid_client without a valid node secret, 400 invalid_request (“no access”) for any refusal and 429 after too many failed attempts. See Security. |
POST /auth/sftp-check |
NeedsDatabase |
For remote nodes: whether an open SFTP connection may stay open (internal/web/auth/sftp_check.go), asked every 30 seconds per connection. Same node secret, body {"username": "email#serverId"}. Answers 200 {"scope": "<serverId>:server.sftp"}, 400 invalid_request (“no access”) when the user, their login or server.sftp, or the server is gone (the node closes the connection), 401 without a valid node secret and 500 on a database error (the node keeps the connection). No attempt limit. |
Sign-in, sign-up and password reset check the captcha first: Turnstile when it is enabled and configured, otherwise the built-in proof of work when panel.captcha.builtin is on.
Token endpoint (/oauth2/*)
Section titled “Token endpoint (/oauth2/*)”POST /oauth2/token → setHeaders (no-store), recovery, NeedsDatabase, handleTokenRequestOPTIONS /oauth2/tokenAuthentication
Section titled “Authentication”Session (cookie or bearer)
Section titled “Session (cookie or bearer)”POST /auth/login(and/auth/otpwhen 2FA is on) creates a row insessionsand returns the token in the body (session) and in theskypanel_authcookie.AuthMiddlewarereadsAuthorization: Bearer <token>first and falls back to theskypanel_authcookie. Paths under/auth/,/error/and/api/configare skipped, except/auth/reauthand/auth/logout.- The token is looked up by its SHA-256 hash and must not be expired. Sessions last one hour, sliding: activity renews the expiry in the database, and a cookie-based request also renews the cookie.
- On every request the user must still hold the
loginscope (in their panel-wide permissions or role); taking it away ends sessions already open. A session of a deleted OAuth2 client is also ended.
The skypanel_auth cookie is HttpOnly, SameSite=Lax, path /, and Secure when the request came over HTTPS (directly or with X-Forwarded-Proto: https) or panel.web.cookies.secure is on. It is not Secure on plain HTTP on purpose: browsers drop Secure cookies there, and the installer’s default address is http://IP:8080.
OAuth2
Section titled “OAuth2”POST /oauth2/token (form-encoded) supports two grants:
| Grant | Who | Result |
|---|---|---|
client_credentials |
Personal or server OAuth2 clients (client_id, client_secret, created in /api/self/oauth2) |
A session token (access_token, Bearer, one hour) with the scopes the client’s user holds. A client never gets more than it was registered for, and a server client only reaches its own server. |
password |
Nodes only, to check SFTP passwords | Requires Authorization: Bearer <node secret>. Username is email#serverId; the server must be on that node and the user must hold server.sftp on it. The answer only carries the scope <serverId>:server.sftp, never a usable token, so a password alone cannot bypass the account’s 2FA. |
Client secrets are stored as bcrypt hashes.
API keys (billing API)
Section titled “API keys (billing API)”- Created by an administrator in Settings (
/api/settings/apikeys). Keys start withak_; only a SHA-256 hash and an 8-character prefix are stored. - Sent as
X-Api-Key: ak_...orAuthorization: Bearer ak_...; only accepted on/api/v1/*. - Each key can be limited to actions:
create,terminate,suspend(also unsuspend),change-packageandsso. A key with no actions, or with the legacyprovision, may do everything.
| Route | Action |
|---|---|
GET /api/v1/ping |
none |
POST /api/v1/provision |
create |
POST /api/v1/terminate |
terminate |
POST /api/v1/suspend, POST /api/v1/unsuspend |
suspend |
POST /api/v1/change-package |
change-package |
POST /api/v1/sso |
sso |
Password recovery
Section titled “Password recovery”POST /auth/forgot-passwordwith{"email": "..."}creates a token (stored as a BLAKE2b-256 hash, valid 30 minutes) and emails a link to the address stored on the account.POST /auth/reset-passwordwith{"token": "...", "password": "..."}consumes it and changes the password.- Both answer
204whenever possible so accounts cannot be enumerated. The link usespanel.settings.masterUrlwhen it is set.
Panel to node requests
Section titled “Panel to node requests”Every call the panel makes to a node’s /daemon/* (including the proxied /api/servers/{id}/... routes and the console websocket) carries Authorization: Bearer <JWT>:
- Signed by the panel with EdDSA (Ed25519 key from
panel.token), key IDSkyPanel. - Valid for 15 minutes, with one minute of leeway for clock differences.
- Its audience is a SHA-256 hash derived from the target node’s secret, so a token captured on its way to one node cannot be replayed against another.
ValidateJWT on the node accepts only EdDSA tokens with an expiry and that audience (the local node’s secret, or daemon.auth.clientSecret on a remote node). The verification key is the panel’s own key set when the panel runs on the same binary, or the JWKS fetched from token.public on a remote node. Unauthenticated daemon routes: GET/HEAD /daemon (status) and GET /daemon/features.
A token the node cannot verify (signed by another panel, made for another node, or expired because a clock is off) is answered 401 with WWW-Authenticate: Bearer error="invalid_token" and logged on the node. The panel does not pass such a refusal on to the browser, where it would look like the user’s own session ending and send them back to the login page: when a node answers 401, or 403 with a WWW-Authenticate header, the panel logs the node’s name with a hint (check the node secret and both clocks) and answers 502 ErrNodeAuthFailed (answerNodeAuthFailure, internal/web/api/node_auth.go). This covers the proxied server routes and the direct calls that create, edit, delete and back up servers, the world map and provisioning. Any other answer from the node (a plain 403, 409, 400…) is passed through as before.
The node calls the panel only to check SFTP logins: passwords through the password grant above, SSH keys through POST /auth/sftp-key. See Configuration: remote node and Security.
Scope checks
Section titled “Scope checks”g.Handle("GET", "/:serverId", middleware.RequiresPermission(scopes.ScopeServerView), middleware.ResolveServerPanel, getServer)g.GET("/:serverId/flags", middleware.RequiresAnyPermission(scopes.ScopeServerViewFlags, scopes.ScopeServerEditFlags), ...)RequiresPermission (checkPermission):
- Needs a user in the context (set by
AuthMiddleware); otherwise403. - A server scope needs
:serverIdin the route. - A session from an OAuth2 client is first bounded by the client’s own scopes and server.
- Collects the scopes: on a server, that server’s permission row, the panel-wide row an administrator set, and the owner scopes when the user owns the server. A role’s server scopes never count on a server (they would give every sub-user the owner’s powers); off a server, the role’s scopes and the panel-wide row count.
- Bounds the collected list by the OAuth2 client, when the session has one (
boundByClient), and stores it on the request asscopes. Handlers read that list (callerScopes) for their own second checks (database passwords, locked environment variables, scopes a user may grant, task step scopes) and to pick the WebSocket streams, so those checks see exactly what the route check saw. - Checks the scope with
scopes.ContainsScope:admincovers every scope andserver.adminevery server scope. Any unexpected failure answers500, never access.
Rate limits
Section titled “Rate limits”| What | Limit | Key |
|---|---|---|
Password attempts (panel sign-in, OAuth2 password, SFTP) |
10 per 15 minutes | account |
SSH key checks for remote nodes (/auth/sftp-key) |
10 per 15 minutes | account, a budget of its own (sshkey:user:<id>) |
| Two-factor codes | 10 per 15 minutes | account |
| Password reset requests | 5 per hour | |
| AI requests (log analysis and assistant together) | 30 per 10 minutes | user |
| Subdomain changes | 1 per 10 seconds | server |
| Notification tests | 1 per 10 seconds | server |
The limiters are keyed by account rather than by IP, so they behave the same behind a reverse proxy; they live in memory and reset when the panel restarts. Too many attempts answer 429.
Port management
Section titled “Port management”PUT /api/servers/{id}/data (administrator)
Section titled “PUT /api/servers/{id}/data (administrator)”Saves the full port list and the main port. Requires server.data.edit.admin.
{ "ports": [25565, 25575, 25585], "primaryPort": 25565 }primaryPortmust be one of the ports; it is moved to the front.- Every port is checked against the node: range 1024–65535, no duplicates, not one of the panel’s own ports on that node, not assigned to another server there. A refusal answers
400withErrPortOutOfRange,ErrPortDuplicated,ErrPortUsedByPanelorErrPortTaken, and the port inmetadata.port(for a port template variable, the number and the variable, such as25566 (rcon_port)); the web client translates them. - The node is updated first; if it refuses or is down, the panel does not save ports or limits the server does not have. The daemon rewrites
port,port2,port3… (removing stale ones). - Updates
server.Portandserver.Portsin the panel database and refreshes the subdomain records when the main port changed.
PUT /api/servers/{id}/port-settings (owner)
Section titled “PUT /api/servers/{id}/port-settings (owner)”Changes the main port among the assigned ones and the port labels, without touching the list. Requires server.data.edit.
{ "primaryPort": 25565, "portNotes": { "25575": "RCON", "25585": "Voice" } }// → { "success": true }- Moves
primaryPortto the front ofserver.Portsand savesserver.PortNotes. - Sends the current list to the node so
port/port2/port3… stay in sync, and refreshes the subdomain when the main port changed.
POST /api/nodes/{id}/ports/check
Section titled “POST /api/nodes/{id}/ports/check”Used by the new-server wizard before anything is created. Requires server.create.
{ "ports": { "port": 25565, "port2": 25566 }, "exclude": "<server being edited>" }For each variable it answers whether the port is busy, why (range below 1024, panel for the panel’s own ports on that node, server for another server, container for another running container), a free port to use instead and, for administrators only, who holds it.
Swagger
Section titled “Swagger”The OpenAPI documentation is generated from the handler comments (internal/web/swagger/swagger.json and .yaml) and served at /swagger/index.html through gin-swagger, only to signed-in users with the admin scope, since it maps every route. The files are regenerated with swag from the annotations; the current ones predate the latest annotation fixes (several backup, task, definition and world map routes), so regenerate them before relying on those entries.
