Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

how to authenticate with crowd using node.js?

fjavadpour April 10, 2018

hi, I'm new in crowd and node. I want to authenticate a user with crowd rest APIs. 

i  saw 'POST /rest/usermanagement/1/session' in the documentation but I don't know how to use it. should I install any packages like HTTP to use the crowd? anybody can help me here?

2 answers

1 vote
Bruno Vincent
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 10, 2018

Hi @fjavadpour

There is a dedicated NPM package for this: https://www.npmjs.com/package/atlassian-crowd-client

fjavadpour April 10, 2018

@Bruno Vincent I still have some problems. here are some of my code. whats the problem? I get the below error

UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 2): Error: Application Permission Denied

and this is my codes: 

var CrowdClient = require('atlassian-crowd-client');

var crowd = new CrowdClient({

baseUrl: 'http://myhost/crowd',

application: { name: 'app-name', password: 'app-pass' } });


module.exports = {

show : function(req,res) {

crowd.session.create(('username''password'), (errorsession=> {

if(error) { console.log(error); }

else {

console.log(session.token); } }) }
};

Bruno Vincent
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 10, 2018

@fjavadpour

You must add the IP address of the computer or server on which you're running that code to the list of authorized remote addresses for your application in Crowd's console: https://confluence.atlassian.com/crowd/specifying-an-application-s-address-or-hostname-25788433.html

fjavadpour April 10, 2018

@Bruno Vincent, you mean that I should add localhost there? I'm getting a new error:

 TypeError: validationFactors.toCrowd is not a function!!!

whats this for?

Bruno Vincent
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 10, 2018

You must add the IP address of the computer on which you're running the Node.js code. If Crowd and Node.js are installed on the same server/computer, yes 127.0.0.1 should work.

fjavadpour April 10, 2018

@Bruno Vincent i still get TypeError: validationFactors.toCrowd is not a function error    :(((

Bruno Vincent
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 10, 2018
var CrowdClient = require('atlassian-crowd-client');

var User = require('atlassian-crowd-client/lib/models/user');

 

// Initialize the Crowd client:

var crowd = new CrowdClient({

        baseUrl: 'http://localhost:8095/crowd/',

        application: {

                name: 'test',

                password: 'password'

        }

});

 

// Authenticate to Crowd:

crowd.session.create('john.doe', 'password').then(function (session) {

    // Display Crowd SSO token

        console.log('Crowd SSO token is ' + session.token);

    // Fetch the user profile:

    crowd.session.getUser(session.token).then(function (user) {

        console.log('Hello, ' + user.displayname);

    });

});

cwdclient1.pngcwdclient2.png

fjavadpour April 10, 2018

@Bruno Vincent and how can I get the response in JSON format?

Bruno Vincent
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 11, 2018
console.log('JSON response is ' + JSON.stringify(session));

cwdclient3.png

fjavadpour April 15, 2018

@Bruno Vincent

tnx for your answer. and I have another question that is how can I get all users using rest API? i cant' understand it well

Bruno Vincent
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 15, 2018

In Node.js:

var CrowdClient = require('atlassian-crowd-client');

var User = require('atlassian-crowd-client/lib/models/user');

 

// Initialize the Crowd client:

var crowd = new CrowdClient({

        baseUrl: 'http://localhost:8095/crowd/',

        application: {

                name: 'test',

                password: 'password'

        }

});

 

// Find all users

crowd.search.user('').then(function (users) {

        console.log('Found ' + users.length + ' users:');

        users.forEach(function(username){

                console.log(username);

        });

});

With curl:

curl -X GET -u 'test:password' -H 'Accept: application/json' 'http://localhost:8095/crowd/rest/usermanagement/1/search?entity-type=user'
fjavadpour April 15, 2018

@Bruno Vincent sorry that I ask a lot but I read Atlassian-crowd-client in npm website but I didn't see these methods like crowd.search or others. is there any link to find more info about this package usage?

Bruno Vincent
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 16, 2018

Hi @fjavadpour

I personally find the documentation pretty clear but if you want to know more about the package and the provided API I guess you should take a look at the source code.

fjavadpour April 17, 2018

@Bruno Vincent and can you help me how to connect my crowd by the API? I saw POST /rest/usermanagement/1/user in the documentation but I don't know how should I use it. i'm using angular 5 by the way

Bruno Vincent
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 17, 2018

It depends on what you want to do. Crowd's REST API is fully explained and detailed here: https://docs.atlassian.com/atlassian-crowd/3.1.3/REST/

fjavadpour April 17, 2018

 @Bruno Vincentassume that I want to add a user using angular 5 and node.  I saw POST /rest/usermanagement/1/user in the documentation but how should I use it in my HTTP services? I don't know what URL I should send a request to? what should the URL contain?

in my services I use :

return http.post('URL').map(res => res.json()) ;

but I don't know the correct URL for connecting crowd.

Bruno Vincent
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 17, 2018

The URI structure is explained in the first paragraph of Atlassian's documentation. Thus your URL should be something like:

http://localhost:8095/crowd/rest/usermanagement/1/user

But you should NOT request Crowd's REST endpoint with Javascript code running in your browser as this would require to embed your application's name and password within that code.

Call your Crowd server from the server side (Nodejs) using the NPM package we've already discussed. Adding a user is the very first example provided on the package's documentation page:

crowd.user.create(new User('John', 'Doe', 'John Doe', 'johndoe@example.com', 'johndoe', 'secret'));
0 votes
温静
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
October 31, 2019

Hi, I'm new. I want to use crowdsrest API to authenticate users. I want to get the user's group. Can someone help me?

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events