@prismshadow/penguin-library-auth · library/auth/src/service.ts:90

AuthService

component

No JSDoc comment in source yet.

API

@Component({
  name: "AuthServiceImpl",
  key: "@prismshadow/penguin-server#AuthService",
})
export class AuthService implements Auth {
  @Use() private readonly users!: Users;
  @Use() private readonly authSessions!: AuthSessions;
  @Use() private readonly state!: AuthRuntimeState;
  @Use() private readonly provisioner!: InitialProjectProvisioner;
  @Use() private readonly config!: AuthConfig;
  @Use() private readonly clock!: AuthClock;
  @Use() private readonly hasher!: PasswordHasher;
  get sessionTtlMs(): number;
  setup(): void;
  mintFirstLogin(): string | null;
  async adminPasswordIs(password: string): Promise<boolean>;
  async seedAdmin(): Promise<void>;
  redeemFirstLogin(given: string): string | null;
  adminPasswordIsInitial(): boolean;
  async login(userId: string, password: string): Promise<{ user: UserInfo; token: string }>;
  isAdmin(userId: string): boolean;
  loginDesktop(): { user: UserInfo; token: string };
  issueRemoteSession(): { token: string; expiresAt: string; maxAgeSeconds: number };
  async changePassword(userId: string, oldPassword: string, newPassword: string): Promise<void>;
  async setInitialPassword(userId: string, newPassword: string): Promise<void>;
  logout(token: string): void;
  localApiToken(): string | null;
  authenticateApiToken(token: string): { user: UserRow; via: SessionVia } | null;
  authenticateWithMeta(token: string): { user: UserRow; via: SessionVia; renewed: boolean } | null;
  sessionIsLive(token: string): boolean;
}

Dependencies & Wiring

Extracted from class decorators
implementsinterfaces
@Userequires (7)
users: Users · authSessions: AuthSessions · state: AuthRuntimeState · provisioner: InitialProjectProvisioner · config: AuthConfig · clock: AuthClock · hasher: PasswordHasher

Methods (17)

setup

setup(): void
@returnsvoid

mintFirstLogin

mintFirstLogin(): string | null

Null once the server is claimed, so the only setup session that exists is a printed one. On demand rather than in the constructor: seedAdmin() runs after it, so "claimed?" has no answer there. A cached link whose row is gone is re-minted, not handed out dead.

@returnsstring | null

adminPasswordIs

async adminPasswordIs(password: string): Promise<boolean>

For the startup notice's pinned-seed gate (index.ts): a pin usually means the operator knows the password, but an offline reset makes it stale, and the gate must see through that or the rescue link stays suppressed.

@parampasswordstring
@returnsPromise<boolean>

seedAdmin

async seedAdmin(): Promise<void>

Startup seeding (idempotent): creates the built-in admin and adopts default_project when the users table is empty; if the initial Project fails, the user row is rolled back and the server retries on next startup.

@returnsPromise<void>

redeemFirstLogin

redeemFirstLogin(given: string): string | null

Compared against the printed value, not merely verified: a cookie made out of ANY valid token would let one person hand another a link into the sender's account. Not single-use, because a prefetching browser would burn it before its reader clicked.

@paramgivenstring
@returnsstring | null

adminPasswordIsInitial

adminPasswordIsInitial(): boolean

Whether the built-in admin still runs on its initial password (gates the first-login link).

@returnsboolean

login

async login(userId: string, password: string): Promise<{ user: UserInfo; token: string }>
@paramuserIdstring
@parampasswordstring
@returnsPromise<{ user: UserInfo; token: string }>

isAdmin

isAdmin(userId: string): boolean

Whether a user is an admin, by id — for a caller that has a user's NAME rather than an authenticated request: the terminal relay reads the owner out of a pty reference the runtime has already matched against the signed-in user (machines/terminal-relay.ts), and what is left to decide is whether that user may reach machines at all. Unknown users are not admins.

@paramuserIdstring
@returnsboolean

loginDesktop

loginDesktop(): { user: UserInfo; token: string }

Desktop-mode sign-in: an admin session with no password check — the claim route already redeemed the shell's one-shot token, which is the credential here. Throws only on a broken deployment (seeding runs before the route exists).

@returns{ user: UserInfo; token: string }

issueRemoteSession

issueRemoteSession(): { token: string; expiresAt: string; maxAgeSeconds: number }
@returns{ token: string; expiresAt: string; maxAgeSeconds: number }

changePassword

async changePassword(userId: string, oldPassword: string, newPassword: string): Promise<void>

Self password change (user settings): validates the old password first; the current session stays valid.

@paramuserIdstring
@paramoldPasswordstring
@paramnewPasswordstring
@returnsPromise<void>

setInitialPassword

async setInitialPassword(userId: string, newPassword: string): Promise<void>

No old-password check; the me route gates which sessions may call this.

@paramuserIdstring
@paramnewPasswordstring
@returnsPromise<void>

logout

logout(token: string): void

Ends a session: the row is the session, so logout deletes it. Unknown token is a no-op.

@paramtokenstring
@returnsvoid

localApiToken

localApiToken(): string | null

The current boot's local API token (what control-env injection hands to tool subprocesses); null when none was minted. It lives on the runtime state, not on this service: the token was written to <root>/api-token by the process, so it must outlive the App a push replaces (auth/runtime-state.ts).

@returnsstring | null

authenticateApiToken

authenticateApiToken(token: string): { user: UserRow; via: SessionVia } | null

Validates a Bearer token against the boot's local API token (constant-time compare): a match authenticates as the built-in admin — holding the token proves filesystem access to the data root, which is admin authority (see auth/api-token.ts). Returns null on mismatch, when no token was minted, or when the admin row is missing.

@paramtokenstring
@returns{ user: UserRow; via: SessionVia } | null

authenticateWithMeta

authenticateWithMeta(token: string): { user: UserRow; via: SessionVia; renewed: boolean } | null

Validates the cookie token. Sliding renewal tops the expiry up IN PLACE, so the cookie value never changes and there is no second identity to revoke. Only a session whose own span reaches the renewal window slides — a one-hour minted token must expire at its hour, not stretch by being used.

@paramtokenstring
@returns{ user: UserRow; via: SessionVia; renewed: boolean } | null

sessionIsLive

sessionIsLive(token: string): boolean

Whether the session behind a cookie token still exists and has not expired — the same two facts authenticateWithMeta establishes, without its sliding renewal or its user lookup. A connection that outlives the request that opened it has to re-ask periodically (the SSE heartbeat does, once per beat), and asking through the renewing path would top the expiry up every time: a window nobody has touched since last week would stay signed in purely because it is still connected.

@paramtokenstring
@returnsboolean