-
Notifications
You must be signed in to change notification settings - Fork 1
/
arcgis_server.js
131 lines (112 loc) · 3.38 KB
/
arcgis_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
var OAuth = Package.oauth.OAuth;
Arcgis.whitelistedFields = ["username", "email"];
OAuth.registerService('arcgis', 2, null, function(query){
var response = getTokens(query);
var refresh_token = response.refresh_token;
var identity = JSON.parse(getIdentity(response.access_token, response.username));
//console.log("ZZZZZZZZZZZZZZZZZ"+JSON.stringify(identity));
//console.log("asdfsadf"+refresh_token)
var serviceData = {
access_token: response.access_token,
expires_in: (+new Date) + (1000 * response.expires_in),
id: response.username
};
if(refresh_token){
serviceData.refresh_token = refresh_token;
}
var fields = _.pick(identity, Arcgis.whitelistedFields);
_.extend(serviceData, fields);
return{
serviceData: serviceData,
options: { profile: fields }
}
});
var isJSON = function (str) {
try {
JSON.parse(str);
return true;
} catch (e) {
return false;
}
};
var getTokens= function(query){
var config = ServiceConfiguration.configurations.findOne({service: 'arcgis'});
if(!config)
throw new ServiceConfiguration.ConfigError();
//console.log(OAuth._redirectUri('arcgis', config))
var response;
try{
response = HTTP.post("https://www.arcgis.com/sharing/rest/oauth2/token/", { params:{
client_id: config.clientId,
client_secret: OAuth.openSecret(config.secret),
redirect_uri: OAuth._redirectUri('arcgis', config),
grant_type: 'authorization_code',
code: query.code
}});
}catch(err){
throw _.extend(new Error("Failed"),{response: err.response})
}
//console.log(response);
if (isJSON(response)){
throw new Error("failed to complete")
}else if(! JSON.parse(response.content).access_token){
throw new Error("no access token")
}else{
jsoncontent = JSON.parse(response.content);
return{
access_token: jsoncontent.access_token,
refresh_token: jsoncontent.refresh_token,
expires_in: jsoncontent.expires_in,
username: jsoncontent.username
};
}
//var response;
//try{
//var response =
// HTTP.post("https://www.arcgis.com/sharing/rest/oauth2/token/", { params:{
// client_id: config.clientId,
// client_secret: OAuth.openSecret(config.secret),
// redirect_uri: OAuth._redirectUri('arcgis', config),
// grant_type: 'authorization_code',
// code: query.code
//
// }
// }, function(error, result){
// if(error){
// console.log("error", error);
// }
// if(result){
// console.log(result)
// if(isJSON(result)){
//
// }else if(!result.access_token){
//
// }else{
// return{
// access_token: result.access_token,
// refresh_token: result.refresh_token,
// expires_in: result.expires_in
// }
// }
//
//
// }
// });
//}
};
var getIdentity = function(accessToken, username){
try{
return HTTP.get(
"https://arcgis.com/sharing/rest/community/users/"+username,
{params: {
token: accessToken,
f: "json"
}}
).content;
}catch (err){
throw _.extend(new Error("Failed to get Identify" + err.message), {response: err.response});
}
}
Arcgis.retrieveCredential = function(credentialToken, credentialSecret){
return OAuth.retrieveCredential(credentialToken, credentialSecret);
}