@@ -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 : {
0 commit comments