/
PutIOOAuthHelper.m
156 lines (124 loc) · 5.9 KB
/
PutIOOAuthHelper.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
//
// OAuthController.m
// Puttio
//
// Created by orta therox on 13/05/2012.
// Copyright (c) 2012 ortatherox.com. All rights reserved.
//
#import "PutIOOAuthHelper.h"
#import "APP_SECRET.h"
#import "AFNetworking.h"
#import "PutIONetworkConstants.h"
// https://put.io/v2/docs/#authentication
// The order of this is
// Login in via website in webkit
// Redirect to the OAuth dialog
// Make a request to the OAuth authenticate URL ( getAccessTokenFromOauthCode )
// Load Accounts page and parse out the tokens
// Then call delegate method.
@interface PutIOOAuthHelper (){
NSString *_username;
NSString *_password;
}
@end
@implementation PutIOOAuthHelper
@synthesize webView, delegate;
- (void)loginWithUsername:(NSString *)username andPassword:(NSString *)password {
webView.delegate = self;
[self loadRootPage];
_username = username;
_password = password;
}
- (void)getAccessTokenFromOauthCode:(NSString *)code {
// https://api.put.io/v2/oauth2/access_token?client_id=YOUR_CLIENT_ID&client_secret=YOUR_CLIENT_SECRET&grant_type=authorization_code&redirect_uri=YOUR_REGISTERED_REDIRECT_URI&code=CODE
//
NSString *address = [NSString stringWithFormat:PTFormatOauthTokenURL, @"10", APP_SECRET, @"authorization_code", PTCallbackOriginal, code];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:address]];
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
[self loadAccountSettingsPage];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:[JSON valueForKeyPath:@"access_token"] forKey:AppAuthTokenDefault];
[defaults synchronize];
[[NSNotificationCenter defaultCenter] postNotificationName:OAuthTokenWasSavedNotification object:nil userInfo:nil];
}failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
NSLog(@"error %@", error);
}];
[operation start];
}
- (void)loadRootPage {
NSURL * url = [NSURL URLWithString:PTLoginURL];
[webView loadRequest:[NSURLRequest requestWithURL:url]];
}
- (void)loadAuthPage {
NSString *address = [NSString stringWithFormat:PTFormatOauthLoginURL, AppOAuthID, AppOAuthCallback];
NSURL * url = [NSURL URLWithString:address];
[webView loadRequest:[NSURLRequest requestWithURL:url]];
}
- (void)loadAccountSettingsPage {
NSURL * url = [NSURL URLWithString:PTSettingsURL];
[webView loadRequest:[NSURLRequest requestWithURL:url]];
}
- (void)parseForV1Tokens {
NSString *apiKey = [webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByClassName('api-key')[0].getElementsByTagName('input')[0].value"];
NSString *apiSecret = [webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByClassName('api-key')[0].getElementsByTagName('input')[1].value"];
if (apiKey && apiSecret) {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:apiKey forKey:APIKeyDefault];
[defaults setObject:apiSecret forKey:APISecretDefault];
[defaults synchronize];
}else{
NSLog(@"HTML Syntax changed!");
}
[[NSNotificationCenter defaultCenter] postNotificationName:V1TokensWereSavedNotification object:nil userInfo:nil];
}
#pragma mark -
#pragma mark Webview delegate methods
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
// after you log in, it redrects to root, we actually want it
if ([[request.URL absoluteString] isEqualToString: PTRootURL]) {
[self loadAuthPage];
return NO;
}
return YES;
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
if (error.code == 101) {
NSString *code = [error userInfo][@"NSErrorFailingURLStringKey"];
NSArray *URLComponents = [code componentsSeparatedByString:@"%3D"];
if (URLComponents.count > 1 && [code hasPrefix: PTCallbackModified]) {
[self getAccessTokenFromOauthCode:URLComponents[1]];
}
}else{
if (error.code == 102 || error.code == -999) {
// no-op as the puttio:// url causes both errors 101/102
}else if (error.code == -1009) {
[self.delegate authHelperLoginFailedWithDesription:@"Your iPad is currently offline."];
}else {
// actually unexpected
NSString *error = [NSString stringWithFormat:@"WebView not acting as expected %@", error];
[self.delegate authHelperLoginFailedWithDesription:error];
}
}
}
- (void)webViewDidFinishLoad:(UIWebView *)aWebView {
NSString *address = aWebView.request.URL.absoluteString;
NSLog(@"addrsses %@", address);
if (![address hasPrefix:@"https://put.io/?err=1"]) {
if([aWebView.request.URL.absoluteString isEqualToString:PTSettingsURL]){
[self parseForV1Tokens];
[self.delegate authHelperDidLogin:self];
}
if([address isEqualToString:PTLoginURL]){
NSString *setUsername = [NSString stringWithFormat:@"document.getElementsByTagName('input')[0].value = '%@'", _username];
[webView stringByEvaluatingJavaScriptFromString:setUsername];
NSString *setPassword = [NSString stringWithFormat:@"document.getElementsByTagName('input')[1].value = '%@'", _password];
[webView stringByEvaluatingJavaScriptFromString:setPassword];
[webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('form')[0].submit()"];
}
} else {
NSLog(@"URH?");
NSLog(@"addrses %@", address);
[self.delegate authHelperLoginFailedWithDesription:@"Wrong Username / Password combo"];
}
}
@end