-
Notifications
You must be signed in to change notification settings - Fork 12
/
index.js
179 lines (147 loc) · 5.09 KB
/
index.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
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
// heavily inspired by pycloud - https://github.com/picklepete/pyicloud/
var request = require('request');
var uuid = require('node-uuid');
var _ = require('lodash');
// configuration
var setup = 'https://p12-setup.icloud.com/setup/ws/1';
var conf = {
home : 'https://www.icloud.com',
setup : setup,
login : setup + "/login",
validate : setup + "/validate",
}
module.exports = function() {
// default request object
var req = request.defaults({
headers : {
'host': 'setup.icloud.com',
'origin' : conf.home,
'referer' : conf.home,
'User-Agent': 'Opera/9.52 (X11; Linux i686; U; en)'
},
jar : request.jar(),
json : true
});
// store various request meta credentials
var session = { params : {
clientBuildNumber : '14FPlus30',
clientId : uuid.v1().toUpperCase(),
} };
/*
Queries the /validate endpoint and fetches two key values we need:
1. "dsInfo" is a nested object which contains the "dsid" integer.
This object doesn't exist until *after* the login has taken place,
the first request will compain about a X-APPLE-WEBAUTH-TOKEN cookie
*/
function refresh_validate(cb) {
// make the request via the session params
req.get({
url : conf.validate,
qs : session.params
}, function(err, resp, data) {
if (err) return cb(err);
// capture the dsid
if (data.dsInfo) { session.params.dsid = data.dsInfo.dsid; }
cb(null);
});
}
function login(apple_id, password, cb) {
// store the user info
session.user = {
apple_id : apple_id,
password : password
}
// validate before login
refresh_validate(function(err, results) {
if (err) return cb(err);
// craft data for login request
var data = _.clone(session.user);
data.id = session.params.id;
data.extended_login = false;
// login request
req.post({
url : conf.login,
qs : session.params,
json: data
}, function(err, resp, data) {
if (err || data.error)
return cb("Invalid email/password combination.");
// store the results
session.discovery = data;
session.webservices = data.webservices;
// refresh after login
refresh_validate(cb);
});
});
}
// fetch contacts
function contacts(cb) {
if (!session.webservices || !session.webservices.contacts)
return cb("No webservice found for contacts");
var params = _.extend({}, session.params, {
clientVersion : "2.1",
locale : "en_US",
order : "last,first",
});
var url = session.webservices.contacts.url.replace(':443', '');
req.get({
url : session.webservices.contacts.url + "/co/startup",
qs : params,
headers : {
host : session.webservices.contacts.url.split('//')[1].split(':')[0],
}
}, function(err, resp, body) {
if (err) return cb(err);
cb(null, body);
});
}
// fetch events
function events(from,to,timezone,startup, cb) {
if (!session.webservices || !session.webservices.calendar)
return cb("No webservice found for calendars");
var params = _.extend({}, session.params, {
lang: 'en-us',
usertz: timezone,
startDate: from,
endDate: to
});
var url = session.webservices.calendar.url.replace(':443', '');
var urlpath = (startup ? 'startup' : 'events');
req.get({
url : session.webservices.calendar.url + "/ca/" + urlpath,
qs : params,
headers : {
host : session.webservices.calendar.url.split('//')[1].split(':')[0],
}
}, function(err, resp, body) {
if (err) return cb(err);
cb(null, body);
});
}
// fetch event details
function event(calId,eventId,timezone,cb) {
if (!session.webservices || !session.webservices.calendar)
return cb("No webservice found for calendars");
var params = _.extend({}, session.params, {
lang: 'en-us',
usertz: timezone
});
var url = session.webservices.calendar.url.replace(':443', '');
req.get({
url : session.webservices.calendar.url + "/ca/eventdetail/" + calId + "/" + eventId,
qs : params,
headers : {
host : session.webservices.calendar.url.split('//')[1].split(':')[0],
}
}, function(err, resp, body) {
if (err) return cb(err);
cb(null, body);
});
}
return {
login: login,
contacts: contacts,
events: events,
event: event
}
}