WIP implicit grant complete
This commit is contained in:
		
							parent
							
								
									4dd07c9f80
								
							
						
					
					
						commit
						ab4dfdce58
					
				| @ -171,7 +171,6 @@ | |||||||
|         var args = directive[type]; |         var args = directive[type]; | ||||||
|         var uri = args.url; |         var uri = args.url; | ||||||
|         var state = opts.state || OAUTH3.utils.randomState(); |         var state = opts.state || OAUTH3.utils.randomState(); | ||||||
|         console.log('implicit grant opts.state', opts.state); |  | ||||||
|         var params = { |         var params = { | ||||||
|           debug: opts.debug || undefined |           debug: opts.debug || undefined | ||||||
|         , client_uri: opts.client_uri || opts.clientUri || undefined |         , client_uri: opts.client_uri || opts.clientUri || undefined | ||||||
| @ -247,7 +246,7 @@ | |||||||
|         if (directives && directives.issuer) { |         if (directives && directives.issuer) { | ||||||
|           return directives; |           return directives; | ||||||
|         } |         } | ||||||
|         return OAUTH3._discover(providerUri, opts).then(function (directives) { |         return OAUTH3._discoverHelper(providerUri, opts).then(function (directives) { | ||||||
|           directives.issuer = directives.issuer || OAUTH3.utils.url.normalize(providerUri); |           directives.issuer = directives.issuer || OAUTH3.utils.url.normalize(providerUri); | ||||||
|           // OAUTH3.PromiseA.resolve() is taken care of because this is wrapped
 |           // OAUTH3.PromiseA.resolve() is taken care of because this is wrapped
 | ||||||
|           return OAUTH3.hooks.directives.set(providerUri, directives); |           return OAUTH3.hooks.directives.set(providerUri, directives); | ||||||
| @ -285,19 +284,21 @@ | |||||||
|     } |     } | ||||||
|   , _discoverThenImplicitGrant: function(providerUri, opts) { |   , _discoverThenImplicitGrant: function(providerUri, opts) { | ||||||
|       opts.windowType = opts.windowType || 'popup'; |       opts.windowType = opts.windowType || 'popup'; | ||||||
|       return OAUTH3._discover(providerUri, opts).then(function (directives) { |       return OAUTH3.discover(providerUri, opts).then(function (directives) { | ||||||
|         console.info('discover complete'); |         console.info('Discover complete'); | ||||||
|         console.log(directives); |  | ||||||
|         console.log('DISCOVER COMPLETE opts._state', opts._state); |  | ||||||
|         return OAUTH3._implicitGrant(directives, opts).then(function (tokens) { |         return OAUTH3._implicitGrant(directives, opts).then(function (tokens) { | ||||||
|           console.info('implicit grant complete', tokens); |           console.info('Implicit Grant complete', tokens); | ||||||
|           OAUTH3._browser.closeFrame(tokens.state || opts._state); |           OAUTH3._browser.closeFrame(tokens.state || opts._state); | ||||||
|           //opts._state = undefined;
 |           //opts._state = undefined;
 | ||||||
|           return tokens; |           return tokens; | ||||||
|         }); |         }); | ||||||
|       }); |       }); | ||||||
|     } |     } | ||||||
|  |   , _discoverHelper: function(providerUri, opts) { | ||||||
|  |       return OAUTH3._discover(providerUri, opts); | ||||||
|  |     } | ||||||
|   , _discover: function(providerUri, opts) { |   , _discover: function(providerUri, opts) { | ||||||
|  |       opts = opts || {}; | ||||||
|       providerUri = OAUTH3.utils.url.normalize(providerUri); |       providerUri = OAUTH3.utils.url.normalize(providerUri); | ||||||
| 
 | 
 | ||||||
|       if (providerUri.match(OAUTH3._browser.window.location.hostname)) { |       if (providerUri.match(OAUTH3._browser.window.location.hostname)) { | ||||||
| @ -334,18 +335,29 @@ | |||||||
| 
 | 
 | ||||||
|       // TODO allow postMessage from providerUri in addition to callback
 |       // TODO allow postMessage from providerUri in addition to callback
 | ||||||
|       // TODO allow node to open a desktop browser window
 |       // TODO allow node to open a desktop browser window
 | ||||||
|  |       opts._windowType = opts.windowType; | ||||||
|  |       opts.windowType = opts.windowType || 'background'; | ||||||
|       return OAUTH3._browser.frameRequest( |       return OAUTH3._browser.frameRequest( | ||||||
|         OAUTH3.utils.url.resolve(providerUri, discReq.url) |         OAUTH3.utils.url.resolve(providerUri, discReq.url) | ||||||
|       , discReq.state |       , discReq.state | ||||||
|  |         // why not just pass opts whole?
 | ||||||
|       , { windowType: opts.windowType |       , { windowType: opts.windowType | ||||||
|         , reuseWindow: opts.broker && '-broker' |         , reuseWindow: opts.broker && '-broker' | ||||||
|         , debug: opts.debug |         , debug: opts.debug | ||||||
|         } |         } | ||||||
|       ).then(function (params) { |       ).then(function (params) { | ||||||
|         // discWin.child.close()
 |         opts.windowType = opts._windowType; | ||||||
|  | 
 | ||||||
|  |         // caller will call OAUTH3._browser.closeFrame(discReq.state, { debug: opts.debug || params.debug });
 | ||||||
|  |         if (params.error) { | ||||||
|  |           // TODO directives.issuer || directives.audience
 | ||||||
|  |           return OAUTH3.PromiseA.reject(OAUTH3.utils._formatError(providerUri, params)); | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|         // TODO params should have response_type indicating json, binary, etc
 |         // TODO params should have response_type indicating json, binary, etc
 | ||||||
|         var directives = JSON.parse(OAUTH3.utils.atob(OAUTH3.utils._urlSafeBase64ToBase64(params.result || params.directives))); |         var directives = JSON.parse(OAUTH3.utils.atob(OAUTH3.utils._urlSafeBase64ToBase64(params.result || params.directives))); | ||||||
|         return OAUTH3.hooks.directives.set(providerUri, directives); |         // caller will call OAUTH3.hooks.directives.set(providerUri, directives);
 | ||||||
|  |         return directives; | ||||||
|       }); |       }); | ||||||
|     } |     } | ||||||
|   , _implicitGrant: function(directives, opts) { |   , _implicitGrant: function(directives, opts) { | ||||||
| @ -365,13 +377,6 @@ | |||||||
|         window.alert("DEBUG MODE: Pausing so you can look at logs and whatnot :) Fire at will!"); |         window.alert("DEBUG MODE: Pausing so you can look at logs and whatnot :) Fire at will!"); | ||||||
|       } |       } | ||||||
| 
 | 
 | ||||||
|       if (opts._state) { |  | ||||||
|         console.log('equal states authReq?', authReq.state === opts._state); |  | ||||||
|         console.log(opts._state); |  | ||||||
|         console.log(authReq.state); |  | ||||||
|       } |  | ||||||
| 
 |  | ||||||
|       return new OAUTH3.PromiseA(function (resolve, reject) { |  | ||||||
|       console.log("framing request for implicit grant"); |       console.log("framing request for implicit grant"); | ||||||
|       return OAUTH3._browser.frameRequest( |       return OAUTH3._browser.frameRequest( | ||||||
|         OAUTH3.utils.url.resolve(directives.issuer, authReq.url) |         OAUTH3.utils.url.resolve(directives.issuer, authReq.url) | ||||||
| @ -384,14 +389,13 @@ | |||||||
|         console.log("completed implicit grant"); |         console.log("completed implicit grant"); | ||||||
|         if (tokens.error) { |         if (tokens.error) { | ||||||
|           // TODO directives.audience
 |           // TODO directives.audience
 | ||||||
|             return reject(OAUTH3.utils._formatError(directives.issuer /*providerUri*/, tokens)); |           return OAUTH3.PromiseA.reject(OAUTH3.utils._formatError(directives.issuer /*providerUri*/, tokens)); | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         OAUTH3._browser.closeFrame(authReq.state, { debug: opts.debug || tokens.debug }); |         OAUTH3._browser.closeFrame(authReq.state, { debug: opts.debug || tokens.debug }); | ||||||
| 
 | 
 | ||||||
|         return tokens; |         return tokens; | ||||||
|       }); |       }); | ||||||
|       }); |  | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| @ -441,55 +445,6 @@ | |||||||
|           xhr.send(); |           xhr.send(); | ||||||
|         }); |         }); | ||||||
|       } |       } | ||||||
|     , discover: function (providerUri, opts) { |  | ||||||
|         opts = opts || {}; |  | ||||||
|         //opts.debug = true;
 |  | ||||||
|         providerUri = OAUTH3.utils.url.normalize(providerUri); |  | ||||||
|         if (providerUri.match(OAUTH3._browser.window.location.hostname)) { |  | ||||||
|           console.warn("It looks like you're a provider checking for your own directive," |  | ||||||
|             + " so we we're just gonna use OAUTH3.request({ method: 'GET', url: '.well-known/oauth3/directive.json' })"); |  | ||||||
|           return OAUTH3.request({ |  | ||||||
|             method: 'GET' |  | ||||||
|           , url: OAUTH3.utils.url.normalize(providerUri) + '/.well-known/oauth3/directives.json' |  | ||||||
|           }); |  | ||||||
|         } |  | ||||||
| 
 |  | ||||||
|         if (!(opts.client_id || opts.client_uri).match(OAUTH3._browser.window.location.hostname)) { |  | ||||||
|           console.warn("It looks like your client_id doesn't match your current window... this probably won't end well"); |  | ||||||
|           console.warn(opts.client_id || opts.client_uri, OAUTH3._browser.window.location.hostname); |  | ||||||
|         } |  | ||||||
|         var discReq = OAUTH3.urls.discover( |  | ||||||
|           providerUri |  | ||||||
|         , { client_id: (opts.client_id || opts.client_uri || OAUTH3.clientUri(OAUTH3._browser.window.location)) |  | ||||||
|           , state: opts._state || undefined |  | ||||||
|           , debug: opts.debug } |  | ||||||
|         ); |  | ||||||
| 
 |  | ||||||
|         if (opts._state) { |  | ||||||
|           console.log('equal states discObj?', discReq.state === opts._state); |  | ||||||
|         } |  | ||||||
| 
 |  | ||||||
|         // TODO ability to reuse iframe instead of closing
 |  | ||||||
|         opts._windowType = opts.windowType; |  | ||||||
|         opts.windowType = opts.windowType || 'background'; |  | ||||||
|         return OAUTH3._browser.frameRequest( |  | ||||||
|           OAUTH3.utils.url.resolve(providerUri, discReq.url) |  | ||||||
|         , discReq.state |  | ||||||
|         , opts |  | ||||||
|         ).then(function (params) { |  | ||||||
|           opts.windowType = opts._windowType; |  | ||||||
|           OAUTH3._browser.closeFrame(discReq.state, { debug: opts.debug || params.debug }); |  | ||||||
|           if (params.error) { |  | ||||||
|             // TODO directives.issuer || directives.audience
 |  | ||||||
|             return OAUTH3.PromiseA.reject(OAUTH3.utils._formatError(providerUri, params)); |  | ||||||
|           } |  | ||||||
|           var directives = JSON.parse(OAUTH3.utils.atob(OAUTH3.utils._urlSafeBase64ToBase64(params.result || params.directives))); |  | ||||||
|           return directives; |  | ||||||
|         }, function (err) { |  | ||||||
|           OAUTH3._browser.closeFrame(discReq.state, { debug: opts.debug || err.debug }); |  | ||||||
|           return OAUTH3.PromiseA.reject(err); |  | ||||||
|         }); |  | ||||||
|       } |  | ||||||
|     , frameRequest: function (url, state, opts) { |     , frameRequest: function (url, state, opts) { | ||||||
|         console.log('frameRequest state', state); |         console.log('frameRequest state', state); | ||||||
|         opts = opts || {}; |         opts = opts || {}; | ||||||
| @ -592,6 +547,7 @@ | |||||||
|         }); |         }); | ||||||
|       } |       } | ||||||
|     , closeFrame: function (state, opts) { |     , closeFrame: function (state, opts) { | ||||||
|  |         opts = opts || {}; | ||||||
|         function close() { |         function close() { | ||||||
|           console.log("Attempting to close... ", OAUTH3._browser._frames[state]); |           console.log("Attempting to close... ", OAUTH3._browser._frames[state]); | ||||||
|           try { |           try { | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user