forked from developerforce/Force.com-JavaScript-REST-Toolkit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathforceoauth.js
More file actions
executable file
·207 lines (165 loc) · 6.56 KB
/
forceoauth.js
File metadata and controls
executable file
·207 lines (165 loc) · 6.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
"use strict";
let // The login URL for the OAuth process
// To override default, pass loginURL in init(props)
loginURL = 'https://login.salesforce.com',
// The Connected App client Id. Default app id provided - Not for production use.
// This application supports http://localhost:8200/oauthcallback.html as a valid callback URL
// To override default, pass appId in init(props)
appId = '3MVG9fMtCkV6eLheIEZplMqWfnGlf3Y.BcWdOf1qytXo9zxgbsrUbS.ExHTgUPJeb3jZeT8NYhc.hMyznKU92',
// The force.com API version to use.
// To override default, pass apiVersion in init(props)
apiVersion = 'v35.0',
// Keep track of OAuth data (access_token, refresh_token, and instance_url)
oauthData,
// By default we store fbtoken in sessionStorage. This can be overridden in init()
tokenStore = {},
// if page URL is http://localhost:3000/myapp/index.html, context is /myapp
context = window.location.pathname.substring(0, window.location.pathname.lastIndexOf("/")),
// if page URL is http://localhost:3000/myapp/index.html, serverURL is http://localhost:3000
serverURL = window.location.protocol + '//' + window.location.hostname + (window.location.port ? ':' + window.location.port : ''),
// if page URL is http://localhost:3000/myapp/index.html, baseURL is http://localhost:3000/myapp
baseURL = serverURL + context,
// Only required when using REST APIs in an app hosted on your own server to avoid cross domain policy issues
// To override default, pass proxyURL in init(props)
proxyURL = baseURL,
// if page URL is http://localhost:3000/myapp/index.html, oauthCallbackURL is http://localhost:3000/myapp/oauthcallback.html
// To override default, pass oauthCallbackURL in init(props)
oauthCallbackURL = baseURL + '/oauthcallback.html',
// Whether or not to use a CORS proxy. Defaults to false if app running in Cordova, in a VF page,
// or using the Salesforce console. Can be overriden in init()
useProxy = (window.SfdcApp || window.sforce) ? false : true;
let parseQueryString = queryString => {
let qs = decodeURIComponent(queryString),
obj = {},
params = qs.split('&');
params.forEach(param => {
let splitter = param.split('=');
obj[splitter[0]] = splitter[1];
});
return obj;
};
let toQueryString = obj => {
let parts = [],
i;
for (i in obj) {
if (obj.hasOwnProperty(i)) {
parts.push(encodeURIComponent(i) + "=" + encodeURIComponent(obj[i]));
}
}
return parts.join("&");
};
let refreshToken = () => new Promise((resolve, reject) => {
if (!oauthData.refresh_token) {
console.log('ERROR: refresh token does not exist');
reject();
return;
}
let xhr = new XMLHttpRequest(),
params = {
'grant_type': 'refresh_token',
'refresh_token': oauthData.refresh_token,
'client_id': appId
},
url = useProxy ? proxyURL : loginURL;
url = url + '/services/oauth2/token?' + toQueryString(params);
xhr.onreadystatechange = () => {
if (xhr.readyState === 4) {
if (xhr.status === 200) {
console.log('Token refreshed');
let res = JSON.parse(xhr.responseText);
oauthData.access_token = res.access_token;
tokenStore.forceOAuth = JSON.stringify(oauthData);
resolve();
} else {
console.log('Error while trying to refresh token: ' + xhr.responseText);
reject();
}
}
};
xhr.open('POST', url, true);
if (!useProxy) {
xhr.setRequestHeader("Target-URL", loginURL);
}
xhr.send();
});
/**
* Initialize ForceJS
* @param params
* appId (optional)
* loginURL (optional)
* proxyURL (optional)
* oauthCallbackURL (optional)
* apiVersion (optional)
* accessToken (optional)
* instanceURL (optional)
* refreshToken (optional)
*/
export let init = params => {
if (params) {
appId = params.appId || appId;
apiVersion = params.apiVersion || apiVersion;
loginURL = params.loginURL || loginURL;
oauthCallbackURL = params.oauthCallbackURL || oauthCallbackURL;
proxyURL = params.proxyURL || proxyURL;
useProxy = params.useProxy === undefined ? useProxy : params.useProxy;
if (params.accessToken) {
if (!oauthData) oauthData = {};
oauthData.access_token = params.accessToken;
}
if (params.instanceURL) {
if (!oauthData) oauthData = {};
oauthData.instance_url = params.instanceURL;
}
if (params.refreshToken) {
if (!oauthData) oauthData = {};
oauthData.refresh_token = params.refreshToken;
}
}
console.log("useProxy: " + useProxy);
};
/**
* Discard the OAuth access_token. Use this function to test the refresh token workflow.
*/
export let discardToken = () => {
delete oauthData.access_token;
tokenStore.forceOAuth = JSON.stringify(oauthData);
};
export let login = () => new Promise((resolve, reject) => {
console.log('loginURL: ' + loginURL);
console.log('oauthCallbackURL: ' + oauthCallbackURL);
let loginWindowURL = loginURL + '/services/oauth2/authorize?client_id=' + appId + '&redirect_uri=' + oauthCallbackURL + '&response_type=token';
document.addEventListener("oauthCallback", (event) => {
// Parse the OAuth data received from Salesforce
let url = event.detail,
queryString,
obj;
if (url.indexOf("access_token=") > 0) {
queryString = url.substr(url.indexOf('#') + 1);
obj = parseQueryString(queryString);
oauthData = obj;
tokenStore.forceOAuth = JSON.stringify(oauthData);
resolve(oauthData);
} else if (url.indexOf("error=") > 0) {
queryString = decodeURIComponent(url.substring(url.indexOf('?') + 1));
obj = parseQueryString(queryString);
reject(obj);
} else {
reject({status: 'access_denied'});
}
});
window.open(loginWindowURL, '_blank', 'location=no');
});
/**
* Gets the user's ID (if logged in)
* @returns {string} | undefined
*/
export let getUserId = () => (typeof(oauthData) !== 'undefined') ? oauthData.id.split('/').pop() : undefined;
/**
* Get the OAuth data returned by the Salesforce login process
*/
export let getOAuthData = () => oauthData;
/**
* Check the login status
* @returns {boolean}
*/
export let isAuthenticated = () => (oauthData && oauthData.access_token) ? true : false;