Skip to content

Commit be8d273

Browse files
Merge pull request #629 from jfyne/gh-628/createNonce
Correct implementation of rfc7636 section 4.1
2 parents d49021a + 5b14963 commit be8d273

File tree

1 file changed

+13
-13
lines changed

1 file changed

+13
-13
lines changed

projects/lib/src/oauth-service.ts

+13-13
Original file line numberDiff line numberDiff line change
@@ -785,8 +785,7 @@ export class OAuthService extends AuthConfig implements OnDestroy {
785785
tap(result => this.storeIdToken(result)),
786786
map(_ => tokenResponse)
787787
);
788-
}
789-
else {
788+
} else {
790789
return of(tokenResponse);
791790
}
792791
}))
@@ -1402,8 +1401,7 @@ export class OAuthService extends AuthConfig implements OnDestroy {
14021401
public tryLogin(options: LoginOptions = null): Promise<boolean> {
14031402
if (this.config.responseType === 'code') {
14041403
return this.tryLoginCodeFlow().then(_ => true);
1405-
}
1406-
else {
1404+
} else {
14071405
return this.tryLoginImplicitFlow(options);
14081406
}
14091407
}
@@ -2141,26 +2139,28 @@ export class OAuthService extends AuthConfig implements OnDestroy {
21412139
}
21422140

21432141
/*
2144-
* This alphabet uses a-z A-Z 0-9 _- symbols.
2145-
* Symbols order was changed for better gzip compression.
2142+
* This alphabet is from:
2143+
* https://tools.ietf.org/html/rfc7636#section-4.1
2144+
*
2145+
* [A-Z] / [a-z] / [0-9] / "-" / "." / "_" / "~"
21462146
*/
2147-
const url = 'Uint8ArdomValuesObj012345679BCDEFGHIJKLMNPQRSTWXYZ_cfghkpqvwxyz-';
2147+
const unreserved = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~';
21482148
let size = 45;
21492149
let id = '';
21502150

21512151
const crypto = self.crypto || self['msCrypto'];
21522152
if (crypto) {
2153-
const bytes = crypto.getRandomValues(new Uint8Array(size));
2154-
while (0 < size--) {
2155-
id += url[bytes[size] & 63];
2156-
}
2153+
let bytes = new Uint8Array(size);
2154+
crypto.getRandomValues(bytes);
2155+
bytes = bytes.map(x => unreserved.charCodeAt(x % unreserved.length));
2156+
id = String.fromCharCode.apply(null, bytes);
21572157
} else {
21582158
while (0 < size--) {
2159-
id += url[Math.random() * 64 | 0];
2159+
id += unreserved[Math.random() * unreserved.length | 0];
21602160
}
21612161
}
21622162

2163-
resolve(id);
2163+
resolve(base64UrlEncode(id));
21642164
});
21652165
}
21662166

0 commit comments

Comments
 (0)