It's all about the answers!

Ask a question

Nodejs: Authentification with Jazz RTC failed


Amadou Beye (111) | asked Nov 30 '18, 6:53 a.m.

 I'm trying to sign in with jazz rtc api but I always get  302 FOUND


Acually I'm converting a java project to Nodejs. On java the authentification works great with this:

HttpPost postFormRequest = new HttpPost(host + "/auth/j_security_check");
List<BasicNameValuePair> nvps = new ArrayList<BasicNameValuePair>();
nvps.add(new BasicNameValuePair("j_username", rtcLogin));
nvps.add(new BasicNameValuePair("j_password", rtcPassword));
postFormRequest.setEntity(new UrlEncodedFormEntity(nvps, "UTF-8"));
HttpResponse postFormResponse = client.execute(postFormRequest);
    

Now in Nodejs I'm trying this:

const host = 'https://host.com/ccm';
const rtcAuthPath = '/auth/j_security_check';
const credentials = { j_username: 'login', j_password: '**' };
const authOptions = {
    url: host + rtcAuthPath,
    body: JSON.stringify(credentials)
};

function auth(error, response, body){ log(response); }

request.post(authOptions, auth);

These two pieces of code seems to do the same thing but only the java one works.
What's wrong with the code in Nodejs ?
Note that host have same value in java and nodejs

3 answers



permanent link
Ralph Schoon (63.1k33645) | answered Nov 30 '18, 10:23 a.m.
FORUM ADMINISTRATOR / FORUM MODERATOR / JAZZ DEVELOPER

 You should ask questions like this on stackoverflow. This is a programming question and does not have anything to do with the tools as far as I know.


Your nodeJS does at least not properly encode the URL and data. See the Java code:
postFormRequest.setEntity(new UrlEncodedFormEntity(nvps, "UTF-8"));
    


permanent link
Leonardo Schuler (113) | answered Oct 13 '20, 4:05 p.m.

Were you able to solve this issue? I am having the same problem with node js, I am able to login using curl commands , but nodejs is getting authentication fail


Comments
Amadou Beye commented Oct 13 '20, 4:32 p.m.

Hi Leonardo,


please check my new answer. I solve it 


permanent link
Amadou Beye (111) | answered Oct 13 '20, 4:27 p.m.
edited Oct 13 '20, 4:31 p.m.

 I finally found a solution:


let identityOptions = {
    url: rtcHost + '/authenticated/identity',
    jar: request.jar()
}
const authentificationOptions = {
    form = formurlencoded({ j_username: loginj_password: password }, 'utf8'),
    followAllRedirects: true,  // for FORM based authentication
    strictSSL: false,
    url: rtcHost + '/jts/j_security_check',
    jar: request.jar(),
    headers: {
        'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8',
        'X-Requested-With': 'XMLHttpRequest'
    }
};
return new Promise(function (resolvereject) {
    request.post(identityOptionsfunction (errorresponsebody) {
        // User is authenticated, then return OK
        if (body && JSON.stringify(body).includes(login)) return resolve(200);
        // User should authenticate
        request.post(authentificationOptionsfunction (errorresponsebody) {
            if (body && JSON.stringify(body).includes(login.toUpperCase())) {
                return resolve(200);
            }
            return reject(response ? response.statusCode : 400);
        });
    });
});

Your answer


Register or to post your answer.