Skip to content

Commit 763e0cd

Browse files
committed
feat: prevent crash of manual username and password
1 parent 4d926ed commit 763e0cd

File tree

3 files changed

+44
-23
lines changed

3 files changed

+44
-23
lines changed

apps/backend/src/api/routes/integrations.controller.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,7 @@ export class IntegrationsController {
394394
}
395395

396396
const {
397+
error,
397398
accessToken,
398399
expiresIn,
399400
refreshToken,
@@ -412,6 +413,17 @@ export class IntegrationsController {
412413
details ? JSON.parse(details) : undefined
413414
);
414415

416+
if (typeof auth === 'string') {
417+
return res({
418+
error: auth,
419+
accessToken: '',
420+
id: '',
421+
name: '',
422+
picture: '',
423+
username: '',
424+
});
425+
}
426+
415427
if (refresh && integrationProvider.reConnect) {
416428
const newAuth = await integrationProvider.reConnect(
417429
auth.id,
@@ -424,6 +436,10 @@ export class IntegrationsController {
424436
return res(auth);
425437
});
426438

439+
if (error) {
440+
throw new NotEnoughScopes(error);
441+
}
442+
427443
if (!id) {
428444
throw new NotEnoughScopes('Invalid API key');
429445
}

libraries/nestjs-libraries/src/integrations/social/bluesky.provider.ts

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
SocialProvider,
66
} from '@gitroom/nestjs-libraries/integrations/social/social.integrations.interface';
77
import { makeId } from '@gitroom/nestjs-libraries/services/make.is';
8-
import { SocialAbstract } from '@gitroom/nestjs-libraries/integrations/social.abstract';
8+
import { NotEnoughScopes, SocialAbstract } from '@gitroom/nestjs-libraries/integrations/social.abstract';
99
import { BskyAgent, RichText } from '@atproto/api';
1010
import dayjs from 'dayjs';
1111
import { Integration } from '@prisma/client';
@@ -72,30 +72,34 @@ export class BlueskyProvider extends SocialAbstract implements SocialProvider {
7272
}) {
7373
const body = JSON.parse(Buffer.from(params.code, 'base64').toString());
7474

75-
const agent = new BskyAgent({
76-
service: body.service,
77-
});
75+
try {
76+
const agent = new BskyAgent({
77+
service: body.service,
78+
});
7879

79-
const {
80-
data: { accessJwt, refreshJwt, handle, did },
81-
} = await agent.login({
82-
identifier: body.identifier,
83-
password: body.password,
84-
});
80+
const {
81+
data: { accessJwt, refreshJwt, handle, did },
82+
} = await agent.login({
83+
identifier: body.identifier,
84+
password: body.password,
85+
});
8586

86-
const profile = await agent.getProfile({
87-
actor: did,
88-
});
87+
const profile = await agent.getProfile({
88+
actor: did,
89+
});
8990

90-
return {
91-
refreshToken: refreshJwt,
92-
expiresIn: dayjs().add(100, 'years').unix() - dayjs().unix(),
93-
accessToken: accessJwt,
94-
id: did,
95-
name: profile.data.displayName!,
96-
picture: profile.data.avatar!,
97-
username: profile.data.handle!,
98-
};
91+
return {
92+
refreshToken: refreshJwt,
93+
expiresIn: dayjs().add(100, 'years').unix() - dayjs().unix(),
94+
accessToken: accessJwt,
95+
id: did,
96+
name: profile.data.displayName!,
97+
picture: profile.data.avatar!,
98+
username: profile.data.handle!,
99+
};
100+
} catch (e) {
101+
return 'Invalid credentials';
102+
}
99103
}
100104

101105
async post(

libraries/nestjs-libraries/src/integrations/social/social.integrations.interface.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export interface IAuthenticator {
1313
refresh?: string;
1414
},
1515
clientInformation?: ClientInformation
16-
): Promise<AuthTokenDetails>;
16+
): Promise<AuthTokenDetails|string>;
1717
refreshToken(refreshToken: string): Promise<AuthTokenDetails>;
1818
reConnect?(id: string, requiredId: string, accessToken: string): Promise<AuthTokenDetails>;
1919
generateAuthUrl(
@@ -51,6 +51,7 @@ export type GenerateAuthUrlResponse = {
5151
export type AuthTokenDetails = {
5252
id: string;
5353
name: string;
54+
error?: string;
5455
accessToken: string; // The obtained access token
5556
refreshToken?: string; // The refresh token, if applicable
5657
expiresIn?: number; // The duration in seconds for which the access token is valid

0 commit comments

Comments
 (0)