ConsoleAuth
A internal static class in TavernLib, TavernLib.Backend.RemoteConsole. Reach it through att-stubs' publicized assemblies or Harmony's AccessTools.
Internal: console logins. Keeps server_secret.key and the owner token in console_token.txt, signs named tokens, and checks every console login. Server.
- Listed in TavernLib.Backend.RemoteConsole
- Nested types
LoginPath
Fields
| Member | What it is |
|---|---|
const string TokenPolicy | tavern_token:, the policy prefix that marks a named token's login, followed by its id. |
9 private and internal fields
| Member | What it is |
|---|---|
private static byte[] _secret | The loaded server secret, or null if it can't be read. |
private static bool _secretErrorLogged | Set once the unreadable-secret error has been logged, so it is logged once. |
private static readonly string[] BasePolicies | Policies every named token carries. |
private static readonly string Header | The fixed base64url JWT header for HS256. |
private static readonly AuthLimits LoginLimits | Tracks failed logins per address to hold off repeated failures. |
private static readonly string OwnerTokenPath | Path of console_token.txt. |
private static readonly string SecretPath | Path of server_secret.key. |
private static readonly object SecretSync | Lock around loading the secret. |
private static readonly ConditionalWeakTable<UserInfoWithPermissions, string> TokenIds | Caches the named token id found for each user. |
Properties
| Member | What it is |
|---|---|
static byte[] Secret { get; } | The 32-byte key every console token is signed with, made on first use. null while it can't be read; an existing file is never replaced. |
Methods
| Member | What it is |
|---|---|
static string B64Url(byte[] bytes) | Base64url text with no padding. |
static string CurrentOwnerToken() | The owner token's current text, or null while the secret or console_tokens.json can't be read. |
static void EnsureOwnerToken() | Writes console_token.txt when it's missing or isn't the current owner token. Called at server start. |
static string Mint(ConsoleToken token) | A named token's signed text. The same row always gives the same text. |
static string TokenIdOf(UserInfoWithPermissions user) | The named token id a console login carries, or null for the owner token and the server itself. |
static bool Validate(JwtSecurityToken wrapper, ConsoleAuth.LoginPath path, string address, out string tokenId, out string reason) | Checks a console login's signature, expiry, generation and status. Refuses for a while after too many failures from one address. |
static string WriteOwnerToken(string text) | Writes console_token.txt through a temp file. Returns null once written, otherwise why it couldn't be. |
15 private and internal methods
| Member | What it is |
|---|---|
private static string B64(byte[] bytes) | Standard base64 with the padding trimmed. |
private static string BytesToHex(byte[] bytes) | Turns bytes into a lowercase hex string. |
private static string Check(JwtSecurityToken wrapper, ConsoleAuth.LoginPath path, string address, ref string tokenId) | Returns the reason a console login is refused, or null if it is allowed. |
private static byte[] FromB64(string text) | Reads base64 or base64url, or returns null. |
private static long GenerationOf(JObject claims) | Reads the gen claim; 0 if absent and -1 if it isn't a number. |
private static byte[] HexToBytes(string hex) | Reads a hex string into bytes, or returns null. |
private static bool IsCurrentOwnerToken(string raw) | True if the text is a validly signed owner token of the current generation. |
private static bool IsLoopback(string address) | True if the address is a loopback address. |
private static byte[] LoadOrCreateSecret() | Reads server_secret.key, creating it if missing, and retries a few times; returns null if it can't be read. |
private static bool OwnerLocalOnly() | True if console_owner_local_only is set in the server settings. |
private static string OwnerToken(byte[] secret, int generation) | Builds the signed owner token text for a generation. |
private static List<string> PoliciesOf(JObject claims) | Reads the Policy claim into a list, splitting comma separated values. |
private static string Sign(string payload, byte[] secret) | Signs a payload with HMAC-SHA256 and returns the full token text. |
private static bool TryCreateSecret(out byte[] secret) | Writes a new random 32 byte secret unless the file appeared meanwhile; false if it did. |
private static bool Verify(string raw, byte[] secret, out JObject claims) | Checks a token's signature in constant time and returns its claims. |