-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathAuthWebView.m
130 lines (115 loc) · 4.46 KB
/
AuthWebView.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
//
// AuthWebView.m
// Shukofukurou
//
// Created by 小鳥遊六花 on 4/24/18.
// Copyright © 2018 MAL Updater OS X Group. All rights reserved.
//
#import "AuthWebView.h"
#import "ClientConstants.h"
#import "PKCEGenerator.h"
#import "Utility.h"
@interface AuthWebView ()
@property (strong) WKWebView *webView;
@end
@implementation AuthWebView
- (void)loadView {
WKWebViewConfiguration *webConfiguration = [WKWebViewConfiguration new];
_webView = [[WKWebView alloc] initWithFrame:NSZeroRect configuration:webConfiguration];
_webView.UIDelegate = self;
_webView.navigationDelegate = self;
_webView.customUserAgent = @"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.1 Safari/605.1.15";
self.view = _webView;
}
- (void)viewDidLoad {
[super viewDidLoad];
[NSNotificationCenter.defaultCenter addObserver:self selector:@selector(oauthredirectreceived:) name:@"hachidori_auth" object:nil];
// Do view setup here.
[self loadAuthorization:_service];
}
- (void)dealloc {
[NSNotificationCenter.defaultCenter removeObserver:self];
}
- (void)reloadAuth {
[self loadAuthorization:_service];
}
- (void)oauthredirectreceived: (NSNotification *)notification {
NSString *redirectURL;
switch (_service) {
case 1:
redirectURL = @"hachidoriauth://anilistauth/?code=";
break;
case 2:
redirectURL = @"hachidoriauth://malauth/?code=";
break;
default:
break;
}
if ([(NSString *)notification.object containsString:redirectURL]) {
// Save Pin
[self resetWebView];
_completion([(NSString *)notification.object stringByReplacingOccurrencesOfString:redirectURL withString:@""]);
}
}
- (NSURL *)authURL {
NSString *authurl;
switch (_service) {
case 1:
authurl = [NSString stringWithFormat:@"https://anilist.co/api/v2/oauth/authorize?client_id=%@&response_type=code",kanilistclient];
break;
case 2:
_verifier = [PKCEGenerator generateCodeChallenge:[PKCEGenerator createVerifierString]];
authurl = [NSString stringWithFormat:@"https://myanimelist.net/v1/oauth2/authorize?response_type=code&client_id=%@&redirect_uri=%@&code_challenge=%@&code_challenge_method=plain", kmalclient, [Utility urlEncodeString:@"hachidoriauth://malauth/"], _verifier];
break;
default:
break;
}
return [NSURL URLWithString:authurl];
}
- (void)loadAuthorization:(int)nservice {
_service = nservice;
[_webView loadRequest:[NSURLRequest requestWithURL:[self authURL]]];
}
- (void)webView:(WKWebView *)webView
decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction
decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
NSString *redirectURL;
switch (_service) {
case 1:
redirectURL = @"hachidoriauth://anilistauth/?code=";
break;
case 2:
redirectURL = @"hachidoriauth://malauth/?code=";
break;
default:
break;
}
if ([navigationAction.request.URL.absoluteString containsString:redirectURL]) {
// Save Pin
decisionHandler(WKNavigationActionPolicyCancel);
[self resetWebView];
_completion([navigationAction.request.URL.absoluteString stringByReplacingOccurrencesOfString:redirectURL withString:@""]);
}
else if ([navigationAction.request.URL.absoluteString isEqualToString:@"https://myanimelist.net/"]) {
// Redirect to OAuth URL for MyAnimeList
decisionHandler(WKNavigationActionPolicyCancel);
[_webView loadRequest:[NSURLRequest requestWithURL:[self authURL]]];
}
else {
decisionHandler(WKNavigationActionPolicyAllow);
}
}
- (void)resetWebView {
// Clears WebView cookies and cache
NSSet *websiteDataTypes;
if (floor(NSAppKitVersionNumber) > NSAppKitVersionNumber10_12) {
websiteDataTypes = [NSSet setWithArray:@[WKWebsiteDataTypeDiskCache,WKWebsiteDataTypeOfflineWebApplicationCache,WKWebsiteDataTypeMemoryCache,WKWebsiteDataTypeLocalStorage,WKWebsiteDataTypeCookies,WKWebsiteDataTypeSessionStorage,WKWebsiteDataTypeIndexedDBDatabases, WKWebsiteDataTypeWebSQLDatabases]];
NSDate *dateFrom = [NSDate dateWithTimeIntervalSince1970:0];
[[WKWebsiteDataStore defaultDataStore] removeDataOfTypes:websiteDataTypes modifiedSince:dateFrom completionHandler:^{
}];
}
else {
return;
}
}
@end