PageRenderTime 45ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/ajax/libs/salesforce-canvas/27.0/oauth.js

https://gitlab.com/Mirros/cdnjs
JavaScript | 308 lines | 155 code | 32 blank | 121 comment | 36 complexity | 9a4f5bb145b1583437716eb9b962d1be MD5 | raw file
  1. /**
  2. *@namespace Sfdc.canvas.oauth
  3. *@name Sfdc.canvas.oauth
  4. */
  5. (function ($$) {
  6. "use strict";
  7. var module = (function() {
  8. var accessToken,
  9. instUrl,
  10. instId,
  11. tOrigin,
  12. childWindow;
  13. function init() {
  14. // Get the access token from the cookie (needed to survive refresh),
  15. // and then remove the cookie per security's request.
  16. accessToken = $$.cookies.get("access_token");
  17. $$.cookies.remove("access_token");
  18. }
  19. function query(params) {
  20. var r = [], n;
  21. if (!$$.isUndefined(params)) {
  22. for (n in params) {
  23. if (params.hasOwnProperty(n)) {
  24. // probably should encode these
  25. r.push(n + "=" + params[n]);
  26. }
  27. }
  28. return "?" + r.join('&');
  29. }
  30. return '';
  31. }
  32. /**
  33. *@private
  34. */
  35. function refresh() {
  36. // Temporarily set the oauth token in a cookie and then remove it
  37. // after the refresh.
  38. $$.cookies.set("access_token", accessToken);
  39. self.location.reload();
  40. }
  41. /**
  42. * @name Sfdc.canvas.oauth#login
  43. * @function
  44. * @description Opens the OAuth popup window to retrieve an OAuth token
  45. * @param {Object} ctx Context object that contains the url, the response type, the client id and callback url
  46. * @docneedsimprovement
  47. * @example
  48. * function clickHandler(e)
  49. * {
  50. * var uri;
  51. * if (! connect.oauth.loggedin())
  52. * {
  53. * uri = connect.oauth.loginUrl();
  54. * connect.oauth.login(
  55. * {uri : uri,
  56. * params: {
  57. * response_type : "token",
  58. * client_id : "<%=consumerKey%>",
  59. * redirect_uri : encodeURIComponent("/sdk/callback.html")
  60. * }});
  61. * } else {
  62. * connect.oauth.logout();
  63. * }
  64. * return false;
  65. * }
  66. */
  67. function login(ctx) {
  68. var uri;
  69. ctx = ctx || {};
  70. uri = ctx.uri || "/rest/oauth2";
  71. ctx.params = ctx.params || {state : ""};
  72. ctx.params.state = ctx.params.state || ctx.callback || window.location.pathname; // @TODO REVIEW THIS
  73. ctx.params.display= ctx.params.display || 'popup';
  74. uri = uri + query(ctx.params);
  75. childWindow = window.open(uri, 'OAuth', 'status=0,toolbar=0,menubar=0,resizable=0,scrollbars=1,top=50,left=50,height=500,width=680');
  76. }
  77. /**
  78. * @name Sfdc.canvas.oauth#token
  79. * @function
  80. * @description Sets, gets or removes the <code>access_token</code> from this JS object <br>
  81. <p>This function does one of three things <br>
  82. If the 't' parameter is not passed in, the current value for the <code>access_token</code> value is returned. <br>
  83. If the the 't' parameter is null, the <code>access_token</code> value is removed. <br>
  84. Note: for longer term storage of the OAuth token store it server side in the session, access tokens
  85. should never be stored in cookies.
  86. Otherwise the <code>access_token</code> value is set to the 't' parameter and then returned.
  87. * @param {String} [t] The oauth token to set as the <code>access_token</code> value
  88. * @returns {String} The resulting <code>access_token</code> value if set, otherwise null
  89. */
  90. function token(t) {
  91. if (arguments.length === 0) {
  92. if (!$$.isNil(accessToken)) {return accessToken;}
  93. }
  94. else {
  95. accessToken = t;
  96. }
  97. return accessToken;
  98. }
  99. /**
  100. * @name Sfdc.canvas.oauth#instance
  101. * @function
  102. * @description Sets, gets or removes the <code>instance_url</code> cookie <br>
  103. <p> This function does one of three things <br>
  104. If the 'i' parameter is not passed in, the current value for the <code>instance_url</code> cookie is returned. <br>
  105. If the 'i' parameter is null, the <code>instance_url</code> cookie is removed. <br>
  106. Otherwise the <code>instance_url</code> cookie value is set to the 'i' parameter and then returned.
  107. * @param {String} [i] The value to set as the <code>instance_url</code> cookie
  108. * @returns {String} The resulting <code>instance_url</code> cookie value if set, otherwise null
  109. */
  110. function instanceUrl(i) {
  111. if (arguments.length === 0) {
  112. if (!$$.isNil(instUrl)) {return instUrl;}
  113. instUrl = $$.cookies.get("instance_url");
  114. }
  115. else if (i === null) {
  116. $$.cookies.remove("instance_url");
  117. instUrl = null;
  118. }
  119. else {
  120. $$.cookies.set("instance_url", i);
  121. instUrl = i;
  122. }
  123. return instUrl;
  124. }
  125. /**
  126. *@private
  127. */
  128. // Example Results of tha hash....
  129. // Name [access_token] Value [00DU0000000Xthw!ARUAQMdYg9ScuUXB5zPLpVyfYQr9qXFO7RPbKf5HyU6kAmbeKlO3jJ93gETlJxvpUDsz3mqMRL51N1E.eYFykHpoda8dPg_z]
  130. // Name [instance_url] Value [https://na12.salesforce.com]
  131. // Name [id] Value [https://login.salesforce.com/id/00DU0000000XthwMAC/005U0000000e6PoIAI]
  132. // Name [issued_at] Value [1331000888967]
  133. // Name [signature] Value [LOSzVZIF9dpKvPU07icIDOf8glCFeyd4vNGdj1dhW50]
  134. // Name [state] Value [/crazyrefresh.html]
  135. function parseHash(hash) {
  136. var i, nv, nvp, n, v;
  137. if (! $$.isNil(hash)) {
  138. if (hash.indexOf('#') === 0) {
  139. hash = hash.substr(1);
  140. }
  141. nvp = hash.split("&");
  142. for (i = 0; i < nvp.length; i += 1) {
  143. nv = nvp[i].split("=");
  144. n = nv[0];
  145. v = decodeURIComponent(nv[1]);
  146. if ("access_token" === n) {
  147. token(v);
  148. }
  149. else if ("instance_url" === n) {
  150. instanceUrl(v);
  151. }
  152. else if ("target_origin" === n) {
  153. tOrigin = decodeURIComponent(v);
  154. }
  155. else if ("instance_id" === n) {
  156. instId = v;
  157. }
  158. }
  159. }
  160. }
  161. /**
  162. * @name Sfdc.canvas.oauth#checkChildWindowStatus
  163. * @function
  164. * @description Refreshes the parent window only if the child window is closed.
  165. */
  166. function checkChildWindowStatus() {
  167. if (!childWindow || childWindow.closed) {
  168. refresh();
  169. }
  170. }
  171. /**
  172. * @name Sfdc.canvas.oauth#childWindowUnloadNotification
  173. * @function
  174. * @description Parses the hash value that is passed in and sets the
  175. <code>access_token</code> and <code>instance_url</code> cookies if they exist. Use during
  176. User-Agent OAuth Authentication Flow to pass the OAuth token
  177. * @param {String} hash Typically a string of key-value pairs delimited by
  178. the ampersand character.
  179. * @example
  180. * Sfdc.canvas.oauth.childWindowUnloadNotification(self.location.hash);
  181. */
  182. function childWindowUnloadNotification(hash) {
  183. // Here we get notification from child window. Here we can decide if such notification is
  184. // raised because user closed child window, or because user is playing with F5 key.
  185. // NOTE: We can not trust on "onUnload" event of child window, because if user reload or refresh
  186. // such window in fact he is not closing child. (However "onUnload" event is raised!)
  187. //checkChildWindowStatus();
  188. parseHash(hash);
  189. setTimeout(window.Sfdc.canvas.oauth.checkChildWindowStatus, 50);
  190. }
  191. /**
  192. * @name Sfdc.canvas.oauth#logout
  193. * @function
  194. * @description Removes the <code>access_token</code> oauth token from this object.
  195. */
  196. function logout() {
  197. // Remove the oauth token and refresh the browser
  198. token(null);
  199. // @todo: do we want to do this?
  200. //var home = $$.cookies.get("home");
  201. //window.location = home || window.location;
  202. }
  203. /**
  204. * @name Sfdc.canvas.oauth#loggedin
  205. * @function
  206. * @description Returns the login state
  207. * @returns {Boolean} <code>true</code> if the <code>access_token</code> is available in this JS object.
  208. * Note: <code>access tokens</code> (i.e. OAuth tokens) should be stored server side for more durability.
  209. * Never store OAuth tokens in cookies as this can lead to a security risk.
  210. */
  211. function loggedin() {
  212. return !$$.isNil(token());
  213. }
  214. /**
  215. * @name Sfdc.canvas.oauth#loginUrl
  216. * @function
  217. * @description Returns the url for the OAuth authorization service
  218. * @returns {String} The url for the OAuth authorization service or default if there is
  219. * not a value for loginUrl in the current url's query string.
  220. */
  221. function loginUrl() {
  222. var i, nvs, nv, q = self.location.search;
  223. if (q) {
  224. q = q.substring(1);
  225. nvs = q.split("&");
  226. for (i = 0; i < nvs.length; i += 1)
  227. {
  228. nv = nvs[i].split("=");
  229. if ("loginUrl" === nv[0]) {
  230. return decodeURIComponent(nv[1]) + "/services/oauth2/authorize";
  231. }
  232. }
  233. }
  234. return "https://login.salesforce.com/services/oauth2/authorize";
  235. }
  236. function targetOrigin(to) {
  237. if (!$$.isNil(to)) {
  238. tOrigin = to;
  239. return to;
  240. }
  241. if (!$$.isNil(tOrigin)) {return tOrigin;}
  242. // This relies on the parent passing it in. This may not be there as the client can do a
  243. // redirect or link to another page
  244. parseHash(document.location.hash);
  245. return tOrigin;
  246. }
  247. function instanceId(id) {
  248. if (!$$.isNil(id)) {
  249. instId = id;
  250. return id;
  251. }
  252. if (!$$.isNil(instId)) {return instId;}
  253. // This relies on the parent passing it in. This may not be there as the client can do a
  254. // redirect or link to another page
  255. parseHash(document.location.hash);
  256. return instId;
  257. }
  258. function client() {
  259. return {oauthToken : token(), instanceId : instanceId(), targetOrigin : targetOrigin()};
  260. }
  261. return {
  262. init : init,
  263. login : login,
  264. logout : logout,
  265. loggedin : loggedin,
  266. loginUrl : loginUrl,
  267. token : token,
  268. instance : instanceUrl,
  269. client : client,
  270. checkChildWindowStatus : checkChildWindowStatus,
  271. childWindowUnloadNotification: childWindowUnloadNotification
  272. };
  273. }());
  274. $$.module('Sfdc.canvas.oauth', module);
  275. $$.oauth.init();
  276. }(Sfdc.canvas));