@@ -6,18 +6,14 @@ export class Authorization {
6
6
/**
7
7
* @param {AbstractStorage } storage Key value storage
8
8
* @param {string } loginUrl Url the user get's redirected to authorize
9
- * @param {string } refreshUrl Url the app will send a POST to request a new access token
10
9
* @param {string } clientId OAuth2 client id
11
10
* @param {string } businessPartnerId OAuth2 business partner id
12
11
*/
13
- constructor ( storage , loginUrl = '' , refreshUrl = '' , clientId = '' , businessPartnerId = '' ) {
12
+ constructor ( storage , loginUrl = '' , clientId = '' , businessPartnerId = '' ) {
14
13
this . storage = storage ;
15
14
this . loginUrl = loginUrl ;
16
- this . refreshUrl = refreshUrl ;
17
15
this . clientId = clientId ;
18
16
this . businessPartnerId = businessPartnerId ;
19
- // Check if the access token will expire soon and we can refresh it
20
- this . checkExpiration ( ) ;
21
17
}
22
18
23
19
/**
@@ -40,26 +36,6 @@ export class Authorization {
40
36
}
41
37
}
42
38
43
- /**
44
- * Refresh access token if it is expired and a refresh token is available
45
- * @returns {void }
46
- * @private
47
- */
48
- checkExpiration ( ) {
49
- const expiresAt = this . storage . get ( 'expires_at' ) || 0 ;
50
- const refreshToken = this . storage . get ( 'refresh_token' ) ;
51
- if ( ! refreshToken ) {
52
- return ;
53
- }
54
- // Check if expiration date is one minute before expiration
55
- if ( new Date ( ) . getTime ( ) > expiresAt - 60000 ) {
56
- this . refresh ( refreshToken ) ;
57
- }
58
- // Check every 59 seconds if the token expires.
59
- // Use 59 seconds to prevent exact overlapping of check and expiration
60
- setTimeout ( ( ) => this . checkExpiration ( ) , 59000 ) ;
61
- }
62
-
63
39
/**
64
40
* Tries to parse the access token from the given query string
65
41
* @param {string } queryString Query string without leading ?
@@ -89,14 +65,13 @@ export class Authorization {
89
65
90
66
/**
91
67
* Update the tokens and notify the header
92
- * @param {Object } params Response parameters containing access and refresh token
68
+ * @param {Object } params Response parameters containing access token
93
69
* @returns {void }
94
70
* @private
95
71
*/
96
72
updateTokens ( params ) {
97
73
const expires = params . expires_in ? parseInt ( params . expires_in , 10 ) : 3600 ;
98
74
this . storage . set ( 'access_token' , params . access_token ) ;
99
- this . storage . set ( 'refresh_token' , params . refresh_token ) ;
100
75
this . storage . set ( 'expires_at' , new Date ( ) . getTime ( ) + expires * 1000 ) ;
101
76
// Put data into authorized stream
102
77
this . changeAccessToken ( params . access_token ) ;
@@ -117,47 +92,12 @@ export class Authorization {
117
92
location . href = `${ this . loginUrl } ?${ query } ` ;
118
93
}
119
94
120
- /**
121
- * Request a new access token
122
- * @param {string } token Refresh token
123
- * @returns {void }
124
- */
125
- refresh ( token ) {
126
- // Abort if we have not enough information to refresh the token
127
- if ( ! this . refreshUrl || ! token ) {
128
- return ;
129
- }
130
- const data = this . buildQuery ( [
131
- [ 'business_partner_id' , this . businessPartnerId ] ,
132
- [ 'client_id' , this . clientId ] ,
133
- [ 'grant_type' , 'refresh_token' ] ,
134
- [ 'refresh_token' , token ] ,
135
- [ 'state' , this . createAndRememberUUID ( ) ] ,
136
- [ 'response_type' , 'token' ]
137
- ] ) ;
138
-
139
- const xhr = new XMLHttpRequest ( ) ;
140
- xhr . open ( 'POST' , this . refreshUrl , true ) ;
141
- xhr . setRequestHeader ( 'Content-Type' , 'application/x-www-form-urlencoded' ) ;
142
- xhr . addEventListener ( 'load' , ( ) => {
143
- if ( xhr . readyState === XMLHttpRequest . DONE ) {
144
- if ( xhr . status === 200 ) {
145
- this . updateTokens ( JSON . parse ( xhr . responseText ) ) ;
146
- } else {
147
- throw new Error ( `Could not refresh token: ${ xhr . responseText } ` ) ;
148
- }
149
- }
150
- } ) ;
151
- xhr . send ( data ) ;
152
- }
153
-
154
95
/**
155
96
* Remove authorization
156
97
* @returns {void }
157
98
*/
158
99
unauthorize ( ) {
159
100
this . storage . remove ( 'access_key' ) ;
160
- this . storage . remove ( 'refresh_key' ) ;
161
101
this . storage . remove ( 'expires_at' ) ;
162
102
this . changeAccessToken ( null ) ;
163
103
}
0 commit comments