forked from codebytere/node-mac-permissions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpermissions.mm
253 lines (216 loc) · 9.55 KB
/
permissions.mm
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
#include <napi.h>
// Apple APIs
#import <AppKit/AppKit.h>
#import <AVFoundation/AVFoundation.h>
#import <Contacts/Contacts.h>
#import <EventKit/EventKit.h>
#import <Foundation/Foundation.h>
#import <Photos/Photos.h>
#import <pwd.h>
/***** HELPER FUNCTIONS *****/
NSString* GetUserHomeFolderPath() {
NSString* path;
BOOL isSandboxed = (nil != NSProcessInfo.processInfo.environment[@"APP_SANDBOX_CONTAINER_ID"]);
if (isSandboxed) {
struct passwd *pw = getpwuid(getuid());
assert(pw);
path = [NSString stringWithUTF8String:pw->pw_dir];
} else {
path = NSHomeDirectory();
}
return path;
}
// Returns a status indicating whether or not the user has authorized Contacts access
std::string ContactAuthStatus() {
std::string auth_status = "not determined";
CNEntityType entity_type = CNEntityTypeContacts;
CNAuthorizationStatus status = [CNContactStore authorizationStatusForEntityType:entity_type];
if (status == CNAuthorizationStatusAuthorized)
auth_status = "authorized";
else if (status == CNAuthorizationStatusDenied)
auth_status = "denied";
else if (status == CNAuthorizationStatusRestricted)
auth_status = "restricted";
return auth_status;
}
// Returns a status indicating whether or not the user has authorized Calendar/Reminders access
std::string EventAuthStatus(const std::string& type) {
std::string auth_status = "not determined";
EKEntityType entity_type = (type == "calendar") ? EKEntityTypeEvent : EKEntityTypeReminder;
EKAuthorizationStatus status = [EKEventStore authorizationStatusForEntityType:entity_type];
if (status == EKAuthorizationStatusAuthorized)
auth_status = "authorized";
else if (status == EKAuthorizationStatusDenied)
auth_status = "denied";
else if (status == EKAuthorizationStatusRestricted)
auth_status = "restricted";
return auth_status;
}
// Returns a status indicating whether or not the user has Full Disk Access
std::string FDAAuthStatus() {
std::string auth_status = "not determined";
NSString *path;
NSString* home_folder = GetUserHomeFolderPath();
if (@available(macOS 10.15, *)) {
path = [home_folder stringByAppendingPathComponent:@"Library/Safari/CloudTabs.db"];
} else {
path = [home_folder stringByAppendingPathComponent:@"Library/Safari/Bookmarks.plist"];
}
NSFileManager* manager = [NSFileManager defaultManager];
BOOL file_exists = [manager fileExistsAtPath:path];
NSData *data = [NSData dataWithContentsOfFile:path];
if (data == nil && file_exists) {
auth_status = "denied";
} else if (file_exists) {
auth_status = "authorized";
}
return auth_status;
}
// Returns a status indicating whether or not the user has authorized Camera/Microphone access
std::string MediaAuthStatus(const std::string& type) {
std::string auth_status = "not determined";
if (@available(macOS 10.14, *)) {
AVMediaType media_type = (type == "microphone") ? AVMediaTypeAudio : AVMediaTypeVideo;
AVAuthorizationStatus status = [AVCaptureDevice authorizationStatusForMediaType:media_type];
if (status == AVAuthorizationStatusAuthorized)
auth_status = "authorized";
else if (status == AVAuthorizationStatusDenied)
auth_status = "denied";
else if (status == AVAuthorizationStatusRestricted)
auth_status = "restricted";
} else {
auth_status = "authorized";
}
return auth_status;
}
/***** EXPORTED FUNCTIONS *****/
// Returns the user's access consent status as a string
Napi::Value GetAuthStatus(const Napi::CallbackInfo &info) {
Napi::Env env = info.Env();
std::string auth_status;
const std::string type = info[0].As<Napi::String>().Utf8Value();
if (type == "contacts") {
auth_status = ContactAuthStatus();
} else if (type == "calendar") {
auth_status = EventAuthStatus("calendar");
} else if (type == "reminders") {
auth_status = EventAuthStatus("reminders");
} else if (type == "full-disk-access") {
auth_status = FDAAuthStatus();
} else if (type == "microphone") {
auth_status = MediaAuthStatus("microphone");
} else if (type == "camera") {
auth_status = MediaAuthStatus("camera");
}
return Napi::Value::From(env, auth_status);
}
// Request access to the Contacts store.
void AskForContactsAccess(const Napi::CallbackInfo &info) {
Napi::Env env = info.Env();
Napi::ThreadSafeFunction ts_fn = Napi::ThreadSafeFunction::New(env,
info[0].As<Napi::Function>(),
"Resource Name",
0,
1,
[](Napi::Env){});
if (@available(macOS 10.11, *)) {
CNContactStore *store = [CNContactStore new];
[store requestAccessForEntityType:CNEntityTypeContacts
completionHandler:^(BOOL granted, NSError* error) {
auto callback = [](Napi::Env env, Napi::Function js_cb, const char* granted) {
js_cb.Call({Napi::String::New(env, granted)});
};
ts_fn.BlockingCall(granted ? "authorized" : "denied", callback);
}];
} else {
Napi::FunctionReference fn = Napi::Persistent(info[0].As<Napi::Function>());
fn.Call({Napi::String::New(env, "authorized")});
}
}
// Request access to Calendar.
void AskForCalendarAccess(const Napi::CallbackInfo &info) {
Napi::Env env = info.Env();
Napi::ThreadSafeFunction ts_fn = Napi::ThreadSafeFunction::New(env,
info[0].As<Napi::Function>(),
"Resource Name",
0,
1,
[](Napi::Env){});
[[EKEventStore new] requestAccessToEntityType:EKEntityTypeEvent
completion:^(BOOL granted, NSError * error) {
auto callback = [](Napi::Env env, Napi::Function js_cb, const char* granted) {
js_cb.Call({Napi::String::New(env, granted)});
};
ts_fn.BlockingCall(granted ? "authorized" : "denied", callback);
}];
}
// Request access to Reminders.
void AskForRemindersAccess(const Napi::CallbackInfo &info) {
Napi::Env env = info.Env();
Napi::ThreadSafeFunction ts_fn = Napi::ThreadSafeFunction::New(env,
info[0].As<Napi::Function>(),
"Resource Name",
0,
1,
[](Napi::Env){});
[[EKEventStore new] requestAccessToEntityType:EKEntityTypeReminder
completion:^(BOOL granted, NSError * error) {
auto callback = [](Napi::Env env, Napi::Function js_cb, const char* granted) {
js_cb.Call({Napi::String::New(env, granted)});
};
ts_fn.BlockingCall(granted ? "authorized" : "denied", callback);
}];
}
// Request Full Disk Access.
void AskForFullDiskAccess(const Napi::CallbackInfo &info) {
NSWorkspace* workspace = [[NSWorkspace alloc] init];
NSString* pref_string = @"x-apple.systempreferences:com.apple.preference.security?Privacy_AllFiles";
[workspace openURL:[NSURL URLWithString:pref_string]];
}
// Request access to either the Camera or the Microphone.
void AskForMediaAccess(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
const std::string type = info[0].As<Napi::String>().Utf8Value();
Napi::ThreadSafeFunction ts_fn = Napi::ThreadSafeFunction::New(env,
info[1].As<Napi::Function>(),
"Resource Name",
0,
1,
[](Napi::Env){});
if (@available(macOS 10.14, *)) {
AVMediaType media_type = (type == "microphone") ? AVMediaTypeAudio : AVMediaTypeVideo;
[AVCaptureDevice requestAccessForMediaType:media_type
completionHandler:^(BOOL granted) {
auto callback = [](Napi::Env env, Napi::Function js_cb, const char* granted) {
js_cb.Call({Napi::String::New(env, granted)});
};
ts_fn.BlockingCall(granted ? "authorized" : "denied", callback);
}];
} else {
Napi::FunctionReference fn = Napi::Persistent(info[0].As<Napi::Function>());
fn.Call({Napi::String::New(env, "authorized")});
}
}
// Initializes all functions exposed to JS
Napi::Object Init(Napi::Env env, Napi::Object exports) {
exports.Set(
Napi::String::New(env, "getAuthStatus"), Napi::Function::New(env, GetAuthStatus)
);
exports.Set(
Napi::String::New(env, "askForContactsAccess"), Napi::Function::New(env, AskForContactsAccess)
);
exports.Set(
Napi::String::New(env, "askForCalendarAccess"), Napi::Function::New(env, AskForCalendarAccess)
);
exports.Set(
Napi::String::New(env, "askForRemindersAccess"), Napi::Function::New(env, AskForRemindersAccess)
);
exports.Set(
Napi::String::New(env, "askForFullDiskAccess"), Napi::Function::New(env, AskForFullDiskAccess)
);
exports.Set(
Napi::String::New(env, "askForMediaAccess"), Napi::Function::New(env, AskForMediaAccess)
);
return exports;
}
NODE_API_MODULE(permissions, Init)