PageRenderTime 42ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/js/ydn/oauth2/core.js

https://bitbucket.org/ytkyaw/ydn-auth
JavaScript | 245 lines | 132 code | 19 blank | 94 comment | 23 complexity | e913ec163909744c8442466d464f4a82 MD5 | raw file
Possible License(s): Apache-2.0
  1. // Copyright 2011 Google Inc. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. /**
  15. * @license Portions of this code are from Google Inc's
  16. * oauth2-postmessage-profile project,
  17. * received by YDN Authors under the Apache 2.0 license.
  18. */
  19. /**
  20. * http://code.google.com/p/oauth2-postmessage-profile/
  21. */
  22. /** OAuth 2 core utilities. */
  23. goog.provide('ydn.oauth2');
  24. goog.provide('ydn.oauth2.core');
  25. goog.require('ydn.oauth2.core.config');
  26. ydn.oauth2.applicationUri = null;
  27. ydn.oauth2.clientId = null;
  28. /**
  29. *
  30. * @type {?string}
  31. */
  32. ydn.oauth2.scopes = null;
  33. ydn.oauth2.oauth2userHint = null;
  34. /**
  35. *
  36. * @type {?string}
  37. */
  38. ydn.oauth2.overrideSessionIndex = null;
  39. /**
  40. *
  41. * @type {?string}
  42. */
  43. ydn.oauth2.overrideHostedDomain = null;
  44. goog.scope(function() {
  45. var oauth2 = ydn.oauth2;
  46. var config = oauth2.core.config;
  47. var applicationUri = ydn.oauth2.applicationUri;
  48. var clientId = ydn.oauth2.clientId;
  49. var scopes = ydn.oauth2.scopes;
  50. var oauth2userHint = ydn.oauth2.oauth2userHint;
  51. var overrideSessionIndex = ydn.oauth2.overrideSessionIndex;
  52. var overrideHostedDomain = ydn.oauth2.overrideHostedDomain;
  53. /**
  54. * Configure OAuth 2 client ID.
  55. * @param {?string} value The client ID. A null will be treated as
  56. * an empty string which will then be omitted from an
  57. * authorization URL.
  58. */
  59. oauth2.core.setClientId = function(value) {
  60. clientId = String((value == null) ? '' : value);
  61. };
  62. /**
  63. * Configure required authorization scopes.
  64. * @param {string|Array.<string>|null} value The list of required
  65. * scopes; an array will be joined with spaces, and null will
  66. * be treated as an empty string which will then be omitted
  67. * from an authorization URL.
  68. */
  69. oauth2.core.setScopes = function(value) {
  70. if (typeof(value) != 'string') {
  71. if (value == null) {
  72. scopes = '';
  73. } else if (value.length >= 0) {
  74. scopes = value.join(' ');
  75. }
  76. } else {
  77. scopes = value;
  78. }
  79. scopes = String(scopes);
  80. };
  81. /**
  82. * Returns the current session index for Google's multiple sign-in
  83. * system.
  84. * @return {?string} The session index, or null.
  85. */
  86. oauth2.core.getSessionIndex = function() {
  87. if (overrideSessionIndex != null) {
  88. return overrideSessionIndex;
  89. }
  90. if (!applicationUri) {
  91. applicationUri = shindig.uri(window.location.href);
  92. }
  93. return applicationUri.getQP(config.SESSION_INDEX_PARAM);
  94. };
  95. /**
  96. * Sets the session index for Google's multiple sign-in system.
  97. * @param {?string} sessionIndex The session index, or null to
  98. * reset to the default value based on the 'authuser' URL
  99. * parameter.
  100. */
  101. oauth2.core.setSessionIndex = function(sessionIndex) {
  102. overrideSessionIndex = sessionIndex;
  103. };
  104. /**
  105. * Returns the Google Apps hosted domain.
  106. * @return {?string} The domain, or null.
  107. */
  108. oauth2.core.getHostedDomain = function() {
  109. if (overrideHostedDomain != null) {
  110. return overrideHostedDomain;
  111. }
  112. if (!applicationUri) {
  113. applicationUri = shindig.uri(window.location.href);
  114. }
  115. return applicationUri.getQP(config.HOSTED_DOMAIN_PARAM);
  116. };
  117. /**
  118. * Sets the Google Apps hosted domain.
  119. * @param {?string} hostedDomain The domain, or null to reset to
  120. * the default value based on the 'hd' URL parameter.
  121. */
  122. oauth2.core.setHostedDomain = function(hostedDomain) {
  123. overrideHostedDomain = hostedDomain;
  124. };
  125. /**
  126. * Sets the user hint for subsequent immediate-mode authorization
  127. * flows.
  128. * @param {?string} userHint The user hint (e.g. an email
  129. * address), or null to reset the hint.
  130. */
  131. oauth2.core.setUserHint = function(userHint) {
  132. oauth2userHint = userHint;
  133. };
  134. /**
  135. * Returns the current user hint for immediate-mode authorization
  136. * flows.
  137. * @return {?string} The user hint, or null if no hint is set.
  138. */
  139. oauth2.core.getUserHint = function() {
  140. return oauth2userHint;
  141. };
  142. /**
  143. * Generates the URL for an OAuth 2 end-user authorization flow.
  144. * @param {boolean} isImmediate If true, the authorization flow will use the
  145. * "immediate" mode which is IFRAME-able and skips all user
  146. * interface and either immediately succeeds (due to prior
  147. * approval) or fails; otherwise user interface may be
  148. * displayed and the flow is not IFRAME-able.
  149. * @return {string} The authorization URL.
  150. */
  151. oauth2.core.getAuthUrl = function(isImmediate) {
  152. var sessionIndex = oauth2.core.getSessionIndex();
  153. var hostedDomain = oauth2.core.getHostedDomain();
  154. var urlParts = [];
  155. // Request an access token.
  156. urlParts.push(
  157. config.AUTH_URL +
  158. '?' +
  159. encodeURIComponent(config.RESPONSE_TYPE_PARAM) +
  160. '=' +
  161. encodeURIComponent(config.RESPONSE_TYPE_ACCESS_TOKEN_PARAM_VALUE));
  162. if (clientId) {
  163. // Client ID.
  164. urlParts.push(
  165. encodeURIComponent(config.CLIENT_ID_PARAM) +
  166. '=' +
  167. encodeURIComponent(clientId));
  168. }
  169. if (scopes) {
  170. // Authorization scopes.
  171. urlParts.push(
  172. encodeURIComponent(config.SCOPE_PARAM) +
  173. '=' +
  174. encodeURIComponent(scopes));
  175. }
  176. // State to prevent replay and spoofing attacks.
  177. urlParts.push(
  178. encodeURIComponent(config.STATE_PARAM) +
  179. '=' +
  180. encodeURIComponent(config.AUTH_STATE));
  181. // Request postMessage result return
  182. urlParts.push(
  183. encodeURIComponent(config.POSTMESSAGE_PARAM) +
  184. '=' +
  185. encodeURIComponent(config.POSTMESSAGE_PARAM_VALUE));
  186. // Proxy IFRAME ID for postMessage result return.
  187. urlParts.push(
  188. encodeURIComponent(config.PROXY_PARAM) +
  189. '=' +
  190. encodeURIComponent(config.PROXY_ID));
  191. // Application URL origin for postMessage.
  192. urlParts.push(
  193. encodeURIComponent(config.ORIGIN_PARAM) +
  194. '=' +
  195. encodeURIComponent(config.ORIGIN));
  196. if (isImmediate) {
  197. urlParts.push(
  198. encodeURIComponent(config.IMMEDIATE_PARAM) +
  199. '=' +
  200. encodeURIComponent(config.IMMEDIATE_PARAM_VALUE));
  201. if (oauth2userHint) {
  202. urlParts.push(
  203. encodeURIComponent(config.USER_ID_PARAM) +
  204. '=' +
  205. encodeURIComponent(oauth2userHint));
  206. sessionIndex = null;
  207. hostedDomain = null;
  208. } else if (!(sessionIndex || hostedDomain)) {
  209. sessionIndex = '0';
  210. }
  211. }
  212. if (sessionIndex) {
  213. urlParts.push(
  214. encodeURIComponent(config.SESSION_INDEX_PARAM) +
  215. '=' +
  216. encodeURIComponent(sessionIndex));
  217. }
  218. if (hostedDomain) {
  219. urlParts.push(
  220. encodeURIComponent(config.HOSTED_DOMAIN_PARAM) +
  221. '=' +
  222. encodeURIComponent(hostedDomain));
  223. }
  224. return urlParts.join('&');
  225. };
  226. });