@riao/authn-sso-google 1.0.0 → 3.0.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/CHANGELOG.md +11 -1
- package/LICENSE.md +20 -20
- package/README.md +328 -254
- package/authentication-google.js +13 -28
- package/authentication-google.js.map +1 -1
- package/package.json +3 -3
package/CHANGELOG.md
CHANGED
|
@@ -1 +1,11 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @riao/authn-sso-google - Changelog
|
|
2
|
+
|
|
3
|
+
## 3.0.0
|
|
4
|
+
|
|
5
|
+
- Require SSO 3 and IAM 5; validate state when supplied.
|
|
6
|
+
- Forward all inherited security options, including protection and encryption.
|
|
7
|
+
- Classify expected OAuth/token rejection separately from operational
|
|
8
|
+
failures; validate responses and bound requests with safe error messages.
|
|
9
|
+
- Update the server example to store the authenticated principal in session.
|
|
10
|
+
- Regenerate lockfile registry integrity for IAM 5 and SSO 3 after their
|
|
11
|
+
coordinated publication; no local package references are shipped.
|
package/LICENSE.md
CHANGED
|
@@ -1,20 +1,20 @@
|
|
|
1
|
-
Copyright (c) 2022 auth-template and others
|
|
2
|
-
|
|
3
|
-
Permission is hereby granted, free of charge, to any person obtaining
|
|
4
|
-
a copy of this software and associated documentation files (the
|
|
5
|
-
"Software"), to deal in the Software without restriction, including
|
|
6
|
-
without limitation the rights to use, copy, modify, merge, publish,
|
|
7
|
-
distribute, sublicense, and/or sell copies of the Software, and to
|
|
8
|
-
permit persons to whom the Software is furnished to do so, subject to
|
|
9
|
-
the following conditions:
|
|
10
|
-
|
|
11
|
-
The above copyright notice and this permission notice shall be
|
|
12
|
-
included in all copies or substantial portions of the Software.
|
|
13
|
-
|
|
14
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
15
|
-
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
16
|
-
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
17
|
-
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
18
|
-
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
19
|
-
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
20
|
-
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
1
|
+
Copyright (c) 2022 auth-template and others
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
|
4
|
+
a copy of this software and associated documentation files (the
|
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
|
9
|
+
the following conditions:
|
|
10
|
+
|
|
11
|
+
The above copyright notice and this permission notice shall be
|
|
12
|
+
included in all copies or substantial portions of the Software.
|
|
13
|
+
|
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,254 +1,328 @@
|
|
|
1
|
-
# @riao/authn-sso-google
|
|
2
|
-
|
|
3
|
-
Google OAuth2/OIDC authentication driver for riao-iam.
|
|
4
|
-
|
|
5
|
-
## Installation
|
|
6
|
-
|
|
7
|
-
```bash
|
|
8
|
-
npm install @riao/authn-sso-google @riao/iam @riao/dbal
|
|
9
|
-
npm install --save-dev @riao/cli
|
|
10
|
-
```
|
|
11
|
-
|
|
12
|
-
## Database Setup
|
|
13
|
-
|
|
14
|
-
Import SSO tables into your database:
|
|
15
|
-
|
|
16
|
-
```bash
|
|
17
|
-
npx riao migration:create import-sso-google-tables
|
|
18
|
-
```
|
|
19
|
-
|
|
20
|
-
`database/main/migrations/123456789-import-sso-tables.ts`:
|
|
21
|
-
|
|
22
|
-
```typescript
|
|
23
|
-
import { AuthenticationSSOMigrations } from '@riao/authn-sso/authentication-sso-migrations';
|
|
24
|
-
|
|
25
|
-
export default AuthenticationSSOMigrations;
|
|
26
|
-
```
|
|
27
|
-
|
|
28
|
-
Then run migrations:
|
|
29
|
-
|
|
30
|
-
```bash
|
|
31
|
-
npm run migration up
|
|
32
|
-
```
|
|
33
|
-
|
|
34
|
-
## Google OAuth Setup
|
|
35
|
-
|
|
36
|
-
### 1. Create a Google Cloud Project
|
|
37
|
-
|
|
38
|
-
1. Go to [Google Cloud Console](https://console.cloud.google.com)
|
|
39
|
-
2. Create a new project
|
|
40
|
-
3. Enable the Google+ API
|
|
41
|
-
|
|
42
|
-
### 2. Create OAuth 2.0 Credentials
|
|
43
|
-
|
|
44
|
-
1. Navigate to "Credentials" → "Create Credentials" → "OAuth client ID"
|
|
45
|
-
2. Choose "Web application"
|
|
46
|
-
3. Add authorized redirect URIs:
|
|
47
|
-
- `https://yourdomain.com/auth/google/callback`
|
|
48
|
-
- `http://localhost:3000/auth/google/callback` (for development)
|
|
49
|
-
4. Save the Client ID and Client Secret
|
|
50
|
-
|
|
51
|
-
### 3. Configuration
|
|
52
|
-
|
|
53
|
-
```typescript
|
|
54
|
-
import { GoogleAuthentication } from '@riao/authn-sso-google';
|
|
55
|
-
|
|
56
|
-
const googleAuth = new GoogleAuthentication({
|
|
57
|
-
db: database,
|
|
58
|
-
clientId: process.env.GOOGLE_CLIENT_ID!,
|
|
59
|
-
clientSecret: process.env.GOOGLE_CLIENT_SECRET!,
|
|
60
|
-
redirectUri: process.env.GOOGLE_REDIRECT_URI!,
|
|
61
|
-
});
|
|
62
|
-
```
|
|
63
|
-
|
|
64
|
-
### Environment Variables
|
|
65
|
-
|
|
66
|
-
```bash
|
|
67
|
-
GOOGLE_CLIENT_ID=your-client-id.apps.googleusercontent.com
|
|
68
|
-
GOOGLE_CLIENT_SECRET=your-client-secret
|
|
69
|
-
GOOGLE_REDIRECT_URI=https://yourdomain.com/auth/google/callback
|
|
70
|
-
```
|
|
71
|
-
|
|
72
|
-
### Custom Scopes
|
|
73
|
-
|
|
74
|
-
By default, the driver requests: `openid`, `profile`, `email`.
|
|
75
|
-
|
|
76
|
-
To customize scopes:
|
|
77
|
-
|
|
78
|
-
```typescript
|
|
79
|
-
const googleAuth = new GoogleAuthentication({
|
|
80
|
-
// ... other options
|
|
81
|
-
scopes: ['openid', 'profile', 'email', 'https://www.googleapis.com/auth/calendar'],
|
|
82
|
-
});
|
|
83
|
-
```
|
|
84
|
-
|
|
85
|
-
## Usage
|
|
86
|
-
|
|
87
|
-
### Get Authorization URL
|
|
88
|
-
|
|
89
|
-
```typescript
|
|
90
|
-
const state = await googleAuth.generateState();
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
)
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
const
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
1
|
+
# @riao/authn-sso-google
|
|
2
|
+
|
|
3
|
+
Google OAuth2/OIDC authentication driver for riao-iam.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @riao/authn-sso-google @riao/iam @riao/dbal
|
|
9
|
+
npm install --save-dev @riao/cli
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
## Database Setup
|
|
13
|
+
|
|
14
|
+
Import SSO tables into your database:
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
npx riao migration:create import-sso-google-tables
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
`database/main/migrations/123456789-import-sso-tables.ts`:
|
|
21
|
+
|
|
22
|
+
```typescript
|
|
23
|
+
import { AuthenticationSSOMigrations } from '@riao/authn-sso/authentication-sso-migrations';
|
|
24
|
+
|
|
25
|
+
export default AuthenticationSSOMigrations;
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
Then run migrations:
|
|
29
|
+
|
|
30
|
+
```bash
|
|
31
|
+
npm run migration up
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
## Google OAuth Setup
|
|
35
|
+
|
|
36
|
+
### 1. Create a Google Cloud Project
|
|
37
|
+
|
|
38
|
+
1. Go to [Google Cloud Console](https://console.cloud.google.com)
|
|
39
|
+
2. Create a new project
|
|
40
|
+
3. Enable the Google+ API
|
|
41
|
+
|
|
42
|
+
### 2. Create OAuth 2.0 Credentials
|
|
43
|
+
|
|
44
|
+
1. Navigate to "Credentials" → "Create Credentials" → "OAuth client ID"
|
|
45
|
+
2. Choose "Web application"
|
|
46
|
+
3. Add authorized redirect URIs:
|
|
47
|
+
- `https://yourdomain.com/auth/google/callback`
|
|
48
|
+
- `http://localhost:3000/auth/google/callback` (for development)
|
|
49
|
+
4. Save the Client ID and Client Secret
|
|
50
|
+
|
|
51
|
+
### 3. Configuration
|
|
52
|
+
|
|
53
|
+
```typescript
|
|
54
|
+
import { GoogleAuthentication } from '@riao/authn-sso-google';
|
|
55
|
+
|
|
56
|
+
const googleAuth = new GoogleAuthentication({
|
|
57
|
+
db: database,
|
|
58
|
+
clientId: process.env.GOOGLE_CLIENT_ID!,
|
|
59
|
+
clientSecret: process.env.GOOGLE_CLIENT_SECRET!,
|
|
60
|
+
redirectUri: process.env.GOOGLE_REDIRECT_URI!,
|
|
61
|
+
});
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
### Environment Variables
|
|
65
|
+
|
|
66
|
+
```bash
|
|
67
|
+
GOOGLE_CLIENT_ID=your-client-id.apps.googleusercontent.com
|
|
68
|
+
GOOGLE_CLIENT_SECRET=your-client-secret
|
|
69
|
+
GOOGLE_REDIRECT_URI=https://yourdomain.com/auth/google/callback
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
### Custom Scopes
|
|
73
|
+
|
|
74
|
+
By default, the driver requests: `openid`, `profile`, `email`.
|
|
75
|
+
|
|
76
|
+
To customize scopes:
|
|
77
|
+
|
|
78
|
+
```typescript
|
|
79
|
+
const googleAuth = new GoogleAuthentication({
|
|
80
|
+
// ... other options
|
|
81
|
+
scopes: ['openid', 'profile', 'email', 'https://www.googleapis.com/auth/calendar'],
|
|
82
|
+
});
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
## Usage
|
|
86
|
+
|
|
87
|
+
### Get Authorization URL
|
|
88
|
+
|
|
89
|
+
```typescript
|
|
90
|
+
const { state, stateBinding } = await googleAuth.generateState();
|
|
91
|
+
req.session.oauthState = state;
|
|
92
|
+
req.session.oauthStateBinding = stateBinding;
|
|
93
|
+
await new Promise<void>((resolve, reject) => {
|
|
94
|
+
req.session.save((error) => error ? reject(error) : resolve());
|
|
95
|
+
});
|
|
96
|
+
const authUrl = googleAuth.getAuthorizationUrl(state);
|
|
97
|
+
|
|
98
|
+
res.redirect(authUrl);
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
### Handle Callback
|
|
102
|
+
|
|
103
|
+
After user authorizes, Google redirects to your callback endpoint with `code` and `state`:
|
|
104
|
+
|
|
105
|
+
Require the callback's `state` to match the value saved in the browser session.
|
|
106
|
+
Pass it and the saved session binding to `authenticate`; the state is consumed
|
|
107
|
+
before the authorization code is exchanged.
|
|
108
|
+
|
|
109
|
+
```typescript
|
|
110
|
+
app.get('/auth/google/callback', async (req, res) => {
|
|
111
|
+
const code = req.query['code'];
|
|
112
|
+
const state = req.query['state'];
|
|
113
|
+
const session = req.session;
|
|
114
|
+
const stateBinding = session.oauthStateBinding;
|
|
115
|
+
if (
|
|
116
|
+
typeof code !== 'string' || !code ||
|
|
117
|
+
typeof state !== 'string' || !state ||
|
|
118
|
+
session.oauthState !== state ||
|
|
119
|
+
typeof stateBinding !== 'string' || !stateBinding
|
|
120
|
+
) {
|
|
121
|
+
res.redirect('/login?error=authentication_failed');
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
delete session.oauthState;
|
|
126
|
+
delete session.oauthStateBinding;
|
|
127
|
+
await new Promise<void>((resolve, reject) => {
|
|
128
|
+
session.save((error) => error ? reject(error) : resolve());
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
const principal = await googleAuth.authenticate({
|
|
132
|
+
code,
|
|
133
|
+
state,
|
|
134
|
+
stateBinding,
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
if (principal) {
|
|
138
|
+
// Store the authenticated principal in the application session.
|
|
139
|
+
} else {
|
|
140
|
+
res.redirect('/login?error=authentication_failed');
|
|
141
|
+
}
|
|
142
|
+
});
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
### Refresh Tokens
|
|
146
|
+
|
|
147
|
+
Automatically refresh access tokens using stored refresh tokens:
|
|
148
|
+
|
|
149
|
+
```typescript
|
|
150
|
+
// Retrieve stored token record for user
|
|
151
|
+
// `principal` is the authenticated principal from the callback/session.
|
|
152
|
+
const principalId = principal.id;
|
|
153
|
+
const tokenRecord = await googleAuth.getStoredToken(principalId);
|
|
154
|
+
|
|
155
|
+
// Refresh if expired
|
|
156
|
+
if (tokenRecord && tokenRecord.expires_at < new Date()) {
|
|
157
|
+
await googleAuth.refreshAccessToken(principalId);
|
|
158
|
+
}
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
## Complete Example
|
|
162
|
+
|
|
163
|
+
```typescript
|
|
164
|
+
import express from 'express';
|
|
165
|
+
import session from 'express-session';
|
|
166
|
+
import { GoogleAuthentication } from '@riao/authn-sso-google';
|
|
167
|
+
import { Principal } from '@riao/iam';
|
|
168
|
+
|
|
169
|
+
declare module 'express-session' {
|
|
170
|
+
interface SessionData {
|
|
171
|
+
principal?: Principal;
|
|
172
|
+
oauthState?: string;
|
|
173
|
+
oauthStateBinding?: string;
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
const app = express();
|
|
178
|
+
|
|
179
|
+
// Setup session middleware
|
|
180
|
+
app.use(session({
|
|
181
|
+
secret: process.env.SESSION_SECRET!,
|
|
182
|
+
resave: false,
|
|
183
|
+
saveUninitialized: false,
|
|
184
|
+
}));
|
|
185
|
+
|
|
186
|
+
const googleAuth = new GoogleAuthentication({
|
|
187
|
+
db: database,
|
|
188
|
+
clientId: process.env.GOOGLE_CLIENT_ID!,
|
|
189
|
+
clientSecret: process.env.GOOGLE_CLIENT_SECRET!,
|
|
190
|
+
redirectUri: process.env.GOOGLE_REDIRECT_URI!,
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
// Redirect to Google login
|
|
194
|
+
// Bind generated state to this browser session for CSRF protection.
|
|
195
|
+
app.get('/auth/google', async (req, res) => {
|
|
196
|
+
try {
|
|
197
|
+
const { state, stateBinding } = await googleAuth.generateState();
|
|
198
|
+
req.session.oauthState = state;
|
|
199
|
+
req.session.oauthStateBinding = stateBinding;
|
|
200
|
+
await new Promise<void>((resolve, reject) => {
|
|
201
|
+
req.session.save((error) => error ? reject(error) : resolve());
|
|
202
|
+
});
|
|
203
|
+
const authUrl = googleAuth.getAuthorizationUrl(state);
|
|
204
|
+
res.redirect(authUrl);
|
|
205
|
+
} catch (error) {
|
|
206
|
+
console.error('Google authentication setup failed', {
|
|
207
|
+
errorType: error instanceof Error ? error.name : typeof error,
|
|
208
|
+
});
|
|
209
|
+
res.redirect('/login?error=auth_error');
|
|
210
|
+
}
|
|
211
|
+
});
|
|
212
|
+
|
|
213
|
+
// Handle Google callback
|
|
214
|
+
app.get('/auth/google/callback', async (req, res) => {
|
|
215
|
+
const code = req.query['code'];
|
|
216
|
+
const state = req.query['state'];
|
|
217
|
+
const session = req.session;
|
|
218
|
+
|
|
219
|
+
try {
|
|
220
|
+
const stateBinding = session.oauthStateBinding;
|
|
221
|
+
if (
|
|
222
|
+
typeof code !== 'string' || !code ||
|
|
223
|
+
typeof state !== 'string' || !state ||
|
|
224
|
+
session.oauthState !== state ||
|
|
225
|
+
typeof stateBinding !== 'string' || !stateBinding
|
|
226
|
+
) {
|
|
227
|
+
return res.redirect('/login?error=auth_failed');
|
|
228
|
+
}
|
|
229
|
+
delete session.oauthState;
|
|
230
|
+
delete session.oauthStateBinding;
|
|
231
|
+
await new Promise<void>((resolve, reject) => {
|
|
232
|
+
session.save((saveError) =>
|
|
233
|
+
saveError ? reject(saveError) : resolve()
|
|
234
|
+
);
|
|
235
|
+
});
|
|
236
|
+
|
|
237
|
+
const principal = await googleAuth.authenticate({
|
|
238
|
+
code,
|
|
239
|
+
state,
|
|
240
|
+
stateBinding,
|
|
241
|
+
});
|
|
242
|
+
|
|
243
|
+
if (principal) {
|
|
244
|
+
req.session.principal = principal;
|
|
245
|
+
res.redirect('/dashboard');
|
|
246
|
+
} else {
|
|
247
|
+
res.redirect('/login?error=auth_failed');
|
|
248
|
+
}
|
|
249
|
+
} catch (error) {
|
|
250
|
+
console.error('Google authentication operation failed', {
|
|
251
|
+
errorType: error instanceof Error ? error.name : typeof error,
|
|
252
|
+
});
|
|
253
|
+
res.redirect('/login?error=auth_error');
|
|
254
|
+
}
|
|
255
|
+
});
|
|
256
|
+
```
|
|
257
|
+
|
|
258
|
+
## Troubleshooting
|
|
259
|
+
|
|
260
|
+
### Invalid state
|
|
261
|
+
|
|
262
|
+
The callback is rejected if its state does not match the value saved in the
|
|
263
|
+
browser session, the session binding is missing, or the state has expired or
|
|
264
|
+
already been consumed.
|
|
265
|
+
|
|
266
|
+
**Solution:** Save both values returned by `generateState()` in the browser
|
|
267
|
+
session before redirecting:
|
|
268
|
+
|
|
269
|
+
```typescript
|
|
270
|
+
// ❌ WRONG - A manually generated state has no session binding
|
|
271
|
+
app.get('/auth/google', (req, res) => {
|
|
272
|
+
const state = randomBytes(32).toString('hex');
|
|
273
|
+
const authUrl = googleAuth.getAuthorizationUrl(state);
|
|
274
|
+
res.redirect(authUrl);
|
|
275
|
+
});
|
|
276
|
+
|
|
277
|
+
// ✅ CORRECT - Save the generated state and binding in the browser session
|
|
278
|
+
app.get('/auth/google', async (req, res) => {
|
|
279
|
+
const { state, stateBinding } = await googleAuth.generateState();
|
|
280
|
+
req.session.oauthState = state;
|
|
281
|
+
req.session.oauthStateBinding = stateBinding;
|
|
282
|
+
await new Promise<void>((resolve, reject) => {
|
|
283
|
+
req.session.save((error) => error ? reject(error) : resolve());
|
|
284
|
+
});
|
|
285
|
+
const authUrl = googleAuth.getAuthorizationUrl(state);
|
|
286
|
+
res.redirect(authUrl);
|
|
287
|
+
});
|
|
288
|
+
```
|
|
289
|
+
|
|
290
|
+
Store the generated state and binding in the user's session before redirecting
|
|
291
|
+
to Google. On callback, require the returned state to match the session value,
|
|
292
|
+
consume both session values, and pass `{ code, state, stateBinding }` to
|
|
293
|
+
`authenticate()` so it can validate the browser binding and prevent CSRF.
|
|
294
|
+
|
|
295
|
+
### "Database table not found"
|
|
296
|
+
|
|
297
|
+
Ensure you've run the SSO migrations to create the required tables:
|
|
298
|
+
|
|
299
|
+
```bash
|
|
300
|
+
npx riao migration:create import-sso-google-tables
|
|
301
|
+
```
|
|
302
|
+
|
|
303
|
+
Then add to your migration file:
|
|
304
|
+
|
|
305
|
+
```typescript
|
|
306
|
+
import { AuthenticationSSOMigrations } from '@riao/authn-sso/authentication-sso-migrations';
|
|
307
|
+
export default AuthenticationSSOMigrations;
|
|
308
|
+
```
|
|
309
|
+
|
|
310
|
+
Run migrations:
|
|
311
|
+
|
|
312
|
+
```bash
|
|
313
|
+
npm run migration up
|
|
314
|
+
```
|
|
315
|
+
|
|
316
|
+
## Contributing & Development
|
|
317
|
+
|
|
318
|
+
See [contributing.md](docs/contributing/contributing.md) for information on how to develop or contribute to this project!
|
|
319
|
+
|
|
320
|
+
## Related Drivers
|
|
321
|
+
|
|
322
|
+
- [@riao/authn-sso](https://github.com/riao-project/authn-sso) - Base SSO driver
|
|
323
|
+
- [@riao/authn-sso-entra](https://github.com/riao-project/authn-sso-entra) - Microsoft Entra ID
|
|
324
|
+
- [@riao/authn-password](https://github.com/riao-project/authn-password) - Password authentication
|
|
325
|
+
|
|
326
|
+
## License
|
|
327
|
+
|
|
328
|
+
MIT
|
package/authentication-google.js
CHANGED
|
@@ -13,10 +13,7 @@ class GoogleAuthentication extends authn_sso_1.SSOAuthentication {
|
|
|
13
13
|
redirectUri;
|
|
14
14
|
scopes;
|
|
15
15
|
constructor(options) {
|
|
16
|
-
super({
|
|
17
|
-
db: options.db,
|
|
18
|
-
provider: 'google',
|
|
19
|
-
});
|
|
16
|
+
super({ ...options, provider: 'google' });
|
|
20
17
|
this.clientId = options.clientId;
|
|
21
18
|
this.clientSecret = options.clientSecret;
|
|
22
19
|
this.redirectUri = options.redirectUri;
|
|
@@ -49,8 +46,7 @@ class GoogleAuthentication extends authn_sso_1.SSOAuthentication {
|
|
|
49
46
|
* Exchange authorization code for tokens
|
|
50
47
|
*/
|
|
51
48
|
async exchangeAuthorizationCode(code) {
|
|
52
|
-
const
|
|
53
|
-
const response = await fetch(tokenUrl, {
|
|
49
|
+
const data = await (0, authn_sso_1.requestProviderJson)('https://oauth2.googleapis.com/token', {
|
|
54
50
|
method: 'POST',
|
|
55
51
|
headers: {
|
|
56
52
|
'Content-Type': 'application/x-www-form-urlencoded',
|
|
@@ -62,31 +58,25 @@ class GoogleAuthentication extends authn_sso_1.SSOAuthentication {
|
|
|
62
58
|
redirect_uri: this.redirectUri,
|
|
63
59
|
grant_type: 'authorization_code',
|
|
64
60
|
}).toString(),
|
|
65
|
-
});
|
|
66
|
-
|
|
67
|
-
throw new Error(`Google token exchange failed: ${response.status}` +
|
|
68
|
-
` ${response.statusText}`);
|
|
69
|
-
}
|
|
70
|
-
return response.json();
|
|
61
|
+
}, 'Google token exchange', this.requestTimeoutMs);
|
|
62
|
+
return (0, authn_sso_1.validateTokenResponse)(data);
|
|
71
63
|
}
|
|
72
64
|
/**
|
|
73
65
|
* Fetch user information from Google userinfo endpoint
|
|
74
66
|
*/
|
|
75
67
|
async getUserInfo(accessToken) {
|
|
76
|
-
const
|
|
68
|
+
const data = await (0, authn_sso_1.requestProviderJson)('https://www.googleapis.com/oauth2/v2/userinfo', {
|
|
77
69
|
method: 'GET',
|
|
78
70
|
headers: {
|
|
79
71
|
Authorization: `Bearer ${accessToken}`,
|
|
80
72
|
'Content-Type': 'application/json',
|
|
81
73
|
},
|
|
82
|
-
});
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
throw new Error(
|
|
86
|
-
` ${response.statusText}. ` +
|
|
87
|
-
`Error details: ${errorBody}`);
|
|
74
|
+
}, 'Google user info', this.requestTimeoutMs, true);
|
|
75
|
+
(0, authn_sso_1.validateProviderUser)(data, ['email', 'name', 'picture', 'locale'], true);
|
|
76
|
+
if (!(0, authn_sso_1.isProviderString)(data['email'])) {
|
|
77
|
+
throw new Error('Malformed Google user response: missing email');
|
|
88
78
|
}
|
|
89
|
-
const userData =
|
|
79
|
+
const userData = data;
|
|
90
80
|
return {
|
|
91
81
|
id: userData.id,
|
|
92
82
|
login: userData.email || '',
|
|
@@ -100,8 +90,7 @@ class GoogleAuthentication extends authn_sso_1.SSOAuthentication {
|
|
|
100
90
|
* Exchange refresh token for new access token
|
|
101
91
|
*/
|
|
102
92
|
async exchangeRefreshToken(refreshToken) {
|
|
103
|
-
const
|
|
104
|
-
const response = await fetch(tokenUrl, {
|
|
93
|
+
const data = await (0, authn_sso_1.requestProviderJson)('https://oauth2.googleapis.com/token', {
|
|
105
94
|
method: 'POST',
|
|
106
95
|
headers: {
|
|
107
96
|
'Content-Type': 'application/x-www-form-urlencoded',
|
|
@@ -112,12 +101,8 @@ class GoogleAuthentication extends authn_sso_1.SSOAuthentication {
|
|
|
112
101
|
refresh_token: refreshToken,
|
|
113
102
|
grant_type: 'refresh_token',
|
|
114
103
|
}).toString(),
|
|
115
|
-
});
|
|
116
|
-
|
|
117
|
-
throw new Error(`Google token refresh failed: ${response.status}` +
|
|
118
|
-
` ${response.statusText}`);
|
|
119
|
-
}
|
|
120
|
-
return response.json();
|
|
104
|
+
}, 'Google token refresh', this.requestTimeoutMs);
|
|
105
|
+
return (0, authn_sso_1.validateTokenResponse)(data);
|
|
121
106
|
}
|
|
122
107
|
}
|
|
123
108
|
exports.GoogleAuthentication = GoogleAuthentication;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"authentication-google.js","sourceRoot":"","sources":["../../src/authentication-google.ts"],"names":[],"mappings":";;;AAAA,+
|
|
1
|
+
{"version":3,"file":"authentication-google.js","sourceRoot":"","sources":["../../src/authentication-google.ts"],"names":[],"mappings":";;;AAAA,+CASyB;AAazB;;;;G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,KAAK,CAAC,oBAAoB,CACnC,YAAoB;QAEpB,MAAM,IAAI,GAAG,MAAM,IAAA,+BAAmB,EACrC,qCAAqC,EACrC;YACC,MAAM,EAAE,MAAM;YACd,OAAO,EAAE;gBACR,cAAc,EAAE,mCAAmC;aACnD;YACD,IAAI,EAAE,IAAI,eAAe,CAAC;gBACzB,SAAS,EAAE,IAAI,CAAC,QAAQ;gBACxB,aAAa,EAAE,IAAI,CAAC,YAAY;gBAChC,aAAa,EAAE,YAAY;gBAC3B,UAAU,EAAE,eAAe;aAC3B,CAAC,CAAC,QAAQ,EAAE;SACb,EACD,sBAAsB,EACtB,IAAI,CAAC,gBAAgB,CACrB,CAAC;QACF,OAAO,IAAA,iCAAqB,EAAC,IAAI,CAAC,CAAC;IACpC,CAAC;CACD;AAvID,oDAuIC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@riao/authn-sso-google",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "3.0.0",
|
|
4
4
|
"description": "Google OAuth2/OIDC SSO authentication driver for riao-iam",
|
|
5
5
|
"typescript-template": {
|
|
6
6
|
"base": "1.0.1",
|
|
@@ -9,9 +9,9 @@
|
|
|
9
9
|
},
|
|
10
10
|
"scripts": {},
|
|
11
11
|
"dependencies": {
|
|
12
|
-
"@riao/authn-sso": "^
|
|
12
|
+
"@riao/authn-sso": "^3.0.0",
|
|
13
13
|
"@riao/dbal": "^3.8.0",
|
|
14
|
-
"@riao/iam": "^
|
|
14
|
+
"@riao/iam": "^5.0.2"
|
|
15
15
|
},
|
|
16
16
|
"repository": {
|
|
17
17
|
"type": "git",
|