Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix safari10 initialization error #232

Merged
merged 8 commits into from
Oct 10, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions FAQ.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,21 +63,19 @@ Note that even though the workaround doesn't cause any weird side effects in bro

For more context see this [issue](https://github.com/auth0-samples/auth0-react-samples/issues/145).


## Why do I get `auth0_spa_js_1.default is not a function` when using Typescript?

If you're hitting this issue, set `esModuleInterop: true` in your `tsconfig.json` file (inside `compilerOptions`).

Due to how the type system works in Typescript, if one of your dependencies uses `allowSyntheticDefaultImports: true`, then all the consumers of that dependency must use `allowSyntheticDefaultImports: true` as well. This, of course, is not ideal and might break your app if you depend on this setting being `false`. The Typescript team added the `esModuleInterop` flag that helps in this scenario.


## Why do I get `auth0-spa-js must run on a secure origin`?

Internally, the SDK uses [Web Cryptography API](https://developer.mozilla.org/en-US/docs/Web/API/Web_Crypto_API) to create [SHA-256 digest](https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/digest).

According to the spec ([via Github issues](https://github.com/w3c/webcrypto/issues/28)), Web Cryptography API requires a secure origin, so that accessing `Crypto.subtle` in a not secure context return undefined.

In most browsers, secure origins are origins that match at least one of the following (scheme, host, port) patterns:
In most browsers, secure origins are origins that match at least one of the following (scheme, host, port) patterns:

```
(https, *, *)
Expand All @@ -86,4 +84,6 @@ In most browsers, secure origins are origins that match at least one of the foll
(*, 127/8, *)
(*, ::1/128, *)
(file, *, —)
```
```

If you're running your application from a secure origin, it's possible that your browser doesn't support the Web Crypto API. For a compatibility table, please check https://caniuse.com/#feat=mdn-api_subtlecrypto
12 changes: 11 additions & 1 deletion __tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,20 @@ describe('Auth0', () => {
})
).rejects.toThrowError(`
auth0-spa-js must run on a secure origin.
See https://github.com/auth0/auth0-spa-js/blob/master/FAQ.md#why-do-i-get-error-invalid-state-in-firefox-when-refreshing-the-page-immediately-after-a-login
See https://github.com/auth0/auth0-spa-js/blob/master/FAQ.md#why-do-i-get-auth0-spa-js-must-run-on-a-secure-origin
for more information.
`);
});
it('should use crypto.webkitSubtle when available', async () => {
(<any>global).crypto = { webkitSubtle: 'webkit' };

const auth0 = await createAuth0Client({
domain: TEST_DOMAIN,
client_id: TEST_CLIENT_ID
});
expect(auth0).toBeDefined();
expect((<any>global).crypto.subtle).toBe('webkit');
});
it('should return, logging a warning if crypto is unavailable', async () => {
(<any>global).crypto = undefined;
(<any>global).msCrypto = undefined;
Expand Down
7 changes: 6 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import * as ClientStorage from './storage';
import './global';

export default async function createAuth0Client(options: Auth0ClientOptions) {
// ie11 compat
if (!window.crypto && (<any>window).msCrypto) {
(<any>window).crypto = (<any>window).msCrypto;
}
Expand All @@ -20,10 +21,14 @@ export default async function createAuth0Client(options: Auth0ClientOptions) {
'For security reasons, `window.crypto` is required to run `auth0-spa-js`.'
);
}
// safari 10 compat
if ((<any>window.crypto).webkitSubtle) {
luisrudge marked this conversation as resolved.
Show resolved Hide resolved
(<any>window.crypto).subtle = (<any>window.crypto).webkitSubtle;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No non-Auth0 code can be running in the Window while we're doing this right?

Trying to decide if replacing a global is safe in this context or whether we should be creating our own variable to use instead to avoid non-Auth0 code getting confused.

}
if (typeof window.crypto.subtle === 'undefined') {
throw new Error(`
auth0-spa-js must run on a secure origin.
See https://github.com/auth0/auth0-spa-js/blob/master/FAQ.md#why-do-i-get-error-invalid-state-in-firefox-when-refreshing-the-page-immediately-after-a-login
See https://github.com/auth0/auth0-spa-js/blob/master/FAQ.md#why-do-i-get-auth0-spa-js-must-run-on-a-secure-origin
for more information.
`);
}
Expand Down
142 changes: 73 additions & 69 deletions static/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,88 +27,92 @@
createAuth0Client({
domain: 'auth.brucke.club',
client_id: 'wLSIP47wM39wKdDmOj6Zb5eSEw3JVhVp'
}).then(function(auth0) {
window.auth0 = auth0;
$('#login_popup').click(function() {
auth0
.loginWithPopup({
redirect_uri: 'http://localhost:3000/callback.html'
})
.then(function() {
auth0.getTokenSilently().then(function(token) {
console.log(token);
});
auth0.getUser().then(function(user) {
console.log(user);
})
.then(function(auth0) {
window.auth0 = auth0;
$('#login_popup').click(function() {
auth0
.loginWithPopup({
redirect_uri: 'http://localhost:3000/callback.html'
})
.then(function() {
auth0.getTokenSilently().then(function(token) {
console.log(token);
});
auth0.getUser().then(function(user) {
console.log(user);
});
});
});
$('#login_redirect').click(function() {
auth0.loginWithRedirect({
redirect_uri: 'http://localhost:3000/'
});
});
$('#login_redirect').click(function() {
auth0.loginWithRedirect({
redirect_uri: 'http://localhost:3000/'
});
});
$('#login_redirect_callback').click(function() {
auth0.handleRedirectCallback().then(function() {
window.history.replaceState(
{},
document.title,
window.location.origin + '/'
);
$('#login_redirect_callback').click(function() {
auth0.handleRedirectCallback().then(function() {
window.history.replaceState(
{},
document.title,
window.location.origin + '/'
);
});
});
});
$('#getToken').click(function() {
auth0.getTokenSilently().then(function(token) {
alert(token);
$('#getToken').click(function() {
auth0.getTokenSilently().then(function(token) {
alert(token);
});
});
});

$('#getTokenPopup').click(function() {
auth0
.getTokenWithPopup({
audience: 'https://brucke.auth0.com/api/v2/',
scope: 'read:rules'
})
.then(function(token) {
console.log(token);
$('#getTokenPopup').click(function() {
auth0
.getTokenWithPopup({
audience: 'https://brucke.auth0.com/api/v2/',
scope: 'read:rules'
})
.then(function(token) {
console.log(token);
});
});
$('#getUser').click(function() {
auth0.getUser().then(function(user) {
alert(JSON.stringify(user, null, 1));
});
});
$('#getUser').click(function() {
auth0.getUser().then(function(user) {
alert(JSON.stringify(user, null, 1));
});
});
$('#getIdTokenClaims').click(function() {
auth0.getIdTokenClaims().then(function(claims) {
console.log(claims);
//if you need the raw id_token, you can access it in the __raw property
const id_token = claims.__raw;
$('#getIdTokenClaims').click(function() {
auth0.getIdTokenClaims().then(function(claims) {
console.log(claims);
//if you need the raw id_token, you can access it in the __raw property
const id_token = claims.__raw;
});
});
});
$('#getToken_audience').click(function() {
var differentAudienceOptions = {
audience: 'https://brucke.auth0.com/api/v2/',
scope: 'read:rules',
redirect_uri: 'http://localhost:3000/callback.html'
};
auth0
.getTokenSilently(differentAudienceOptions)
.then(function(token) {
alert(token);
$('#getToken_audience').click(function() {
var differentAudienceOptions = {
audience: 'https://brucke.auth0.com/api/v2/',
scope: 'read:rules',
redirect_uri: 'http://localhost:3000/callback.html'
};
auth0
.getTokenSilently(differentAudienceOptions)
.then(function(token) {
alert(token);
});
});
$('#logout').click(function() {
auth0.logout({
returnTo: 'http://localhost:3000/'
});
});
$('#logout').click(function() {
auth0.logout({
returnTo: 'http://localhost:3000/'
});
});
$('#logout-no-clientid').click(function() {
auth0.logout({
client_id: null,
returnTo: 'http://localhost:3000/'
$('#logout-no-clientid').click(function() {
auth0.logout({
client_id: null,
returnTo: 'http://localhost:3000/'
});
});
})
.catch(function(err) {
console.error(err);
});
});
});
</script>
</body>
Expand Down