@tiledesk/tiledesk-server 2.10.36 → 2.10.38

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -5,6 +5,10 @@
5
5
  🚀 IN PRODUCTION 🚀
6
6
  (https://www.npmjs.com/package/@tiledesk/tiledesk-server/v/2.3.77)
7
7
 
8
+ # 2.10.38
9
+ - updated whatsapp-worker to 0.1.11
10
+ - added index to request model
11
+
8
12
  # 2.10.36
9
13
  - updated tybot-connector to 0.2.138
10
14
 
@@ -72,6 +72,14 @@ if (process.env.GOOGLE_SIGNIN_ENABLED=="true" || process.env.GOOGLE_SIGNIN_ENABL
72
72
  winston.info('Authentication Google Signin enabled : ' + enableGoogleSignin);
73
73
 
74
74
 
75
+
76
+ var enableOauth2Signin = false;
77
+ if (process.env.OAUTH2_SIGNIN_ENABLED=="true" || process.env.OAUTH2_SIGNIN_ENABLED == true) {
78
+ enableOauth2Signin = true;
79
+ }
80
+ winston.info('Authentication Oauth2 Signin enabled : ' + enableOauth2Signin);
81
+
82
+
75
83
  var jwthistory = undefined;
76
84
  try {
77
85
  jwthistory = require('@tiledesk-ent/tiledesk-server-jwthistory');
@@ -573,6 +581,248 @@ if (enableGoogleSignin==true) {
573
581
  }
574
582
 
575
583
 
584
+ if (enableOauth2Signin==true) {
585
+
586
+ const OAuth2Strategy = require('passport-oauth2');
587
+ OAuth2Strategy.prototype.userProfile = function(accessToken, done) {
588
+
589
+ winston.debug("accessToken " + accessToken)
590
+
591
+
592
+ /*
593
+ https://stackoverflow.com/questions/66452108/keycloak-get-users-returns-403-forbidden
594
+ The service account associated with your client needs to be allowed to view the realm users.
595
+ Go to http://localhost:8080/auth/admin/{realm_name}/console/#/realms/{realm_name}/clients
596
+ Select your client (which must be a confidential client)
597
+ In the settings tab, switch Service Account Enabled to ON
598
+ Click on save, the Service Account Roles tab will appear
599
+ In Client Roles, select realm_management
600
+ Scroll through available roles until you can select view_users
601
+ Click on Add selected
602
+ You should have something like this :
603
+ */
604
+
605
+
606
+ // ATTENTION You have to add a client scope after as described here: https://keycloak.discourse.group/t/issue-on-userinfo-endpoint-at-keycloak-20/18461/4
607
+
608
+ // console.log("this._oauth2", this._oauth2)
609
+ this._oauth2._useAuthorizationHeaderForGET = true;
610
+ this._oauth2.get( process.env.OAUTH2_USER_INFO_URL, accessToken, (err, body) => {
611
+ if (err) {
612
+ return done(err);
613
+ }
614
+
615
+ try {
616
+ winston.debug("body", body);
617
+
618
+ const json = JSON.parse(body);
619
+ const userInfo = {
620
+ keycloakId: json.sub,
621
+ fullName: json.name,
622
+ firstName: json.given_name,
623
+ lastName: json.family_name,
624
+ username: json.preferred_username,
625
+ email: json.email,
626
+ // avatar: json.avatar,
627
+ // realm: this.options.realm,
628
+ };
629
+ winston.debug("userInfo", userInfo);
630
+
631
+ done(null, userInfo);
632
+ } catch (e) {
633
+ done(e);
634
+ }
635
+ });
636
+ };
637
+
638
+
639
+ passport.use(new OAuth2Strategy({
640
+ authorizationURL: process.env.OAUTH2_AUTH_URL,
641
+ tokenURL: process.env.OAUTH2_TOKEN_URL,
642
+ clientID: process.env.OAUTH2_CLIENT_ID,
643
+ clientSecret: process.env.OAUTH2_CLIENT_SECRET,
644
+ callbackURL: process.env.OAUTH2_CALLBACK_URL || "http://localhost:3000/auth/oauth2/callback"
645
+ },
646
+ function(accessToken, refreshToken, params, profile, cb) {
647
+ winston.debug("params", params);
648
+
649
+
650
+ const token = jwt.decode(accessToken); // user id lives in here
651
+ winston.debug("token", token);
652
+
653
+ const profileInfo = jwt.decode(params.id_token); // user email lives in here
654
+ winston.debug("profileInfo", profileInfo);
655
+
656
+ winston.debug("profile", profile);
657
+
658
+ winston.debug("accessToken", accessToken);
659
+
660
+ winston.debug("refreshToken", refreshToken);
661
+
662
+ var issuer = token.iss;
663
+ var email = profile.email;
664
+
665
+ var query = {providerId : issuer, subject: profile.keycloakId};
666
+ winston.debug("query", query)
667
+
668
+ Auth.findOne(query, function(err, cred){
669
+ winston.debug("cred", cred, err);
670
+ if (err) { return cb(err); }
671
+ if (!cred) {
672
+ // The oauth account has not logged in to this app before. Create a
673
+ // new user record and link it to the oauth account.
674
+ var password = uniqid()
675
+ // signup ( email, password, firstname, lastname, emailverified) {
676
+ userService.signup(email, password, profile.displayName, "", true)
677
+ .then(function (savedUser) {
678
+
679
+ winston.debug("savedUser", savedUser)
680
+
681
+ var auth = new Auth({
682
+ providerId: issuer,
683
+ email: email,
684
+ subject: profile.keycloakId,
685
+ });
686
+ auth.save(function (err, authSaved) {
687
+ if (err) { return cb(err); }
688
+ winston.debug("authSaved", authSaved);
689
+
690
+ return cb(null, savedUser);
691
+ });
692
+ }).catch(function(err) {
693
+ winston.error("Error signup oauth ", err);
694
+ return cb(err);
695
+ });
696
+ } else {
697
+ // The Oauth account has previously logged in to the app. Get the
698
+ // user record linked to the Oauth account and log the user in.
699
+
700
+ User.findOne({
701
+ email: email, status: 100
702
+ }, 'email firstname lastname emailverified id', function (err, user) {
703
+
704
+ winston.debug("user",user, err);
705
+ // winston.debug("usertoJSON()",user.toJSON());
706
+
707
+ if (err) {
708
+ winston.error("Error getting user",user, err);
709
+ return cb(err);
710
+ }
711
+
712
+ if (!user) {
713
+ winston.info("User not found",user, err);
714
+ return cb(null, false);
715
+ }
716
+
717
+ return cb(null, user);
718
+ });
719
+ }
720
+ });
721
+ }
722
+ ));
723
+ }
724
+
725
+
726
+
727
+ // const KeycloakStrategy = require('@exlinc/keycloak-passport')
728
+
729
+
730
+ // // Register the strategy with passport
731
+ // passport.use(
732
+ // "keycloak",
733
+ // new KeycloakStrategy(
734
+ // {
735
+ // host: process.env.KEYCLOAK_HOST,
736
+ // realm: process.env.KEYCLOAK_REALM,
737
+ // clientID: process.env.KEYCLOAK_CLIENT_ID,
738
+ // clientSecret: process.env.KEYCLOAK_CLIENT_SECRET,
739
+ // callbackURL: `${process.env.AUTH_KEYCLOAK_CALLBACK}`,
740
+ // authorizationURL : `${process.env.KEYCLOAK_HOST}/realms/${process.env.KEYCLOAK_REALM}/protocol/openid-connect/auth`,
741
+ // tokenURL : `${process.env.KEYCLOAK_HOST}/realms/${process.env.KEYCLOAK_REALM}/protocol/openid-connect/token`,
742
+ // userInfoURL : `${process.env.KEYCLOAK_HOST}/realms/${process.env.KEYCLOAK_REALM}/protocol/openid-connect/userinfo`
743
+ // // authorizationURL: '123',
744
+ // // tokenURL : '123',
745
+ // // userInfoURL: '123'
746
+ // },
747
+ // (accessToken, refreshToken, profile, done) => {
748
+
749
+
750
+ // const token = jwt.decode(accessToken); // user id lives in here
751
+ // console.log("token", token);
752
+
753
+ // console.log("profile", profile);
754
+
755
+ // console.log("accessToken", accessToken);
756
+
757
+ // console.log("refreshToken", refreshToken);
758
+
759
+ // var issuer = token.iss;
760
+ // var email = profile.email;
761
+
762
+ // var query = {providerId : issuer, subject: profile.keycloakId};
763
+ // winston.info("query", query)
764
+
765
+ // Auth.findOne(query, function(err, cred){
766
+ // winston.info("cred", cred, err);
767
+ // if (err) { return cb(err); }
768
+ // if (!cred) {
769
+ // // The oauth account has not logged in to this app before. Create a
770
+ // // new user record and link it to the oauth account.
771
+ // var password = uniqid()
772
+ // // signup ( email, password, firstname, lastname, emailverified) {
773
+ // userService.signup(email, password, profile.displayName, "", true)
774
+ // .then(function (savedUser) {
775
+
776
+ // winston.info("savedUser", savedUser)
777
+
778
+ // var auth = new Auth({
779
+ // providerId: issuer,
780
+ // email: email,
781
+ // subject: profile.keycloakId,
782
+ // });
783
+ // auth.save(function (err, authSaved) {
784
+ // if (err) { return cb(err); }
785
+ // winston.info("authSaved", authSaved);
786
+
787
+ // return cb(null, savedUser);
788
+ // });
789
+ // }).catch(function(err) {
790
+ // winston.error("Error signup oauth ", err);
791
+ // return cb(err);
792
+ // });
793
+ // } else {
794
+ // // The Oauth account has previously logged in to the app. Get the
795
+ // // user record linked to the Oauth account and log the user in.
796
+
797
+ // User.findOne({
798
+ // email: email, status: 100
799
+ // }, 'email firstname lastname emailverified id', function (err, user) {
800
+
801
+ // winston.info("user",user, err);
802
+ // winston.info("usertoJSON()",user.toJSON());
803
+
804
+ // if (err) {
805
+ // winston.error("Error getting user",user, err);
806
+ // return cb(err);
807
+ // }
808
+
809
+ // if (!user) {
810
+ // winston.info("User not found",user, err);
811
+ // return cb(null, false);
812
+ // }
813
+
814
+ // return done(null, user);
815
+ // });
816
+ // }
817
+ // });
818
+ // }
819
+ // ));
820
+
821
+
822
+
823
+
824
+
825
+
576
826
 
577
827
  // var OidcStrategy = require('passport-openidconnect').Strategy;
578
828
 
package/models/request.js CHANGED
@@ -504,6 +504,7 @@ RequestSchema.index({ id_project: 1, preflight: 1, createdAt: 1 })
504
504
  RequestSchema.index({ participants: 1, id_project: 1, createdAt: -1, status: 1 })
505
505
  RequestSchema.index({ id_project: 1, "snapshot.lead.email": 1, createdAt: -1, status: 1 })
506
506
  RequestSchema.index({ id_project: 1, createdAt: -1, status: 1 })
507
+ RequestSchema.index({ id_project: 1, preflight: 1, smartAssignment: 1, "snapshot.department.routing": 1, createdAt: 1, status: 1 })
507
508
 
508
509
  // ERROR DURING DEPLOY OF 2.10.27
509
510
  //RequestSchema.index({ id_project: 1, participants: 1, "snapshot.agents.id_user": 1, createdAt: -1, status: 1 })
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@tiledesk/tiledesk-server",
3
3
  "description": "The Tiledesk server module",
4
- "version": "2.10.36",
4
+ "version": "2.10.38",
5
5
  "scripts": {
6
6
  "start": "node ./bin/www",
7
7
  "pretest": "mongodb-runner start",
@@ -50,11 +50,12 @@
50
50
  "@tiledesk/tiledesk-train-jobworker": "^0.0.11",
51
51
  "@tiledesk/tiledesk-tybot-connector": "^0.2.138",
52
52
  "@tiledesk/tiledesk-whatsapp-connector": "^0.1.75",
53
- "@tiledesk/tiledesk-whatsapp-jobworker": "^0.0.10",
53
+ "@tiledesk/tiledesk-whatsapp-jobworker": "^0.0.11",
54
54
  "@tiledesk/tiledesk-sms-connector": "^0.1.11",
55
55
  "@tiledesk/tiledesk-vxml-connector": "^0.1.67",
56
56
  "@tiledesk/tiledesk-voice-twilio-connector": "^0.1.12",
57
57
  "@tiledesk/tiledesk-multi-worker": "^0.1.6",
58
+ "passport-oauth2": "^1.8.0",
58
59
  "amqplib": "^0.5.5",
59
60
  "app-root-path": "^3.0.0",
60
61
  "bcrypt-nodejs": "0.0.3",
package/routes/auth.js CHANGED
@@ -770,6 +770,97 @@ router.get("/google/callback", passport.authenticate("google", { session: false
770
770
 
771
771
  }
772
772
  );
773
+
774
+
775
+
776
+ router.get("/oauth2", function(req,res,next){
777
+ winston.debug("redirect_url: "+ req.query.redirect_url );
778
+ req.session.redirect_url = req.query.redirect_url;
779
+
780
+ winston.debug("forced_redirect_url: "+ req.query.forced_redirect_url );
781
+ req.session.forced_redirect_url = req.query.forced_redirect_url;
782
+
783
+ passport.authenticate(
784
+ 'oauth2'
785
+ )(req,res,next);
786
+ });
787
+
788
+ // router.get('/oauth2',
789
+ // passport.authenticate('oauth2'));
790
+
791
+ router.get('/oauth2/callback',
792
+ passport.authenticate('oauth2', { session: false}),
793
+ function(req, res) {
794
+ winston.debug("'/oauth2/callback: ");
795
+
796
+ var user = req.user;
797
+ winston.debug("user", user);
798
+ winston.debug("req.session.redirect_url: "+ req.session.redirect_url);
799
+
800
+
801
+ var userJson = user.toObject();
802
+
803
+ delete userJson.password;
804
+
805
+
806
+ var signOptions = {
807
+ issuer: 'https://tiledesk.com',
808
+ subject: 'user',
809
+ audience: 'https://tiledesk.com',
810
+ jwtid: uuidv4()
811
+
812
+ };
813
+
814
+ var alg = process.env.GLOBAL_SECRET_ALGORITHM;
815
+ if (alg) {
816
+ signOptions.algorithm = alg;
817
+ }
818
+
819
+
820
+ var token = jwt.sign(userJson, configSecret, signOptions); //priv_jwt pp_jwt
821
+
822
+
823
+ // return the information including token as JSON
824
+ // res.json(returnObject);
825
+
826
+ let dashboard_base_url = process.env.EMAIL_BASEURL || config.baseUrl;
827
+ winston.debug("Google Redirect dashboard_base_url: ", dashboard_base_url);
828
+
829
+ let homeurl = "/#/";
830
+
831
+ if (req.session.redirect_url) {
832
+ homeurl = req.session.redirect_url;
833
+ }
834
+
835
+ var url = dashboard_base_url+homeurl+"?token=JWT "+token;
836
+
837
+ if (req.session.forced_redirect_url) {
838
+ url = req.session.forced_redirect_url+"?jwt=JWT "+token; //attention we use jwt= (ionic) instead token=(dashboard) for ionic
839
+ }
840
+
841
+ winston.debug("Google Redirect: "+ url);
842
+
843
+ res.redirect(url);
844
+
845
+
846
+
847
+ });
848
+
849
+ router.get(
850
+ "/keycloak",
851
+ passport.authenticate("keycloak")
852
+ );
853
+ router.get(
854
+ "/keycloak/callback",
855
+ passport.authenticate("keycloak"),
856
+ function(req, res) {
857
+ winston.info("'/keycloak/callback: ");
858
+ // Successful authentication, redirect home.
859
+ res.redirect('/');
860
+ }
861
+ );
862
+
863
+
773
864
  // profile route after successful sign in</em>
774
865
  // router.get("/profile", (req, res) => {
775
866
  // console.log(req);