-
Notifications
You must be signed in to change notification settings - Fork 53
/
auth-helper-salesforce.ts
68 lines (64 loc) · 2.17 KB
/
auth-helper-salesforce.ts
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
/// <reference path="references.d.ts" />
import * as URL from 'url';
import * as http from 'http';
import * as querystring from 'querystring';
import * as tnsOauth from './tns-oauth';
import { AuthHelper } from './auth-helper';
import * as TnsOAuth from './tns-oauth-interfaces';
/**
Contains Salesforce connection credentials
*/
export class AuthHelperSalesforce extends AuthHelper implements TnsOAuth.ITnsAuthHelper {
//Constructs the the object with specified id, secret and scope
constructor(
authority: string,
clientId: string,
redirectUri: string,
responseType: string,
scope: Array<string>,
clientSecret?: string,
) {
super();
var scopeStr = scope.join('%20');
this.credentials = {
authority: authority,
authorizeEndpoint: '/services/oauth2/authorize',
tokenEndpoint: '/services/oauth2/token',
revokeEndpoint: '/services/oauth2/revoke',
clientId: clientId,
clientSecret: clientSecret,
redirectUri: redirectUri,
responseType: responseType,
scope: scopeStr
};
}
//Gets cookie domains for logging out
public logout(): Promise<void> {
return new Promise<void>((resolve, reject) => {
try {
// call revoke token here
var revoke_url = this.credentials.authority + this.credentials.revokeEndpoint;
var post_headers = {
// XXX: Not sure if this header is needed
//'Host': URL.parse(revoke_url, true),
'Content-Type': 'application/x-www-form-urlencoded'
};
var post_body = querystring.stringify({token: this.tokenResult.refreshToken});
http.request({
url: revoke_url,
method: 'POST',
headers: post_headers,
content: post_body
}).then((response: http.HttpResponse) => {
if (response.statusCode != 200) {
throw new Error(`Failed logout with status ${ response.statusCode }.`);
}
});
this.tokenResult = null;
resolve();
} catch (er) {
reject(er);
}
});
}
}