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

Commit d9c1d47

Browse files
authored
Merge pull request #499 from appirio-tech/dev
resolve topcoder user-id for SSO user
2 parents e7cec27 + a0bcfa2 commit d9c1d47

File tree

4 files changed

+81
-3
lines changed

4 files changed

+81
-3
lines changed

initializers/helper.js

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1214,9 +1214,23 @@ helper.socialProviders = {
12141214
"twitter": 3,
12151215
"github": 4,
12161216
"salesforce": 5,
1217-
"ad": 50
1217+
"dribbble": 10,
1218+
"behance": 11,
1219+
"stackoverflow": 12,
1220+
"linkedin": 13,
1221+
"bitbucket": 14,
1222+
"ad": 50,
1223+
"samlp": 102
12181224
};
12191225

1226+
helper.isTCADProvider = function (providerId) {
1227+
return providerId === helper.socialProviders.ad;
1228+
}
1229+
1230+
helper.isSSOProvider = function (providerId) {
1231+
return providerId === helper.socialProviders.samlp;
1232+
}
1233+
12201234
/**
12211235
* Retrieve provider information from the provider name.
12221236
*
@@ -1240,15 +1254,34 @@ helper.getProviderId = function (provider, callback) {
12401254
if (provider.startsWith("salesforce")) {
12411255
providerId = helper.socialProviders.salesforce;
12421256
}
1257+
if (provider.startsWith("dribbble")) {
1258+
providerId = helper.socialProviders.dribbble;
1259+
}
1260+
if (provider.startsWith("behance")) {
1261+
providerId = helper.socialProviders.behance;
1262+
}
1263+
if (provider.startsWith("stackoverflow")) {
1264+
providerId = helper.socialProviders.stackoverflow;
1265+
}
1266+
if (provider.startsWith("linkedin")) {
1267+
providerId = helper.socialProviders.linkedin;
1268+
}
1269+
if (provider.startsWith("bitbucket")) {
1270+
providerId = helper.socialProviders.bitbucket;
1271+
}
12431272
if (provider.startsWith("ad") || provider.startsWith("auth0")) {
12441273
providerId = helper.socialProviders.ad;
12451274
}
1275+
if (provider.startsWith("samlp")) {
1276+
providerId = helper.socialProviders.samlp;
1277+
}
12461278
if (providerId) {
12471279
callback(null, providerId);
12481280
} else {
12491281
callback(new Error('Social provider: ' + provider + ' is not defined in config'));
12501282
}
12511283
};
1284+
12521285
/* Encrypt the password using the specified key. After being
12531286
* encrypted with a Blowfish key, the encrypted byte array is
12541287
* then encoded with a base 64 encoding, resulting in the String

initializers/middleware.js

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,14 @@ exports.middleware = function (api, next) {
7676
var authHeader = connection.rawConnection.req.headers.authorization,
7777
connectionMap = { "common_oltp": api.dataAccess.createConnection("common_oltp") },
7878
isTopcoderAD,
79+
isSSO,
7980
cachePrefix = "authorizationPreProcessor::",
8081
decoded,
8182
isCachedReturned,
8283
cacheKey,
8384
socialUserId,
8485
socialProvider,
86+
authConnection,
8587
cookieToken = api.utils.parseCookies(connection.rawConnection.req)[process.env.JWT_TOKEN_COOKIE_KEY];
8688

8789
if (_.isUndefined(authHeader) && _.isUndefined(cookieToken)) {
@@ -113,8 +115,10 @@ exports.middleware = function (api, next) {
113115
cb(new IllegalArgumentError('Malformed Auth header. No sub in token!'));
114116
return;
115117
}
118+
// connection name
119+
authConnection = getAuth0Connection(decoded);
116120
var split = decoded.sub.split("|");
117-
if (split.length === 1) {
121+
if (split.length === 1) {
118122
// token.sub should contain "|"
119123
cb(new IllegalArgumentError('Malformed Auth header. token.sub is in bad format!'));
120124
return;
@@ -136,7 +140,8 @@ exports.middleware = function (api, next) {
136140
}
137141
api.helper.getProviderId(socialProvider, cb);
138142
}, function (providerId, cb) {
139-
isTopcoderAD = providerId === api.helper.socialProviders.ad;
143+
isTopcoderAD = api.helper.isTCADProvider(providerId);
144+
isSSO = api.helper.isSSOProvider(providerId);
140145
cacheKey = cachePrefix + decoded.sub;
141146
api.cache.load(cacheKey, function (err, value) {
142147
var userId;
@@ -155,6 +160,16 @@ exports.middleware = function (api, next) {
155160
cbx(api.helper.checkPositiveInteger(userId, "userId"));
156161
return;
157162
}
163+
if (isSSO) {
164+
api.dataAccess.executeQuery("get_user_by_sso_login",
165+
{
166+
sso_user_id: socialUserId,
167+
auth_connection: authConnection,
168+
},
169+
connectionMap,
170+
cbx);
171+
return;
172+
}
158173
api.dataAccess.executeQuery("get_user_by_social_login",
159174
{
160175
social_user_id: socialUserId,
@@ -251,6 +266,26 @@ exports.middleware = function (api, next) {
251266
}
252267
/*jslint */
253268

269+
/**
270+
* Extract Auth0 connection name from JWT token.
271+
* JWT Example:
272+
* ....
273+
* "identities": [
274+
* {
275+
* "connection": "sfdc-aspdev",
276+
* "isSocial": false,
277+
* "provider": "samlp",
278+
* "user_id": "user1@asp.appirio.com.dev"
279+
* }
280+
* ]
281+
* sfdc-aspdev is returned.
282+
*
283+
* @param {Object} decoded JWT token issued by Auth0 (v2 token)
284+
*/
285+
function getAuth0Connection(jwt) {
286+
return (jwt && jwt.identities && Array.isArray(jwt.identities) && jwt.identities.length>0 ) ? jwt.identities[0].connection : null;
287+
}
288+
254289
/**
255290
* The pre-processor that checks if user is slamming.
256291
*

queries/get_user_by_sso_login

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
SELECT s.user_id
2+
FROM user_sso_login s
3+
JOIN sso_login_provider p ON s.provider_id = p.sso_login_provider_id
4+
WHERE p.name = '@auth_connection@'
5+
AND s.sso_user_id = '@sso_user_id@'

queries/get_user_by_sso_login.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"name" : "get_user_by_sso_login",
3+
"db" : "common_oltp",
4+
"sqlfile" : "get_user_by_sso_login"
5+
}

0 commit comments

Comments
 (0)