1+ function ( user , context , callback ) {
2+ if ( context . clientID === configuration . CLIENT_ACCOUNTS_LOGIN ) { // client/application specific
3+ global . AUTH0_CLAIM_NAMESPACE = "https://" + configuration . DOMAIN + "/" ;
4+ const _ = require ( 'lodash' ) ;
5+ console . log ( "Enter Rule: Enterprise-User-Registration" ) ;
6+
7+ const baseApiUrl = "https://api." + configuration . DOMAIN + "/v3" ;
8+ //console.log("register user rule executed- user", user);
9+ //console.log("register user rule executed - context", context);
10+
11+ const isEnterprise = ( _ . get ( user , "identities[0].provider" ) !== 'auth0' ) &&
12+ ! ( _ . get ( user , "identities[0].isSocial" ) ) ? true : false ;
13+
14+ console . log ( "Is enterprise login: " , isEnterprise ) ;
15+ if ( isEnterprise ) {
16+ let provider = _ . get ( user , "identities[0].connection" ) ;
17+ const providerType = _ . get ( user , "identities[0].provider" ) ;
18+ let userId = _ . get ( user , "identities[0].user_id" ) ;
19+ userId = userId . substring ( userId . lastIndexOf ( '|' ) + 1 ) ;
20+
21+ let handle = _ . get ( user , "nickname" , "" ) ;
22+ const lastName = _ . get ( user , "family_name" ) ;
23+ const firstName = _ . get ( user , "given_name" ) ;
24+ const email = _ . get ( user , "email" ) ;
25+ //const emailVerified = _.get(user, "email_verified", true);
26+ const name = _ . get ( user , "name" ) ;
27+
28+ let isoAlpha2Code = _ . get ( context , "request.geoip.country_code" ) ;
29+ let isoAlpha3Code = _ . get ( context , "request.geoip.country_code3" ) ;
30+ let countryCode = _ . get ( context , "request.geoip.country_name" ) ;
31+ let regSource = _ . get ( context , "request.query.regSource" , null ) ;
32+ let retUrl = _ . get ( context , "request.query.returnUrl" , null ) ;
33+ let utmSource = _ . get ( context , "request.query.utmSource" , null ) ;
34+ let utmMedium = _ . get ( context , "request.query.utmMedium" , null ) ;
35+ let utmCampaign = _ . get ( context , "request.query.utmCampaign" , null ) ;
36+
37+ const resourcePath = '/identityproviders?filter=handle=' + email ;
38+ const afterActivationURL = configuration . DEFAULT_AFTER_ACTIVATION_URL ;
39+ const hostName = _ . get ( context , "request.hostname" , null ) ;
40+ const registrationCompletetUrl = "https://" + hostName + "/continue" ;
41+ //const userHandleRedirectUrl = configuration.CUSTOM_PAGES_BASE_URL + '/signup.html?source='+ utmSource + '&formAction=' + registrationCompletetUrl;
42+ const userHandleRedirectUrl = configuration . CUSTOM_PAGES_BASE_URL +
43+ "/signup.html?regSource=" + regSource +
44+ "&firstName=" + encodeURIComponent ( firstName ) +
45+ "&lastName=" + encodeURIComponent ( lastName ) +
46+ "&utmSource=" + encodeURIComponent ( utmSource ) +
47+ "&utmMedium=" + encodeURIComponent ( utmMedium ) +
48+ "&utmCampaign=" + encodeURIComponent ( utmCampaign ) +
49+ "&formAction=" + registrationCompletetUrl +
50+ "&returnUrl=" + retUrl ;
51+
52+ console . log ( "provider" , provider , email ) ;
53+ try {
54+ request . get ( {
55+ url : baseApiUrl + resourcePath
56+ } , function ( err , response , body ) {
57+ console . log ( "Enterprise user check - responseBody" , body ) ;
58+
59+ if ( err ) {
60+ console . log ( "Enterprise validation error:" , err ) ;
61+ }
62+
63+ /**
64+ * check if enterprise profile is valid for our TC database
65+ */
66+ /*
67+ Aug 2021 adding new wipro-sso connection with name wipro_azuread
68+ */
69+ if ( _ . includes ( [ configuration . WIPRO_SSO_AZURE_AD_CONNECTION_NAME ] , provider ) ) {
70+ provider = configuration . WIPRO_SSO_ADFS_CONNECTION_NAME ;
71+ }
72+
73+ let isSSOUserExist = ( _ . get ( JSON . parse ( body ) , "result.content.name" ) === provider ) ?true : false ;
74+
75+ console . log ( "Enterprise customer alreday available:" , isSSOUserExist ) ;
76+
77+ if ( ! isSSOUserExist ) {
78+ console . log ( "register enterprise user." ) ;
79+ if ( context . protocol === "redirect-callback" ) {
80+ // User was redirected to the /continue endpoint
81+ console . log ( "print data" , context , user ) ;
82+ console . log ( "get user extra data from query param" ) ;
83+ handle = _ . get ( context , "request.query.handle" , handle ) ;
84+ const countryStr = _ . get ( context , "request.query.country" , null ) ;
85+ const countryObj = JSON . parse ( countryStr ) ;
86+ if ( countryObj ) {
87+ countryCode = _ . get ( countryObj , "code" , countryCode ) ;
88+ isoAlpha2Code = _ . get ( countryObj , "alpha2" , isoAlpha2Code ) ;
89+ isoAlpha3Code = _ . get ( countryObj , "alpha3" , isoAlpha3Code ) ;
90+ }
91+ utmSource = _ . get ( context , "request.query.source" , utmSource ) ;
92+ utmMedium = _ . get ( context , "request.query.utmMedium" , utmMedium ) ;
93+ utmCampaign = _ . get ( context , "request.query.utmCampaign" , utmCampaign ) ;
94+ } else {
95+ console . log ( 'Redirect to choose user handle page.' ) ;
96+ context . redirect = {
97+ url : userHandleRedirectUrl
98+ } ;
99+ return callback ( null , user , context ) ;
100+ }
101+ // Enterprise profile will be active default
102+ let data = {
103+ "param" : {
104+ "handle" : handle ,
105+ "firstName" : firstName ,
106+ "lastName" : lastName ,
107+ "email" : email ,
108+ "country" : {
109+ "code" : countryCode ,
110+ "isoAlpha3Code" : isoAlpha3Code ,
111+ "isoAlpha2Code" : isoAlpha2Code
112+ } ,
113+ "utmSource" : utmSource ,
114+ "utmMedium" : utmMedium ,
115+ "utmCampaign" : utmCampaign ,
116+ "active" : true ,
117+ "profile" : {
118+ "name" : name ,
119+ "email" : email ,
120+ "providerType" : providerType ,
121+ "provider" : provider ,
122+ "userId" : userId
123+ }
124+ } ,
125+ "options" : {
126+ "afterActivationURL" : afterActivationURL
127+ }
128+ } ;
129+ request . post ( {
130+ url : "https://api." + configuration . DOMAIN + "/v3/users" ,
131+ json : data
132+ } , function ( error , response , body ) {
133+ if ( response . statusCode !== 200 ) {
134+ console . log ( "Enterprise registration error" , error ) ;
135+ }
136+ // on success
137+ return callback ( null , user , context ) ;
138+ //if (response.statusCode === 401) return callback();
139+ } ) ;
140+ } else { // valid social user if block end
141+ return callback ( null , user , context ) ;
142+ }
143+ }
144+ ) ; // end validatesocial request
145+ } catch ( e ) {
146+ console . log ( `Error in calling validate enterprise user ${ e } ` ) ;
147+ return callback ( null , user , context ) ;
148+ }
149+ } else { // end isSocial if-block
150+ console . log ( "existing from Enterprise-User-Registration rule." ) ;
151+ return callback ( null , user , context ) ;
152+ }
153+ } else { // END client-id check
154+ return callback ( null , user , context ) ;
155+ }
156+ }
0 commit comments