You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

72 lines
2.1 KiB
JavaScript

/**
* This framework is intended to work and handle authentication with the cloud view engine
* DEPENDENCIES :
* rpc.js HttpClient, urlparser
* @TODO: A registration process maybe required
*/
/**
* @param id service identifier
* @param key callback/ user key
*/
jx.cloudview = { popup:null,cache: {},oauth:{},host:'the-phi.com',protocol:'https'}
jx.cloudview.init = function(host,protocol){
jx.cloudview.host = host
jx.cloudview.protocol = protocol
}
jx.cloudview.oauth.init = function (id, key,callback) {
var url = ":protocol://:host/cloud-view/" +id+"/get"
url = url.replace(/:protocol/,jx.cloudview.protocol).replace(/:host/,jx.cloudview.host)
var httpclient = HttpClient.instance()
httpclient.setHeader("platform",navigator.appName)
httpclient.post(url, function (x) {
var url = x.responseText
var oauth_uri = url.match(/redirect_uri=(.+)&/)[1];
url = url.replace(oauth_uri, key)
jx.cloudview.handler = null
jx.cloudview.popup = window.open(url, 'oauth', 'width=405, height=900')
jx.cloudview.oauth.listen(key,callback)
})
}
/**
* @param key
*/
jx.cloudview.oauth.listen = function (key,callback) {
if (jx.cloudview.handler != null) {
clearInterval(jx.cloudview.handler)
}
jx.cloudview.handler = setInterval(function () {
try {
if (jx.cloudview.popup.location.search.match(/code=/)) {
clearInterval(jx.cloudview.handler)
var p = urlparser(jx.cloudview.popup.location.search)
var url = ([":protocol://:host/cloud-view/", p.state, "/set/authentication"]).join('')
url = url.replace(/:protocol/,jx.cloudview.protocol).replace(/:host/,jx.cloudview.host)
var http = HttpClient.instance()
http.setHeader('code', encodeURIComponent(p.code))
http.setHeader('pid', 'authentication')
http.setHeader('platform', navigator.appName)
http.setHeader('redirect_uri', key)
http.post(url, function (x) {
var info = JSON.parse(x.responseText)
callback(info)
// jx.dom.set.value('name', info.user.uii)
})
}
jx.cloudview.popup.close()
} catch (error) {
// console.log(error)
}
},1500)
}