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

Commit fc4e133

Browse files
authored
Merge pull request #345 from afrisalyp/feature/Auth0-RS256-Token
Fix logout, cleanup auth.
2 parents ee4f12e + c0aaa51 commit fc4e133

File tree

6 files changed

+49
-82
lines changed

6 files changed

+49
-82
lines changed

package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@
77
"private": true,
88
"main": "src/app.js",
99
"engines": {
10-
"node": "8",
11-
"npm": "5"
10+
"node": "14",
11+
"npm": "6"
1212
},
1313
"scripts": {
1414
"start": "node -r esm src/app.js",
1515
"serve": "./node_modules/.bin/concurrently \"npm run start:be\" \"npm run start:fe\"",
16-
"start:be": "nodemon src/app.js ",
16+
"start:be": "nodemon -r esm src/app.js",
1717
"start:fe": "gulp build:watch",
1818
"build": "gulp build",
1919
"test": "node ./node_modules/mocha/bin/mocha --recursive --timeout 999999 --colors tests/*.test.js --bail",
@@ -53,7 +53,7 @@
5353
"cors": "^2.8.4",
5454
"debug": "~2.6.3",
5555
"dynamoose": "^1.1.0",
56-
"esm": "^3.2.25",
56+
"esm": "^3.2.25",
5757
"express": "^4.15.4",
5858
"express-jwt": "^5.3.0",
5959
"express-session": "^1.15.5",

src/config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ module.exports = {
6161
},
6262
TOPCODER_VALUES: {
6363
dev: {
64-
TC_LOGIN_URL: process.env.TC_LOGIN_URL || 'https://accounts-auth.topcoder-dev.com',
64+
TC_LOGIN_URL: process.env.TC_LOGIN_URL || 'https://accounts-auth0.topcoder-dev.com',
6565
TC_USER_PROFILE_URL: process.env.TC_USER_PROFILE_URL || 'https://api.topcoder-dev.com/v2/user/profile',
6666
},
6767
prod: {

src/front/src/app/auth/auth.config.js

Lines changed: 3 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,6 @@
99

1010
angular.module('topcoderX')
1111
.config(['$httpProvider', 'jwtInterceptorProvider', function ($httpProvider, jwtInterceptorProvider) {
12-
var refreshingToken = null;
13-
14-
function handleRefreshResponse(res, $authService) {
15-
var ref;
16-
var ref1;
17-
var ref2;
18-
19-
const newToken = (ref = res.data) != null ? (ref1 = ref.result) != null ?
20-
(ref2 = ref1.content) != null ? ref2.token : void 0 : void 0 : void 0;
21-
22-
$authService.setTokenV3(newToken);
23-
24-
return newToken;
25-
};
26-
27-
function refreshingTokenComplete() {
28-
refreshingToken = null;
29-
};
30-
3112
jwtInterceptorProvider.tokenGetter = [
3213
'AuthService', '$http', 'Helper', '$rootScope', 'config',
3314
function (AuthService, $http, Helper, $rootScope, config) {
@@ -43,18 +24,9 @@ angular.module('topcoderX')
4324
var currentToken = AuthService.getTokenV3();
4425

4526
if (AuthService.getTokenV3() && AuthService.isTokenV3Expired()) {
46-
if (refreshingToken === null) {
47-
refreshingToken = $http({
48-
method: 'GET',
49-
url: $rootScope.appConfig.API_URL + "/v3/authorizations/1",
50-
headers: {
51-
'Authorization': "Bearer " + currentToken
52-
}
53-
}).then(function (res) { handleRefreshResponse(res, AuthService) })["finally"](refreshingTokenComplete).catch(function () {
54-
AuthService.login();
55-
});
56-
}
57-
return refreshingToken;
27+
var token = AuthService.getToken('v3jwt')
28+
if (token) return token
29+
else AuthService.login()
5830
} else {
5931
return currentToken;
6032
}

src/front/src/app/auth/auth.service.js

Lines changed: 40 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,9 @@ angular.module('topcoderX')
44
.factory('AuthService', [
55
'$q', '$log', 'jwtHelper', '$cookies', '$window', '$state', '$rootScope', '$http', 'Helper',
66
function ($q, $log, jwtHelper, $cookies, $window, $state, $rootScope, $http, Helper) {
7-
// these constants are for AuthService internal usage only
8-
// they don't depend on the environment thus don't have to be placed in global config
9-
10-
var GET_FRESH_TOKEN_REQUEST = 'GET_FRESH_TOKEN_REQUEST';
11-
var GET_FRESH_TOKEN_SUCCESS = 'GET_FRESH_TOKEN_SUCCESS';
12-
var GET_FRESH_TOKEN_FAILURE = 'GET_FRESH_TOKEN_FAILURE';
13-
14-
//var LOGOUT_REQUEST = 'LOGOUT_REQUEST';
15-
//var LOGOUT_SUCCESS = 'LOGOUT_SUCCESS';
16-
//var LOGOUT_FAILURE = 'LOGOUT_FAILURE';
177

188
// local variables
19-
var connectorIFrame, url, loading;
9+
var connectorIFrame, loading;
2010

2111
/**
2212
* Create invisible iframe and append it to the body
@@ -49,35 +39,17 @@ angular.module('topcoderX')
4939
/**
5040
* Proxies calls to the iframe from main window
5141
*
52-
* @param {String} REQUEST request id
53-
* @param {String} SUCCESS success respond id
54-
* @param {String} FAILURE failure respond id
55-
* @param {Object} params params of the request
5642
* @return {Promise} promise of the request
5743
*/
58-
function proxyCall(REQUEST, SUCCESS, FAILURE, params) {
44+
function proxyCall() {
5945
if (!connectorIFrame) {
6046
throw new Error('connector has not yet been configured.')
6147
}
6248

63-
params = arguments.length > 3 && angular.isDefined(arguments[3]) ? arguments[3] : {};
64-
6549
function request() {
6650
return $q(function (resolve, reject) {
67-
function receiveMessage(e) {
68-
var safeFormat = e.data.type === SUCCESS || e.data.type === FAILURE
69-
if (safeFormat) {
70-
window.removeEventListener('message', receiveMessage)
71-
if (e.data.type === SUCCESS) resolve(e.data)
72-
if (e.data.type === FAILURE) reject(e.error)
73-
}
74-
}
75-
76-
window.addEventListener('message', receiveMessage)
77-
78-
var payload = $.extend({}, { type: REQUEST }, params)
79-
80-
connectorIFrame.contentWindow.postMessage(payload, url)
51+
var token = AuthService.getToken('v3jwt')
52+
token ? resolve({ token: token }) : reject("v3jwt cookie not found") // eslint-disable-line no-unused-expressions
8153
})
8254
}
8355

@@ -95,7 +67,6 @@ angular.module('topcoderX')
9567
$log.warn('iframe connector can only be configured once, this request has been ignored.')
9668
} else {
9769
connectorIFrame = createFrame(options.frameId, options.connectorUrl)
98-
url = options.connectorUrl
9970

10071
loading = $q(function (resolve) {
10172
connectorIFrame.onload = function () {
@@ -105,13 +76,46 @@ angular.module('topcoderX')
10576
}
10677
}
10778

79+
function fromPairs(arr) {
80+
return arr.reduce(function(accumulator, value) {
81+
accumulator[value[0]] = value[1];
82+
return accumulator;
83+
}, {})
84+
}
85+
86+
/**
87+
* parse cookie to find a key data.
88+
*
89+
* @param {String} cookie cookie data
90+
* @return {Object} parsed cookie
91+
*/
92+
function parseCookie(cookie) {
93+
return fromPairs(
94+
cookie
95+
.split(';')
96+
.map(
97+
function (pair) { return pair.split('=').map(function(part) { return part.trim() }) }
98+
)
99+
)
100+
}
101+
108102
var AuthService = {
109103
ERROR: {
110104
NO_PERMISSIONS: 'Current user doesn\'t have permissions.',
111105
},
112106
PermissionDenied: false,
113107
};
114108

109+
/**
110+
* Get token in cookie based on key.
111+
*
112+
* @param {String} key the key
113+
* @return {Object} token data object
114+
*/
115+
AuthService.getToken = function(key) {
116+
return parseCookie(document.cookie)[key]
117+
}
118+
115119
/**
116120
* Returns promise which is resolved when connector iframe is loaded
117121
*
@@ -132,7 +136,7 @@ angular.module('topcoderX')
132136
* @return {Promise} promise to get token v3
133137
*/
134138
AuthService.retriveFreshToken = function () {
135-
return proxyCall(GET_FRESH_TOKEN_REQUEST, GET_FRESH_TOKEN_SUCCESS, GET_FRESH_TOKEN_FAILURE)
139+
return proxyCall()
136140
.then(function (data) {
137141
AuthService.setTokenV3(data.token);
138142
return AuthService.isAuthorized();
@@ -146,16 +150,9 @@ angular.module('topcoderX')
146150
* @return {Promise} promise which is resolved when user is logged out on the server
147151
*/
148152
AuthService.logout = function () {
149-
// send request to the server that we want to log out
150-
// save loggingOut promise to be accessed any time
151-
//AuthService.logginOut = proxyCall(LOGOUT_REQUEST, LOGOUT_SUCCESS, LOGOUT_FAILURE).then(function () {
152-
//AuthService.logginOut = null;
153-
// remove only token V3, which we set from the script manually
154-
// token V2 will be removed automatically during logout server request
155-
//$cookies.remove($rootScope.appConfig.JWT_V3_NAME, { path: '/' });
156-
//});
153+
$cookies.remove($rootScope.appConfig.JWT_V3_NAME, { path: '/' });
157154
$window.location.href = $rootScope.appConfig.TC_LOGIN_URL + '?logout=true&retUrl=' + encodeURIComponent($window.location.href);
158-
return AuthService.logginOut;
155+
// return AuthService.logginOut;
159156
}
160157

161158
AuthService.login = function () {
@@ -312,7 +309,6 @@ angular.module('topcoderX')
312309
$rootScope.appConfig = res.data;
313310
if (connectorIFrame && !connectorIFrame.src) {
314311
connectorIFrame.src = $rootScope.appConfig.ACCOUNTS_CONNECTOR_URL;
315-
url = $rootScope.appConfig.ACCOUNTS_CONNECTOR_URL;
316312
}
317313
return $q.resolve(res.data);
318314
}).catch(function (err) {

src/front/src/app/main/main.controller.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ angular.module('topcoderX')
5656

5757
$scope.logout = function () {
5858
AuthService.logout();
59-
$state.go('auth');
6059
};
6160

6261
// auth

src/front/src/components/common/topnavbar.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
</a>
1717
</li>
1818
<li>
19-
<a href="javasript:;" ng-click="logout()">
19+
<a ng-click="logout()">
2020
<i class="fa fa-sign-out"></i>
2121
<small>Sign Out</small>
2222
</a>

0 commit comments

Comments
 (0)