-
Notifications
You must be signed in to change notification settings - Fork 7
/
OnePasswordSource.m
196 lines (176 loc) · 6.8 KB
/
OnePasswordSource.m
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
//
// OnePasswordSource.m
// OnePassword
//
// Created by Patrick Robertson on 15/01/2010.
// Copyright Patrick Roberston 2010. All rights reserved.
//
// This file is part of Quicksilver 1Password Module.
//
// Quicksilver 1Password Module is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Quicksilver 1Password Module is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Quicksilver 1Password Module. If not, see <http://www.gnu.org/licenses/>.
//
// See https://support.1password.com/integration-mac/
#import "OnePasswordSource.h"
#import "OnePasswordDefines.h"
#import <YAJL/YAJL.h>
QSObject *onePasswordURLObject(NSString *itemURL, QSObject *parentObject) {
NSString *ident = [NSString stringWithFormat:@"1PasswordURL:%@", itemURL];
NSString *name = [NSString stringWithFormat:@"Website for %@", [parentObject displayName]];
QSObject *newObject = [QSObject makeObjectWithIdentifier:ident];
[newObject setName:name];
[newObject setDetails:itemURL];
[newObject setObject:itemURL forType:QSURLType];
[newObject setObject:itemURL forType:QS1PasswordURLType];
[newObject setPrimaryType:QS1PasswordURLType];
[newObject setParentID:[parentObject identifier]];
return newObject;
}
@implementation OnePasswordSource
- (void)dealloc
{
self.bundleID = nil;
}
static id _sharedInstance;
+ (id)sharedInstance {
if (!_sharedInstance) _sharedInstance = [[[self class] alloc] init];
return _sharedInstance;
}
-(id)init {
if (self = [super init]) {
self.bundleID = kVersion7BundleID;
// QSDefaults probably won't be read until after this, so maybe pointless
NSString *location = [k1Password3rdPartyItemsPath stringByStandardizingPath];
NSFileManager *fm = [[NSFileManager alloc] init];
// valid location exists
BOOL valid = location && [fm fileExistsAtPath:location];
if (!valid) {
NSImage *icon = [QSResourceManager imageNamed:kQS1PasswordIcon];
QSShowNotifierWithAttributes(@{
QSNotifierType: @"OnePasswordNotifType",
QSNotifierIcon: icon ? icon : [QSResourceManager imageNamed:@"com.blacktree.Quicksilver"],
QSNotifierTitle : @"Unable to locate 1Password items",
QSNotifierText : @"Please enable 3rd Party integration in 1Password to use the 1Password Plugin"
});
}
}
return self;
}
- (NSString *)keychainPath {
return [k1Password3rdPartyItemsPath stringByStandardizingPath];
}
- (BOOL)indexIsValidFromDate:(NSDate *)indexDate forEntry:(NSDictionary *)theEntry{
// Check to see if keychain has been modified since last scan
NSError *error = nil;
NSFileManager *fm = [[NSFileManager alloc] init];
NSDate *modDate=[[fm attributesOfItemAtPath:[self keychainPath] error:&error] fileModificationDate];
if (error) {
NSLog(@"Error: %@", error);
return NO;
}
// return the difference between the keychain mod date and the last index time
return ([modDate compare:indexDate]==NSOrderedAscending);
}
- (NSString *)detailsOfObject:(QSObject *)object
{
if ([[object primaryType] isEqualToString:QS1PasswordItemType]) {
return [object objectForMeta:kOnePasswordItemDetails];
}
return nil;
}
- (BOOL)objectHasChildren:(QSObject *)object
{
if ([[object primaryType] isEqualToString:QS1PasswordItemType]) {
if ([[object objectForMeta:kOnePasswordItemURLs] count]) {
return YES;
}
}
return NO;
}
- (BOOL)loadChildrenForObject:(QSObject *)object
{
if ([[object primaryType] isEqualToString:QS1PasswordItemType]) {
NSArray *URLs = [object objectForMeta:kOnePasswordItemURLs];
NSMutableArray *urlObjects = [NSMutableArray arrayWithCapacity:[URLs count]];
QSObject *newObject;
NSString *name;
NSInteger URLCount = 1;
for (NSString *itemURL in URLs) {
newObject = onePasswordURLObject(itemURL, object);
if (URLCount > 1) {
name = [NSString stringWithFormat:@"Website %lu for %@", (long)URLCount, [object displayName]];
[newObject setName:name];
}
[urlObjects addObject:newObject];
URLCount += 1;
}
[object setChildren:urlObjects];
} else {
// For the children to 1Pwd, just load items from the catalog
NSMutableArray *children = [[QSLib scoredArrayForType:QS1PasswordItemType] mutableCopy];
[object setChildren:children];
}
return YES;
}
- (NSArray *)objectsForEntry:(QSCatalogEntry *)theEntry
{
NSDictionary *settings = [theEntry sourceSettings];
NSString *scanCategory = [settings objectForKey:kOnePasswordItemCategory];
// Define the objects (Empty to start with) we're going to send back to QS
NSMutableArray *objects = [NSMutableArray array];
QSObject *newObject;
NSString *location = [self keychainPath];
NSSet *searchLocations = [NSSet setWithObject:location];
NSMetadataQuery *passwordSearch = [[NSMetadataQuery alloc] init];
NSArray *OPItems = [passwordSearch resultsForSearchString:@"kMDItemContentType == 'com.agilebits.itemmetadata'" inFolders:searchLocations];
for (NSMetadataItem *item in OPItems) {
NSDictionary *metadata = [item valuesForAttributes:@[(NSString *)kMDItemPath]];
NSString *itemPath = metadata[(NSString *)kMDItemPath];
NSData *JSONData = [NSData dataWithContentsOfFile:itemPath];
NSDictionary *OPItem = [JSONData yajl_JSON];
NSString *category = OPItem[kOnePasswordItemCategory];
if (![category isEqualToString:scanCategory]) {
continue;
}
NSString *uuid = OPItem[@"uuid"];
NSString *title = OPItem[@"itemTitle"];
NSArray *urls = OPItem[@"websiteURLs"];
NSString *details = OPItem[@"itemDescription"];
NSString *vault = OPItem[@"profileUUID"];
newObject = [QSObject makeObjectWithIdentifier:[NSString stringWithFormat:@"1PasswordItem:%@", uuid]];
[newObject setName:title];
[newObject setObject:itemPath forType:QSFilePathType];
[newObject setObject:category forMeta:kOnePasswordItemCategory];
[newObject setObject:details forMeta:kOnePasswordItemDetails];
[newObject setObject:vault forMeta:kOnePasswordVaultIdentifier];
if (urls) {
NSString *firstURL = urls[0];
[newObject setObject:firstURL forType:QSURLType];
[newObject setObject:urls forMeta:kOnePasswordItemURLs];
}
[newObject setObject:uuid forType:QS1PasswordItemType];
[newObject setPrimaryType:QS1PasswordItemType];
[newObject setIcon:[QSResourceManager imageNamed:self.bundleID]];
[objects addObject:newObject];
}
return objects;
}
// Object Handler Methods
// An icon that is either already in memory or easy to load
- (void)setQuickIconForObject:(QSObject *)object{
if ([[object primaryType] isEqualToString:QS1PasswordItemType])
{
[object setIcon:[QSResourceManager imageNamed:self.bundleID]];
}
}
@end