MinecraftAuth.me

API documentation

Contents: Caddy

Caddy

The MinecraftAuth module for Caddy puts websites and routes behind a MinecraftAuth.me login. You decide which Minecraft players get through: specific players, anyone who follows, subscribes to or supports you on Twitch, Discord, Patreon or YouTube, or anyone with a LuckPerms permission. Behind the scenes it uses the redirect flow of access requests to log players in, and membership checks for the rules.

The source and a complete Caddyfile for each use case are in the integrations repository.

Install#

Build Caddy with the module using xcaddy:

xcaddy build --with github.com/MinecraftAuthentication/integrations/caddy/v2

or with Docker:

FROM caddy:2-builder AS builder
RUN xcaddy build --with github.com/MinecraftAuthentication/integrations/caddy/v2

FROM caddy:2
COPY --from=builder /usr/bin/caddy /usr/bin/caddy

Setup#

  1. On the Developers page, create an application and add your callback URL (e.g. https://example.com/mcauth/callback) to its redirect URLs.
  2. If you'll use membership rules (Twitch, Discord, Patreon, YouTube), also copy the application's secret. Membership rules run as you, the application's owner. See Authentication.
  3. Configure Caddy:
{
	mcauth {
		app_id       {env.MCAUTH_APP_ID}
		redirect     https://example.com/mcauth/callback
		app_secret   {env.MCAUTH_APP_SECRET}     # only for membership rules
		session_key  {env.MCAUTH_SESSION_KEY}    # 32+ random characters

		realm staff {
			player 069a79f4-44e9-4726-a5be-fca90e38aaf5 Notch
		}
		realm supporters {
			twitch_subscriber 2
			discord_role 123456789012345678
			patreon_member "Gold"
		}
	}
}

example.com {
	mcauth_callback /mcauth/callback

	route /admin/* {
		mcauth staff
		reverse_proxy localhost:9000 {
			header_up X-Minecraft-Name {mcauth.name}
		}
	}

	mcauth supporters
	file_server
}

A realm is a named set of rules, defined once in the global mcauth option. The mcauth <realm> directive lets in the players a realm allows. You don't need an order option: mcauth_callback and mcauth are ordered around basic_auth.

Global options#

Option Default
app_id required your application's ID
redirect required absolute URL of your mcauth_callback route, registered on the application
app_secret none your application's secret; required by membership rules. Keep it out of version control
server_token none deprecated: a retired server token, used when app_secret isn't set. Logs a warning, and stops working when server tokens are retired
session_key random per start encrypts the cookies (AES-256-GCM). Set it, or every restart logs everyone out
session_ttl 24h how long a login lasts
membership_ttl 5m how long membership and LuckPerms answers are cached
on_api_error deny when MinecraftAuth.me, a platform or LuckPerms can't answer: deny (respond 503) or allow
persist true remember the player's approval; false asks them at every login
scopes none extra platforms to request at login (minecraft is always requested)
cookie_prefix mcauth cookies are <prefix>_session and <prefix>_state
luckperms <url> { api_key <key> } none your LuckPerms REST API; required by permission and group rules
realm <name> { ... } a set of rules (see Rules)

The cookies are set for the registrable domain of redirect (example.com for https://auth.example.com/...), so one callback and one login serve every subdomain.

Rules#

A realm lets a player in when any of its rules matches.

Rule Lets in players who... Needs app_secret
player <uuid|name>... are one of these Minecraft players no
linked logged in with any MinecraftAuth.me account no
discord_server <guild id> are in that Discord server (the MinecraftAuth bot must be too) yes
discord_role <role id> or discord_role <guild id> <role id|name> have that Discord role yes
twitch_follower follow your Twitch channel yes
twitch_subscriber [tier] subscribe on Twitch (at tier 1-3 or higher) yes
patreon_member [tier title] are active patrons (of that tier) yes
youtube_subscriber subscribe to your YouTube channel yes
youtube_member are members of your YouTube channel yes
permission <permission> [<context>=<value>...] have that LuckPerms permission (in those contexts) no, needs luckperms
group <group> [<context>=<value>...] are in that LuckPerms group, directly or inherited (same as permission group.<group>) no, needs luckperms

"Your" means the owner of the application. For Discord rules, the MinecraftAuth bot must be in the server, and your linked Discord account must own it or have the Administrator or Manage Messages permission there. A role name always needs its guild ID, because anyone can create a role with the same name in a server of their own.

Combining rules#

  • not <rule> matches when the rule doesn't, e.g. not group shunned or not player Notch.
  • all { ... } matches only when every rule inside it matches. It can hold not rules and other all blocks, and not all { ... } matches unless all of them do.
  • Several mcauth directives on one route require every one of their realms.
realm vip {
	player Notch                  # always let in
	all {                         # or: a tier 2+ subscriber who isn't shunned
		twitch_subscriber 2
		not group shunned
	}
}
  • When a check can't be answered because an API is down, not doesn't turn that into a match: an outage never lets anyone through a not rule.
  • An all stops at the first rule that doesn't match, and checks player and linked rules before calling any API.
  • A realm whose only rule is not ... lets in every logged-in player that rule doesn't match.

LuckPerms#

permission and group rules ask a LuckPerms REST API (the standalone LuckPerms app with the REST extension) connected to the same database as your Minecraft servers. That way you can gate routes with the permissions and groups you already manage in game:

mcauth {
	# ...
	luckperms http://localhost:8080 {
		api_key {env.LUCKPERMS_REST_KEY}   # one of LUCKPERMS_REST_AUTH_KEYS; omit if auth is off
	}

	realm staff {
		permission website.admin                 # global permission
		permission website.mod server=survival   # only counts in the survival server context
		group admin                              # same as: permission group.admin
	}
}
  • Contexts (server=survival, world=nether) are sent as the check's query contexts. Keys and values are lowercase letters and digits.
  • Answers are cached for membership_ttl. A REST API that's down or rejects the key counts as an API error (see on_api_error).
  • Run the REST API on a private network or behind HTTPS: anyone who can reach it with a key can edit your permissions.

Directives#

  • mcauth_callback [<matcher>] serves the redirect URL. It checks the login's state, verifies the signed grant token and starts the session.
  • mcauth [<matcher>] <realm> lets matching players through. Everyone else gets:
Status When
redirect no session, and the request is a GET or HEAD: the browser is sent to MinecraftAuth.me to log in
401 no session, and any other method
403 logged in, but not allowed into the realm (or the player canceled the login)
400 the login expired, or was started in another browser
503 MinecraftAuth.me, a platform or LuckPerms couldn't answer, and on_api_error is deny

Customize these pages with handle_errors:

example.com {
	mcauth_callback /mcauth/callback
	mcauth subs
	file_server

	handle_errors 403 {
		respond "This page is for Twitch subscribers. Subscribe, then come back!" 403
	}
	handle_errors 400 {
		redir / 302   # send them through the login again
	}
	handle_errors 503 {
		header Retry-After 60
		respond "We couldn't check your subscription right now. Try again in a minute." 503
	}
}

Passing the player to a backend#

Requests that mcauth lets through get these placeholders, which work in any directive:

Placeholder
{mcauth.uuid} the player's Minecraft UUID (also set as {http.auth.user.id}, so it shows up in access logs)
{mcauth.name} the player's Minecraft name
{mcauth.identity} the player's MinecraftAuth.me identity ID
reverse_proxy localhost:9000 {
	# drop anything the client sent under these names, so they can't be spoofed
	header_up -X-Minecraft-*
	header_up X-Minecraft-UUID {mcauth.uuid}
	header_up X-Minecraft-Name {mcauth.name}
}

Security notes#

  • The grant token comes back in the callback's query string. Keep it out of your access logs:

    log {
      format filter {
          request>uri query {
              delete token
          }
      }
    }
  • The module rejects a grant token whose aud isn't your application, or whose 10-minute exp has passed, so a token issued to another application can't be replayed here.

  • Membership and LuckPerms rules are checked again on every request (cached for membership_ttl), so access goes away when a player stops supporting you. Removing a player from a player rule takes effect immediately.

Examples#

Each of these is a complete Caddyfile in the examples directory:

Example Use case
basic any logged-in MinecraftAuth.me player
players an allowlist of specific players
twitch Twitch followers, subscribers and tiers
discord Discord server members and roles
patreon Patreon patrons and tiers
youtube YouTube subscribers and members
luckperms LuckPerms permissions, groups and contexts
combining all, not and several mcauth directives
routes different realms per path, public paths, APIs
backend-headers passing the player to a backend
error-pages custom 400 / 401 / 403 / 503 pages
options every mcauth option