@tapni/auth 1.0.12 → 1.0.13
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/package.json +1 -1
- package/src/mixins/saml.mixin.js +90 -40
package/package.json
CHANGED
package/src/mixins/saml.mixin.js
CHANGED
|
@@ -6,21 +6,30 @@ export default {
|
|
|
6
6
|
data () {
|
|
7
7
|
return {
|
|
8
8
|
code_verifier: '',
|
|
9
|
-
code_challenge: ''
|
|
10
|
-
|
|
9
|
+
code_challenge: '',
|
|
10
|
+
shouldCloseWindow: false,
|
|
11
|
+
popupWindow: null,
|
|
12
|
+
checkInterval: null
|
|
13
|
+
};
|
|
11
14
|
},
|
|
12
15
|
computed: {
|
|
13
16
|
},
|
|
14
17
|
watch: {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
+
'$route.path' (nv) {
|
|
19
|
+
if (nv === '/callback/saml') {
|
|
20
|
+
this.handleSamlRedirect(nv)
|
|
21
|
+
}
|
|
18
22
|
}
|
|
19
|
-
*/
|
|
20
23
|
},
|
|
21
24
|
methods: {
|
|
22
|
-
|
|
23
|
-
|
|
25
|
+
closeWindow() {
|
|
26
|
+
try {
|
|
27
|
+
window.close()
|
|
28
|
+
} catch (e) {
|
|
29
|
+
console.log('Window close error', e);
|
|
30
|
+
}
|
|
31
|
+
},
|
|
32
|
+
async samlLogin(loginUrl) {
|
|
24
33
|
// Create and store a new PKCE code_verifier (the plaintext random secret)
|
|
25
34
|
this.code_verifier = UtilService.generateRandomString(28);
|
|
26
35
|
localStorage.setItem("pkce_code_verifier", this.code_verifier);
|
|
@@ -31,57 +40,98 @@ export default {
|
|
|
31
40
|
const platform = Capacitor.getPlatform();
|
|
32
41
|
|
|
33
42
|
// append public key as relayState
|
|
34
|
-
let dataString = `code_challenge=${this.code_challenge}&platform=${platform}&redirect_uri=${
|
|
43
|
+
let dataString = `code_challenge=${this.code_challenge}&platform=${platform}&redirect_uri=${location.origin + '/callback/saml'}&realm=${this.realm}&display=${this.display}`;
|
|
35
44
|
let relayState = btoa(dataString);
|
|
36
45
|
|
|
37
46
|
loginUrl = `${loginUrl}&RelayState=${relayState}`
|
|
38
47
|
|
|
39
48
|
let self = this;
|
|
40
49
|
|
|
41
|
-
window.addEventListener(
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
if (this.
|
|
45
|
-
|
|
50
|
+
window.addEventListener(
|
|
51
|
+
'message',
|
|
52
|
+
async (message) => {
|
|
53
|
+
if (!this.allowedOriginsAuth.includes(message.origin)) return console.log('Origin is not allowed! ' + message.origin);
|
|
54
|
+
if (message.data.code) {
|
|
55
|
+
console.log('post message from opener', message.data, self.display);
|
|
56
|
+
|
|
57
|
+
if (self.display === 'popup') {
|
|
58
|
+
return window.parent?.postMessage({ code: message.data.code, state: message.data.state, code_verifier: localStorage.getItem('pkce_code_verifier') }, '*');
|
|
59
|
+
}
|
|
60
|
+
await self.exchangeAuthCode({ code: message.data.code, code_verifier: localStorage.getItem('pkce_code_verifier') });
|
|
61
|
+
localStorage.removeItem('pkce_code_verifier');
|
|
46
62
|
}
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
63
|
+
},
|
|
64
|
+
{ once: true }
|
|
65
|
+
);
|
|
66
|
+
|
|
67
|
+
if (this.isNative) {
|
|
68
|
+
this.popupWindow = await Browser.open({ url: loginUrl, presentationStyle: 'popover' });
|
|
69
|
+
} else {
|
|
70
|
+
this.checkInterval = setInterval(() => {
|
|
71
|
+
// check localStorage
|
|
72
|
+
const code = localStorage.getItem('auth_code');
|
|
73
|
+
const pkce = localStorage.getItem('pkce_code_verifier');
|
|
51
74
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
75
|
+
console.log('interval', code, pkce);
|
|
76
|
+
|
|
77
|
+
if (code) {
|
|
78
|
+
clearInterval(this.checkInterval);
|
|
79
|
+
localStorage.removeItem('auth_code');
|
|
80
|
+
localStorage.removeItem('pkce_code_verifier');
|
|
81
|
+
|
|
82
|
+
// handle code exchange
|
|
83
|
+
window.parent?.postMessage({ code: code, state: this.$route.query.state, code_verifier: pkce }, '*');
|
|
84
|
+
|
|
85
|
+
// attempt to close child if possible
|
|
86
|
+
// if (this.popupWindow?.closed === false) this.popupWindow.close();
|
|
87
|
+
}
|
|
88
|
+
}, 500);
|
|
89
|
+
|
|
90
|
+
this.popupWindow = window.open('https://auth.tapni.com/callback/redirect?uri=' + btoa(loginUrl), 'popup', 'width=600,height=600');
|
|
55
91
|
}
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
92
|
+
|
|
93
|
+
if (this.popupWindow) {
|
|
94
|
+
this.popupWindow.addEventListener('beforeunload', () => {
|
|
95
|
+
console.log('popup window closed');
|
|
96
|
+
})
|
|
97
|
+
}
|
|
98
|
+
|
|
62
99
|
},
|
|
63
100
|
async handleSamlRedirect() {
|
|
64
101
|
let code;
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
102
|
+
if (this.$route.query.code) {
|
|
103
|
+
code = this.$route.query.code
|
|
104
|
+
}
|
|
105
|
+
console.log('window1', window.opener);
|
|
106
|
+
console.log('window2', window.parent);
|
|
107
|
+
if (window.opener) {
|
|
108
|
+
window.opener.postMessage({ type: 'saml', code }, '*');
|
|
109
|
+
this.closeWindow();
|
|
70
110
|
} else {
|
|
71
|
-
if(this.isNative && this.isiOS) await Browser.close();
|
|
72
|
-
EventBus.$emit('ssoEvent', {name: 'setLoading', data: true})
|
|
73
|
-
if (this.$route.query.code) code = this.$route.query.code
|
|
111
|
+
if (this.isNative && this.isiOS) await Browser.close();
|
|
112
|
+
EventBus.$emit('ssoEvent', { name: 'setLoading', data: true });
|
|
74
113
|
|
|
75
|
-
console.log(this.display, {code});
|
|
114
|
+
console.log(this.display, { code }, window.parent, window.opener);
|
|
76
115
|
|
|
77
116
|
if (code) {
|
|
78
117
|
if (this.display === 'popup') {
|
|
79
|
-
|
|
118
|
+
localStorage.setItem('auth_code', code);
|
|
119
|
+
this.shouldCloseWindow = true;
|
|
120
|
+
window.parent?.postMessage({ code: code, state: this.$route.query.state }, '*');
|
|
121
|
+
this.closeWindow()
|
|
122
|
+
}
|
|
123
|
+
// TODO: Replace URLs with .env variables to support staging
|
|
124
|
+
if (this.display === 'redirect' || location.host === 'auth.tapni.com') {
|
|
125
|
+
let redirect_url = 'https://auth.tapni.com/callback/auth';
|
|
126
|
+
if (this.realm === 'dashboard') {
|
|
127
|
+
redirect_url = 'https://business.tapni.com/login'
|
|
128
|
+
}
|
|
129
|
+
return location.href = redirect_url + '?code=' + code + '&code_verifier=' + localStorage.getItem('pkce_code_verifier');
|
|
80
130
|
}
|
|
81
|
-
await this.exchangeAuthCode({code, code_verifier: localStorage.getItem(
|
|
131
|
+
await this.exchangeAuthCode({ code, code_verifier: localStorage.getItem('pkce_code_verifier') });
|
|
82
132
|
}
|
|
83
|
-
localStorage.removeItem(
|
|
84
|
-
EventBus.$emit('ssoEvent', {name: 'setLoading', data: false})
|
|
133
|
+
// localStorage.removeItem('pkce_code_verifier');
|
|
134
|
+
EventBus.$emit('ssoEvent', { name: 'setLoading', data: false });
|
|
85
135
|
}
|
|
86
136
|
}
|
|
87
137
|
},
|