| 1 | import { |
| 2 | AuthenticatedConfig, |
| 3 | login as doLogin, |
| 4 | logout as doLogout, |
| 5 | ensureOrganization, |
| 6 | isAuthenticated, |
| 7 | listUserOrganizations, |
| 8 | loadAuthConfig, |
| 9 | saveAuthConfig, |
| 10 | } from "../auth/workos.js"; |
| 11 | import { logger } from "../util/logger.js"; |
| 12 | |
| 13 | import { BaseService } from "./BaseService.js"; |
| 14 | import { AuthServiceState } from "./types.js"; |
| 15 | |
| 16 | /** |
| 17 | * Service for managing authentication state and operations |
| 18 | * Now extends BaseService for common functionality |
| 19 | */ |
| 20 | export class AuthService extends BaseService<AuthServiceState> { |
| 21 | constructor() { |
| 22 | super("AuthService", { |
| 23 | authConfig: null, |
| 24 | isAuthenticated: false, |
| 25 | }); |
| 26 | } |
| 27 | |
| 28 | /** |
| 29 | * Initialize the auth service by loading current config |
| 30 | */ |
| 31 | async doInitialize(): Promise<AuthServiceState> { |
| 32 | const authConfig = loadAuthConfig(); |
| 33 | const authenticated = isAuthenticated(); |
| 34 | |
| 35 | const state: AuthServiceState = { |
| 36 | authConfig, |
| 37 | isAuthenticated: authenticated, |
| 38 | organizationId: authConfig?.organizationId || undefined, |
| 39 | }; |
| 40 | |
| 41 | logger.debug("AuthService initialized", { |
| 42 | authenticated, |
| 43 | hasConfig: !!authConfig, |
| 44 | orgId: state.organizationId, |
| 45 | }); |
| 46 | |
| 47 | return state; |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Perform login flow |
| 52 | */ |
| 53 | async login(): Promise<AuthServiceState> { |
| 54 | logger.debug("Starting login flow"); |
| 55 | |
| 56 | try { |
| 57 | const newAuthConfig = await doLogin(); |
| 58 | |
| 59 | this.setState({ |
| 60 | authConfig: newAuthConfig, |
| 61 | isAuthenticated: true, |
| 62 | organizationId: newAuthConfig?.organizationId || undefined, |
| 63 | }); |
| 64 | |
| 65 | logger.debug("Login successful", { |
| 66 | orgId: this.currentState.organizationId, |
| 67 | }); |
| 68 | |
| 69 | return this.getState(); |
| 70 | } catch (error: any) { |
| 71 | logger.error("Login failed:", error); |
| 72 | this.emit("error", error); |
| 73 | throw error; |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Perform logout |
| 79 | */ |
| 80 | async logout(): Promise<AuthServiceState> { |
| 81 | logger.debug("Logging out"); |
| 82 | |
| 83 | doLogout(); |
| 84 | |
| 85 | this.setState({ |
| 86 | authConfig: null, |
| 87 | isAuthenticated: false, |
| 88 | organizationId: undefined, |
| 89 | }); |
| 90 | |
| 91 | logger.debug("Logout complete"); |
| 92 | return this.getState(); |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Ensure organization is selected, prompting if necessary |
| 97 | */ |
| 98 | async ensureOrganization( |
| 99 | isHeadless: boolean = false, |
| 100 | ): Promise<AuthServiceState> { |
| 101 | if (!this.currentState.authConfig) { |
| 102 | throw new Error("Not authenticated - cannot ensure organization"); |
| 103 | } |
| 104 | |
| 105 | logger.debug("Ensuring organization is selected", { |
| 106 | currentOrgId: this.currentState.organizationId, |
| 107 | isHeadless, |
| 108 | }); |
| 109 | |
| 110 | const updatedConfig = await ensureOrganization( |
| 111 | this.currentState.authConfig, |
| 112 | isHeadless, |
| 113 | ); |
| 114 | |
| 115 | this.setState({ |
| 116 | authConfig: updatedConfig, |
| 117 | isAuthenticated: true, |
| 118 | organizationId: updatedConfig?.organizationId || undefined, |
| 119 | }); |
| 120 | |
| 121 | logger.debug("Organization ensured", { |
| 122 | orgId: this.currentState.organizationId, |
| 123 | }); |
| 124 | |
| 125 | return this.getState(); |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Switch to a different organization |
| 130 | */ |
| 131 | async switchOrganization( |
| 132 | organizationId: string | null, |
| 133 | ): Promise<AuthServiceState> { |
| 134 | if ( |
| 135 | !this.currentState.authConfig || |
| 136 | !("userId" in this.currentState.authConfig) |
| 137 | ) { |
| 138 | throw new Error( |
| 139 | "Not authenticated with file-based auth - cannot switch organizations", |
| 140 | ); |
| 141 | } |
| 142 | |
| 143 | logger.debug("Switching organization", { |
| 144 | from: this.currentState.organizationId, |
| 145 | to: organizationId, |
| 146 | }); |
| 147 | |
| 148 | const authenticatedConfig = this.currentState |
| 149 | .authConfig as AuthenticatedConfig; |
| 150 | |
| 151 | const updatedConfig: AuthenticatedConfig = { |
| 152 | ...authenticatedConfig, |
| 153 | organizationId, |
| 154 | }; |
| 155 | |
| 156 | saveAuthConfig(updatedConfig); |
| 157 | |
| 158 | this.setState({ |
| 159 | authConfig: updatedConfig, |
| 160 | isAuthenticated: true, |
| 161 | organizationId: organizationId || undefined, |
| 162 | }); |
| 163 | |
| 164 | logger.debug("Organization switched", { |
| 165 | newOrgId: this.currentState.organizationId, |
| 166 | }); |
| 167 | |
| 168 | return this.getState(); |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * Get available organizations for the current user |
| 173 | */ |
| 174 | async getAvailableOrganizations(): Promise< |
| 175 | { id: string; name: string }[] | null |
| 176 | > { |
| 177 | if (!this.currentState.isAuthenticated) { |
| 178 | return null; |
| 179 | } |
| 180 | |
| 181 | try { |
| 182 | return await listUserOrganizations(); |
| 183 | } catch (error: any) { |
| 184 | logger.error("Failed to list organizations:", error); |
| 185 | this.emit("error", error); |
| 186 | return null; |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | /** |
| 191 | * Check if the current user has multiple organizations available |
| 192 | */ |
| 193 | async hasMultipleOrganizations(): Promise<boolean> { |
| 194 | const orgs = await this.getAvailableOrganizations(); |
| 195 | return orgs !== null && orgs.length > 0; |
| 196 | } |
| 197 | |
| 198 | /** |
| 199 | * Refresh auth state from disk (useful after external changes) |
| 200 | */ |
| 201 | async refresh(): Promise<AuthServiceState> { |
| 202 | logger.debug("Refreshing auth state from disk"); |
| 203 | return this.reload(); |
| 204 | } |
| 205 | } |