1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 | (function (define, global) {
|
9 | 'use strict';
|
10 |
|
11 | define(function (require) {
|
12 |
|
13 | var interceptor, UrlBuilder, pubsub, when;
|
14 |
|
15 | interceptor = require('../interceptor');
|
16 | UrlBuilder = require('../UrlBuilder');
|
17 | pubsub = require('../util/pubsub');
|
18 | when = require('when');
|
19 |
|
20 | function defaultOAuthCallback(hash) {
|
21 | var params, queryString, regex, m;
|
22 |
|
23 | queryString = hash.indexOf('#') === 0 ? hash.substring(1) : hash;
|
24 | params = {};
|
25 | regex = /([^&=]+)=([^&]*)/g;
|
26 |
|
27 | m = regex.exec(queryString);
|
28 | do {
|
29 | params[decodeURIComponent(m[1])] = decodeURIComponent(m[2]);
|
30 | m = regex.exec(queryString);
|
31 | } while (m);
|
32 |
|
33 |
|
34 | pubsub.publish(params.state, params.token_type + ' ' + params.access_token);
|
35 | }
|
36 |
|
37 | function defaultWindowStrategy(url) {
|
38 | var w = window.open(url, '_blank', 'width=500,height=400');
|
39 | return function () {
|
40 | w.close();
|
41 | };
|
42 | }
|
43 |
|
44 | function authorize(config) {
|
45 | var state, url, dismissWindow;
|
46 |
|
47 | return when.promise(function (resolve) {
|
48 |
|
49 | state = Math.random() * new Date().getTime();
|
50 | url = new UrlBuilder(config.authorizationUrlBase).build({
|
51 | 'response_type': 'token',
|
52 | 'redirect_uri': config.redirectUrl,
|
53 | 'client_id': config.clientId,
|
54 | 'scope': config.scope,
|
55 | 'state': state
|
56 | });
|
57 |
|
58 | dismissWindow = config.windowStrategy(url);
|
59 |
|
60 | pubsub.subscribe(state, function (authorization) {
|
61 | dismissWindow();
|
62 | resolve(authorization);
|
63 | });
|
64 |
|
65 | });
|
66 | }
|
67 |
|
68 | |
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 |
|
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 |
|
91 |
|
92 |
|
93 |
|
94 |
|
95 |
|
96 |
|
97 |
|
98 |
|
99 |
|
100 | return interceptor({
|
101 | init: function (config) {
|
102 | config.redirectUrl = new UrlBuilder(config.redirectUrl).fullyQualify().build();
|
103 | config.windowStrategy = config.windowStrategy || defaultWindowStrategy;
|
104 | config.oAuthCallback = config.oAuthCallback || defaultOAuthCallback;
|
105 | config.oAuthCallbackName = config.oAuthCallbackName || 'oAuthCallback';
|
106 |
|
107 | global[config.oAuthCallbackName] = config.oAuthCallback;
|
108 |
|
109 | return config;
|
110 | },
|
111 | request: function (request, config) {
|
112 | request.headers = request.headers || {};
|
113 |
|
114 | if (config.token) {
|
115 | request.headers.Authorization = config.token;
|
116 | return request;
|
117 | }
|
118 | else {
|
119 | return authorize(config).then(function (authorization) {
|
120 | request.headers.Authorization = config.token = authorization;
|
121 | return request;
|
122 | });
|
123 | }
|
124 | },
|
125 | response: function (response, config, client) {
|
126 | if (response.status.code === 401) {
|
127 |
|
128 | return authorize(config).then(function (authorization) {
|
129 | config.token = authorization;
|
130 | return client(response.request);
|
131 | });
|
132 | }
|
133 | else if (response.status.code === 403) {
|
134 | return when.reject(response);
|
135 | }
|
136 |
|
137 | return response;
|
138 | }
|
139 | });
|
140 |
|
141 | });
|
142 |
|
143 | }(
|
144 | typeof define === 'function' && define.amd ? define : function (factory) { module.exports = factory(require); },
|
145 | typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : void 0
|
146 |
|
147 | ));
|