Skip to content

Commit a2d3f6d

Browse files
authored
fix(header): Removed refresh token logic (#134)
1 parent 44737e6 commit a2d3f6d

File tree

3 files changed

+2
-70
lines changed

3 files changed

+2
-70
lines changed

src/ws-header/authorization.js

+2-62
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,14 @@ export class Authorization {
66
/**
77
* @param {AbstractStorage} storage Key value storage
88
* @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
109
* @param {string} clientId OAuth2 client id
1110
* @param {string} businessPartnerId OAuth2 business partner id
1211
*/
13-
constructor(storage, loginUrl = '', refreshUrl = '', clientId = '', businessPartnerId = '') {
12+
constructor(storage, loginUrl = '', clientId = '', businessPartnerId = '') {
1413
this.storage = storage;
1514
this.loginUrl = loginUrl;
16-
this.refreshUrl = refreshUrl;
1715
this.clientId = clientId;
1816
this.businessPartnerId = businessPartnerId;
19-
// Check if the access token will expire soon and we can refresh it
20-
this.checkExpiration();
2117
}
2218

2319
/**
@@ -40,26 +36,6 @@ export class Authorization {
4036
}
4137
}
4238

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-
6339
/**
6440
* Tries to parse the access token from the given query string
6541
* @param {string} queryString Query string without leading ?
@@ -89,14 +65,13 @@ export class Authorization {
8965

9066
/**
9167
* 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
9369
* @returns {void}
9470
* @private
9571
*/
9672
updateTokens(params) {
9773
const expires = params.expires_in ? parseInt(params.expires_in, 10) : 3600;
9874
this.storage.set('access_token', params.access_token);
99-
this.storage.set('refresh_token', params.refresh_token);
10075
this.storage.set('expires_at', new Date().getTime() + expires * 1000);
10176
// Put data into authorized stream
10277
this.changeAccessToken(params.access_token);
@@ -117,47 +92,12 @@ export class Authorization {
11792
location.href = `${this.loginUrl}?${query}`;
11893
}
11994

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-
15495
/**
15596
* Remove authorization
15697
* @returns {void}
15798
*/
15899
unauthorize() {
159100
this.storage.remove('access_key');
160-
this.storage.remove('refresh_key');
161101
this.storage.remove('expires_at');
162102
this.changeAccessToken(null);
163103
}

src/ws-header/ws-header.js

-5
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ import {WSDropdown} from '../ws-dropdown/ws-dropdown';
1010
* Optionally call WSHeader.setStorageType('cookie', 'zalando') If you want a to use cookies instead of localStorage
1111
* to persist the tokens. You can call WSHeader.getAccessToken().then(token => ...) to get the current access token.
1212
* It will resolve null when no access token is present and therefore the user isn't logged in.
13-
* If you configured the header with a refreshUrl you should subscribe the ws-auth-changed event. It will be emitted
14-
* when the access token was refreshed and it will have the access token in the event details.
1513
*/
1614
export class WSHeader extends Component {
1715

@@ -23,7 +21,6 @@ export class WSHeader extends Component {
2321

2422
static defaultProps = {
2523
loginUrl: 'https://identity.zalando.com/oauth2/authorize',
26-
refreshUrl: null,
2724
businessPartnerId: '810d1d00-4312-43e5-bd31-d8373fdd24c7',
2825
clientId: null,
2926
links: [],
@@ -35,7 +32,6 @@ export class WSHeader extends Component {
3532

3633
static propTypes = {
3734
loginUrl: PropTypes.string,
38-
refreshUrl: PropTypes.string,
3935
businessPartnerId: PropTypes.string,
4036
clientId: PropTypes.string,
4137
links: PropTypes.array,
@@ -146,7 +142,6 @@ export class WSHeader extends Component {
146142
this.authorization = new Authorization(
147143
WSHeader.storage,
148144
props.loginUrl,
149-
props.refreshUrl,
150145
props.clientId,
151146
props.businessPartnerId
152147
);

tests/ws-header/ws-header.spec.js

-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import {LocalStorage} from '../../src/ws-header/storage/local-storage';
66

77
function clearStorage() {
88
WSHeader.storage.remove('access_token');
9-
WSHeader.storage.remove('refresh_token');
109
WSHeader.storage.remove('expires_at');
1110
}
1211

@@ -52,14 +51,12 @@ describe('A WSHeader', () => {
5251
WSHeader.storage.set('locale', 'de');
5352
const header = new WSHeader({
5453
loginUrl: '1111',
55-
refreshUrl: 222,
5654
businessPartnerId: '333',
5755
clientId: 444
5856
});
5957

6058
expect(header.authorization).toBeTruthy();
6159
expect(header.authorization.loginUrl).toBe('1111');
62-
expect(header.authorization.refreshUrl).toBe(222);
6360
expect(header.authorization.businessPartnerId).toBe('333');
6461
expect(header.authorization.clientId).toBe(444);
6562
expect(header.state.locale).toBe('de');

0 commit comments

Comments
 (0)