Skip to content

Commit 1e8d5f6

Browse files
committed
Improve SAML error responses with workspace context
Enhanced error messages in the SAML controller to include workspace IDs and specific error details, providing clearer feedback for SSO-related issues. This change improves debugging and user experience by making error responses more informative.
1 parent c8b18bf commit 1e8d5f6

File tree

2 files changed

+21
-16
lines changed

2 files changed

+21
-16
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "hawk.api",
3-
"version": "1.3.0",
3+
"version": "1.3.1",
44
"main": "index.ts",
55
"license": "BUSL-1.1",
66
"scripts": {

src/sso/saml/controller.ts

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ export default class SamlController {
5656
*/
5757
if (!this.isValidWorkspaceId(workspaceId)) {
5858
this.log('warn', 'Invalid workspace ID format:', sgr(workspaceId, Effect.ForegroundRed));
59-
res.status(400).json({ error: 'Invalid workspace ID' });
59+
res.status(400).json({ error: `Invalid workspace ID format: ${workspaceId}` });
6060

6161
return;
6262
}
@@ -68,7 +68,7 @@ export default class SamlController {
6868

6969
if (!workspace || !workspace.sso?.enabled) {
7070
this.log('warn', 'SSO not enabled for workspace:', sgr(workspaceId, Effect.ForegroundCyan));
71-
res.status(400).json({ error: 'SSO is not enabled for this workspace' });
71+
res.status(400).json({ error: `SSO is not enabled for workspace: ${workspaceId}` });
7272

7373
return;
7474
}
@@ -153,14 +153,15 @@ export default class SamlController {
153153

154154
res.redirect(redirectUrl.toString());
155155
} catch (error) {
156+
const errorMessage = error instanceof Error ? error.message : 'Unknown error';
156157
this.log(
157158
'error',
158159
'SSO initiation error for workspace:',
159160
sgr(workspaceId, Effect.ForegroundCyan),
160161
'|',
161-
sgr(error instanceof Error ? error.message : 'Unknown error', Effect.ForegroundRed)
162+
sgr(errorMessage, Effect.ForegroundRed)
162163
);
163-
res.status(500).json({ error: 'Failed to initiate SSO login' });
164+
res.status(500).json({ error: `Failed to initiate SSO login for workspace ${workspaceId}: ${errorMessage}` });
164165
}
165166
}
166167

@@ -182,7 +183,7 @@ export default class SamlController {
182183
*/
183184
if (!this.isValidWorkspaceId(workspaceId)) {
184185
this.log('warn', '[ACS] Invalid workspace ID format:', sgr(workspaceId, Effect.ForegroundRed));
185-
res.status(400).json({ error: 'Invalid workspace ID' });
186+
res.status(400).json({ error: `Invalid workspace ID format: ${workspaceId}` });
186187

187188
return;
188189
}
@@ -192,7 +193,7 @@ export default class SamlController {
192193
*/
193194
if (!samlResponse) {
194195
this.log('warn', '[ACS] Missing SAML response for workspace:', sgr(workspaceId, Effect.ForegroundCyan));
195-
res.status(400).json({ error: 'SAML response is required' });
196+
res.status(400).json({ error: `SAML response is required for workspace: ${workspaceId}` });
196197

197198
return;
198199
}
@@ -204,7 +205,7 @@ export default class SamlController {
204205

205206
if (!workspace || !workspace.sso?.enabled) {
206207
this.log('warn', '[ACS] SSO not enabled for workspace:', sgr(workspaceId, Effect.ForegroundCyan));
207-
res.status(400).json({ error: 'SSO is not enabled for this workspace' });
208+
res.status(400).json({ error: `SSO is not enabled for workspace: ${workspaceId}` });
208209

209210
return;
210211
}
@@ -269,27 +270,29 @@ export default class SamlController {
269270
}
270271

271272
if (!isValidRequest) {
273+
const requestIdShort = samlData.inResponseTo.slice(0, 8);
272274
this.log(
273275
'error',
274276
'[ACS] InResponseTo validation failed for workspace:',
275277
sgr(workspaceId, Effect.ForegroundCyan),
276278
'| Request ID:',
277-
sgr(samlData.inResponseTo.slice(0, 8), Effect.ForegroundGray)
279+
sgr(requestIdShort, Effect.ForegroundGray)
278280
);
279-
res.status(400).json({ error: 'Invalid SAML response: InResponseTo validation failed' });
281+
res.status(400).json({ error: `Invalid SAML response: InResponseTo validation failed for workspace ${workspaceId}, request ID: ${requestIdShort}` });
280282

281283
return;
282284
}
283285
}
284286
} catch (error) {
287+
const errorMessage = error instanceof Error ? error.message : 'Unknown error';
285288
this.log(
286289
'error',
287290
'[ACS] SAML validation error for workspace:',
288291
sgr(workspaceId, Effect.ForegroundCyan),
289292
'|',
290-
sgr(error instanceof Error ? error.message : 'Unknown error', Effect.ForegroundRed)
293+
sgr(errorMessage, Effect.ForegroundRed)
291294
);
292-
res.status(400).json({ error: 'Invalid SAML response' });
295+
res.status(400).json({ error: `Invalid SAML response for workspace ${workspaceId}: ${errorMessage}` });
293296

294297
return;
295298
}
@@ -383,26 +386,28 @@ export default class SamlController {
383386
* Handle specific error types
384387
*/
385388
if (error instanceof Error && error.message.includes('SAML')) {
389+
const errorMessage = error.message;
386390
this.log(
387391
'error',
388392
'[ACS] SAML processing error for workspace:',
389393
sgr(workspaceId, Effect.ForegroundCyan),
390394
'|',
391-
sgr(error.message, Effect.ForegroundRed)
395+
sgr(errorMessage, Effect.ForegroundRed)
392396
);
393-
res.status(400).json({ error: 'Invalid SAML response' });
397+
res.status(400).json({ error: `Invalid SAML response for workspace ${workspaceId}: ${errorMessage}` });
394398

395399
return;
396400
}
397401

402+
const errorMessage = error instanceof Error ? error.message : 'Unknown error';
398403
this.log(
399404
'error',
400405
'[ACS] ACS callback error for workspace:',
401406
sgr(workspaceId, Effect.ForegroundCyan),
402407
'|',
403-
sgr(error instanceof Error ? error.message : 'Unknown error', Effect.ForegroundRed)
408+
sgr(errorMessage, Effect.ForegroundRed)
404409
);
405-
res.status(500).json({ error: 'Failed to process SSO callback' });
410+
res.status(500).json({ error: `Failed to process SSO callback for workspace ${workspaceId}: ${errorMessage}` });
406411
}
407412
}
408413

0 commit comments

Comments
 (0)