Commits

Andreas Knecht committed 2826d23

Retrieving group memberships now also works!

  • Participants
  • Parent commits a912477

Comments (0)

Files changed (3)

examples/login/app.js

 passport.use(new AtlassianCrowdStrategy({
         crowdServer:"http://localhost:2990/jira",
         crowdApplication:"nodejs",
-        crowdApplicationPassword:"password"
+        crowdApplicationPassword:"password",
+        retrieveGroupMemberships:true
     },
     function (userprofile, done) {
         // asynchronous verification, for effect...

examples/login/views/account.ejs

 <p>Username: <%= user.username %></p>
 <p>Email: <%= user.email %></p>
 
+<% if(user.groups) { %>
+<div>
+    <p>Groups:</p>
+    <ul>
+    <% for(var i = 0; i<user.groups.length; i++) { %>
+        <li><%= user.groups[i] %></li>
+    <% } %>
+    </ul>
+</div>
+<% } %>

lib/passport-atlassian-crowd/strategy.js

     passport.Strategy.call(this);
     this.name = 'atlassian-crowd';
     this._verify = verify;
-    this._passReqToCallback = options.passReqToCallback;
+    this._retrieveGroupMemberships = options.retrieveGroupMemberships;
 }
 
 /**
         self.success(user, info);
     }
 
-    function passBackControl(response, result) {
+    function handleGroupResponse(response, result) {
+        if(response.statusCode !== 200) {
+            return self.fail();
+        } else {
+            var resultObject = JSON.parse(result);
+            var groups =[];
+            resultObject.groups.forEach(function(group) {
+                groups.push(group.GroupEntity.name);
+            });
+
+            return groups;
+        }
+    }
+
+    function handleAuthenticationResponse(response, result) {
         var resultObject = JSON.parse(result);
         console.log("Got Status [" + response.statusCode + "]: " + JSON.stringify(resultObject));
         if (response.statusCode !== 200) {
                 id:resultObject.name,
                 username:resultObject.name,
                 displayName:resultObject["display-name"],
-                name: {
+                name:{
                     familyName:resultObject["last-name"],
                     givenName:resultObject["first-name"]
                 },
-                email: resultObject.email,
-                emails: [
+                email:resultObject.email,
+                emails:[
                     {value:resultObject.email}
                 ],
                 _raw:result,
                 _json:resultObject
             };
-            return self._verify(userprofile, verified);
+
+            if (self._retrieveGroupMemberships) {
+                var groupResult = "";
+                http_library.get({
+                    host:parsedUrl.hostname,
+                    port:parsedUrl.port,
+                    path:parsedUrl.pathname + "/rest/usermanagement/latest/user/group/direct?username=" + username,
+                    headers:{
+                        "Content-Type":"application/json",
+                        "Accept":"application/json",
+                        "Authorization":applicationAuth
+                    }
+                }, function (response) {
+                    response.on("data", function (chunk) {
+                        groupResult += chunk;
+                    });
+                    response.on("close", function (err) {
+                        userprofile.groups = handleGroupResponse(response, groupResult);
+                        return self._verify(userprofile, verified);
+                    });
+                    response.addListener("end", function () {
+                        userprofile.groups = handleGroupResponse(response, groupResult);
+                        return self._verify(userprofile, verified);
+                    });
+                });
+            } else {
+                return self._verify(userprofile, verified);
+            }
         }
     }
 
             result += chunk;
         });
         response.on("close", function (err) {
-            passBackControl(response, result);
+            handleAuthenticationResponse(response, result);
         });
         response.addListener("end", function () {
-            passBackControl(response, result);
+            handleAuthenticationResponse(response, result);
         });
     });
     crowdRequest.write(postData);