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 pathSimpleAuthGoogleWebProvider.m
More file actions
200 lines (166 loc) · 9.21 KB
/
SimpleAuthGoogleWebProvider.m
File metadata and controls
200 lines (166 loc) · 9.21 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
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
//
// SimpleAuthGoogleWebProvider.m
// SimpleAuth
//
// Created by Ramon Vicente on 2/24/15.
// Copyright (c) 2015 UMOBI. All rights reserved.
//
#import "SimpleAuthGoogleWebProvider.h"
#import "SimpleAuthGoogleWebLoginViewController.h"
#import "UIViewController+SimpleAuthAdditions.h"
@implementation SimpleAuthGoogleWebProvider
#pragma mark - SimpleAuthProvider
+ (NSString *)type {
return @"google-web";
}
+ (NSDictionary *)defaultOptions {
// Default present block
SimpleAuthInterfaceHandler presentBlock = ^(UIViewController *controller) {
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:controller];
navigationController.modalPresentationStyle = UIModalPresentationFormSheet;
UIViewController *presentedViewController = [UIViewController SimpleAuth_presentedViewController];
[presentedViewController presentViewController:navigationController
animated:YES
completion:nil];
};
// Default dismiss block
SimpleAuthInterfaceHandler dismissBlock = ^(id viewController) {
[viewController dismissViewControllerAnimated:YES
completion:nil];
};
NSMutableDictionary *options = [NSMutableDictionary dictionaryWithDictionary:[super defaultOptions]];
options[SimpleAuthPresentInterfaceBlockKey] = presentBlock;
options[SimpleAuthDismissInterfaceBlockKey] = dismissBlock;
options[SimpleAuthRedirectURIKey] = @"http://localhost";
options[@"scope"] = @"email openid profile";
return options;
}
- (void)authorizeWithCompletion:(SimpleAuthRequestHandler)completion {
dispatch_async(dispatch_get_main_queue(), ^{
SimpleAuthGoogleWebLoginViewController *loginViewController = [[SimpleAuthGoogleWebLoginViewController alloc] initWithOptions:self.options];
loginViewController.completion = ^(UIViewController *viewController, NSURL *URL, NSError *error) {
SimpleAuthInterfaceHandler dismissBlock = self.options[SimpleAuthDismissInterfaceBlockKey];
dismissBlock(viewController);
NSString *query = [URL query];
NSDictionary *dictionary = [CMDQueryStringSerialization dictionaryWithQueryString:query];
NSString *code = dictionary[@"code"];
if ([code length] > 0) {
[self userWithCode:code
completion:completion];
} else {
completion(nil, error);
}
};
SimpleAuthInterfaceHandler block = self.options[SimpleAuthPresentInterfaceBlockKey];
block(loginViewController);
});
}
#pragma mark - Private
- (void)userWithCode:(NSString *)code completion:(SimpleAuthRequestHandler)completion
{
NSDictionary *parameters = @{ @"code" : code,
@"client_id" : self.options[@"client_id"],
@"client_secret" : self.options[@"client_secret"],
@"redirect_uri": self.options[@"redirect_uri"],
@"grant_type": @"authorization_code"};
NSString *data = [CMDQueryStringSerialization queryStringWithDictionary:parameters];
NSString *URLString = [NSString stringWithFormat:@"https://accounts.google.com/o/oauth2/token"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:URLString]];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:[data dataUsingEncoding:NSUTF8StringEncoding]];
[NSURLConnection sendAsynchronousRequest:request
queue:self.operationQueue
completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
NSIndexSet *indexSet = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(200, 99)];
NSInteger statusCode = [(NSHTTPURLResponse *)response statusCode];
if ([indexSet containsIndex:statusCode] && data) {
NSError *parseError;
NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data
options:kNilOptions
error:&parseError];
NSString *token = dictionary[@"access_token"];
if ([token length] > 0) {
NSDictionary *credentials = @{
@"access_token" : token,
@"expires" : [NSDate dateWithTimeIntervalSinceNow:[dictionary[@"expires_in"] doubleValue]],
@"token_type" : @"bearer",
@"id_token": dictionary[@"id_token"]
};
[self userWithCredentials:credentials
completion:completion];
} else {
completion(nil, parseError);
}
} else {
completion(nil, connectionError);
}
}];
}
- (void)userWithCredentials:(NSDictionary *)credentials completion:(SimpleAuthRequestHandler)completion {
NSString *URLString = [NSString stringWithFormat:@"https://www.googleapis.com/userinfo/v2/me"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:URLString]];
[request setValue:[NSString stringWithFormat:@"Bearer %@", credentials[@"access_token"]] forHTTPHeaderField:@"Authorization"];
[NSURLConnection sendAsynchronousRequest:request
queue:self.operationQueue
completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
NSIndexSet *indexSet = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(200, 99)];
NSInteger statusCode = [(NSHTTPURLResponse *)response statusCode];
if ([indexSet containsIndex:statusCode] && data) {
NSError *parseError;
NSDictionary *userInfo = [NSJSONSerialization JSONObjectWithData:data
options:kNilOptions
error:&parseError];
if (userInfo) {
completion ([self dictionaryWithAccount:userInfo credentials:credentials], nil);
} else {
completion(nil, parseError);
}
} else {
completion(nil, connectionError);
}
}];
}
- (NSDictionary *)dictionaryWithAccount:(NSDictionary *)account
credentials:(NSDictionary *)credentials
{
NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
// Provider
dictionary[@"provider"] = [[self class] type];
// Credentials
dictionary[@"credentials"] = @{
@"token" : credentials[@"access_token"],
@"expires_at" : credentials[@"expires"]
};
// User ID
dictionary[@"uid"] = account[@"id"];
// Raw response
dictionary[@"extra"] = @{
@"raw_info" : account
};
// Location
NSString *location = account[@"location"][@"name"];
// User info
NSMutableDictionary *user = [NSMutableDictionary new];
if (account[@"email"]) {
user[@"email"] = account[@"email"];
}
user[@"name"] = account[@"name"];
user[@"first_name"] = account[@"given_name"];
user[@"last_name"] = account[@"family_name"];
if (account[@"gender"]) {
user[@"gender"] = account[@"gender"];
}
user[@"image"] = account[@"picture"];
if (location) {
user[@"location"] = location;
}
user[@"verified"] = account[@"verified_email"] ? @YES : @NO;
if (account[@"link"]) {
user[@"urls"] = @{
@"Google +" : account[@"link"],
};
}
dictionary[@"info"] = user;
return dictionary;
}
@end