Skip to content

Commit

Permalink
feat: add input-monitoring auth status
Browse files Browse the repository at this point in the history
Closes #33
  • Loading branch information
codebytere committed Apr 25, 2022
1 parent f419d22 commit 188b6f2
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 37 deletions.
29 changes: 16 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,19 @@ $ npm i node-mac-permissions

This native Node.js module allows you to manage an app's access to:

* Contacts
* Full Disk Access
* Accessibility
* Calendar
* Reminders
* Camera
* Contacts
* Full Disk Access
* Input Monitoring
* Location
* Microphone
* Photos
* Accessibility
* Location
* Protected Folders
* Reminders
* Screen Capture
* Speech Recognition
* Protected Folders

If you need to ask for permissions, your app must be allowed to ask for permission :

Expand All @@ -33,7 +34,7 @@ If you're using macOS 12.3 or newer, you'll need to ensure you have Python insta

## `permissions.getAuthStatus(type)`

* `type` String - The type of system component to which you are requesting access. Can be one of `accessibility`, `bluetooth`, `calendar`, `camera`, `contacts`, `full-disk-access`, `speech-recognition`, `location`, `microphone`, `photos`, `screen`, or `reminders`.
* `type` String - The type of system component to which you are requesting access. Can be one of `accessibility`, `bluetooth`, `calendar`, `camera`, `contacts`, `full-disk-access`, `input-monitoring`, `location`, `microphone`,`photos`, `reminders`, `screen`, or `speech-recognition`.

Returns `String` - Can be one of `not determined`, `denied`, `authorized`, or `restricted`.

Expand All @@ -46,13 +47,14 @@ Return Value Descriptions:
* `authorized` - The application is authorized to access `type` data.

**Notes:**
* Access to `contacts` will always return a status of `authorized` prior to macOS 10.11, as the underlying API was not introduced until that version.
* Access to `bluetooth` will always return a status of `authorized` prior to macOS 10.15, as the underlying API was not introduced until that version.
* Access to `camera` and `microphone` will always return a status of `authorized` prior to macOS 10.14, as the underlying API was not introduced until that version.
* Access to `screen` will always return a status of `authorized` prior to macOS 10.15, as the underlying API was not introduced until that version.
* Access to `contacts` will always return a status of `authorized` prior to macOS 10.11, as the underlying API was not introduced until that version.
* Access to `input-monitoring` will always return a status of `authorized` prior to macOS 10.15, as the underlying API was not introduced until that version.
* Access to `music-library` will always return a status of `authorized` prior to macOS 11.0, as the underlying API was not introduced until that version.
* Access to `photos` will always return a status of `authorized` prior to macOS 10.13, as the underlying API was not introduced until that version.
* Access to `screen` will always return a status of `authorized` prior to macOS 10.15, as the underlying API was not introduced until that version.
* Access to `speech-recognition` will always return a status of `authorized` prior to macOS 10.15, as the underlying API was not introduced until that version.
* Access to `bluetooth` will always return a status of `authorized` prior to macOS 10.15, as the underlying API was not introduced until that version.
* Access to `music-library` will always return a status of `authorized` prior to macOS 11.0, as the underlying API was not introduced until that version.

Example:
```js
Expand All @@ -62,14 +64,15 @@ const types = [
'calendar',
'camera',
'contacts',
'reminders',
'full-disk-access',
'input-monitoring',
'location',
'microphone',
'music-library',
'photos',
'reminders',
'speech-recognition',
'screen',
'speech-recognition'
]

for (const type of types) {
Expand Down
1 change: 1 addition & 0 deletions binding.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"-framework CoreGraphics",
"-framework Contacts",
"-framework EventKit",
"-framework IOKit",
"-framework Photos",
"-framework Speech",
"-framework Storekit"
Expand Down
15 changes: 8 additions & 7 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,19 @@ export function askForAccessibilityAccess(): undefined
export function getAuthStatus(authType: AuthType): PermissionType | 'not determined'

export type AuthType =
| 'accessibility'
| 'bluetooth'
| 'contacts'
| 'calendar'
| 'reminders'
| 'full-disk-access'
| 'camera'
| 'photos'
| 'speech-recognition'
| 'contacts'
| 'full-disk-access'
| 'input-monitoring'
| 'location'
| 'microphone'
| 'music-library'
| 'accessibility'
| 'location'
| 'photos'
| 'reminders'
| 'speech-recognition'
| 'screen'

export type PermissionType = 'authorized' | 'denied' | 'restricted'
21 changes: 11 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,20 @@ const permissions = require('bindings')('permissions.node')

function getAuthStatus(type) {
const validTypes = [
'contacts',
'accessibility',
'bluetooth',
'calendar',
'reminders',
'full-disk-access',
'camera',
'photos',
'speech-recognition',
'contacts',
'full-disk-access',
'input-monitoring',
'location',
'microphone',
'music-library',
'accessibility',
'location',
'photos',
'reminders',
'speech-recognition',
'screen',
'bluetooth',
]

if (!validTypes.includes(type)) {
Expand All @@ -35,17 +36,17 @@ function askForFoldersAccess(folder) {
}

module.exports = {
askForAccessibilityAccess: permissions.askForAccessibilityAccess,
askForCalendarAccess: permissions.askForCalendarAccess,
askForCameraAccess: permissions.askForCameraAccess,
askForContactsAccess: permissions.askForContactsAccess,
askForFoldersAccess,
askForFullDiskAccess: permissions.askForFullDiskAccess,
askForRemindersAccess: permissions.askForRemindersAccess,
askForCameraAccess: permissions.askForCameraAccess,
askForMicrophoneAccess: permissions.askForMicrophoneAccess,
askForMusicLibraryAccess: permissions.askForMusicLibraryAccess,
askForPhotosAccess: permissions.askForPhotosAccess,
askForSpeechRecognitionAccess: permissions.askForSpeechRecognitionAccess,
askForScreenCaptureAccess: permissions.askForScreenCaptureAccess,
askForAccessibilityAccess: permissions.askForAccessibilityAccess,
getAuthStatus,
}
20 changes: 20 additions & 0 deletions permissions.mm
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#import <CoreLocation/CoreLocation.h>
#import <EventKit/EventKit.h>
#import <Foundation/Foundation.h>
#import <IOKit/hidsystem/IOHIDLib.h>
#import <Photos/Photos.h>
#import <Speech/Speech.h>
#import <Storekit/Storekit.h>
Expand Down Expand Up @@ -136,6 +137,23 @@ bool HasOpenSystemPreferencesDialog() {
return kAuthorized;
}

// Returns a status indicating whether the user has authorized
// input monitoring access.
std::string InputMonitoringAuthStatus() {
if (@available(macOS 10.15, *)) {
switch (IOHIDCheckAccess(kIOHIDRequestTypeListenEvent)) {
case kIOHIDAccessTypeGranted:
return kAuthorized;
case kIOHIDAccessTypeDenied:
return kDenied;
default:
return kNotDetermined;
}
}

return kAuthorized;
}

// Returns a status indicating whether the user has authorized Apple Music
// Library access.
std::string MusicLibraryAuthStatus() {
Expand Down Expand Up @@ -361,6 +379,8 @@ bool HasOpenSystemPreferencesDialog() {
auth_status = BluetoothAuthStatus();
} else if (type == "music-library") {
auth_status = MusicLibraryAuthStatus();
} else if (type == "input-monitoring") {
auth_status = InputMonitoringAuthStatus();
}

return Napi::Value::From(env, auth_status);
Expand Down
16 changes: 9 additions & 7 deletions test/module.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,19 @@ describe('node-mac-permissions', () => {

it('should return a string', () => {
const types = [
'contacts',
'accessibility',
'bluetooth',
'calendar',
'reminders',
'full-disk-access',
'camera',
'photos',
'speech-recognition',
'contacts',
'full-disk-access',
'input-monitoring',
'location',
'microphone',
'music-library',
'accessibility',
'location',
'photos',
'reminders',
'speech-recognition',
'screen',
]

Expand Down

0 comments on commit 188b6f2

Please sign in to comment.