@gzl10/nexus-plugin-google-auth 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,102 @@
1
+ # @gzl10/nexus-plugin-google-auth
2
+
3
+ Google authentication plugin for [Nexus BaaS](https://gitlab.gzl10.com/oss/nexus). Adds login via Google using OpenID Connect.
4
+
5
+ ## Features
6
+
7
+ - OIDC Authorization Code Flow
8
+ - Auto-registration or manual account linking
9
+ - Google Workspace domain restriction (hd parameter)
10
+ - Email domain allowlist
11
+
12
+ ## Requirements
13
+
14
+ - `@gzl10/nexus-backend` >= 0.13.0
15
+ - `@gzl10/nexus-sdk` >= 0.13.0
16
+ - A Google Cloud project with OAuth 2.0 credentials
17
+
18
+ ## Installation
19
+
20
+ ```bash
21
+ pnpm add @gzl10/nexus-plugin-google-auth
22
+ ```
23
+
24
+ ## 1. Configure Google Cloud
25
+
26
+ In the [Google Auth Platform](https://console.cloud.google.com/auth/clients):
27
+
28
+ 1. Go to **Clients** > **Create Client** > **OAuth client ID**
29
+ 2. Set **Application type**: Web application
30
+ 3. Add **Authorized redirect URI**: `https://your-backend.com/api/v1/google_auth/callback`
31
+ 4. Download or copy the **Client ID** and **Client Secret** immediately
32
+
33
+ > **Important:** Since June 2025, client secrets are masked after creation and cannot be retrieved later. Save them when you create the client.
34
+ >
35
+ > The redirect URI must point to your Nexus backend's callback endpoint, not the frontend.
36
+
37
+ ## 2. Configure Nexus
38
+
39
+ Add environment variables:
40
+
41
+ ```env
42
+ GOOGLE_CLIENT_ID=your-client-id
43
+ GOOGLE_CLIENT_SECRET=your-client-secret
44
+ ```
45
+
46
+ Register the plugin in your backend:
47
+
48
+ ```typescript
49
+ import { start } from '@gzl10/nexus-backend'
50
+ import { googleAuthPlugin } from '@gzl10/nexus-plugin-google-auth'
51
+
52
+ await start({ plugins: [googleAuthPlugin] })
53
+ ```
54
+
55
+ On first start, the plugin auto-seeds its configuration from the environment variables.
56
+
57
+ ## 3. Frontend
58
+
59
+ The plugin registers an auth provider that nexus-ui picks up automatically. A "Sign in with Google" button appears on the login page.
60
+
61
+ For custom frontends, redirect users to:
62
+
63
+ ```
64
+ GET /api/v1/google_auth/authorize?redirect_uri=https://your-frontend.com/login
65
+ ```
66
+
67
+ After authentication, the backend redirects back with tokens in the URL fragment:
68
+
69
+ ```
70
+ https://your-frontend.com/login#accessToken=...&refreshToken=...
71
+ ```
72
+
73
+ On error:
74
+
75
+ ```
76
+ https://your-frontend.com/login#error=Your+account+does+not+exist
77
+ ```
78
+
79
+ ## Configuration
80
+
81
+ The plugin creates a `google_auth_config` table on first run. Options:
82
+
83
+ | Field | Default | Description |
84
+ |-------|---------|-------------|
85
+ | `enabled` | `true` | Enable/disable Google login |
86
+ | `hosted_domain` | `null` | Restrict to a Google Workspace domain |
87
+ | `default_role` | `VIEWER` | Role assigned to new users |
88
+ | `allowed_domains` | `null` | JSON array of allowed email domains (null = all) |
89
+ | `scopes` | `openid profile email` | OIDC scopes |
90
+
91
+ ## API Endpoints
92
+
93
+ | Method | Endpoint | Auth | Description |
94
+ |--------|----------|------|-------------|
95
+ | GET | `/api/v1/google_auth/authorize` | No | Start OIDC flow |
96
+ | GET | `/api/v1/google_auth/callback` | No | Handle OIDC callback |
97
+ | GET | `/api/v1/google_auth/link` | Yes | Link existing account |
98
+ | GET | `/api/v1/google_auth/status` | Yes | Check if Google is linked |
99
+
100
+ ## License
101
+
102
+ MIT
@@ -0,0 +1,205 @@
1
+ import { ConfigEntityDefinition, ModuleManifest, OidcState, ActionEntityDefinition, PluginManifest } from '@gzl10/nexus-sdk';
2
+ export { IdTokenClaims, OidcClient, OidcDiscoveryDocument, OidcTokens, OidcUserInfo, createOidcClient, getOidcClient } from '@gzl10/nexus-sdk';
3
+
4
+ /**
5
+ * Google Auth Configuration Types
6
+ */
7
+ /**
8
+ * Google Auth config stored in database
9
+ */
10
+ interface GoogleAuthConfig {
11
+ /** Enable/disable Google authentication */
12
+ enabled: boolean;
13
+ /** OIDC Client ID from Google Cloud Console */
14
+ client_id: string;
15
+ /** OIDC Client Secret (encrypted) */
16
+ client_secret: string;
17
+ /** Scopes to request (default: openid profile email) */
18
+ scopes: string;
19
+ /** Allowed email domains (JSON array, optional) */
20
+ allowed_domains: string | null;
21
+ /** Google Workspace hosted domain (hd parameter) */
22
+ hosted_domain: string | null;
23
+ /** Default role for new users */
24
+ default_role: string | null;
25
+ }
26
+ /**
27
+ * Config service interface
28
+ */
29
+ interface ConfigService {
30
+ /** Get current config */
31
+ getConfig(): Promise<GoogleAuthConfig | null>;
32
+ /** Check if Google Auth is enabled */
33
+ isEnabled(): Promise<boolean>;
34
+ }
35
+
36
+ /**
37
+ * Google Auth Config Entity
38
+ *
39
+ * Stores OIDC configuration for Google authentication.
40
+ * Note: Google OIDC issuer is always https://accounts.google.com
41
+ */
42
+ declare const googleAuthConfigEntity: ConfigEntityDefinition;
43
+
44
+ declare function getConfigService(): ConfigService;
45
+ declare function setConfigService(service: ConfigService): void;
46
+ /**
47
+ * Google Auth Config Module
48
+ */
49
+ declare const configModule: ModuleManifest;
50
+
51
+ /**
52
+ * Google Auth Types
53
+ */
54
+
55
+ /**
56
+ * Session result after successful authentication
57
+ */
58
+ interface SessionResult {
59
+ accessToken: string;
60
+ refreshToken: string;
61
+ expiresIn: number;
62
+ user: {
63
+ id: string;
64
+ email: string;
65
+ name?: string;
66
+ };
67
+ /** Frontend URL to redirect user after auth (server-side flow) */
68
+ returnUrl?: string;
69
+ }
70
+ /**
71
+ * Google-specific OIDC state (extends base)
72
+ */
73
+ interface GoogleAuthState extends OidcState {
74
+ /** User ID if linking account */
75
+ linkUserId?: string;
76
+ /** Frontend URL to redirect user after auth completes */
77
+ returnUrl?: string;
78
+ }
79
+ /**
80
+ * Subset of core AuthService used by this plugin.
81
+ * Registered as ctx.services.get('auth') by nexus-backend.
82
+ */
83
+ interface CoreAuthService {
84
+ findUserById(id: string): Promise<{
85
+ id: string;
86
+ email: string;
87
+ name?: string;
88
+ } | null>;
89
+ findUserByEmail(email: string): Promise<{
90
+ id: string;
91
+ email: string;
92
+ name?: string;
93
+ } | null>;
94
+ createUser(data: {
95
+ email: string;
96
+ name?: string;
97
+ role?: string;
98
+ }): Promise<{
99
+ id: string;
100
+ email: string;
101
+ name?: string;
102
+ }>;
103
+ createTokens(user: {
104
+ id: string;
105
+ }): Promise<{
106
+ accessToken: string;
107
+ refreshToken: string;
108
+ expiresIn: number;
109
+ }>;
110
+ findIdentity(provider: string, providerUserId: string): Promise<AuthIdentity | undefined>;
111
+ findIdentitiesByUser(userId: string, provider?: string): Promise<AuthIdentity[]>;
112
+ linkIdentity(input: LinkIdentityInput): Promise<AuthIdentity>;
113
+ unlinkIdentity(provider: string, providerUserId: string): Promise<boolean>;
114
+ updateIdentityLogin(provider: string, providerUserId: string): Promise<void>;
115
+ }
116
+ /**
117
+ * External auth identity (mirrors core auth_identities table)
118
+ */
119
+ interface AuthIdentity {
120
+ id: string;
121
+ user_id: string;
122
+ provider: string;
123
+ provider_user_id: string;
124
+ provider_email: string | null;
125
+ metadata: Record<string, unknown> | null;
126
+ linked_at: string;
127
+ last_login_at: string | null;
128
+ }
129
+ /**
130
+ * Input for linking an external identity
131
+ */
132
+ interface LinkIdentityInput {
133
+ userId: string;
134
+ provider: string;
135
+ providerUserId: string;
136
+ providerEmail?: string | null;
137
+ metadata?: Record<string, unknown>;
138
+ }
139
+ /**
140
+ * Auth service interface
141
+ */
142
+ interface GoogleAuthService {
143
+ /** Generate authorization URL and state */
144
+ getAuthorizationUrl(redirectUri: string, linkUserId?: string, returnUrl?: string): Promise<{
145
+ url: string;
146
+ state: string;
147
+ }>;
148
+ /** Handle callback from Google */
149
+ handleCallback(code: string, state: string): Promise<SessionResult>;
150
+ /** Verify state is valid */
151
+ verifyState(state: string): Promise<GoogleAuthState | null>;
152
+ /** Clear state after use */
153
+ clearState(state: string): Promise<void>;
154
+ }
155
+
156
+ declare function getAuthService(): GoogleAuthService;
157
+ declare function setAuthService(service: GoogleAuthService): void;
158
+
159
+ /**
160
+ * Google Auth Controller
161
+ *
162
+ * Handles OIDC endpoints:
163
+ * - GET /google_auth/authorize - Start OIDC flow
164
+ * - GET /google_auth/callback - Handle OIDC callback (from Google redirect)
165
+ * - GET /google_auth/link - Link existing account (requires auth)
166
+ * - GET /google_auth/status - Check if Google is linked
167
+ */
168
+
169
+ declare const authActions: ActionEntityDefinition[];
170
+
171
+ /**
172
+ * Google Auth Module
173
+ *
174
+ * Provides OIDC authentication endpoints:
175
+ * - GET /google_auth/authorize - Start auth flow
176
+ * - GET /google_auth/callback - Handle callback
177
+ * - GET /google_auth/link - Link existing account
178
+ * - GET /google_auth/status - Check link status
179
+ */
180
+ declare const authModule: ModuleManifest;
181
+
182
+ /**
183
+ * Google Auth Plugin for Nexus.
184
+ *
185
+ * Provides OIDC authentication with Google for OAuth login.
186
+ *
187
+ * Features:
188
+ * - OIDC Authorization Code Flow
189
+ * - User auto-registration or linking
190
+ * - Google Workspace domain restriction (hd parameter)
191
+ * - Domain allowlist support
192
+ *
193
+ * @example
194
+ * ```typescript
195
+ * import { createNexus } from '@gzl10/nexus-backend'
196
+ * import { googleAuthPlugin } from '@gzl10/nexus-plugin-google-auth'
197
+ *
198
+ * const nexus = createNexus({
199
+ * plugins: [googleAuthPlugin]
200
+ * })
201
+ * ```
202
+ */
203
+ declare const googleAuthPlugin: PluginManifest;
204
+
205
+ export { type AuthIdentity, type ConfigService, type CoreAuthService, type GoogleAuthConfig, type GoogleAuthService, type GoogleAuthState, type LinkIdentityInput, type SessionResult, authActions, authModule, configModule, googleAuthPlugin as default, getAuthService, getConfigService, googleAuthConfigEntity, googleAuthPlugin, setAuthService, setConfigService };