@@ -4,19 +4,9 @@ angular.module('topcoderX')
4
4
. factory ( 'AuthService' , [
5
5
'$q' , '$log' , 'jwtHelper' , '$cookies' , '$window' , '$state' , '$rootScope' , '$http' , 'Helper' ,
6
6
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';
17
7
18
8
// local variables
19
- var connectorIFrame , url , loading ;
9
+ var connectorIFrame , loading ;
20
10
21
11
/**
22
12
* Create invisible iframe and append it to the body
@@ -49,35 +39,17 @@ angular.module('topcoderX')
49
39
/**
50
40
* Proxies calls to the iframe from main window
51
41
*
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
56
42
* @return {Promise } promise of the request
57
43
*/
58
- function proxyCall ( REQUEST , SUCCESS , FAILURE , params ) {
44
+ function proxyCall ( ) {
59
45
if ( ! connectorIFrame ) {
60
46
throw new Error ( 'connector has not yet been configured.' )
61
47
}
62
48
63
- params = arguments . length > 3 && angular . isDefined ( arguments [ 3 ] ) ? arguments [ 3 ] : { } ;
64
-
65
49
function request ( ) {
66
50
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
81
53
} )
82
54
}
83
55
@@ -95,7 +67,6 @@ angular.module('topcoderX')
95
67
$log . warn ( 'iframe connector can only be configured once, this request has been ignored.' )
96
68
} else {
97
69
connectorIFrame = createFrame ( options . frameId , options . connectorUrl )
98
- url = options . connectorUrl
99
70
100
71
loading = $q ( function ( resolve ) {
101
72
connectorIFrame . onload = function ( ) {
@@ -105,13 +76,46 @@ angular.module('topcoderX')
105
76
}
106
77
}
107
78
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
+
108
102
var AuthService = {
109
103
ERROR : {
110
104
NO_PERMISSIONS : 'Current user doesn\'t have permissions.' ,
111
105
} ,
112
106
PermissionDenied : false ,
113
107
} ;
114
108
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
+
115
119
/**
116
120
* Returns promise which is resolved when connector iframe is loaded
117
121
*
@@ -132,7 +136,7 @@ angular.module('topcoderX')
132
136
* @return {Promise } promise to get token v3
133
137
*/
134
138
AuthService . retriveFreshToken = function ( ) {
135
- return proxyCall ( GET_FRESH_TOKEN_REQUEST , GET_FRESH_TOKEN_SUCCESS , GET_FRESH_TOKEN_FAILURE )
139
+ return proxyCall ( )
136
140
. then ( function ( data ) {
137
141
AuthService . setTokenV3 ( data . token ) ;
138
142
return AuthService . isAuthorized ( ) ;
@@ -146,16 +150,9 @@ angular.module('topcoderX')
146
150
* @return {Promise } promise which is resolved when user is logged out on the server
147
151
*/
148
152
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 : '/' } ) ;
157
154
$window . location . href = $rootScope . appConfig . TC_LOGIN_URL + '?logout=true&retUrl=' + encodeURIComponent ( $window . location . href ) ;
158
- return AuthService . logginOut ;
155
+ // return AuthService.logginOut;
159
156
}
160
157
161
158
AuthService . login = function ( ) {
@@ -312,7 +309,6 @@ angular.module('topcoderX')
312
309
$rootScope . appConfig = res . data ;
313
310
if ( connectorIFrame && ! connectorIFrame . src ) {
314
311
connectorIFrame . src = $rootScope . appConfig . ACCOUNTS_CONNECTOR_URL ;
315
- url = $rootScope . appConfig . ACCOUNTS_CONNECTOR_URL ;
316
312
}
317
313
return $q . resolve ( res . data ) ;
318
314
} ) . catch ( function ( err ) {
0 commit comments