cloud-view.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /**
  2. * This framework is intended to work and handle authentication with the cloud view engine
  3. * DEPENDENCIES :
  4. * rpc.js HttpClient, urlparser
  5. * @TODO: A registration process maybe required
  6. */
  7. /**
  8. * @param id service identifier
  9. * @param key callback/ user key
  10. */
  11. jx.cloudview = { popup:null,cache: {},oauth:{},url:'https://the-phi.com/cloud-view',host:'the-phi.com',protocol:'https'}
  12. jx.cloudview.init = function(url,redir_url){//host,protocol){
  13. // jx.cloudview.host = host
  14. // jx.cloudview.protocol = protocol
  15. jx.cloudview.url = url.trim().replace(/\/$/,'')
  16. if(redir_url != null){
  17. jx.cloudview.redirect_url = redir_url
  18. }
  19. }
  20. jx.cloudview.oauth.init = function (id, key,callback,err) {
  21. // var url = ":protocol://:host/cloud-view/" +id+"/get"
  22. // url = url.replace(/:protocol/,jx.cloudview.protocol).replace(/:host/,jx.cloudview.host)
  23. var url = ([jx.cloudview.url,id,'get']).join('/')
  24. var httpclient = HttpClient.instance()
  25. httpclient.setHeader("platform",navigator.appName)
  26. try{
  27. httpclient.post(url, function (x) {
  28. var url = x.responseText
  29. if(url.match(/^http.+$/)){
  30. var oauth_uri = url.match(/redirect_uri=(.+)\&/)[1];
  31. url = url.replace(oauth_uri, key)
  32. jx.cloudview.handler = null
  33. jx.cloudview.popup = window.open(url, 'oauth', 'width=405, height=900')
  34. jx.cloudview.popup.focus()
  35. jx.cloudview.oauth.listen(key,callback,err)
  36. }else{
  37. setTimeout(()=>{
  38. jx.dom.set.value('dialog.status','<i class="fa fa-times" style="color:maroon; font-size:12px"></i> http error '+x.status+' ... closing window')
  39. err()
  40. },2500);
  41. }
  42. })
  43. }catch(error){
  44. err()
  45. }
  46. }
  47. /**
  48. * @param key
  49. */
  50. jx.cloudview.oauth.listen = function (key,callback,err) {
  51. if (jx.cloudview.handler != null) {
  52. clearInterval(jx.cloudview.handler)
  53. }
  54. jx.cloudview.handler = setInterval(function () {
  55. try {
  56. if (jx.cloudview.popup.location.search.match(/code=/)) {
  57. clearInterval(jx.cloudview.handler)
  58. var p = urlparser(jx.cloudview.popup.location.search)
  59. var url = ([":protocol://:host/cloud-view/", p.state, "/set/authentication"]).join('')
  60. url = url.replace(/:protocol/,jx.cloudview.protocol).replace(/:host/,jx.cloudview.host)
  61. var http = HttpClient.instance()
  62. http.setHeader('code', encodeURIComponent(p.code))
  63. http.setHeader('pid', 'authentication')
  64. http.setHeader('platform', navigator.appName)
  65. http.setHeader('redirect_uri', key)
  66. http.post(url, function (x) {
  67. var info = JSON.parse(x.responseText)
  68. callback(info)
  69. // jx.dom.set.value('name', info.user.uii)
  70. })
  71. }
  72. //
  73. // Until the control is returned an exception will be generated
  74. // So the popup.close will never be executed ...
  75. jx.cloudview.popup.close()
  76. } catch (error) {
  77. //
  78. // If the window was closed chances are the user closed the window without loging in
  79. if(jx.cloudview.popup != null){
  80. if(jx.cloudview.popup.closed){
  81. clearInterval(jx.cloudview.handler)
  82. if (err != null){
  83. err()
  84. }
  85. }
  86. }
  87. console.log([jx.cloudview.popup.closed,error])
  88. }
  89. },1500)
  90. }