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

Commit 2f5fba8

Browse files
committed
Merge branch 'dev' of https://github.com/cloudspokes/tc-api into thabo-I-129863-new-registration-emails
2 parents b680134 + 034471f commit 2f5fba8

File tree

6 files changed

+223
-9
lines changed

6 files changed

+223
-9
lines changed

actions/user.js

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
/*
22
* Copyright (C) 2014 TopCoder Inc., All Rights Reserved.
33
*
4-
* @version 1.1
4+
* @version 1.2
55
* @author muzehyun, Ghost_141
66
* Changes in 1.1:
77
* - Implement user activation email api.
8+
* Changes in 1.2:
9+
* - Implement get user identity api.
810
*/
911
'use strict';
1012
var async = require('async');
@@ -258,3 +260,65 @@ exports.userActivationEmail = {
258260
}
259261
}
260262
};
263+
264+
/**
265+
* Get user identity information api.
266+
* @param {Object} api - The api object.
267+
* @param {Object} connection - The database connection map object.
268+
* @param {Function} next - The callback function.
269+
* @since 1.2
270+
*/
271+
function getUserIdentity(api, connection, next) {
272+
var helper = api.helper, caller = connection.caller, dbConnectionMap = connection.dbConnectionMap, response;
273+
async.waterfall([
274+
function (cb) {
275+
cb(helper.checkMember(connection, 'You need login for this endpoint.'));
276+
},
277+
function (cb) {
278+
api.dataAccess.executeQuery('get_user_email_and_handle', { userId: caller.userId }, dbConnectionMap, cb);
279+
},
280+
function (rs, cb) {
281+
response = {
282+
uid: caller.userId,
283+
handle: rs[0].handle,
284+
email: rs[0].address
285+
};
286+
cb();
287+
}
288+
], function (err) {
289+
if (err) {
290+
helper.handleError(api, connection, err);
291+
} else {
292+
connection.response = response;
293+
}
294+
next(connection, true);
295+
});
296+
297+
}
298+
299+
/**
300+
* The API for activate user
301+
* @since 1.2
302+
*/
303+
exports.getUserIdentity = {
304+
name: 'getUserIdentity',
305+
description: 'Get user identity information',
306+
inputs: {
307+
required: [],
308+
optional: []
309+
},
310+
blockedConnectionTypes: [],
311+
outputExample: {},
312+
version: 'v2',
313+
transaction: 'read',
314+
databases: ['common_oltp'],
315+
cacheEnabled: false,
316+
run: function (api, connection, next) {
317+
if (connection.dbConnectionMap) {
318+
api.log('getUserIdentity#run', 'debug');
319+
getUserIdentity(api, connection, next);
320+
} else {
321+
api.helper.handleNoConnection(api, connection, next);
322+
}
323+
}
324+
};

apiary.apib

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2301,6 +2301,48 @@ Register a new user.
23012301
"description":"Servers are up but overloaded. Try again later."
23022302
}
23032303

2304+
## get User Identity Information [/user/identity]
2305+
### Search My Challenges [GET]
2306+
2307+
Request
2308+
2309+
+ Headers
2310+
Authorization : Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJhZHwxMzI0NTYiLCJleHAiOjEzOTM3MDM1NzEsImF1ZCI6
2311+
2312+
+ Response 200 (application/json)
2313+
2314+
{
2315+
"uid": "uid=132456, ou=members, dc=topcoder, dc=com",
2316+
"handle": "heffan",
2317+
"email": "foo@fooonyou.com"
2318+
}
2319+
2320+
+ Response 401 (application/json)
2321+
2322+
{
2323+
"name":"Unauthorized",
2324+
"value":"401",
2325+
"description":"Authentication credentials were missing or incorrect.",
2326+
"details": "You need login for this endpoint."
2327+
}
2328+
2329+
+ Response 500 (application/json)
2330+
2331+
{
2332+
"name":"Internal Server Error",
2333+
"value":"500",
2334+
"description":"Unknown server error. Please contact support."
2335+
}
2336+
2337+
+ Response 503 (application/json)
2338+
2339+
{
2340+
"name":"Service Unavailable",
2341+
"value":"503",
2342+
"description":"Servers are up but overloaded. Try again later."
2343+
}
2344+
2345+
23042346
## Validate Handle [/users/validate/{handle}]
23052347
### Validate Handle [GET]
23062348

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.59
4+
* @version 1.60
55
* @author vangavroche, Sky_, muzehyun, kurtrips, Ghost_141, ecnu_haozi, hesibo, LazyChild, isv, flytoj2ee,
66
* @author panoptimum, bugbuka, Easyhard
77
*
@@ -138,6 +138,8 @@
138138
* - Add routes for SRM practice problems API.
139139
* Changes in 1.59:
140140
* - Add route for user activation email api.
141+
* Changes in 1.60:
142+
* - Add route for get user identity api.
141143
*/
142144
/*jslint node:true, nomen: true */
143145
"use strict";
@@ -254,6 +256,7 @@ exports.routes = {
254256

255257
{ path: "/:apiVersion/user/challenges", action: "getMyChallenges" },
256258
{ path: "/:apiVersion/user/activation-email", action: "userActivationEmail" },
259+
{ path: "/:apiVersion/user/identity", action: "getUserIdentity" },
257260

258261
{ path: "/:apiVersion/users/tops/:trackType", action: "getTopTrackMembers" },
259262
{ 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)