@@ -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 ) {
0 commit comments