This repository was archived by the owner on Aug 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 128
Expand file tree
/
Copy pathSimpleAuthFacebookProvider.m
More file actions
146 lines (118 loc) · 4.4 KB
/
SimpleAuthFacebookProvider.m
File metadata and controls
146 lines (118 loc) · 4.4 KB
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
//
// SimpleAuthFacebookProvider.m
// SimpleAuth
//
// Created by Caleb Davenport on 11/6/13.
// Copyright (c) 2013-2014 Byliner, Inc. All rights reserved.
//
#import "SimpleAuthFacebookProvider.h"
#import "ACAccountStore+SimpleAuth.h"
#import <ReactiveCocoa/ReactiveCocoa.h>
@import Social;
@implementation SimpleAuthFacebookProvider
#pragma mark - SimpleAuthProvider
+ (NSString *)type {
return @"facebook";
}
+ (NSDictionary *)defaultOptions {
return @{
@"permissions" : @[ @"email" ],
@"audience" : @[ ACFacebookAudienceOnlyMe ]
};
}
- (void)authorizeWithCompletion:(SimpleAuthRequestHandler)completion {
[[[self systemAccount]
flattenMap:^RACStream *(ACAccount *account) {
NSArray *signals = @[
[self remoteAccountWithSystemAccount:account],
[RACSignal return:account]
];
return [self rac_liftSelector:@selector(dictionaryWithRemoteAccount:systemAccount:) withSignalsFromArray:signals];
}]
subscribeNext:^(NSDictionary *response) {
completion(response, nil);
}
error:^(NSError *error) {
completion(nil, error);
}];
}
#pragma mark - Private
- (RACSignal *)allSystemAccounts {
NSDictionary *options = @{
ACFacebookAppIdKey : self.options[@"app_id"],
ACFacebookPermissionsKey : self.options[@"permissions"],
ACFacebookAudienceKey: self.options[@"audience"]
};
return [ACAccountStore SimpleAuth_accountsWithTypeIdentifier:ACAccountTypeIdentifierFacebook options:options];
}
- (RACSignal *)systemAccount {
return [[self allSystemAccounts] map:^(NSArray *accounts) {
return [accounts lastObject];
}];
}
- (RACSignal *)remoteAccountWithSystemAccount:(ACAccount *)account {
return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
NSURL *URL = [NSURL URLWithString:@"https://graph.facebook.com/me"];
SLRequest *request = [SLRequest requestForServiceType:SLServiceTypeFacebook requestMethod:SLRequestMethodGET URL:URL parameters:nil];
request.account = account;
[request performRequestWithHandler:^(NSData *data, NSHTTPURLResponse *response, NSError *connectionError) {
NSIndexSet *indexSet = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(200, 99)];
NSInteger statusCode = [response statusCode];
if ([indexSet containsIndex:statusCode] && data) {
NSError *parseError = nil;
NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&parseError];
if (dictionary) {
[subscriber sendNext:dictionary];
[subscriber sendCompleted];
}
else {
[subscriber sendError:parseError];
}
}
else {
[subscriber sendError:connectionError];
}
}];
return nil;
}];
}
- (NSDictionary *)dictionaryWithRemoteAccount:(NSDictionary *)remoteAccount systemAccount:(ACAccount *)systemAccount {
NSMutableDictionary *dictionary = [NSMutableDictionary new];
// Provider
dictionary[@"provider"] = [[self class] type];
// Credentials
dictionary[@"credentials"] = @{
@"token" : systemAccount.credential.oauthToken
};
// User ID
dictionary[@"uid"] = remoteAccount[@"id"];
// Raw response
dictionary[@"extra"] = @{
@"raw_info" : remoteAccount,
@"account" : systemAccount
};
// Profile image
NSString *avatar = [NSString stringWithFormat:@"https://graph.facebook.com/%@/picture?type=large", remoteAccount[@"id"]];
// Location
NSString *location = remoteAccount[@"location"][@"name"];
// User info
NSMutableDictionary *user = [NSMutableDictionary new];
user[@"nickname"] = remoteAccount[@"username"];
if (remoteAccount[@"email"]) {
user[@"email"] = remoteAccount[@"email"];
}
user[@"name"] = remoteAccount[@"name"];
user[@"first_name"] = remoteAccount[@"first_name"];
user[@"last_name"] = remoteAccount[@"last_name"];
user[@"image"] = avatar;
if (location) {
user[@"location"] = location;
}
user[@"verified"] = remoteAccount[@"verified"] ?: @NO;
user[@"urls"] = @{
@"Facebook" : remoteAccount[@"link"],
};
dictionary[@"info"] = user;
return dictionary;
}
@end