Skip to content

Commit b3116b2

Browse files
authored
feat: Mock oauth user (#44)
1 parent 2e4c99b commit b3116b2

File tree

3 files changed

+120
-2
lines changed

3 files changed

+120
-2
lines changed

apps/event-system/services/api/routes/public.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ export const publicRoute = () => ({
2828

2929
'v1.connection-testing.public.testConnectionModel',
3030
'v1.stripe-webhook.handleWebhook',
31+
32+
'v3.users.mockOauth',
3133
],
3234

3335
aliases: {
@@ -52,7 +54,8 @@ export const publicRoute = () => ({
5254

5355
'POST v1/embed-tokens/get': 'v1.embed-tokens.public.get',
5456
'POST v1/embed-tokens/update': 'v1.embed-tokens.public.update',
55-
'POST v1/stripe-webhook': 'v1.stripe-webhook.handleWebhook'
57+
'POST v1/stripe-webhook': 'v1.stripe-webhook.handleWebhook',
58+
'POST v3/users/mock-oauth': 'v3.users.mockOauth',
5659

5760
},
5861

apps/event-system/services/users/users.service.ts

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -726,6 +726,121 @@ module.exports = {
726726
}
727727
},
728728
},
729+
mockOauth: {
730+
731+
params: {
732+
user: {
733+
type: 'object',
734+
props: {
735+
login: { type: 'string' },
736+
id: { type: 'number' },
737+
node_id: { type: 'string' },
738+
avatar_url: { type: 'string' },
739+
gravatar_id: { type: 'string' },
740+
url: { type: 'string' },
741+
type: { type: 'string' },
742+
user_view_type: { type: 'string' },
743+
site_admin: { type: 'boolean' },
744+
name: { type: 'string' },
745+
},
746+
},
747+
emails: {
748+
type: 'array',
749+
items: {
750+
type: 'object',
751+
props: {
752+
email: { type: 'string' },
753+
primary: { type: 'boolean' },
754+
verified: { type: 'boolean' },
755+
visibility: { type: 'string' },
756+
},
757+
},
758+
},
759+
},
760+
761+
async handler (ctx: any) {
762+
try {
763+
764+
const secretKey = ctx?.meta?.request?.headers?.['x-mock-user-secret-key'];
765+
if (secretKey !== process.env.MOCK_USER_SECRET_KEY) {
766+
throw new MoleculerError(
767+
'The secret key is invalid',
768+
401,
769+
'unauthorized',
770+
{}
771+
);
772+
}
773+
774+
const { user, emails } = ctx.params;
775+
776+
const email = emails?.[0]?.email;
777+
const username = user.login;
778+
const [firstName, lastName] = getFirstAndLastNameFromName(user.name);
779+
const avatar = user.avatar_url;
780+
const profileLink = user.url;
781+
782+
let _user;
783+
784+
// Create or update user
785+
try {
786+
_user = await this.createOrUpdateUser({
787+
ctx,
788+
provider: 'mock',
789+
user,
790+
emails,
791+
email,
792+
username,
793+
firstName,
794+
lastName,
795+
avatar,
796+
profileLink,
797+
});
798+
} catch (error) {
799+
console.error(error);
800+
if (error.type === new OAuthAccountAlreadyAssociated().type) {
801+
throw error;
802+
}
803+
throw new AuthGenericError();
804+
}
805+
806+
// Create token
807+
try {
808+
const { _id, email, userKey, firstName, lastName, state, pointers } =
809+
_user;
810+
// use buildableId in client due to old users having user.buildableId == coreId
811+
const buildableId = get(_user, 'client.buildableId');
812+
const containerId = get(_user, 'client.containers[0]._id');
813+
814+
const token = this.createToken({
815+
_id,
816+
email,
817+
username,
818+
userKey,
819+
buildableId,
820+
containerId,
821+
firstName,
822+
lastName,
823+
pointers,
824+
});
825+
826+
return {
827+
token,
828+
state,
829+
};
830+
} catch (error) {
831+
console.error(error);
832+
throw new AuthGenericError();
833+
}
834+
835+
836+
}
837+
catch (error) {
838+
console.error(error);
839+
throw new AuthGenericError();
840+
}
841+
}
842+
843+
},
729844
},
730845

731846
methods: {

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)