forked from optilude/meteor-jira-auth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
150 lines (128 loc) · 5.34 KB
/
server.js
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
/* global OAuth, ServiceConfiguration, Meteor, HTTP, _, Accounts, Npm, Random */
"use strict";
const JiraClient = Npm.require('jira-connector');
const getAuthorizeURL = Meteor.wrapAsync(JiraClient.oauth_util.getAuthorizeURL, JiraClient.oauth_util);
const swapRequestTokenWithAccessToken = Meteor.wrapAsync(JiraClient.oauth_util.swapRequestTokenWithAccessToken, JiraClient.oauth_util);
Accounts.addAutopublishFields(
{
forLoggedInUser: ['services.jira'],
forOtherUsers: []
}
);
Accounts.oauth.registerService("jira");
OAuth.registerService("jira", "1.0-jira", {}, (oauthInfo, options) => {
let
jiraClient = new JiraClient({
host: oauthInfo.host,
port: oauthInfo.port,
protocol: oauthInfo.protocol,
oauth: {
consumer_key: oauthInfo.consumerKey,
private_key: oauthInfo.privateKey,
token: oauthInfo.accessToken,
token_secret: oauthInfo.accessTokenSecret
}
}),
myself = Meteor.wrapAsync(jiraClient.myself.getMyself, jiraClient.myself)({}),
serviceData = {
id: oauthInfo.host + "::" + myself.key,
username: myself.name,
name: myself.displayName,
email: myself.emailAddress,
host: oauthInfo.host,
port: oauthInfo.port,
protocol: oauthInfo.protocol,
accessToken: OAuth.sealSecret(oauthInfo.accessToken),
accessTokenSecret: OAuth.sealSecret(oauthInfo.accessTokenSecret)
};
return {
serviceData: serviceData,
options: {
profile: {
name: myself.displayName,
email: myself.emailAddress
}
}
};
});
OAuth._requestHandlers['1.0-jira'] = (service, query, res) => {
let
config = ServiceConfiguration.configurations.findOne({service: service.serviceName}),
credentialSecret,
oauthResponse;
if (!config) throw new ServiceConfiguration.ConfigError(service.serviceName);
if (query.requestTokenAndRedirect) {
var callbackUrl = OAuth._redirectUri(
service.serviceName,
config,
{
state: query.state,
jiraHost: query.jiraHost || config.jiraHost,
jiraPort: query.jiraPort || config.jiraPort,
jiraProtocol: query.jiraProtocol || config.jiraProtocol,
cordova: (query.cordova === "true"),
android: (query.android === "true")
}
);
oauthResponse = getAuthorizeURL(
{
host: query.jiraHost || config.jiraHost,
port: query.jiraPort || config.jiraPort,
protocol: query.jiraProtocol || config.jiraProtocol,
oauth: {
consumer_key: config.consumerKey,
private_key: config.privateKey,
callback_url: callbackUrl
}
}
);
// Keep track of request token so we can verify it on the next step
OAuth._storeRequestToken(
OAuth._credentialTokenFromQuery(query),
oauthResponse.token,
oauthResponse.token_secret
);
res.writeHead(302, {'Location': oauthResponse.url});
res.end();
} else {
let requestTokenInfo = OAuth._retrieveRequestToken(OAuth._credentialTokenFromQuery(query));
if (!requestTokenInfo) throw new Error("Unable to retrieve request token");
if (query.oauth_token && query.oauth_token === requestTokenInfo.requestToken) {
let
accessToken = swapRequestTokenWithAccessToken(
{
host: query.jiraHost || config.jiraHost,
port: query.jiraPort || config.jiraPort,
protocol: query.jiraProtocol || config.jiraProtocol,
oauth: {
consumer_key: config.consumerKey,
private_key: config.privateKey,
token: requestTokenInfo.requestToken,
token_secret: requestTokenInfo.requestTokenSecret,
oauth_verifier: query.oauth_verifier
}
}
),
oauthResult = service.handleOauthRequest(
{
host: query.jiraHost || config.jiraHost,
port: query.jiraPort || config.jiraPort,
protocol: query.jiraProtocol || config.jiraProtocol,
consumerKey: config.consumerKey,
privateKey: config.privateKey,
accessToken: accessToken,
accessTokenSecret: requestTokenInfo.requestTokenSecret
},
{query: query}
),
credentialToken = OAuth._credentialTokenFromQuery(query);
credentialSecret = Random.secret();
OAuth._storePendingCredential(credentialToken, {
serviceName: service.serviceName,
serviceData: oauthResult.serviceData,
options: oauthResult.options
}, credentialSecret);
}
OAuth._renderOauthResults(res, query, credentialSecret);
}
};