| 
									
										
										
										
											2017-02-20 11:12:48 -07:00
										 |  |  | ;(function (exports) { | 
					
						
							|  |  |  | 'use strict'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-27 18:19:01 -07:00
										 |  |  | var OAUTH3 = exports.OAUTH3 = exports.OAUTH3 || require('./oauth3.core.js').OAUTH3; | 
					
						
							| 
									
										
										
										
											2017-02-21 11:49:41 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | OAUTH3.query.parse = function (search) { | 
					
						
							| 
									
										
										
										
											2017-02-20 15:22:48 -07:00
										 |  |  |   // parse a query or a hash
 | 
					
						
							|  |  |  |   if (-1 !== ['#', '?'].indexOf(search[0])) { | 
					
						
							|  |  |  |     search = search.substring(1); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   // Solve for case of search within hash
 | 
					
						
							|  |  |  |   // example: #/authorization_dialog/?state=...&redirect_uri=...
 | 
					
						
							|  |  |  |   var queryIndex = search.indexOf('?'); | 
					
						
							|  |  |  |   if (-1 !== queryIndex) { | 
					
						
							|  |  |  |     search = search.substr(queryIndex + 1); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   var args = search.split('&'); | 
					
						
							|  |  |  |   var argsParsed = {}; | 
					
						
							|  |  |  |   var i, arg, kvp, key, value; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   for (i = 0; i < args.length; i += 1) { | 
					
						
							|  |  |  |     arg = args[i]; | 
					
						
							|  |  |  |     if (-1 === arg.indexOf('=')) { | 
					
						
							|  |  |  |       argsParsed[decodeURIComponent(arg).trim()] = true; | 
					
						
							| 
									
										
										
										
											2017-02-20 11:12:48 -07:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-02-20 15:22:48 -07:00
										 |  |  |     else { | 
					
						
							|  |  |  |       kvp = arg.split('='); | 
					
						
							|  |  |  |       key = decodeURIComponent(kvp[0]).trim(); | 
					
						
							|  |  |  |       value = decodeURIComponent(kvp[1]).trim(); | 
					
						
							|  |  |  |       argsParsed[key] = value; | 
					
						
							| 
									
										
										
										
											2017-02-20 11:12:48 -07:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-02-20 15:22:48 -07:00
										 |  |  |   } | 
					
						
							|  |  |  |   return argsParsed; | 
					
						
							|  |  |  | }; | 
					
						
							| 
									
										
										
										
											2017-02-21 11:49:41 -07:00
										 |  |  | OAUTH3.scope.parse = function (scope) { | 
					
						
							| 
									
										
										
										
											2017-02-20 15:22:48 -07:00
										 |  |  |   return (scope||'').split(/[, ]/g); | 
					
						
							|  |  |  | }; | 
					
						
							| 
									
										
										
										
											2017-02-21 11:49:41 -07:00
										 |  |  | OAUTH3.url.parse = function (url) { | 
					
						
							| 
									
										
										
										
											2017-02-20 15:22:48 -07:00
										 |  |  |   // TODO browser
 | 
					
						
							|  |  |  |   // Node should replace this
 | 
					
						
							|  |  |  |   var parser = document.createElement('a'); | 
					
						
							|  |  |  |   parser.href = url; | 
					
						
							|  |  |  |   return parser; | 
					
						
							|  |  |  | }; | 
					
						
							| 
									
										
										
										
											2017-02-21 11:49:41 -07:00
										 |  |  | OAUTH3.url._isRedirectHostSafe = function (referrerUrl, redirectUrl) { | 
					
						
							|  |  |  |   var src = OAUTH3.url.parse(referrerUrl); | 
					
						
							|  |  |  |   var dst = OAUTH3.url.parse(redirectUrl); | 
					
						
							| 
									
										
										
										
											2017-02-20 11:12:48 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-20 15:22:48 -07:00
										 |  |  |   // TODO how should we handle subdomains?
 | 
					
						
							|  |  |  |   // It should be safe for api.example.com to redirect to example.com
 | 
					
						
							|  |  |  |   // But it may not be safe for to example.com to redirect to aj.example.com
 | 
					
						
							|  |  |  |   // It is also probably not safe for sally.example.com to redirect to john.example.com
 | 
					
						
							|  |  |  |   // The client should have a list of allowed URLs to choose from and perhaps a wildcard will do
 | 
					
						
							|  |  |  |   //
 | 
					
						
							|  |  |  |   // api.example.com.evil.com SHOULD NOT match example.com
 | 
					
						
							|  |  |  |   return dst.hostname === src.hostname; | 
					
						
							|  |  |  | }; | 
					
						
							| 
									
										
										
										
											2017-02-21 11:49:41 -07:00
										 |  |  | OAUTH3.url.checkRedirect = function (client, query) { | 
					
						
							| 
									
										
										
										
											2017-02-20 15:22:48 -07:00
										 |  |  |   console.warn("[security] URL path checking not yet implemented"); | 
					
						
							| 
									
										
										
										
											2017-02-20 11:12:48 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-21 11:49:41 -07:00
										 |  |  |   var clientUrl = OAUTH3.url.normalize(client.url); | 
					
						
							|  |  |  |   var redirectUrl = OAUTH3.url.normalize(query.redirect_uri); | 
					
						
							| 
									
										
										
										
											2017-02-20 15:22:48 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // General rule:
 | 
					
						
							|  |  |  |   // I can callback to a shorter domain (fewer subs) or a shorter path (on the same domain)
 | 
					
						
							|  |  |  |   // but not a longer (more subs) or different domain or a longer path (on the same domain)
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // We can callback to an explicitly listed domain (TODO and path)
 | 
					
						
							| 
									
										
										
										
											2017-02-21 11:49:41 -07:00
										 |  |  |   if (OAUTH3.url._isRedirectHostSafe(clientUrl, redirectUrl)) { | 
					
						
							| 
									
										
										
										
											2017-02-20 15:22:48 -07:00
										 |  |  |     return true; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return false; | 
					
						
							|  |  |  | }; | 
					
						
							| 
									
										
										
										
											2017-02-21 11:49:41 -07:00
										 |  |  | OAUTH3.url.redirect = function (clientParams, grants, tokenOrError) { | 
					
						
							| 
									
										
										
										
											2017-02-20 15:22:48 -07:00
										 |  |  |   // TODO OAUTH3.redirect(clientParams, grants, tokenOrError)
 | 
					
						
							|  |  |  |   // TODO check redirect safeness right here with grants.client.urls
 | 
					
						
							|  |  |  |   // TODO check for '#' and '?'. If none, issue warning and use '?' (for backwards compat)
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   var authz = { | 
					
						
							|  |  |  |     access_token: tokenOrError.access_token | 
					
						
							|  |  |  |   , token_type: tokenOrError.token_type         // 'Bearer'
 | 
					
						
							|  |  |  |   , refresh_token: tokenOrError.refresh_token | 
					
						
							|  |  |  |   , expires_in: tokenOrError.expires_in         // 1800 (but superceded by jwt.exp)
 | 
					
						
							|  |  |  |   , scope: tokenOrError.scope                   // superceded by jwt.scp
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   , state: clientParams.state | 
					
						
							|  |  |  |   , debug: clientParams.debug | 
					
						
							| 
									
										
										
										
											2017-02-20 11:12:48 -07:00
										 |  |  |   }; | 
					
						
							| 
									
										
										
										
											2017-02-20 15:22:48 -07:00
										 |  |  |   if (tokenOrError.error) { | 
					
						
							|  |  |  |     authz.error = tokenOrError.error.code || tokenOrError.error; | 
					
						
							|  |  |  |     authz.error_description = tokenOrError.error.message || tokenOrError.error_description; | 
					
						
							|  |  |  |     authz.error_uri = tokenOrError.error.uri || tokenOrError.error_uri; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2017-02-21 11:49:41 -07:00
										 |  |  |   var redirect = clientParams.redirect_uri + '#' + window.OAUTH3.query.stringify(authz); | 
					
						
							| 
									
										
										
										
											2017-02-20 11:12:48 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-20 15:22:48 -07:00
										 |  |  |   if (clientParams.debug) { | 
					
						
							|  |  |  |     console.info('final redirect_uri:', redirect); | 
					
						
							|  |  |  |     window.alert("You're in debug mode so we've taken a pause. Hit OK to continue"); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2017-02-21 11:49:41 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-20 15:22:48 -07:00
										 |  |  |   window.location = redirect; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | OAUTH3.urls.resourceOwnerPassword = function (directive, opts) { | 
					
						
							|  |  |  |   //
 | 
					
						
							|  |  |  |   // Example Resource Owner Password Request
 | 
					
						
							|  |  |  |   // (generally for 1st party and direct-partner mobile apps, and webapps)
 | 
					
						
							|  |  |  |   //
 | 
					
						
							|  |  |  |   // POST https://example.com/api/org.oauth3.provider/access_token
 | 
					
						
							|  |  |  |   //    { "grant_type": "password", "client_id": "<<id>>", "scope": "<<scope>>"
 | 
					
						
							|  |  |  |   //    , "username": "<<username>>", "password": "password" }
 | 
					
						
							|  |  |  |   //
 | 
					
						
							|  |  |  |   opts = opts || {}; | 
					
						
							|  |  |  |   var type = 'access_token'; | 
					
						
							|  |  |  |   var grantType = 'password'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (!opts.password) { | 
					
						
							|  |  |  |     if (opts.otp) { | 
					
						
							|  |  |  |       // for backwards compat
 | 
					
						
							|  |  |  |       opts.password = opts.otp; // 'otp:' + opts.otpUuid + ':' + opts.otp;
 | 
					
						
							| 
									
										
										
										
											2017-02-20 11:12:48 -07:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-02-20 15:22:48 -07:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2017-02-20 11:12:48 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-20 15:22:48 -07:00
										 |  |  |   var scope = opts.scope || directive.authn_scope; | 
					
						
							| 
									
										
										
										
											2017-02-21 20:38:45 -05:00
										 |  |  |   var clientAgreeTos = 'oauth3.org/tos/draft'; // opts.clientAgreeTos || opts.client_agree_tos;
 | 
					
						
							|  |  |  |   var clientUri = opts.client_uri; | 
					
						
							| 
									
										
										
										
											2017-02-20 15:22:48 -07:00
										 |  |  |   var args = directive[type]; | 
					
						
							|  |  |  |   var otpCode = opts.otp || opts.otpCode || opts.otp_code || opts.otpToken || opts.otp_token || undefined; | 
					
						
							| 
									
										
										
										
											2017-03-21 01:02:41 -06:00
										 |  |  |   // TODO require user agent
 | 
					
						
							| 
									
										
										
										
											2017-02-20 15:22:48 -07:00
										 |  |  |   var params = { | 
					
						
							| 
									
										
										
										
											2017-02-24 15:05:07 -05:00
										 |  |  |     client_id: opts.client_id || opts.client_uri | 
					
						
							|  |  |  |   , client_uri: opts.client_uri | 
					
						
							|  |  |  |   , grant_type: grantType | 
					
						
							|  |  |  |   , username: opts.username | 
					
						
							|  |  |  |   , password: opts.password || otpCode || undefined | 
					
						
							|  |  |  |   , totp: opts.totp || opts.totpToken || opts.totp_token || undefined | 
					
						
							|  |  |  |   , otp: otpCode | 
					
						
							|  |  |  |   , password_type: otpCode && 'otp' | 
					
						
							|  |  |  |   , otp_code: otpCode | 
					
						
							|  |  |  |   , otp_uuid: opts.otpUuid || opts.otp_uuid || undefined | 
					
						
							|  |  |  |   , user_agent: opts.userAgent || opts.useragent || opts.user_agent || undefined // AJ's Macbook
 | 
					
						
							|  |  |  |   , jwk: (opts.rememberDevice || opts.remember_device) && opts.jwk || undefined | 
					
						
							| 
									
										
										
										
											2017-02-20 15:22:48 -07:00
										 |  |  |   //, "public_key": opts.rememberDevice && opts.publicKey || undefined
 | 
					
						
							|  |  |  |   //, "public_key_type":  opts.rememberDevice && opts.publicKeyType || undefined // RSA/ECDSA
 | 
					
						
							|  |  |  |   //, "jwt": opts.jwt // TODO sign a proof with a previously loaded public_key
 | 
					
						
							|  |  |  |   , debug: opts.debug || undefined | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  |   var uri = args.url; | 
					
						
							|  |  |  |   var body; | 
					
						
							|  |  |  |   if (opts.totp) { | 
					
						
							|  |  |  |     params.totp = opts.totp; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (clientUri) { | 
					
						
							|  |  |  |     params.clientAgreeTos = clientAgreeTos; | 
					
						
							|  |  |  |     if (!clientAgreeTos) { | 
					
						
							|  |  |  |       throw new Error('Developer Error: missing clientAgreeTos uri'); | 
					
						
							| 
									
										
										
										
											2017-02-20 11:12:48 -07:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-02-20 15:22:48 -07:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (scope) { | 
					
						
							| 
									
										
										
										
											2017-02-21 11:49:41 -07:00
										 |  |  |     params.scope = OAUTH3.scope.stringify(scope); | 
					
						
							| 
									
										
										
										
											2017-02-20 15:22:48 -07:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if ('GET' === args.method.toUpperCase()) { | 
					
						
							| 
									
										
										
										
											2017-02-21 11:49:41 -07:00
										 |  |  |     uri += '?' + OAUTH3.query.stringify(params); | 
					
						
							| 
									
										
										
										
											2017-02-20 15:22:48 -07:00
										 |  |  |   } else { | 
					
						
							|  |  |  |     body = params; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return { | 
					
						
							| 
									
										
										
										
											2017-02-21 20:38:45 -05:00
										 |  |  |     url: OAUTH3.url.resolve(directive.issuer, uri) | 
					
						
							| 
									
										
										
										
											2017-02-20 15:22:48 -07:00
										 |  |  |   , method: args.method | 
					
						
							|  |  |  |   , data: body | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | OAUTH3.urls.grants = function (directive, opts) { | 
					
						
							|  |  |  |   // directive = { issuer, authorization_decision }
 | 
					
						
							|  |  |  |   // opts = { response_type, scopes{ granted, requested, pending, accepted } }
 | 
					
						
							| 
									
										
										
										
											2017-02-20 11:12:48 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-20 15:22:48 -07:00
										 |  |  |   if (!opts) { | 
					
						
							|  |  |  |     throw new Error("You must supply a directive and an options object."); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   if (!opts.client_id) { | 
					
						
							|  |  |  |     throw new Error("You must supply options.client_id."); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   if (!opts.session) { | 
					
						
							|  |  |  |     throw new Error("You must supply options.session."); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   if (!opts.referrer) { | 
					
						
							|  |  |  |     console.warn("You should supply options.referrer"); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   if (!opts.method) { | 
					
						
							|  |  |  |     console.warn("You must supply options.method as either 'GET', or 'POST'"); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   if ('POST' === opts.method) { | 
					
						
							|  |  |  |     if ('string' !== typeof opts.scope) { | 
					
						
							|  |  |  |       console.warn("You should supply options.scope as a space-delimited string of scopes"); | 
					
						
							| 
									
										
										
										
											2017-02-20 11:12:48 -07:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-02-20 15:22:48 -07:00
										 |  |  |     if (-1 === ['token', 'code'].indexOf(opts.response_type)) { | 
					
						
							|  |  |  |       throw new Error("You must supply options.response_type as 'token' or 'code'"); | 
					
						
							| 
									
										
										
										
											2017-02-20 11:12:48 -07:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-02-20 15:22:48 -07:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2017-02-20 11:12:48 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-21 11:49:41 -07:00
										 |  |  |   var url = OAUTH3.url.resolve(directive.issuer, directive.grants.url) | 
					
						
							|  |  |  |     .replace(/(:azp|:client_id)/g, OAUTH3.uri.normalize(opts.client_id || opts.client_uri)) | 
					
						
							| 
									
										
										
										
											2017-02-20 15:22:48 -07:00
										 |  |  |     .replace(/(:sub|:account_id)/g, opts.session.token.sub) | 
					
						
							|  |  |  |     ; | 
					
						
							|  |  |  |   var data = { | 
					
						
							|  |  |  |     client_id: opts.client_id | 
					
						
							|  |  |  |   , client_uri: opts.client_uri | 
					
						
							|  |  |  |   , referrer: opts.referrer | 
					
						
							|  |  |  |   , response_type: opts.response_type | 
					
						
							|  |  |  |   , scope: opts.scope | 
					
						
							|  |  |  |   , tenant_id: opts.tenant_id | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  |   var body; | 
					
						
							| 
									
										
										
										
											2017-02-20 11:12:48 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-20 15:22:48 -07:00
										 |  |  |   if ('GET' === opts.method) { | 
					
						
							| 
									
										
										
										
											2017-02-21 11:49:41 -07:00
										 |  |  |     url += '?' + OAUTH3.query.stringify(data); | 
					
						
							| 
									
										
										
										
											2017-02-20 15:22:48 -07:00
										 |  |  |   } | 
					
						
							|  |  |  |   else { | 
					
						
							|  |  |  |     body = data; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2017-02-20 11:12:48 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-20 15:22:48 -07:00
										 |  |  |   return { | 
					
						
							|  |  |  |     method: opts.method | 
					
						
							|  |  |  |   , url: url | 
					
						
							|  |  |  |   , data: body | 
					
						
							|  |  |  |   , session: opts.session | 
					
						
							| 
									
										
										
										
											2017-02-20 11:12:48 -07:00
										 |  |  |   }; | 
					
						
							| 
									
										
										
										
											2017-02-20 15:22:48 -07:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2017-02-20 11:12:48 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-20 15:22:48 -07:00
										 |  |  | OAUTH3.authn = {}; | 
					
						
							| 
									
										
										
										
											2017-02-21 12:15:58 -07:00
										 |  |  | OAUTH3.authn.loginMeta = function (directive, opts) { | 
					
						
							| 
									
										
										
										
											2017-02-20 15:22:48 -07:00
										 |  |  |   return OAUTH3.request({ | 
					
						
							|  |  |  |     method: directive.credential_meta.method || 'GET' | 
					
						
							|  |  |  |     // TODO lint urls
 | 
					
						
							| 
									
										
										
										
											2017-03-21 01:02:41 -06:00
										 |  |  |     // TODO client_uri
 | 
					
						
							| 
									
										
										
										
											2017-02-21 11:49:41 -07:00
										 |  |  |   , url: OAUTH3.url.resolve(directive.issuer, directive.credential_meta.url) | 
					
						
							| 
									
										
										
										
											2017-02-20 15:22:48 -07:00
										 |  |  |       .replace(':type', 'email') | 
					
						
							|  |  |  |       .replace(':id', opts.email) | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | }; | 
					
						
							| 
									
										
										
										
											2017-02-21 12:15:58 -07:00
										 |  |  | OAUTH3.authn.otp = function (directive, opts) { | 
					
						
							| 
									
										
										
										
											2017-03-21 01:02:41 -06:00
										 |  |  |   // TODO client_uri
 | 
					
						
							| 
									
										
										
										
											2017-02-21 20:38:45 -05:00
										 |  |  |   var preq = { | 
					
						
							|  |  |  |     method: directive.credential_otp.method || 'POST' | 
					
						
							| 
									
										
										
										
											2017-02-21 11:49:41 -07:00
										 |  |  |   , url: OAUTH3.url.resolve(directive.issuer, directive.credential_otp.url) | 
					
						
							| 
									
										
										
										
											2017-02-20 15:22:48 -07:00
										 |  |  |   , data: { | 
					
						
							|  |  |  |       // TODO replace with signed hosted file
 | 
					
						
							|  |  |  |       client_agree_tos: 'oauth3.org/tos/draft' | 
					
						
							| 
									
										
										
										
											2017-03-21 01:02:41 -06:00
										 |  |  |       // TODO unbreak the client_uri option (if broken)
 | 
					
						
							|  |  |  |     , client_id: /*opts.client_id ||*/ OAUTH3.uri.normalize(directive.issuer) // In this case, the issuer is its own client
 | 
					
						
							|  |  |  |     , client_uri: /*opts.client_uri ||*/ OAUTH3.uri.normalize(directive.issuer) | 
					
						
							| 
									
										
										
										
											2017-02-20 15:22:48 -07:00
										 |  |  |     , request_otp: true | 
					
						
							|  |  |  |     , username: opts.email | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-02-21 20:38:45 -05:00
										 |  |  |   }; | 
					
						
							| 
									
										
										
										
											2017-02-24 14:18:45 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-21 20:38:45 -05:00
										 |  |  |   return OAUTH3.request(preq); | 
					
						
							| 
									
										
										
										
											2017-02-20 15:22:48 -07:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2017-02-21 12:15:58 -07:00
										 |  |  | OAUTH3.authn.resourceOwnerPassword = function (directive, opts) { | 
					
						
							| 
									
										
										
										
											2017-02-20 15:22:48 -07:00
										 |  |  |   var providerUri = directive.issuer; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   //var scope = opts.scope;
 | 
					
						
							|  |  |  |   //var appId = opts.appId;
 | 
					
						
							|  |  |  |   return OAUTH3.discover(providerUri, opts).then(function (directive) { | 
					
						
							|  |  |  |     var prequest = OAUTH3.urls.resourceOwnerPassword(directive, opts); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-21 01:02:41 -06:00
										 |  |  |     // TODO return not the raw request?
 | 
					
						
							| 
									
										
										
										
											2017-02-20 15:22:48 -07:00
										 |  |  |     return OAUTH3.request(prequest).then(function (req) { | 
					
						
							|  |  |  |       var data = req.data; | 
					
						
							|  |  |  |       data.provider_uri = providerUri; | 
					
						
							|  |  |  |       if (data.error) { | 
					
						
							| 
									
										
										
										
											2017-02-22 17:58:49 -07:00
										 |  |  |         return OAUTH3.PromiseA.reject(OAUTH3.error.parse(providerUri, data)); | 
					
						
							| 
									
										
										
										
											2017-02-20 11:12:48 -07:00
										 |  |  |       } | 
					
						
							| 
									
										
										
										
											2017-02-24 14:18:45 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-21 20:38:45 -05:00
										 |  |  |       return OAUTH3.hooks.session.refresh( | 
					
						
							| 
									
										
										
										
											2017-02-20 15:22:48 -07:00
										 |  |  |         opts.session || { provider_uri: providerUri, client_uri: opts.client_uri || opts.clientUri } | 
					
						
							|  |  |  |       , data | 
					
						
							|  |  |  |       ); | 
					
						
							| 
									
										
										
										
											2017-02-20 11:12:48 -07:00
										 |  |  |     }); | 
					
						
							| 
									
										
										
										
											2017-02-20 15:22:48 -07:00
										 |  |  |   }); | 
					
						
							|  |  |  | }; | 
					
						
							| 
									
										
										
										
											2017-02-21 11:49:41 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | OAUTH3.authz = {}; | 
					
						
							| 
									
										
										
										
											2017-02-20 15:22:48 -07:00
										 |  |  | OAUTH3.authz.scopes = function (providerUri, session, clientParams) { | 
					
						
							|  |  |  |   // OAuth3.requests.grants(providerUri, {});         // return list of grants
 | 
					
						
							|  |  |  |   // OAuth3.checkGrants(providerUri, {});             //
 | 
					
						
							| 
									
										
										
										
											2017-02-21 11:49:41 -07:00
										 |  |  |   var clientUri = OAUTH3.uri.normalize(clientParams.client_uri || OAUTH3._browser.window.document.referrer); | 
					
						
							| 
									
										
										
										
											2017-02-20 15:22:48 -07:00
										 |  |  |   var scope = clientParams.scope || ''; | 
					
						
							|  |  |  |   var clientObj = clientParams; | 
					
						
							| 
									
										
										
										
											2017-02-20 11:12:48 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-20 15:22:48 -07:00
										 |  |  |   if (!scope) { | 
					
						
							|  |  |  |     scope = 'oauth3_authn'; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2017-02-20 11:12:48 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-20 15:22:48 -07:00
										 |  |  |   return OAUTH3.authz.grants(providerUri, { | 
					
						
							|  |  |  |     method: 'GET' | 
					
						
							|  |  |  |   , client_id: clientUri | 
					
						
							|  |  |  |   , client_uri: clientUri | 
					
						
							|  |  |  |   , session: session | 
					
						
							|  |  |  |   }).then(function (grantResults) { | 
					
						
							|  |  |  |     var grants; | 
					
						
							|  |  |  |     var grantedScopes; | 
					
						
							|  |  |  |     var grantedScopesMap; | 
					
						
							|  |  |  |     var pendingScopes; | 
					
						
							|  |  |  |     var acceptedScopes; | 
					
						
							|  |  |  |     var scopes = scope.split(/[+, ]/g); | 
					
						
							|  |  |  |     var callbackUrl; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // it doesn't matter who the referrer is as long as the destination
 | 
					
						
							|  |  |  |     // is an authorized destination for the client in question
 | 
					
						
							|  |  |  |     // (though it may not hurt to pass the referrer's info on to the client)
 | 
					
						
							| 
									
										
										
										
											2017-02-21 11:49:41 -07:00
										 |  |  |     if (!OAUTH3.url.checkRedirect(grantResults.client, clientObj)) { | 
					
						
							| 
									
										
										
										
											2017-02-20 15:22:48 -07:00
										 |  |  |       callbackUrl = 'https://oauth3.org/docs/errors#E_REDIRECT_ATTACK' | 
					
						
							|  |  |  |         + '?redirect_uri=' + clientObj.redirect_uri | 
					
						
							|  |  |  |         + '&allowed_urls=' + grantResults.client.url | 
					
						
							|  |  |  |         + '&client_id=' + clientUri | 
					
						
							| 
									
										
										
										
											2017-02-21 11:49:41 -07:00
										 |  |  |         + '&referrer_uri=' + OAUTH3.uri.normalize(window.document.referrer) | 
					
						
							| 
									
										
										
										
											2017-02-20 15:22:48 -07:00
										 |  |  |         ; | 
					
						
							| 
									
										
										
										
											2017-02-21 11:49:41 -07:00
										 |  |  |       if (clientParams.debug) { | 
					
						
							| 
									
										
										
										
											2017-02-20 15:22:48 -07:00
										 |  |  |         console.log('grantResults Redirect Attack'); | 
					
						
							|  |  |  |         console.log(grantResults); | 
					
						
							|  |  |  |         console.log(clientObj); | 
					
						
							|  |  |  |         window.alert("DEBUG MODE checkRedirect error encountered. Check the console."); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       location.href = callbackUrl; | 
					
						
							|  |  |  |       return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if ('oauth3_authn' === scope) { | 
					
						
							|  |  |  |       // implicit ppid grant is automatic
 | 
					
						
							|  |  |  |       console.warn('[security] fix scope checking on backend so that we can do automatic grants'); | 
					
						
							|  |  |  |       // TODO check user preference if implicit ppid grant is allowed
 | 
					
						
							|  |  |  |       //return generateToken(session, clientObj);
 | 
					
						
							| 
									
										
										
										
											2017-02-20 11:12:48 -07:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-20 15:22:48 -07:00
										 |  |  |     grants = (grantResults).grants.filter(function (grant) { | 
					
						
							|  |  |  |       if (clientUri === (grant.azp || grant.oauth_client_id || grant.oauthClientId)) { | 
					
						
							|  |  |  |         return true; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2017-02-20 11:12:48 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-20 15:22:48 -07:00
										 |  |  |     grantedScopesMap = {}; | 
					
						
							|  |  |  |     acceptedScopes = []; | 
					
						
							|  |  |  |     pendingScopes = scopes.filter(function (requestedScope) { | 
					
						
							|  |  |  |       return grants.every(function (grant) { | 
					
						
							|  |  |  |         if (!grant.scope) { | 
					
						
							|  |  |  |           grant.scope = 'oauth3_authn'; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         var gscopes = grant.scope.split(/[+, ]/g); | 
					
						
							|  |  |  |         gscopes.forEach(function (s) { grantedScopesMap[s] = true; }); | 
					
						
							|  |  |  |         if (-1 !== gscopes.indexOf(requestedScope)) { | 
					
						
							|  |  |  |           // already accepted in the past
 | 
					
						
							|  |  |  |           acceptedScopes.push(requestedScope); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         else { | 
					
						
							|  |  |  |           // true, is pending
 | 
					
						
							|  |  |  |           return true; | 
					
						
							| 
									
										
										
										
											2017-02-20 11:12:48 -07:00
										 |  |  |         } | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2017-02-20 15:22:48 -07:00
										 |  |  |     grantedScopes = Object.keys(grantedScopesMap); | 
					
						
							| 
									
										
										
										
											2017-02-20 11:12:48 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-20 15:22:48 -07:00
										 |  |  |     return { | 
					
						
							|  |  |  |       pending: pendingScopes    // not yet accepted
 | 
					
						
							|  |  |  |     , granted: grantedScopes    // all granted, ever
 | 
					
						
							|  |  |  |     , requested: scopes         // all requested, now
 | 
					
						
							|  |  |  |     , accepted: acceptedScopes  // granted (ever) and requested (now)
 | 
					
						
							| 
									
										
										
										
											2017-02-20 11:12:48 -07:00
										 |  |  |     }; | 
					
						
							| 
									
										
										
										
											2017-02-20 15:22:48 -07:00
										 |  |  |   }); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | OAUTH3.authz.grants = function (providerUri, opts) { | 
					
						
							|  |  |  |   return OAUTH3.discover(providerUri, { | 
					
						
							|  |  |  |     client_id: providerUri | 
					
						
							|  |  |  |   , debug: opts.debug | 
					
						
							|  |  |  |   }).then(function (directive) { | 
					
						
							| 
									
										
										
										
											2017-02-20 11:12:48 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-20 15:22:48 -07:00
										 |  |  |     return OAUTH3.request(OAUTH3.urls.grants(directive, opts), opts).then(function (grantsResult) { | 
					
						
							|  |  |  |       if ('POST' === opts.method) { | 
					
						
							|  |  |  |         // TODO this is clientToken
 | 
					
						
							|  |  |  |         return grantsResult.originalData || grantsResult.data; | 
					
						
							| 
									
										
										
										
											2017-02-20 11:12:48 -07:00
										 |  |  |       } | 
					
						
							| 
									
										
										
										
											2017-02-20 15:22:48 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |       var grants = grantsResult.originalData || grantsResult.data; | 
					
						
							|  |  |  |       // TODO
 | 
					
						
							|  |  |  |       if (grants.error) { | 
					
						
							| 
									
										
										
										
											2017-02-22 17:58:49 -07:00
										 |  |  |         return OAUTH3.PromiseA.reject(OAUTH3.error.parse(providerUri, grants)); | 
					
						
							| 
									
										
										
										
											2017-02-20 11:12:48 -07:00
										 |  |  |       } | 
					
						
							| 
									
										
										
										
											2017-02-20 15:22:48 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-21 11:49:41 -07:00
										 |  |  |       OAUTH3.hooks.grants.set(opts.client_id + '-client', grants.client); | 
					
						
							| 
									
										
										
										
											2017-02-20 15:22:48 -07:00
										 |  |  |       grants.grants.forEach(function (grant) { | 
					
						
							|  |  |  |         var clientId = grant.client_id || grant.oauth_client_id || grant.oauthClientId; | 
					
						
							|  |  |  |         // TODO should save as an array
 | 
					
						
							| 
									
										
										
										
											2017-02-21 11:49:41 -07:00
										 |  |  |         OAUTH3.hooks.grants.set(clientId, [ grant ]); | 
					
						
							| 
									
										
										
										
											2017-02-20 15:22:48 -07:00
										 |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       return { | 
					
						
							| 
									
										
										
										
											2017-02-21 11:49:41 -07:00
										 |  |  |         client: OAUTH3.hooks.grants.get(opts.client_id + '-client') | 
					
						
							|  |  |  |       , grants: OAUTH3.hooks.grants.get(opts.client_id) || [] | 
					
						
							| 
									
										
										
										
											2017-02-20 15:22:48 -07:00
										 |  |  |       }; | 
					
						
							| 
									
										
										
										
											2017-02-20 11:12:48 -07:00
										 |  |  |     }); | 
					
						
							| 
									
										
										
										
											2017-02-20 15:22:48 -07:00
										 |  |  |   }); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | OAUTH3.authz.redirectWithToken = function (providerUri, session, clientParams, scopes) { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   scopes.new = scopes.new || []; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if ('token' === clientParams.response_type) { | 
					
						
							|  |  |  |     // get token and redirect client-side
 | 
					
						
							|  |  |  |     return OAUTH3.authz.grants(providerUri, { | 
					
						
							| 
									
										
										
										
											2017-02-20 11:12:48 -07:00
										 |  |  |       method: 'POST' | 
					
						
							| 
									
										
										
										
											2017-02-20 15:22:48 -07:00
										 |  |  |     , client_id: clientParams.client_uri | 
					
						
							|  |  |  |     , client_uri: clientParams.client_uri | 
					
						
							|  |  |  |     , scope: scopes.granted.concat(scopes.new).join(',') | 
					
						
							|  |  |  |     , response_type: clientParams.response_type | 
					
						
							|  |  |  |     , referrer: clientParams.referrer | 
					
						
							|  |  |  |     , session: session | 
					
						
							|  |  |  |     , debug: clientParams.debug | 
					
						
							|  |  |  |     }).then(function (results) { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-21 11:49:41 -07:00
										 |  |  |       OAUTH3.url.redirect(clientParams, scopes, results); | 
					
						
							| 
									
										
										
										
											2017-02-20 15:22:48 -07:00
										 |  |  |     }); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   else if ('code' === clientParams.response_type) { | 
					
						
							|  |  |  |     // get token and redirect server-side
 | 
					
						
							|  |  |  |     // (requires insecure form post as per spec)
 | 
					
						
							|  |  |  |     //OAUTH3.requests.authorizationDecision();
 | 
					
						
							|  |  |  |     window.alert("Authorization Code Redirect NOT IMPLEMENTED"); | 
					
						
							|  |  |  |     throw new Error("Authorization Code Redirect NOT IMPLEMENTED"); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | OAUTH3.requests = {}; | 
					
						
							|  |  |  | OAUTH3.requests.accounts = {}; | 
					
						
							|  |  |  | OAUTH3.requests.accounts.update = function (directive, session, opts) { | 
					
						
							|  |  |  |   var dir = directive.update_account || { | 
					
						
							|  |  |  |     method: 'POST' | 
					
						
							|  |  |  |   , url: 'https://' + directive.provider_url + '/api/org.oauth3.provider/accounts/:accountId' | 
					
						
							|  |  |  |   , bearer: 'Bearer' | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  |   var url = dir.url | 
					
						
							|  |  |  |     .replace(/:accountId/, opts.accountId) | 
					
						
							|  |  |  |   ; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return OAUTH3.request({ | 
					
						
							|  |  |  |     method: dir.method || 'POST' | 
					
						
							|  |  |  |   , url: url | 
					
						
							|  |  |  |   , headers: { | 
					
						
							|  |  |  |       'Authorization': (dir.bearer || 'Bearer') + ' ' + session.accessToken | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   , json: { | 
					
						
							|  |  |  |       name: opts.name | 
					
						
							|  |  |  |     , comment: opts.comment | 
					
						
							|  |  |  |     , displayName: opts.displayName | 
					
						
							|  |  |  |     , priority: opts.priority | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | OAUTH3.requests.accounts.create = function (directive, session, account) { | 
					
						
							|  |  |  |   var dir = directive.create_account || { | 
					
						
							|  |  |  |     method: 'POST' | 
					
						
							|  |  |  |   , url: 'https://' + directive.issuer + '/api/org.oauth3.provider/accounts' | 
					
						
							|  |  |  |   , bearer: 'Bearer' | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  |   var data = { | 
					
						
							|  |  |  |     // TODO fix the server to just use one scheme
 | 
					
						
							|  |  |  |     // account = { nick, self: { comment, username } }
 | 
					
						
							|  |  |  |     // account = { name, comment, display_name, priority }
 | 
					
						
							|  |  |  |     account: { | 
					
						
							|  |  |  |       nick: account.display_name | 
					
						
							|  |  |  |     , name: account.name | 
					
						
							|  |  |  |     , comment: account.comment | 
					
						
							|  |  |  |     , display_name: account.display_name | 
					
						
							|  |  |  |     , priority: account.priority | 
					
						
							|  |  |  |     , self: { | 
					
						
							| 
									
										
										
										
											2017-02-20 11:12:48 -07:00
										 |  |  |         nick: account.display_name | 
					
						
							|  |  |  |       , name: account.name | 
					
						
							|  |  |  |       , comment: account.comment | 
					
						
							|  |  |  |       , display_name: account.display_name | 
					
						
							|  |  |  |       , priority: account.priority | 
					
						
							|  |  |  |       } | 
					
						
							| 
									
										
										
										
											2017-02-20 15:22:48 -07:00
										 |  |  |     } | 
					
						
							|  |  |  |   , logins: [ | 
					
						
							|  |  |  |       { | 
					
						
							|  |  |  |         token: session.access_token | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     ] | 
					
						
							| 
									
										
										
										
											2017-02-20 11:12:48 -07:00
										 |  |  |   }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-20 15:22:48 -07:00
										 |  |  |   return OAUTH3.request({ | 
					
						
							|  |  |  |     method: dir.method || 'POST' | 
					
						
							|  |  |  |   , url: dir.url | 
					
						
							|  |  |  |   , session: session | 
					
						
							|  |  |  |   , data: data | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | }; | 
					
						
							| 
									
										
										
										
											2017-02-21 20:38:45 -05:00
										 |  |  | OAUTH3.hooks.grants = { | 
					
						
							|  |  |  |   // Provider Only
 | 
					
						
							|  |  |  |   set: function (clientUri, newGrants) { | 
					
						
							|  |  |  |     clientUri = OAUTH3.uri.normalize(clientUri); | 
					
						
							|  |  |  |     console.warn('[oauth3.hooks.setGrants] PLEASE IMPLEMENT -- Your Fault'); | 
					
						
							|  |  |  |     console.warn(newGrants); | 
					
						
							|  |  |  |     if (!this._cache) { this._cache = {}; } | 
					
						
							|  |  |  |     console.log('clientUri, newGrants'); | 
					
						
							|  |  |  |     console.log(clientUri, newGrants); | 
					
						
							|  |  |  |     this._cache[clientUri] = newGrants; | 
					
						
							|  |  |  |     return newGrants; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | , get: function (clientUri) { | 
					
						
							|  |  |  |     clientUri = OAUTH3.uri.normalize(clientUri); | 
					
						
							|  |  |  |     console.warn('[oauth3.hooks.getGrants] PLEASE IMPLEMENT -- Your Fault'); | 
					
						
							|  |  |  |     if (!this._cache) { this._cache = {}; } | 
					
						
							|  |  |  |     console.log('clientUri, existingGrants'); | 
					
						
							|  |  |  |     console.log(clientUri, this._cache[clientUri]); | 
					
						
							|  |  |  |     return this._cache[clientUri]; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | }; | 
					
						
							| 
									
										
										
										
											2017-02-20 15:22:48 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | OAUTH3._browser.isIframe = function isIframe () { | 
					
						
							|  |  |  |   try { | 
					
						
							|  |  |  |     return window.self !== window.top; | 
					
						
							|  |  |  |   } catch (e) { | 
					
						
							|  |  |  |     return true; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-21 11:49:41 -07:00
										 |  |  | }('undefined' !== typeof exports ? exports : window)); |