-
Notifications
You must be signed in to change notification settings - Fork 35
/
sdk.js
115 lines (96 loc) · 2.94 KB
/
sdk.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/* globals bowser, SecureLogin */
window.addEventListener('load', function(){
var origin = 'https://securelogin.pw'
var sproxy = document.createElement('iframe')
sproxy.src = origin + '/s'
sproxy.style.display='none'
document.body.appendChild(sproxy)
window.addEventListener('message', function msgListener (e) {
if (e.origin === origin) {
if (['web', 'ext', 'app'].indexOf(e.data.client) != -1){
var flow = e.data.client
} else {
// not a user
var flow = 'web'
}
console.log(e.data.client)
localStorage.securelogin = flow
window.removeEventListener('message', msgListener)
}
})
})
window.SecureLogin = function (cb, scope) {
var origin = 'https://securelogin.pw'
SecureLogin.w = false
function singleWindow (url) {
if (SecureLogin.w) {
SecureLogin.w.location = url
} else {
SecureLogin.w = window.open(url)
}
}
function toQuery (obj) {
return Object.keys(obj).reduce(function (a, k) {
a.push(encodeURIComponent(k) + '=' + encodeURIComponent(obj[k]))
return a
}, []).join('&')
}
var useClient = function(flow){
var opts = {}
if (SecureLogin.pubkey) opts.pubkey = SecureLogin.pubkey
if (scope) opts.scope = toQuery(scope)
var query = toQuery(opts)
var webOrigin = window.location.host === 'c.dev' ? 'http://securelogin.dev' : 'https://securelogin.pw'
var extOrigin = 'chrome-extension://abpigncghjblhknbbannlhmgjpjpbajj'
if (flow === 'app') {
window.location = 'securelogin://'
var startInterval = new Date()
var slInterval = setInterval(function () {
var x = new window.WebSocket('ws://127.0.0.1:3101')
x.onmessage = function (e) {
console.log(e.data)
x.send('{"data":"close"}')
x.close()
cb(e.data)
cb = function (str) {
console.log('replay')
}
}
x.onopen = function () {
x.send(JSON.stringify({
data: query
}))
clearInterval(slInterval)
}
if (new Date() - startInterval > 3000) {
alert("Please make sure SecureLogin app is running")
clearInterval(slInterval)
}
}, 200)
} else {
var origin = ''
// ext needs /index.html part
if (flow === 'web') {
origin = webOrigin
singleWindow(webOrigin)
} else {
origin = extOrigin
singleWindow(extOrigin + '/index.html')
}
window.addEventListener('message', function msgListener (e) {
if (e.origin === origin) {
if (e.data === 'ping') {
//if (flow === 'ext') clearInterval(loadext)
e.source.postMessage(query, origin)
} else {
cb(e.data)
window.removeEventListener('message', msgListener)
SecureLogin.w.close()
}
}
})
}
}
useClient(localStorage.securelogin)
return false
}