Skip to content
This repository was archived by the owner on Jan 23, 2025. It is now read-only.

Commit 414f711

Browse files
committed
Topcoder NodeJS Get User Identity API
1 parent acdca68 commit 414f711

File tree

6 files changed

+224
-10
lines changed

6 files changed

+224
-10
lines changed

actions/user.js

Lines changed: 66 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
/*
22
* Copyright (C) 2014 TopCoder Inc., All Rights Reserved.
33
*
4-
* @version 1.0
5-
* @author muzehyun
4+
* @version 1.1
5+
* @author muzehyun, TCSASSEMBLER
6+
* Changes in 1.1:
7+
* - Implement get user identity api.
68
*/
79
'use strict';
810
var async = require('async');
@@ -134,3 +136,65 @@ exports.activateUser = {
134136
});
135137
}
136138
};
139+
140+
/**
141+
* Get user identity information api.
142+
* @param {Object} api - The api object.
143+
* @param {Object} connection - The database connection map object.
144+
* @param {Function} next - The callback function.
145+
* @since 1.1
146+
*/
147+
function getUserIdentity(api, connection, next) {
148+
var helper = api.helper, caller = connection.caller, dbConnectionMap = connection.dbConnectionMap, response;
149+
async.waterfall([
150+
function (cb) {
151+
cb(helper.checkMember(connection, 'You need login for this endpoint.'));
152+
},
153+
function (cb) {
154+
api.dataAccess.executeQuery('get_user_email_and_handle', { userId: caller.userId }, dbConnectionMap, cb);
155+
},
156+
function (rs, cb) {
157+
response = {
158+
uid: api.ldapHelper.generateLDAPUid(caller.userId),
159+
handle: rs[0].handle,
160+
email: rs[0].email
161+
};
162+
cb();
163+
}
164+
], function (err) {
165+
if (err) {
166+
helper.handleError(api, connection, err);
167+
} else {
168+
connection.response = response;
169+
}
170+
next(connection, true);
171+
});
172+
173+
}
174+
175+
/**
176+
* The API for activate user
177+
* @since 1.1
178+
*/
179+
exports.getUserIdentity = {
180+
name: 'getUserIdentity',
181+
description: 'Get user identity information',
182+
inputs: {
183+
required: [],
184+
optional: []
185+
},
186+
blockedConnectionTypes: [],
187+
outputExample: {},
188+
version: 'v2',
189+
transaction: 'read',
190+
databases: ['common_oltp'],
191+
cacheEnabled: false,
192+
run: function (api, connection, next) {
193+
if (connection.dbConnectionMap) {
194+
api.log('getUserIdentity#run', 'debug');
195+
getUserIdentity(api, connection, next);
196+
} else {
197+
api.helper.handleNoConnection(api, connection, next);
198+
}
199+
}
200+
};

apiary.apib

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2242,6 +2242,48 @@ Register a new user.
22422242
"description":"Servers are up but overloaded. Try again later."
22432243
}
22442244
2245+
## get User Identity Information [/user/identity]
2246+
### Search My Challenges [GET]
2247+
2248+
Request
2249+
2250+
+ Headers
2251+
Authorization : Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJhZHwxMzI0NTYiLCJleHAiOjEzOTM3MDM1NzEsImF1ZCI6
2252+
2253+
+ Response 200 (application/json)
2254+
2255+
{
2256+
"uid": "uid=132456, ou=members, dc=topcoder, dc=com",
2257+
"handle": "heffan",
2258+
"email": "foo@fooonyou.com"
2259+
}
2260+
2261+
+ Response 401 (application/json)
2262+
2263+
{
2264+
"name":"Unauthorized",
2265+
"value":"401",
2266+
"description":"Authentication credentials were missing or incorrect.",
2267+
"details": "You need login for this endpoint."
2268+
}
2269+
2270+
+ Response 500 (application/json)
2271+
2272+
{
2273+
"name":"Internal Server Error",
2274+
"value":"500",
2275+
"description":"Unknown server error. Please contact support."
2276+
}
2277+
2278+
+ Response 503 (application/json)
2279+
2280+
{
2281+
"name":"Service Unavailable",
2282+
"value":"503",
2283+
"description":"Servers are up but overloaded. Try again later."
2284+
}
2285+
2286+
22452287
## Validate Handle [/users/validate/{handle}]
22462288
### Validate Handle [GET]
22472289

initializers/ldapHelper.js

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
/*
33
* Copyright (C) 2013 - 2014 TopCoder Inc., All Rights Reserved.
44
*
5-
* Version: 1.3
5+
* Version: 1.4
66
* Author: TCSASSEMBLER, muzehyun, Ghost_141
77
* changes in 1.1
88
* - add retrieveMemberProfileLDAPEntry
@@ -11,6 +11,8 @@
1111
* - updateMemberPasswordLDAPEntry for update member password.
1212
* Changes in 1.3:
1313
* - add resetMemberPasswordLDAPEntry for reset member password.
14+
* Changes in 1.4:
15+
* - Add generateUserDN and generateLDAPUid method.
1416
*/
1517
"use strict";
1618

@@ -68,6 +70,16 @@ var createClient = function () {
6870
});
6971
};
7072

73+
/**
74+
* Generate user dn for ldap server.
75+
* @param {Number} userId - The user id.
76+
* @returns {String} The user id(dn) in LDAP server.
77+
* @since 1.4
78+
*/
79+
var generateUserDN = function (userId) {
80+
return 'uid=' + userId + ', ' + topcoder_member_base_dn;
81+
};
82+
7183
/**
7284
* Function used to bind a ldap server
7385
*
@@ -96,7 +108,7 @@ var bindClient = function (api, client, callback) {
96108
* @param {Function} callback - a async callback function with prototype like callback(err, results)
97109
*/
98110
var addClient = function (api, client, params, callback) {
99-
var dn = 'uid=' + params.userId + ', ' + topcoder_member_base_dn,
111+
var dn = generateUserDN(params.userId),
100112
entry = {
101113
uid: params.userId,
102114
handle: params.handle,
@@ -124,7 +136,7 @@ var addClient = function (api, client, params, callback) {
124136
* @param {Function} callback - a async callback function with prototype like callback(err, results)
125137
*/
126138
var removeClient = function (api, client, userId, callback) {
127-
var dn = 'uid=' + userId + ', ' + topcoder_member_base_dn;
139+
var dn = generateUserDN(userId);
128140
client.del(dn, function (err) {
129141
if (err) {
130142
client.unbind();
@@ -145,7 +157,7 @@ var removeClient = function (api, client, userId, callback) {
145157
* @param {Function} callback - a async callback function with prototype like callback(err, results)
146158
*/
147159
var passwordModify = function (api, client, params, callback) {
148-
var dn = 'uid=' + params.userId + ', ' + topcoder_member_base_dn,
160+
var dn = generateUserDN(params.userId),
149161
op = params.oldPassword || params.password,
150162
np = params.newPassword || params.password,
151163
writer = new Ber.Writer();
@@ -176,7 +188,7 @@ var passwordModify = function (api, client, params, callback) {
176188
* @param {Function} callback - a async callback function with prototype like callback(err, results)
177189
*/
178190
var resetPassword = function (api, client, params, callback) {
179-
var dn = 'uid=' + params.userId + ', ' + topcoder_member_base_dn,
191+
var dn = generateUserDN(params.userId),
180192
np = params.newPassword || params.password,
181193
writer = new Ber.Writer();
182194
writer.startSequence();
@@ -204,7 +216,7 @@ var resetPassword = function (api, client, params, callback) {
204216
* @param {Function} callback - a async callback function with prototype like callback(err, results)
205217
*/
206218
var modifyClient = function (api, client, params, callback) {
207-
var dn = 'uid=' + params.userId + ', ' + topcoder_member_base_dn,
219+
var dn = generateUserDN(params.userId),
208220
change = new ldap.Change({
209221
operation: 'replace',
210222
modification: {
@@ -231,7 +243,7 @@ var modifyClient = function (api, client, params, callback) {
231243
* @param {Function} callback - a async callback function with prototype like callback(err, results)
232244
*/
233245
var retrieveClient = function (api, client, params, callback) {
234-
var dn = 'uid=' + params.userId + ', ' + topcoder_member_base_dn;
246+
var dn = generateUserDN(params.userId);
235247
client.search(dn, {}, function (err, res) {
236248
if (err) {
237249
client.unbind();
@@ -270,6 +282,17 @@ var retrieveClient = function (api, client, params, callback) {
270282
*/
271283
exports.ldapHelper = function (api, next) {
272284
api.ldapHelper = {
285+
286+
/**
287+
* Function for get LDAP user id based on given userId.
288+
* @param {Number} userId - The user id.
289+
* @return the user id (dn) in LDAP server.
290+
* @since 1.4
291+
*/
292+
generateLDAPUid: function (userId) {
293+
return generateUserDN(userId);
294+
},
295+
273296
/**
274297
* Main function of addMemberProfileLDAPEntry
275298
*

routes.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* Copyright (C) 2013 - 2014 TopCoder Inc., All Rights Reserved.
33
*
4-
* @version 1.56
4+
* @version 1.57
55
* @author vangavroche, Sky_, muzehyun, kurtrips, Ghost_141, ecnu_haozi, hesibo, LazyChild, isv, flytoj2ee,
66
* @author panoptimum, bugbuka, Easyhard
77
*
@@ -132,6 +132,8 @@
132132
* - Add routes for set round components and terms.
133133
* Changes in 1.56:
134134
* - Add route for Data Science Past Challenges API.
135+
* Changes in 1.57:
136+
* - Add route for get user identity api.
135137
*/
136138
/*jslint node:true, nomen: true */
137139
"use strict";
@@ -247,6 +249,7 @@ exports.routes = {
247249
{ path: "/:apiVersion/design/download/:submissionId", action: "downloadDesignSubmission" },
248250

249251
{ path: "/:apiVersion/user/challenges", action: "getMyChallenges" },
252+
{ path: "/:apiVersion/user/identity", action: "getUserIdentity" },
250253

251254
{ path: "/:apiVersion/users/tops/:trackType", action: "getTopTrackMembers" },
252255
{ path: "/:apiVersion/users/resetToken", action: "generateResetToken" },

test/test.getUserIdentity.js

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
* Copyright (C) 2014 TopCoder Inc., All Rights Reserved.
3+
*
4+
* @version 1.0
5+
* @author TCSASSEMBLER
6+
*/
7+
'use strict';
8+
/*global describe, it, before, beforeEach, after, afterEach */
9+
/*jslint node: true, stupid: true, unparam: true */
10+
11+
/**
12+
* Module dependencies.
13+
*/
14+
var _ = require('underscore');
15+
var request = require('supertest');
16+
var assert = require('chai').assert;
17+
var async = require('async');
18+
19+
var testHelper = require('./helpers/testHelper');
20+
var API_ENDPOINT = process.env.API_ENDPOINT || 'http://localhost:8080';
21+
22+
describe('Get User Identity Information API', function () {
23+
this.timeout(180000); // The api with testing remote db could be quit slow
24+
25+
var heffan = testHelper.generateAuthHeader({ sub: 'ad|132456' });
26+
27+
/**
28+
* create a http request and test it.
29+
* @param {Number} expectStatus - the expected response status code.
30+
* @param {Object} authHeader - the auth header.
31+
* @param {Function} cb - the call back function.
32+
*/
33+
function createGetRequest(expectStatus, authHeader, cb) {
34+
var req = request(API_ENDPOINT)
35+
.get('/v2/user/identity/')
36+
.set('Accept', 'application/json')
37+
.expect('Content-Type', /json/);
38+
if (authHeader) {
39+
req.set('Authorization', authHeader);
40+
}
41+
req.expect(expectStatus)
42+
.end(cb);
43+
}
44+
45+
/**
46+
* assert the bad response.
47+
* @param {Number} expectStatus - the expect status.
48+
* @param {String} errorMessage - the expected error message.
49+
* @param {Object} authHeader - the request auth header.
50+
* @param {Function} cb - the callback function.
51+
*/
52+
function assertBadResponse(expectStatus, errorMessage, authHeader, cb) {
53+
createGetRequest(expectStatus, authHeader, function (err, result) {
54+
if (!err) {
55+
assert.equal(result.body.error.details, errorMessage, 'invalid error message');
56+
} else {
57+
cb(err);
58+
return;
59+
}
60+
cb();
61+
});
62+
}
63+
64+
/**
65+
* Test when caller is anonymous.
66+
*/
67+
it('should return unauthorized Error. The caller is anonymous.', function (done) {
68+
assertBadResponse(401, 'You need login for this endpoint.', null, done);
69+
});
70+
71+
it('should return success result.', function (done) {
72+
createGetRequest(200, heffan, function (err, result) {
73+
testHelper.assertResponse(err, result, 'test_files/expected_get_user_identity_1', done);
74+
});
75+
});
76+
77+
});
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"uid": "uid=132456, ou=members, dc=topcoder, dc=com",
3+
"handle": "heffan",
4+
"email": "foo@fooonyou.com"
5+
}

0 commit comments

Comments
 (0)