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

RCF-78 - Logout with pigeon #138

Open
wants to merge 3 commits into
base: develop
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
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,6 @@ UserDetailsApi getLoginActivityApi(LoginService loginService) {
MachineDetailsApi getMachineDetailsApi(ClientCryptoManagerService clientCryptoManagerService) {
return new MachineDetailsApi(clientCryptoManagerService);
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ public UserDetailsApi(LoginService loginService) {
this.loginService = loginService;
}

public void logout(){
loginService.clearAuthToken();
}
public void usernameValidation(String username) {
if(username == null || username.trim().length() == 0){
user = new UserPigeon.User.Builder()
Expand Down Expand Up @@ -66,4 +69,10 @@ public UserPigeon.User validateUser(@NonNull String username) {
usernameValidation(username);
return user;
}
@NonNull
@Override
public UserPigeon.User logoutUser() {
logout();
return user;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,15 @@ public List<String> saveAuthToken(@NonNull String token) throws Exception {
return roles;
}

public String clearAuthToken(){
SharedPreferences.Editor editor = this.context.getSharedPreferences(this.context.getString(R.string.app_name),
Context.MODE_PRIVATE).edit();
editor.clear();
editor.apply();
return this.context.getSharedPreferences(this.context.getString(R.string.app_name),
Context.MODE_PRIVATE).getString(USER_TOKEN, null);
}

public String fetchAuthToken() {
return this.context.getSharedPreferences(this.context.getString(R.string.app_name),
Context.MODE_PRIVATE).getString(USER_TOKEN, null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@

import javax.inject.Inject;
import javax.inject.Singleton;
import java.util.logging.Logger;

@Singleton
public class LoginService {

private static final Logger LOGGER = Logger.getLogger(LoginService.class.getName());
private static final String TAG = LoginService.class.getSimpleName();

private SessionManager sessionManager;
Expand Down Expand Up @@ -81,4 +82,16 @@ public List<String> saveAuthToken(String authResponse) throws Exception {
throw ex;
}
}
public String clearAuthToken(){
try{
String AuthTokenReturned=this.sessionManager.clearAuthToken();
LOGGER.info("Auth Token is cleared and its value is "+AuthTokenReturned);
return AuthTokenReturned;
}catch (Exception ex) {
Log.e(TAG, ex.getMessage(), ex);
throw ex;
}


}
}
1 change: 1 addition & 0 deletions ios/Flutter/Debug.xcconfig
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
#include? "Pods/Target Support Files/Pods-Runner/Pods-Runner.debug.xcconfig"
#include "Generated.xcconfig"
1 change: 1 addition & 0 deletions ios/Flutter/Release.xcconfig
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
#include? "Pods/Target Support Files/Pods-Runner/Pods-Runner.release.xcconfig"
#include "Generated.xcconfig"
41 changes: 41 additions & 0 deletions ios/Podfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Uncomment this line to define a global platform for your project
# platform :ios, '11.0'

# CocoaPods analytics sends network stats synchronously affecting flutter build latency.
ENV['COCOAPODS_DISABLE_STATS'] = 'true'

project 'Runner', {
'Debug' => :debug,
'Profile' => :release,
'Release' => :release,
}

def flutter_root
generated_xcode_build_settings_path = File.expand_path(File.join('..', 'Flutter', 'Generated.xcconfig'), __FILE__)
unless File.exist?(generated_xcode_build_settings_path)
raise "#{generated_xcode_build_settings_path} must exist. If you're running pod install manually, make sure flutter pub get is executed first"
end

File.foreach(generated_xcode_build_settings_path) do |line|
matches = line.match(/FLUTTER_ROOT\=(.*)/)
return matches[1].strip if matches
end
raise "FLUTTER_ROOT not found in #{generated_xcode_build_settings_path}. Try deleting Generated.xcconfig, then run flutter pub get"
end

require File.expand_path(File.join('packages', 'flutter_tools', 'bin', 'podhelper'), flutter_root)

flutter_ios_podfile_setup

target 'Runner' do
use_frameworks!
use_modular_headers!

flutter_install_all_ios_pods File.dirname(File.realpath(__FILE__))
end

post_install do |installer|
installer.pods_project.targets.each do |target|
flutter_additional_ios_build_settings(target)
end
end
26 changes: 17 additions & 9 deletions ios/Runner/pigeon.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,33 @@

NS_ASSUME_NONNULL_BEGIN

@class Machine;
@class AuthResponse;

@interface Machine : NSObject
@interface AuthResponse : NSObject
/// `init` unavailable to enforce nonnull fields, see the `make` class method.
- (instancetype)init NS_UNAVAILABLE;
+ (instancetype)makeWithMap:(NSDictionary<NSString *, NSString *> *)map
+ (instancetype)makeWithResponse:(NSString *)response
username:(NSString *)username
isOfficer:(NSNumber *)isOfficer
isDefault:(NSNumber *)isDefault
isSupervisor:(NSNumber *)isSupervisor
errorCode:(nullable NSString *)errorCode;
@property(nonatomic, strong) NSDictionary<NSString *, NSString *> * map;
@property(nonatomic, copy) NSString * response;
@property(nonatomic, copy) NSString * username;
@property(nonatomic, strong) NSNumber * isOfficer;
@property(nonatomic, strong) NSNumber * isDefault;
@property(nonatomic, strong) NSNumber * isSupervisor;
@property(nonatomic, copy, nullable) NSString * errorCode;
@end

/// The codec used by MachineApi.
NSObject<FlutterMessageCodec> *MachineApiGetCodec(void);
/// The codec used by AuthResponseApi.
NSObject<FlutterMessageCodec> *AuthResponseApiGetCodec(void);

@protocol MachineApi
@protocol AuthResponseApi
/// @return `nil` only when `error != nil`.
- (nullable Machine *)getMachineDetailsWithError:(FlutterError *_Nullable *_Nonnull)error;
- (nullable AuthResponse *)loginUsername:(NSString *)username password:(NSString *)password isConnected:(NSNumber *)isConnected error:(FlutterError *_Nullable *_Nonnull)error;
@end

extern void MachineApiSetup(id<FlutterBinaryMessenger> binaryMessenger, NSObject<MachineApi> *_Nullable api);
extern void AuthResponseApiSetup(id<FlutterBinaryMessenger> binaryMessenger, NSObject<AuthResponseApi> *_Nullable api);

NS_ASSUME_NONNULL_END
88 changes: 56 additions & 32 deletions ios/Runner/pigeon.m
Original file line number Diff line number Diff line change
Expand Up @@ -21,56 +21,76 @@ static id GetNullableObjectAtIndex(NSArray *array, NSInteger key) {
return (result == [NSNull null]) ? nil : result;
}

@interface Machine ()
+ (Machine *)fromList:(NSArray *)list;
+ (nullable Machine *)nullableFromList:(NSArray *)list;
@interface AuthResponse ()
+ (AuthResponse *)fromList:(NSArray *)list;
+ (nullable AuthResponse *)nullableFromList:(NSArray *)list;
- (NSArray *)toList;
@end

@implementation Machine
+ (instancetype)makeWithMap:(NSDictionary<NSString *, NSString *> *)map
@implementation AuthResponse
+ (instancetype)makeWithResponse:(NSString *)response
username:(NSString *)username
isOfficer:(NSNumber *)isOfficer
isDefault:(NSNumber *)isDefault
isSupervisor:(NSNumber *)isSupervisor
errorCode:(nullable NSString *)errorCode {
Machine* pigeonResult = [[Machine alloc] init];
pigeonResult.map = map;
AuthResponse* pigeonResult = [[AuthResponse alloc] init];
pigeonResult.response = response;
pigeonResult.username = username;
pigeonResult.isOfficer = isOfficer;
pigeonResult.isDefault = isDefault;
pigeonResult.isSupervisor = isSupervisor;
pigeonResult.errorCode = errorCode;
return pigeonResult;
}
+ (Machine *)fromList:(NSArray *)list {
Machine *pigeonResult = [[Machine alloc] init];
pigeonResult.map = GetNullableObjectAtIndex(list, 0);
NSAssert(pigeonResult.map != nil, @"");
pigeonResult.errorCode = GetNullableObjectAtIndex(list, 1);
+ (AuthResponse *)fromList:(NSArray *)list {
AuthResponse *pigeonResult = [[AuthResponse alloc] init];
pigeonResult.response = GetNullableObjectAtIndex(list, 0);
NSAssert(pigeonResult.response != nil, @"");
pigeonResult.username = GetNullableObjectAtIndex(list, 1);
NSAssert(pigeonResult.username != nil, @"");
pigeonResult.isOfficer = GetNullableObjectAtIndex(list, 2);
NSAssert(pigeonResult.isOfficer != nil, @"");
pigeonResult.isDefault = GetNullableObjectAtIndex(list, 3);
NSAssert(pigeonResult.isDefault != nil, @"");
pigeonResult.isSupervisor = GetNullableObjectAtIndex(list, 4);
NSAssert(pigeonResult.isSupervisor != nil, @"");
pigeonResult.errorCode = GetNullableObjectAtIndex(list, 5);
return pigeonResult;
}
+ (nullable Machine *)nullableFromList:(NSArray *)list {
return (list) ? [Machine fromList:list] : nil;
+ (nullable AuthResponse *)nullableFromList:(NSArray *)list {
return (list) ? [AuthResponse fromList:list] : nil;
}
- (NSArray *)toList {
return @[
(self.map ?: [NSNull null]),
(self.response ?: [NSNull null]),
(self.username ?: [NSNull null]),
(self.isOfficer ?: [NSNull null]),
(self.isDefault ?: [NSNull null]),
(self.isSupervisor ?: [NSNull null]),
(self.errorCode ?: [NSNull null]),
];
}
@end

@interface MachineApiCodecReader : FlutterStandardReader
@interface AuthResponseApiCodecReader : FlutterStandardReader
@end
@implementation MachineApiCodecReader
@implementation AuthResponseApiCodecReader
- (nullable id)readValueOfType:(UInt8)type {
switch (type) {
case 128:
return [Machine fromList:[self readValue]];
return [AuthResponse fromList:[self readValue]];
default:
return [super readValueOfType:type];
}
}
@end

@interface MachineApiCodecWriter : FlutterStandardWriter
@interface AuthResponseApiCodecWriter : FlutterStandardWriter
@end
@implementation MachineApiCodecWriter
@implementation AuthResponseApiCodecWriter
- (void)writeValue:(id)value {
if ([value isKindOfClass:[Machine class]]) {
if ([value isKindOfClass:[AuthResponse class]]) {
[self writeByte:128];
[self writeValue:[value toList]];
} else {
Expand All @@ -79,39 +99,43 @@ - (void)writeValue:(id)value {
}
@end

@interface MachineApiCodecReaderWriter : FlutterStandardReaderWriter
@interface AuthResponseApiCodecReaderWriter : FlutterStandardReaderWriter
@end
@implementation MachineApiCodecReaderWriter
@implementation AuthResponseApiCodecReaderWriter
- (FlutterStandardWriter *)writerWithData:(NSMutableData *)data {
return [[MachineApiCodecWriter alloc] initWithData:data];
return [[AuthResponseApiCodecWriter alloc] initWithData:data];
}
- (FlutterStandardReader *)readerWithData:(NSData *)data {
return [[MachineApiCodecReader alloc] initWithData:data];
return [[AuthResponseApiCodecReader alloc] initWithData:data];
}
@end

NSObject<FlutterMessageCodec> *MachineApiGetCodec(void) {
NSObject<FlutterMessageCodec> *AuthResponseApiGetCodec(void) {
static FlutterStandardMessageCodec *sSharedObject = nil;
static dispatch_once_t sPred = 0;
dispatch_once(&sPred, ^{
MachineApiCodecReaderWriter *readerWriter = [[MachineApiCodecReaderWriter alloc] init];
AuthResponseApiCodecReaderWriter *readerWriter = [[AuthResponseApiCodecReaderWriter alloc] init];
sSharedObject = [FlutterStandardMessageCodec codecWithReaderWriter:readerWriter];
});
return sSharedObject;
}

void MachineApiSetup(id<FlutterBinaryMessenger> binaryMessenger, NSObject<MachineApi> *api) {
void AuthResponseApiSetup(id<FlutterBinaryMessenger> binaryMessenger, NSObject<AuthResponseApi> *api) {
{
FlutterBasicMessageChannel *channel =
[[FlutterBasicMessageChannel alloc]
initWithName:@"dev.flutter.pigeon.MachineApi.getMachineDetails"
initWithName:@"dev.flutter.pigeon.AuthResponseApi.login"
binaryMessenger:binaryMessenger
codec:MachineApiGetCodec()];
codec:AuthResponseApiGetCodec()];
if (api) {
NSCAssert([api respondsToSelector:@selector(getMachineDetailsWithError:)], @"MachineApi api (%@) doesn't respond to @selector(getMachineDetailsWithError:)", api);
NSCAssert([api respondsToSelector:@selector(loginUsername:password:isConnected:error:)], @"AuthResponseApi api (%@) doesn't respond to @selector(loginUsername:password:isConnected:error:)", api);
[channel setMessageHandler:^(id _Nullable message, FlutterReply callback) {
NSArray *args = message;
NSString *arg_username = GetNullableObjectAtIndex(args, 0);
NSString *arg_password = GetNullableObjectAtIndex(args, 1);
NSNumber *arg_isConnected = GetNullableObjectAtIndex(args, 2);
FlutterError *error;
Machine *output = [api getMachineDetailsWithError:&error];
AuthResponse *output = [api loginUsername:arg_username password:arg_password isConnected:arg_isConnected error:&error];
callback(wrapResult(output, error));
}];
} else {
Expand Down
14 changes: 14 additions & 0 deletions lib/platform_android/auth_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,19 @@ class AuthImpl extends Auth {
print('User not fetched! ${e.toString()}');
}
return user;

}

@override
Future<User> logoutUser() async {
late User user;
try {
user = await UserApi().logoutUser();
} on PlatformException {
print('UserApi logout call failed');
} catch(e) {
print('User not fetched! ${e.toString()}');
}
return user;
}
}
3 changes: 2 additions & 1 deletion lib/platform_spi/auth.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ import 'package:registration_client/pigeon/user_pigeon.dart';

abstract class Auth {
Future<User> validateUser(String username);
}
Future<User> logoutUser();
}
Loading