Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Support WeChat iOS native third-party login. #27

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,12 @@ Redirection URL passed to the authentication service must be the same as the URL

For the Sign in with Apple in web_message response mode, postMessage from https://appleid.apple.com is also captured, and the authorization object is returned as a URL fragment encoded as a query string (for compatibility with other providers).

## Support WeChat third-party login on iOS.
The current SDK will display the scan code login after clicking the WeChat third-party login on the web page. It is obviously unreasonable to scan the code on the mobile end.

If you want to click the WeChat login button on the webpage on the iOS mobile terminal to invoke the WeChat native jump login, you need to refer to the [WeChat iOS access guide](https://developers.weixin.qq.com/doc/oplatform/Mobile_App/Access_Guide/iOS.html) to access the WechatOpenSDK-XCFramework of the WeChat SDK, and then untie the comments about the WeChat part in OcCasdoorFlutterSdkPlugin.m, in Fill in app_id and universal_link in the corresponding position.


# API reference interface

#### Get sign up url
Expand Down
29 changes: 0 additions & 29 deletions ios/Classes/CasdoorFlutterSdkPlugin.m

This file was deleted.

18 changes: 18 additions & 0 deletions ios/Classes/OcCasdoorFlutterSdkPlugin.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright 2022 The casbin Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#import <Flutter/Flutter.h>

@interface CasdoorFlutterSdkPlugin : NSObject<FlutterPlugin>
@end
202 changes: 202 additions & 0 deletions ios/Classes/OcCasdoorFlutterSdkPlugin.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@
// Copyright 2022 The casbin Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#import <Foundation/Foundation.h>
#import "CasdoorFlutterSdkPlugin.h"
#import <UIKit/UIKit.h>
// #import <WXApiObject.h>
// #import <WXApi.h>
// #import <WechatAuthSDK.h>
@import WebKit;

// #define APP_ID @"wx049c70e6c2027b0b"
// #define UNIVERSAL_LINK @"https://testdomain.com"


typedef void (^AuthResultCallback)(NSString *);
@interface CasdoorFlutterSdkPlugin () <FlutterPlugin, WKNavigationDelegate, WXApiDelegate>
@property (nonatomic, strong) WKWebView *webView;
@property (nonatomic, copy) NSString *callbackURLScheme;
@property (nonatomic, copy) AuthResultCallback authResultCallback;
@end

@implementation CasdoorFlutterSdkPlugin




+ (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar {
FlutterMethodChannel* channel = [FlutterMethodChannel
methodChannelWithName:@"casdoor_flutter_sdk"
binaryMessenger:[registrar messenger]];
CasdoorFlutterSdkPlugin* instance = [[CasdoorFlutterSdkPlugin alloc] init];
[registrar addMethodCallDelegate:instance channel:channel];
}

- (instancetype)init {
self = [super init];
if (self) {
_webView = [[WKWebView alloc] init];
_webView.navigationDelegate = self;
}
return self;
}

- (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
if ([call.method isEqualToString:@"authenticate"]) {
NSDictionary *arguments = call.arguments;
NSString *urlString = arguments[@"url"];
NSURL *url = [NSURL URLWithString:urlString];
NSString *callbackURLScheme = arguments[@"callbackUrlScheme"];

self.callbackURLScheme = callbackURLScheme;

__weak typeof(self) weakSelf = self;
self.authResultCallback = ^(NSString *authResult) {
result(authResult);
};

UIViewController *viewController = [[[UIApplication sharedApplication] delegate] window].rootViewController;
self.webView.frame = viewController.view.bounds;
self.webView.translatesAutoresizingMaskIntoConstraints = NO;
[viewController.view addSubview:self.webView];
[viewController.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|-0-[view]-0-|" options:0 metrics:nil views:@{@"view": self.webView}]];
[viewController.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[view]-0-|" options:0 metrics:nil views:@{@"view": self.webView}]];

NSURLRequest *req = [NSURLRequest requestWithURL:url];
dispatch_async(dispatch_get_main_queue(), ^{
[weakSelf.webView loadRequest:req];
});
} else if ([call.method isEqualToString:@"cleanUpDanglingCalls"]) {
result(nil);
} else if ([call.method isEqualToString:@"getPlatformVersion"]) {
NSString *version = [NSString stringWithFormat:@"iOS %@", [[NSProcessInfo processInfo] operatingSystemVersionString]];
result(version);
} else if ([call.method isEqualToString:@"registerWXApi"]) {
NSDictionary *arguments = call.arguments;
NSString *app_id = arguments[@"app_id"];
NSString *universal_link = arguments[@"universal_link"];
// register WeChat
// BOOL isWeChatRegistered = [WXApi registerApp:app_id universalLink:universal_link];
// NSLog(@"Is WeChat Registered: %@", isWeChatRegistered ? @"YES" : @"NO");
}else {
result(FlutterMethodNotImplemented);
}
}

// - (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
// return [WXApi handleOpenURL:url delegate:self];
// }

// - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
// return [WXApi handleOpenURL:url delegate:self];
// }

// - (BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity restorationHandler:(void(^)(NSArray<id<UIUserActivityRestoring>> * __nullable restorableObjects))restorationHandler {
// return [WXApi handleOpenUniversalLink:userActivity delegate:self];
// }

// -(void) onReq:(BaseReq*)reqonReq {
//
// }

// -(void) onResp:(BaseResp*)resp {
// /*
// enum WXErrCode {
// WXSuccess = 0, 成功
// WXErrCodeCommon = -1, 普通错误类型
// WXErrCodeUserCancel = -2, 用户点击取消并返回
// WXErrCodeSentFail = -3, 发送失败
// WXErrCodeAuthDeny = -4, 授权失败
// WXErrCodeUnsupport = -5, 微信不支持
// };
// */
// if (resp.errCode == 0) { //Success
// NSLog(@"Login Success.");
// SendAuthResp *resp2 = (SendAuthResp *)resp;
// NSLog(@"code: %@", resp2.code);
//
// }else{ //Failed
// NSLog(@"error %@", resp.errStr);
// UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Login failed." message:[NSString stringWithFormat:@"reason: %@", resp.errStr] preferredStyle:UIAlertControllerStyleAlert];
//
// UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancle" style:UIAlertActionStyleCancel handler:nil];
// UIAlertAction *confirmAction = [UIAlertAction actionWithTitle:@"Confirm" style:UIAlertActionStyleDefault handler:nil];
//
// [alertController addAction:cancelAction];
// [alertController addAction:confirmAction];
//
// UIViewController *rootViewController = UIApplication.sharedApplication.keyWindow.rootViewController;
// [rootViewController presentViewController:alertController animated:YES completion:nil];
// }
// }

#pragma mark - WKNavigationDelegate

- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {

NSURL *callbackUrl = navigationAction.request.URL;
if (callbackUrl.scheme && [callbackUrl.scheme isEqualToString:self.callbackURLScheme]) {

NSLog(@"callbackUrl: %@", callbackUrl);

self.authResultCallback(callbackUrl.absoluteString);

decisionHandler(WKNavigationActionPolicyCancel);
[webView removeFromSuperview];
return;
} else if ([navigationAction.request.URL.absoluteString containsString:@"https://open.weixin.qq.com"]) {

BOOL isInstalled = [WXApi isWXAppInstalled];
NSLog(@"Is WeChat Installed: %@", isInstalled ? @"YES" : @"NO");
if ([WXApi isWXAppInstalled]) {

// Open the log before register, and you can troubleshoot problems based on the log later
// [WXApi startLogByLevel:WXLogLevelDetail logBlock:^(NSString *log) {
// NSLog(@"WeChatSDK: %@", log);
// }];
//
// // Call the self-test function
// [WXApi checkUniversalLinkReady:^(WXULCheckStep step, WXCheckULStepResult* result) {
// NSLog(@"%@, %u, %@, %@", @(step), result.success, result.errorInfo, result.suggestion);
// }];

// SendAuthReq *req = [[SendAuthReq alloc]init];
// req.scope = @"snsapi_userinfo";
// req.state = @"wx_oauth_authorization_state";
// [WXApi sendReq:req completion:nil];


decisionHandler(WKNavigationActionPolicyCancel);
[webView removeFromSuperview];
}else{
// If WeChat is not installed, do nothing, use the scan code of the website to log in.
}

NSLog(@"Clicked on a link containing 'https://open.weixin.qq.com'");
return;
} else if ([navigationAction.request.URL.absoluteString containsString:@"https://openauth.alipay.com"]) {
// If we click on Alipay as a third-party login, write the implementation part here.
// decisionHandler(WKNavigationActionPolicyCancel);
// [webView removeFromSuperview];
NSLog(@"Clicked on a link containing 'https://openauth.alipay.com'");
return;
}

decisionHandler(WKNavigationActionPolicyAllow);
}

@end


121 changes: 0 additions & 121 deletions ios/Classes/SwiftCasdoorFlutterSdkPlugin.swift

This file was deleted.

Loading