Skip to content

Commit

Permalink
Mock oauth user
Browse files Browse the repository at this point in the history
  • Loading branch information
krish221997 committed Dec 2, 2024
1 parent 2e4c99b commit 72f0c96
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 3 deletions.
5 changes: 4 additions & 1 deletion apps/event-system/services/api/routes/public.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ export const publicRoute = () => ({

'v1.connection-testing.public.testConnectionModel',
'v1.stripe-webhook.handleWebhook',

'v3.users.mockOauth',
],

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

'POST v1/embed-tokens/get': 'v1.embed-tokens.public.get',
'POST v1/embed-tokens/update': 'v1.embed-tokens.public.update',
'POST v1/stripe-webhook': 'v1.stripe-webhook.handleWebhook'
'POST v1/stripe-webhook': 'v1.stripe-webhook.handleWebhook',
'POST v3/users/mock-oauth': 'v3.users.mockOauth',

},

Expand Down
116 changes: 116 additions & 0 deletions apps/event-system/services/users/users.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,38 @@ import axios from 'axios';
import { get, omit, pick, uniqBy } from 'lodash';
import DbService from 'moleculer-db';
import MongoDBAdapter from 'moleculer-db-adapter-mongo';
import { faker } from '@faker-js/faker';

function generateFakeUser() {
const firstName = faker.person.firstName();
const lastName = faker.person.lastName();
const username = faker.internet.userName({ firstName, lastName });
const email = faker.internet.email({ firstName, lastName });

const user = {
login: username,
id: faker.number.int({ min: 100000000, max: 999999999 }),
node_id: `U_kgDOC${faker.string.alphanumeric(6)}`,
avatar_url: faker.image.avatar(),
gravatar_id: '',
url: `https://api.github.com/users/${username}`,
type: 'User',
user_view_type: 'private',
site_admin: false,
name: `${firstName} ${lastName}`,
};

const emails = [
{
email: email,
primary: true,
verified: true,
visibility: 'public'
}
];

return { user, emails };
}

const Auth = require('@libs-private/service-logic/mixins/auth');

Expand Down Expand Up @@ -726,6 +758,90 @@ module.exports = {
}
},
},
mockOauth: {

async handler (ctx: any) {
try {

const secretKey = ctx.meta.request.headers?.['x-secret-key'];
if (secretKey !== process.env.SECRET_KEY) {
throw new MoleculerError(
'The secret key is invalid',
401,
'unauthorized',
{}
);
}

const { user, emails } = generateFakeUser();
const email = emails?.[0]?.email;
const username = user.login;
const [firstName, lastName] = getFirstAndLastNameFromName(user.name);
const avatar = user.avatar_url;
const profileLink = user.url;

let _user;

// Create or update user
try {
_user = await this.createOrUpdateUser({
ctx,
provider: 'mock',
user,
emails,
email,
username,
firstName,
lastName,
avatar,
profileLink,
});
} catch (error) {
console.error(error);
if (error.type === new OAuthAccountAlreadyAssociated().type) {
throw error;
}
throw new AuthGenericError();
}

// Create token
try {
const { _id, email, userKey, firstName, lastName, state, pointers } =
_user;
// use buildableId in client due to old users having user.buildableId == coreId
const buildableId = get(_user, 'client.buildableId');
const containerId = get(_user, 'client.containers[0]._id');

const token = this.createToken({
_id,
email,
username,
userKey,
buildableId,
containerId,
firstName,
lastName,
pointers,
});

return {
token,
state,
};
} catch (error) {
console.error(error);
throw new AuthGenericError();
}


}
catch (error) {
console.error(error);
throw new AuthGenericError();
}
}

},
},

methods: {
Expand Down
19 changes: 18 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"@ethersproject/bytes": "^5.7.0",
"@ethersproject/keccak256": "^5.7.0",
"@ethersproject/strings": "^5.7.0",
"@faker-js/faker": "^9.2.0",
"@kubernetes/client-node": "^0.18.1",
"@types/ws": "^8.5.4",
"analytics-node": "^6.2.0",
Expand Down Expand Up @@ -106,4 +107,4 @@
"workspaces": [
"packages/*"
]
}
}

0 comments on commit 72f0c96

Please sign in to comment.