saasufy-components 2.4.0 → 2.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/oauth-handler.js CHANGED
@@ -4,19 +4,6 @@ import { generateRandomHexString } from './utils.js';
4
4
  const DEFAULT_AUTH_TIMEOUT = 10000;
5
5
 
6
6
  class OAuthHandler extends SocketConsumer {
7
- static get observedAttributes() {
8
- return [
9
- 'provider',
10
- 'code-param-name',
11
- 'state-param-name',
12
- 'state-session-storage-key',
13
- 'success-url',
14
- 'success-location-hash',
15
- 'navigate-event-path',
16
- 'auth-timeout'
17
- ];
18
- }
19
-
20
7
  successRedirect() {
21
8
  let successURL = this.getAttribute('success-url');
22
9
  let successLocationHash = this.getAttribute('success-location-hash');
@@ -46,9 +33,19 @@ class OAuthHandler extends SocketConsumer {
46
33
  throw new Error('The provider attribute of oauth-handler was not specified');
47
34
  }
48
35
 
49
- let sessionStorageKey = this.getAttribute('state-session-storage-key') || `oauth.${provider}.state`;
36
+ let sessionStorageKey = this.getAttribute('state-session-storage-key') || 'oauth.state';
50
37
  let expectedOAuthState = sessionStorage.getItem(sessionStorageKey);
51
- if (!expectedOAuthState) return;
38
+ if (!expectedOAuthState) {
39
+ this.innerHTML = `
40
+ <div class="error">OAuth authentication failed because the state query parameter was missing.</div>
41
+ `;
42
+ return;
43
+ };
44
+
45
+ let stateParts = expectedOAuthState.split('-');
46
+ // Do not process. The state may have been set for a different provider
47
+ // which is handled on the same page.
48
+ if (stateParts[0] !== provider) return;
52
49
 
53
50
  let codeParamName = this.getAttribute('code-param-name') || 'code';
54
51
  let stateParamName = this.getAttribute('state-param-name') || 'state';
package/oauth-link.js CHANGED
@@ -27,6 +27,10 @@ class OAuthLink extends HTMLElement {
27
27
 
28
28
  connectedCallback() {
29
29
  this.isReady = true;
30
+
31
+ let urlSearchParams = new URLSearchParams(location.search);
32
+ this.code = urlSearchParams.get('code');
33
+
30
34
  this.render();
31
35
  this.shadowRoot.addEventListener('slotchange', this.handleSlotChangeEvent);
32
36
  }
@@ -36,27 +40,21 @@ class OAuthLink extends HTMLElement {
36
40
  }
37
41
 
38
42
  renderItem() {
39
- let provider = this.getAttribute('provider');
40
- if (!provider) {
41
- throw new Error('The provider attribute of oauth-link was not specified');
42
- }
43
+ if (this.code || !this.state) return;
44
+
43
45
  let viewportNode = this.shadowRoot.querySelector('slot[name="viewport"]').assignedNodes()[0];
44
46
  if (!viewportNode) return;
45
47
  let itemTemplate = this.shadowRoot.querySelector('slot[name="item"]').assignedNodes()[0];
46
48
  if (!itemTemplate) return;
47
49
 
48
50
  let clientId = this.getAttribute('client-id');
49
- let stateSize = Number(this.getAttribute('state-size') || 20);
50
- let sessionStorageKey = this.getAttribute('state-session-storage-key') || `oauth.${provider}.state`;
51
- let state = generateRandomHexString(stateSize);
52
- sessionStorage.setItem(sessionStorageKey, state);
53
51
 
54
52
  let itemString = renderTemplate(
55
53
  itemTemplate.innerHTML,
56
54
  {
57
55
  oauth: {
58
56
  clientId,
59
- state
57
+ state: this.state
60
58
  }
61
59
  }
62
60
  );
@@ -64,14 +62,20 @@ class OAuthLink extends HTMLElement {
64
62
  }
65
63
 
66
64
  render() {
67
- let urlSearchParams = new URLSearchParams(location.search);
68
- let code = urlSearchParams.get('code');
69
-
70
- if (code) {
65
+ if (this.code) {
71
66
  this.shadowRoot.innerHTML = '';
72
67
  return;
73
68
  }
74
69
 
70
+ let provider = this.getAttribute('provider');
71
+ if (!provider) {
72
+ throw new Error('The provider attribute of oauth-link was not specified');
73
+ }
74
+ let stateSize = Number(this.getAttribute('state-size') || 20);
75
+ let sessionStorageKey = this.getAttribute('state-session-storage-key') || 'oauth.state';
76
+ this.state = `${provider}-${generateRandomHexString(stateSize)}`;
77
+ sessionStorage.setItem(sessionStorageKey, this.state);
78
+
75
79
  this.shadowRoot.innerHTML = `
76
80
  <slot name="item"></slot>
77
81
  <slot name="viewport"></slot>
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "saasufy-components",
3
- "version": "2.4.0",
3
+ "version": "2.4.1",
4
4
  "description": "Components for Saasufy.",
5
5
  "main": "index.js",
6
6
  "scripts": {