Skip to content

Commit a906a6b

Browse files
committed
Fixing the random string generation.
1 parent e6eec8e commit a906a6b

File tree

1 file changed

+21
-21
lines changed

1 file changed

+21
-21
lines changed

projects/lib/src/oauth-service.ts

+21-21
Original file line numberDiff line numberDiff line change
@@ -778,8 +778,7 @@ export class OAuthService extends AuthConfig implements OnDestroy {
778778
tap(result => this.storeIdToken(result)),
779779
map(_ => tokenResponse)
780780
);
781-
}
782-
else {
781+
} else {
783782
return of(tokenResponse);
784783
}
785784
}))
@@ -1264,7 +1263,7 @@ export class OAuthService extends AuthConfig implements OnDestroy {
12641263
}
12651264

12661265
return url;
1267-
1266+
12681267
}
12691268

12701269
initImplicitFlowInternal(
@@ -1374,8 +1373,7 @@ export class OAuthService extends AuthConfig implements OnDestroy {
13741373
public tryLogin(options: LoginOptions = null): Promise<boolean> {
13751374
if (this.config.responseType === 'code') {
13761375
return this.tryLoginCodeFlow().then(_ => true);
1377-
}
1378-
else {
1376+
} else {
13791377
return this.tryLoginImplicitFlow(options);
13801378
}
13811379
}
@@ -1397,7 +1395,7 @@ export class OAuthService extends AuthConfig implements OnDestroy {
13971395

13981396
public tryLoginCodeFlow(): Promise<void> {
13991397

1400-
const parts = this.parseQueryString(window.location.search)
1398+
const parts = this.parseQueryString(window.location.search);
14011399

14021400
const code = parts['code'];
14031401
const state = parts['state'];
@@ -1503,32 +1501,32 @@ export class OAuthService extends AuthConfig implements OnDestroy {
15031501
(tokenResponse) => {
15041502
this.debug('refresh tokenResponse', tokenResponse);
15051503
this.storeAccessTokenResponse(
1506-
tokenResponse.access_token,
1507-
tokenResponse.refresh_token,
1504+
tokenResponse.access_token,
1505+
tokenResponse.refresh_token,
15081506
tokenResponse.expires_in,
15091507
tokenResponse.scope);
15101508

15111509
if (this.oidc && tokenResponse.id_token) {
1512-
this.processIdToken(tokenResponse.id_token, tokenResponse.access_token).
1510+
this.processIdToken(tokenResponse.id_token, tokenResponse.access_token).
15131511
then(result => {
15141512
this.storeIdToken(result);
1515-
1513+
15161514
this.eventsSubject.next(new OAuthSuccessEvent('token_received'));
15171515
this.eventsSubject.next(new OAuthSuccessEvent('token_refreshed'));
1518-
1516+
15191517
resolve(tokenResponse);
15201518
})
15211519
.catch(reason => {
15221520
this.eventsSubject.next(new OAuthErrorEvent('token_validation_error', reason));
15231521
console.error('Error validating tokens');
15241522
console.error(reason);
1525-
1523+
15261524
reject(reason);
15271525
});
15281526
} else {
15291527
this.eventsSubject.next(new OAuthSuccessEvent('token_received'));
15301528
this.eventsSubject.next(new OAuthSuccessEvent('token_refreshed'));
1531-
1529+
15321530
resolve(tokenResponse);
15331531
}
15341532
},
@@ -1688,7 +1686,7 @@ export class OAuthService extends AuthConfig implements OnDestroy {
16881686
): boolean {
16891687
const savedNonce = this._storage.getItem('nonce');
16901688
if (savedNonce !== nonceInState) {
1691-
1689+
16921690
const err = 'Validating access_token failed, wrong state/nonce.';
16931691
console.error(err, savedNonce, nonceInState);
16941692
return false;
@@ -2084,22 +2082,24 @@ export class OAuthService extends AuthConfig implements OnDestroy {
20842082
}
20852083

20862084
/*
2087-
* This alphabet uses a-z A-Z 0-9 _- symbols.
2088-
* Symbols order was changed for better gzip compression.
2085+
* This alphabet is from:
2086+
* https://tools.ietf.org/html/rfc7636#section-4.1
2087+
*
2088+
* [A-Z] / [a-z] / [0-9] / "-" / "." / "_" / "~"
20892089
*/
20902090
const unreserved = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~';
20912091
let size = 45;
20922092
let id = '';
20932093

20942094
const crypto = self.crypto || self['msCrypto'];
20952095
if (crypto) {
2096-
const bytes = crypto.getRandomValues(new Uint8Array(size));
2097-
while (0 < size--) {
2098-
id += unreserved[bytes[size] & 63];
2099-
}
2096+
let bytes = new Uint8Array(size);
2097+
crypto.getRandomValues(bytes);
2098+
bytes = bytes.map(x => unreserved.charCodeAt(x % unreserved.length));
2099+
id = String.fromCharCode.apply(null, bytes);
21002100
} else {
21012101
while (0 < size--) {
2102-
id += unreserved[Math.random() * 64 | 0];
2102+
id += unreserved[Math.random() * unreserved.length | 0];
21032103
}
21042104
}
21052105

0 commit comments

Comments
 (0)