@@ -8,6 +8,38 @@ import axios from 'axios';
88import { get , omit , pick , uniqBy } from 'lodash' ;
99import DbService from 'moleculer-db' ;
1010import MongoDBAdapter from 'moleculer-db-adapter-mongo' ;
11+ import { faker } from '@faker-js/faker' ;
12+
13+ function generateFakeUser ( ) {
14+ const firstName = faker . person . firstName ( ) ;
15+ const lastName = faker . person . lastName ( ) ;
16+ const username = faker . internet . userName ( { firstName, lastName } ) ;
17+ const email = faker . internet . email ( { firstName, lastName } ) ;
18+
19+ const user = {
20+ login : username ,
21+ id : faker . number . int ( { min : 100000000 , max : 999999999 } ) ,
22+ node_id : `U_kgDOC${ faker . string . alphanumeric ( 6 ) } ` ,
23+ avatar_url : faker . image . avatar ( ) ,
24+ gravatar_id : '' ,
25+ url : `https://api.github.com/users/${ username } ` ,
26+ type : 'User' ,
27+ user_view_type : 'private' ,
28+ site_admin : false ,
29+ name : `${ firstName } ${ lastName } ` ,
30+ } ;
31+
32+ const emails = [
33+ {
34+ email : email ,
35+ primary : true ,
36+ verified : true ,
37+ visibility : 'public'
38+ }
39+ ] ;
40+
41+ return { user, emails } ;
42+ }
1143
1244const Auth = require ( '@libs-private/service-logic/mixins/auth' ) ;
1345
@@ -726,6 +758,90 @@ module.exports = {
726758 }
727759 } ,
728760 } ,
761+ mockOauth : {
762+
763+ async handler ( ctx : any ) {
764+ try {
765+
766+ const secretKey = ctx . meta . request . headers ?. [ 'x-secret-key' ] ;
767+ if ( secretKey !== process . env . SECRET_KEY ) {
768+ throw new MoleculerError (
769+ 'The secret key is invalid' ,
770+ 401 ,
771+ 'unauthorized' ,
772+ { }
773+ ) ;
774+ }
775+
776+ const { user, emails } = generateFakeUser ( ) ;
777+ const email = emails ?. [ 0 ] ?. email ;
778+ const username = user . login ;
779+ const [ firstName , lastName ] = getFirstAndLastNameFromName ( user . name ) ;
780+ const avatar = user . avatar_url ;
781+ const profileLink = user . url ;
782+
783+ let _user ;
784+
785+ // Create or update user
786+ try {
787+ _user = await this . createOrUpdateUser ( {
788+ ctx,
789+ provider : 'mock' ,
790+ user,
791+ emails,
792+ email,
793+ username,
794+ firstName,
795+ lastName,
796+ avatar,
797+ profileLink,
798+ } ) ;
799+ } catch ( error ) {
800+ console . error ( error ) ;
801+ if ( error . type === new OAuthAccountAlreadyAssociated ( ) . type ) {
802+ throw error ;
803+ }
804+ throw new AuthGenericError ( ) ;
805+ }
806+
807+ // Create token
808+ try {
809+ const { _id, email, userKey, firstName, lastName, state, pointers } =
810+ _user ;
811+ // use buildableId in client due to old users having user.buildableId == coreId
812+ const buildableId = get ( _user , 'client.buildableId' ) ;
813+ const containerId = get ( _user , 'client.containers[0]._id' ) ;
814+
815+ const token = this . createToken ( {
816+ _id,
817+ email,
818+ username,
819+ userKey,
820+ buildableId,
821+ containerId,
822+ firstName,
823+ lastName,
824+ pointers,
825+ } ) ;
826+
827+ return {
828+ token,
829+ state,
830+ } ;
831+ } catch ( error ) {
832+ console . error ( error ) ;
833+ throw new AuthGenericError ( ) ;
834+ }
835+
836+
837+ }
838+ catch ( error ) {
839+ console . error ( error ) ;
840+ throw new AuthGenericError ( ) ;
841+ }
842+ }
843+
844+ } ,
729845 } ,
730846
731847 methods : {
0 commit comments