-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVkontakteAuthProvider.m
406 lines (331 loc) · 16.9 KB
/
VkontakteAuthProvider.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
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
#import <CocoaLumberjack.h>
#import <VKSdk.h>
static const DDLogLevel ddLogLevel = DDLogLevelDebug;
NSString* const kVkontakteAuthProviderName = @"vkontakte";
typedef void(^CallbackLogin)(VKAccessToken *token, NSError* error);
@interface VkontakteAuthProvider() <VKSdkDelegate, VKSdkUIDelegate>
@property (strong ) CallbackLogin callbackLogin;
@property (nonatomic, strong) UIWindow *overWindow;
@property (nonatomic, assign) BOOL inApp;
@end
@implementation VkontakteAuthProvider
@synthesize applicationId = _applicationId;
@synthesize secret = _secret;
- (instancetype)init {
NSString *vkAppId = [[SocialConfiguration defaultConfiguration] objectForKey:@"VkontakteAppID"];
NSString *vkSecret = [[SocialConfiguration defaultConfiguration] objectForKey:@"VkontakteSecret"];
return [self initWithId:vkAppId secret:vkSecret];
}
- (instancetype)initWithId:(NSString *)applicationId secret:(NSString *)secret {
NSParameterAssert(applicationId);
NSParameterAssert(secret);
self = [super init];
if (self) {
self->_applicationId = applicationId;
self->_secret = secret;
DDLogVerbose(@"Initialize vkontake SDK");
[[VKSdk initializeWithAppId:applicationId] registerDelegate:self];
[[VKSdk instance] setUiDelegate:self];
[VKSdk wakeUpSession:@[@"wall", @"photos"] completeBlock:^(VKAuthorizationState state, NSError *error) {
NSLog(@"%lu", (unsigned long)state);
}];
}
return self;
}
#pragma mark - ProviderProtocol
- (NSString *)name {
return kVkontakteAuthProviderName;
}
- (NSString *)token {
return [VKSdk accessToken].accessToken;
}
- (RACSignal *)logIn {
@weakify(self);
return [[RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
RACDisposable *errorDisposable = [[RACSignal merge:@[[[self rac_signalForSelector:@selector(vkSdkUserAuthorizationFailed) fromProtocol:@protocol(VKSdkDelegate)] mapReplace:[NSError errorWithDomain:kAGAuthErrorDomain code:AGAuthErrorCodesOperationCanceled userInfo:@{NSLocalizedDescriptionKey: @"Operation was canceled"}]],
]] subscribeNext:^(id x) {
[subscriber sendError:x];
}];
RACDisposable *updateTokeDisposable = [[self rac_signalForSelector:@selector(vkSdkAccessTokenUpdated:oldToken:) fromProtocol:@protocol(VKSdkDelegate)] subscribeNext:^(RACTuple *tuple) {
VKAccessToken *token = tuple.first;
AuthorizationCredentials *credentials = [AuthorizationCredentials externalCredentialsForProviderName:self.name];
credentials.userId = token.userId;
credentials.token = token.accessToken;
[subscriber sendNext:credentials];
[subscriber sendCompleted];
}];
RACDisposable *loginDisposable = [[self rac_signalForSelector:@selector(vkSdkAccessAuthorizationFinishedWithResult:) fromProtocol:@protocol(VKSdkDelegate)] subscribeNext:^(id result) {
if ([result isKindOfClass:[RACTuple class]]) {
RACTuple *tuple = result;
VKAuthorizationResult *authResult = tuple.first;
if (authResult.error) {
[subscriber sendError:authResult.error];
} else {
[subscriber sendError:nil];
}
} else {
VKAuthorizationResult *authResult = result;
if (!authResult.error) {
AuthorizationCredentials *credentials = [AuthorizationCredentials externalCredentialsForProviderName:self.name];
credentials.userId = authResult.token.userId;
credentials.token = authResult.token.accessToken;
[subscriber sendNext:credentials];
[subscriber sendCompleted];
} else {
[subscriber sendError:authResult.error];
}
}
}];
@strongify(self);
[self willChangeValueForKey:@keypath(self, isLoggedIn)];
NSArray *permissions = @[@"wall", @"photos"];
[VKSdk authorize:permissions withOptions:VKAuthorizationOptionsUnlimitedToken];
return [RACDisposable disposableWithBlock:^{
[errorDisposable dispose];
[updateTokeDisposable dispose];
[loginDisposable dispose];
}];
}] finally:^{
@strongify(self);
[self didChangeValueForKey:@keypath(self, isLoggedIn)];
}];
}
- (RACSignal *)logOut {
DDLogVerbose(@"Logging out from vkontake");
[self willChangeValueForKey:@keypath(self, isLoggedIn)];
[VKSdk forceLogout];
[self didChangeValueForKey:@keypath(self, isLoggedIn)];
return [RACSignal return:nil];
}
- (RACSignal *)getUserInfo {
return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
VKApiUsers *apiUsers = [VKApi users];
VKRequest *request = [apiUsers get:@{@"fields": @"sex, bdate, city, country, photo_max_orig, about, personal"}];
[request executeWithResultBlock:^(VKResponse *response) {
NSDictionary* json = response.json[0];
if (![json isKindOfClass:[NSDictionary class]]) {
NSError* error = [NSError errorWithDomain:kAGAuthErrorDomain
code:AGAuthErrorCodesMalformedResponse
userInfo:@{NSLocalizedDescriptionKey: [NSString stringWithFormat:@"%@ arrived when %@ expected", json.class, [NSDictionary class]]}];
DDLogError(@"%@", error);
[subscriber sendError: error];
return;
}
SocialProfile* profile = [SocialProfile new];
profile.provider = self.name;
NSDictionary *converter = @{@keypath(profile, identifier):@"id",
@keypath(profile, firstName) :@"first_name",
@keypath(profile, lastName) :@"last_name",
@keypath(profile, name) :@"name",
@keypath(profile, avatarUrl) :@"photo_max_orig",
@keypath(profile, bio) :@"about"};
[profile loadObject:json withConvertionTable:converter];
NSString* birthdayString = json[@"bdate"];
if (birthdayString) {
static NSDateFormatter* dateFormatter;
if (!dateFormatter) {
dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = @"dd.MM.yyyy";
}
profile.birthdayDate =[dateFormatter dateFromString:birthdayString];
}
if (!profile.name) {
NSMutableArray* parts = [NSMutableArray array];
if (profile.firstName) [parts addObject:profile.firstName];
if (profile.lastName) [parts addObject:profile.lastName];
if (parts.count > 0) {
profile.name = [parts componentsJoinedByString:@" "];
}
}
NSNumber* sex = [json valueForKeyPath:@"sex"];
if (sex) {
if (sex.integerValue == 1) {
profile.gender = [Gender women];
} else if (sex.integerValue == 2) {
profile.gender = [Gender men];
} else {
profile.gender = [Gender unknown];
}
}
[subscriber sendNext:profile];
[subscriber sendCompleted];
} errorBlock:^(NSError *error) {
[subscriber sendError:error];
}];
return nil;
}];
}
- (BOOL)isLoggedIn {
return [VKSdk accessToken].accessToken != nil;
}
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
if (![url.scheme hasPrefix:@"vk"]) {
return NO;
}
DDLogDebug(@"Process url with vkontakte handler");
return [VKSdk processOpenURL: url fromApplication: sourceApplication];
}
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString *,id> *)options {
if (![url.scheme hasPrefix:@"vk"]) {
return NO;
}
[VKSdk processOpenURL:url fromApplication:options[UIApplicationOpenURLOptionsSourceApplicationKey]];
return YES;
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
if (self.inApp) {
return;
}
if (self.loginSubject) {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
NSError* error = [NSError errorWithDomain:kAGAuthErrorDomain
code:AGAuthErrorCodesOperationCanceled
userInfo:@{NSLocalizedDescriptionKey: @"Operation was canceled"}];
[self.loginSubject sendError:error];
self.loginSubject = nil;
});
}
}
#pragma mark - VKSdkDelegate
- (void)vkSdkNeedCaptchaEnter:(VKError*) captchaError {
VKCaptchaViewController * vc = [VKCaptchaViewController captchaControllerWithError:captchaError];
UIViewController *topVC = [UIApplication sharedApplication].keyWindow.rootViewController;
[vc presentIn:topVC];
}
- (void)vkSdkTokenHasExpired:(VKAccessToken *)expiredToken {
[VKSdk authorize:@[] withOptions:0];
}
- (void)vkSdkShouldPresentViewController:(UIViewController *)controller {
[[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:controller animated:YES completion:nil];
}
#pragma mark - Share
-(RACSignal*)sharePhoto:(UIImage*)photo withTitle:(NSString*)title fromViewController:(UIViewController*)controller {
@weakify(self);
if ([self isLoggedIn]) {
@strongify(self);
AuthorizationCredentials *credentials = [AuthorizationCredentials externalCredentialsForProviderName:self.name];
credentials.userId = [[VKSdk accessToken] userId];
return [[[self uploadPhotoToWall:photo withCredentials:credentials] flattenMap:^RACStream *(VKResponse *response) {
return [self postPhotoToWallFromResponse:response withCredentials:credentials withPhotoTitle:title];
}] catch:^RACSignal *(NSError *error) {
return [[self logIn] flattenMap:^RACStream *(AuthorizationCredentials *credentials) {
@strongify(self);
return [[self uploadPhotoToWall:photo withCredentials:credentials] flattenMap:^RACStream *(VKResponse *response) {
return [self postPhotoToWallFromResponse:response withCredentials:credentials withPhotoTitle:title];
}];
}];
}];
} else {
return [[self logIn] flattenMap:^RACStream *(AuthorizationCredentials *credentials) {
@strongify(self);
return [[self uploadPhotoToWall:photo withCredentials:credentials] flattenMap:^RACStream *(VKResponse *response) {
return [self postPhotoToWallFromResponse:response withCredentials:credentials withPhotoTitle:title];
}];
}];
}
}
- (RACSignal *)uploadPhotoToWall:(UIImage*)photo withCredentials:(AuthorizationCredentials*)credentials{
@weakify(self);
RACSignal *postToWallSignal = [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
@strongify(self);
VKRequest *request = [VKApi uploadWallPhotoRequest:photo parameters:[VKImageParameters jpegImageWithQuality:1.f] userId:credentials.userId.integerValue groupId:nil ];
[request executeWithResultBlock:^(VKResponse * response) {
[subscriber sendNext:response];
[subscriber sendCompleted];
NSLog(@"Json result: %@", response.json);
} errorBlock:^(NSError * error) {
if (error.code != VK_API_ERROR) {
[error.vkError.request repeat];
} else {
[subscriber sendError:error];
}
}];
return [RACDisposable disposableWithBlock:^{
[request cancel];
}];
}];
return [postToWallSignal deliverOnMainThread];
}
- (RACSignal *)postPhotoToWallFromResponse:(VKResponse*)response withCredentials:(AuthorizationCredentials*)credentials withPhotoTitle:(NSString*)title{
@weakify(self);
RACSignal *savePhotoToWallSignal = [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
@strongify(self);
VKPhoto *photoInfo = [(VKPhotoArray*)response.parsedModel objectAtIndex:0];
NSString *photoAttachment = [NSString stringWithFormat:@"photo%@_%@", photoInfo.owner_id, photoInfo.id];
NSString *message = (title.length>0) ?: title;
VKRequest *postReq = [[VKApi wall] post:@{VK_API_ATTACHMENTS : photoAttachment, VK_API_OWNER_ID : credentials.userId, VK_API_MESSAGE : message}];
[postReq executeWithResultBlock:^(VKResponse * response) {
[subscriber sendNext:response];
[subscriber sendCompleted];
NSLog(@"Json result: %@", response.json);
} errorBlock:^(NSError * error) {
if (error.code != VK_API_ERROR) {
[error.vkError.request repeat];
} else {
[subscriber sendError:error];
}
}];
return [RACDisposable disposableWithBlock:^{
[postReq cancel];
}];
}];
return [savePhotoToWallSignal deliverOnMainThread];
}
- (RACSignal *)getPhotoUploadUrlWithCredentials:(AuthorizationCredentials*)credentials {
NSString *getWallUploadServer = [NSString stringWithFormat:@"https://api.vk.com/method/photos.getWallUploadServer?owner_id=%@&access_token=%@", credentials.userId, credentials.token];
return [[self sendRequestWith:getWallUploadServer] flattenMap:^RACStream *(NSDictionary *responseDict) {
NSString *upload_url = [[responseDict objectForKey:@"response"] objectForKey:@"upload_url"];
if (!upload_url) {
return [RACSignal error:nil];
}
return [RACSignal return:upload_url];
}];
}
- (RACSignal *)sendRequestWith:(NSString*)requestString {
RACSignal *requestSignal = [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:requestString]];
urlRequest.HTTPMethod = @"GET";
AFHTTPRequestOperation *requestOperation = [[AFHTTPRequestOperation alloc] initWithRequest:urlRequest];
requestOperation.responseSerializer = [AFJSONResponseSerializer new];
[requestOperation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
[subscriber sendNext:responseObject];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
if (operation.isCancelled) {
[subscriber sendCompleted];
}
else {
[subscriber sendError:error];
}
}];
[requestOperation start];
return [RACDisposable disposableWithBlock:^{
[requestOperation cancel];
}];
}];
return [requestSignal deliverOnMainThread];
}
- (RACSignal *)sendPostRequest:(NSString*)url withImageData:(NSData*)data {
@weakify(self);
RACSignal *requestSignal = [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
@strongify(self);
AFHTTPRequestOperationManager* manager = [AFHTTPRequestOperationManager new];
manager.responseSerializer = [AFJSONResponseSerializer serializer];
AFHTTPRequestOperation *operation = [manager POST:url parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithFileData:data name:@"photo" fileName:@"photo.jpg" mimeType:@"image/jpeg"];
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSInteger httpStatus = operation.response.statusCode;
NSString *test = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
DDLogVerbose(@"HTTP[%p] Response: %ld: %@", url, (long)httpStatus, responseObject);
[subscriber sendNext:responseObject];
[subscriber sendCompleted];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
DDLogError(@"HTTP[%p] Error: %@", url, error);
[subscriber sendError:error];
}];
return [RACDisposable disposableWithBlock:^{
[operation cancel];
}];
}];
return [requestSignal deliverOnMainThread];
}
@end