1//
2// TOROnionAuth.m
3// Tor
4//
5// Created by Benjamin Erhart on 29.09.21.
6//
7
8#import "TOROnionAuth.h"
9
10@implementation TOROnionAuth
11
12- (instancetype)initWithPrivateDirUrl:(nullable NSURL *)privateUrl andPublicDirUrl:(nullable NSURL *)publicUrl
13{
14 if ((self = [super init]))
15 {
16 if (![publicUrl.lastPathComponent isEqualToString:@"authorized_clients"])
17 {
18 publicUrl = [publicUrl URLByAppendingPathComponent:@"authorized_clients" isDirectory:YES];
19 }
20
21 _privateUrl = privateUrl;
22 _publicUrl = publicUrl;
23 _keys = [NSMutableArray new];
24
25 NSMutableArray<NSURL *> *files = [NSMutableArray new];
26 NSError *error;
27
28 NSURL *privateUrl = _privateUrl;
29 if (privateUrl) {
30 NSArray<NSURL *> *privateFiles = [NSFileManager.defaultManager
31 contentsOfDirectoryAtURL:privateUrl
32 includingPropertiesForKeys:nil options:0
33 error:&error];
34
35 if (error)
36 {
37 NSLog(@"[%@] Error while reading keys: %@", NSStringFromClass(self.class), error.localizedDescription);
38 }
39 else {
40 if (privateFiles) [files addObjectsFromArray:privateFiles];
41 }
42 }
43
44 NSURL *publicUrl = _publicUrl;
45 if (publicUrl) {
46 NSArray<NSURL *> *publicFiles = [NSFileManager.defaultManager
47 contentsOfDirectoryAtURL:publicUrl
48 includingPropertiesForKeys:nil options:0
49 error:&error];
50
51 if (error)
52 {
53 NSLog(@"[%@] Error while reading keys: %@", NSStringFromClass(self.class), error.localizedDescription);
54 }
55 else {
56 if (publicFiles) [files addObjectsFromArray:publicFiles];
57 }
58 }
59
60 for (NSURL *file in files)
61 {
62 if ([TORAuthKey isAuthFile:file])
63 {
64 TORAuthKey *key = [[TORAuthKey alloc] initFromUrl:file];
65
66 if (key) [((NSMutableArray *)_keys) addObject:key];
67 }
68 }
69 }
70
71 return self;
72}
73
74- (instancetype)initWithPrivateDir:(NSString *)privatePath andPublicDir:(NSString *)publicPath
75{
76 return [self initWithPrivateDirUrl:[NSURL fileURLWithPath:privatePath] andPublicDirUrl:[NSURL fileURLWithPath:publicPath]];
77}
78
79
80// MARK: Public Methods
81
82- (BOOL)set:(TORAuthKey *)key
83{
84 NSURL *privateUrl = _privateUrl;
85 NSURL *publicUrl = _publicUrl;
86
87 if (key.isPrivate) {
88 if (privateUrl) {
89 [key setDirectory:privateUrl];
90 }
91 else if (publicUrl) {
92 [key setDirectory:publicUrl];
93 }
94 }
95 else {
96 if (publicUrl) {
97 [key setDirectory:publicUrl];
98 }
99 else if (privateUrl) {
100 [key setDirectory:privateUrl];
101 }
102 }
103
104 if ([key persist])
105 {
106 NSUInteger i = [_keys indexOfObject:key];
107
108 if (i == NSNotFound)
109 {
110 [((NSMutableArray *)_keys) addObject:key];
111 }
112 else {
113 ((NSMutableArray *)_keys)[i] = key;
114 }
115
116 return YES;
117 }
118
119 return NO;
120}
121
122- (BOOL)removeKeyAtIndex:(NSInteger)idx
123{
124 if (idx < 0 || (NSUInteger)idx >= _keys.count) return NO;
125
126 TORAuthKey *key = _keys[idx];
127
128 NSError *error;
129 [NSFileManager.defaultManager removeItemAtURL:key.file error:&error];
130
131 if (error)
132 {
133 NSLog(@"[%@] Error while removing key: %@", NSStringFromClass(self.class), error.localizedDescription);
134
135 return NO;
136 }
137
138 [((NSMutableArray *)_keys) removeObjectAtIndex:idx];
139
140 return YES;
141}
142
143
144@end