Skip to content

Commit

Permalink
Clean up - new functions on ios side
Browse files Browse the repository at this point in the history
  • Loading branch information
SinaSeylani committed Jul 15, 2024
1 parent 32b19c7 commit f6faae5
Show file tree
Hide file tree
Showing 9 changed files with 135 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -456,8 +456,8 @@ internal class PassageFlutter(private val activity: Activity, appId: String? = n
?: return invalidArgumentError(result)
CoroutineScope(Dispatchers.IO).launch {
try {
val user = passage.hostedAuthFinish(code, clientSecret, state)
result.success(null)
val AuthResultWithIdToken = passage.hostedAuthFinish(code, clientSecret, state)
result.success(AuthResultWithIdToken)
} catch (e: Exception) {
val error = PassageFlutterError.FINISH_HOSTED_AUTH_ERROR
result.error(error.name, e.message, e.toString())
Expand All @@ -476,6 +476,21 @@ internal class PassageFlutter(private val activity: Activity, appId: String? = n
}
}
}


fun hostedLogout(call: MethodCall, result: MethodChannel.Result) {
val idToken = call.argument<String>("idToken")
?: return invalidArgumentError(result)
CoroutineScope(Dispatchers.IO).launch {
try {
passage.hostedLogout(idToken)
result.success(null)
} catch (e: Exception) {
val error = PassageFlutterError.LOGOUT_HOSTED_AUTH_ERROR
result.error(error.name, e.message, e.toString())
}
}
}

// endregion

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ class PassageFlutterPlugin: FlutterPlugin, MethodCallHandler, ActivityAware {
"hostedAuthStart" -> passageFlutter?.hostedAuthStart(result)
"hostedAuthFinish" -> passageFlutter?.hostedAuthFinish(call, result)
"hostedLogout" -> passageFlutter?.hostedLogout(result)
"hostedLogoutWithIdToken" -> passageFlutter?.hostedLogout(call, result)
else -> {
result.notImplemented()
}
Expand Down
37 changes: 19 additions & 18 deletions ios/Classes/PassageFlutter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -527,11 +527,16 @@ internal class PassageFlutter {
}
}

internal func hostedAuthStart(result: @escaping FlutterResult) {
internal func hostedAuth(result: @escaping FlutterResult) {
guard let clientSecret = (arguments as? [String: String])?["clientSecret"] else {
let error = PassageFlutterError.INVALID_ARGUMENT.defaultFlutterError
result(error)
return
}
Task {
do {
try await passage.hostedAuthStart()
result(nil)
val AuthResultWithIdToken = try await passage.hostedAuth(clientSecret)
result(AuthResultWithIdToken)
} catch {
let error = FlutterError(
code: PassageFlutterError.START_HOSTED_AUTH_ERROR.rawValue,
Expand All @@ -543,23 +548,14 @@ internal class PassageFlutter {
}
}

internal func hostedAuthFinish(arguments: Any?, result: @escaping FlutterResult) {
guard let args = arguments as? [String: Any],
let code = args["code"] as? String,
let clientSecret = args["clientSecret"] as? String,
let state = args["state"] as? String else {
let error = PassageFlutterError.INVALID_ARGUMENT.defaultFlutterError
result(error)
return
}

internal func hostedLogout(result: @escaping FlutterResult) {
Task {
do {
let user = try await passage.hostedAuthFinish(code: code, clientSecret: clientSecret, state: state)
result(nil) // Successfully authenticated
try await passage.hostedLogout()
result(nil)
} catch {
let error = FlutterError(
code: PassageFlutterError.FINISH_HOSTED_AUTH_ERROR.rawValue,
code: PassageFlutterError.LOGOUT_HOSTED_AUTH_ERROR.rawValue,
message: error.localizedDescription,
details: nil
)
Expand All @@ -568,10 +564,15 @@ internal class PassageFlutter {
}
}

internal func hostedLogout(result: @escaping FlutterResult) {
internal func hostedLogout(arguments: Any?, result: @escaping FlutterResult) {
guard let idToken = (arguments as? [String: String])?["idToken"] else {
let error = PassageFlutterError.INVALID_ARGUMENT.defaultFlutterError
result(error)
return
}
Task {
do {
try await passage.hostedLogout()
try await passage.hostedLogout(idToken)
result(nil)
} catch {
let error = FlutterError(
Expand Down
8 changes: 4 additions & 4 deletions ios/Classes/PassageFlutterPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,12 @@ public class PassageFlutterPlugin: NSObject, FlutterPlugin {
passageFlutter.changePhone(arguments: call.arguments, result: result)
case "identifierExists":
passageFlutter.identifierExists(arguments: call.arguments, result: result)
case "hostedAuthStart":
passageFlutter.hostedAuthStart(result: result)
case "hostedAuthFinish":
passageFlutter.hostedAuthFinish(arguments: call.arguments, result: result)
case "hostedAuth":
passageFlutter.hostedAuth(arguments: call.arguments,result: result)
case "hostedLogout":
passageFlutter.hostedLogout(result: result)
case "hostedLogoutWithIdToken":
passageFlutter.hostedLogout(arguments: call.arguments, result: result)
default:
result(FlutterMethodNotImplemented)
}
Expand Down
30 changes: 28 additions & 2 deletions lib/passage_flutter.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'passage_flutter_models/auth_result.dart';
import 'passage_flutter_models/auth_result_with_id_token.dart';
import 'passage_flutter_models/authenticator_attachment.dart';
import 'passage_flutter_models/passage_app_info.dart';
import 'passage_flutter_models/passage_social_connection.dart';
Expand Down Expand Up @@ -354,7 +355,7 @@ class PassageFlutter {
return PassageFlutterPlatform.instance.changePhone(newPhone);
}

// OIDC Methods
// Hosted Auth Methods

/// Authentication Method for Hosted Apps
///
Expand All @@ -366,6 +367,19 @@ class PassageFlutter {
return PassageFlutterPlatform.instance.hostedAuthStart();
}

// Hosted Auth Methods

/// Authentication Method for Hosted Apps
/// ONLY FOR iOS
/// If your Passage app is Hosted, use this method to register and log in your user. This method will open up a Passage login experience
///
/// Throws:
/// `PassageError`
Future<AuthResultWithIdToken> hostedAuthIOS(String clientSecret) {
return PassageFlutterPlatform.instance.hostedAuthIOS(clientSecret);
}

/// Finish Hosted Auth
///
/// Finishes a Hosted login/sign up by exchanging the auth code for Passage tokens.
Expand All @@ -376,7 +390,7 @@ class PassageFlutter {
/// Throws:
/// `PassageError`
Future<void> hostedAuthFinish(String code, String clientSecret, String state) {
Future<AuthResultWithIdToken> hostedAuthFinish(String code, String clientSecret, String state) {
return PassageFlutterPlatform.instance.hostedAuthFinish(code, clientSecret, state);
}

Expand All @@ -390,4 +404,16 @@ class PassageFlutter {
return PassageFlutterPlatform.instance.hostedLogout();
}

// Logout Method for Hosted Apps
///
/// If your Passage app is Hosted, use this method to log out your user. This method will briefly open up a web view where it will log out the
/// Parameters:
/// idToken: The auth id token, used to log the user our of any remaining web sessions.
/// Throws:
/// `PassageError`
Future<void> hostedLogoutWithIdToken(String idToken) {
return PassageFlutterPlatform.instance.hostedLogoutWithIdToken(idToken);
}

}
8 changes: 8 additions & 0 deletions lib/passage_flutter_models/auth_result_with_id_token.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import 'auth_result.dart';

class AuthResultWithIdToken {
final AuthResult authResult;
final String idToken;

AuthResultWithIdToken(this.authResult, this.idToken);
}
3 changes: 3 additions & 0 deletions lib/passage_flutter_models/passage_error_code.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ class PassageErrorCode {
static const appInfoError = 'APP_INFO_ERROR';
static const changeEmailError = 'CHANGE_EMAIL_ERROR';
static const changePhoneError = 'CHANGE_PHONE_ERROR';
static const hostedAuthStart = 'HOSTED_AUTH_START';
static const hostedAuthFinish = 'HOSTED_AUTH_FINISH';
static const hostedAuthIOS = 'HOSTED_AUTH_IOS';
static const identifierExistsError = 'IDENTIFIER_EXISTS_ERROR';
static const otpActivationExceededAttempts =
'OTP_ACTIVATION_EXCEEDED_ATTEMPTS';
Expand Down
52 changes: 44 additions & 8 deletions lib/passage_flutter_platform/passage_flutter_method_channel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';
import 'package:passage_flutter/passage_flutter_models/passage_error_code.dart';

import '../passage_flutter_models/auth_result_with_id_token.dart';
import '../passage_flutter_models/passage_social_connection.dart';
import '/passage_flutter_models/auth_result.dart';
import '/passage_flutter_models/authenticator_attachment.dart';
Expand Down Expand Up @@ -329,22 +330,46 @@ class MethodChannelPassageFlutter extends PassageFlutterPlatform {
}

@override
Future<String> hostedAuthStart() async {
Future<void> hostedAuthStart() async {
if (Platform.isIOS) {
throw PassageError(
code: PassageErrorCode.hostedAuthStart,
message: 'Not supported on iOS. Use hostedAuthIOS instead.');
}
try {
final magicLinkId = await methodChannel
.invokeMethod<String>('hostedAuthStart');
return magicLinkId!;
await methodChannel.invokeMethod<String>('hostedAuthStart');
} catch (e) {
throw PassageError.fromObject(object: e);
}
}

@override
Future<String> hostedAuthFinish(String code, String clientSecret, String state) async {
Future<AuthResultWithIdToken> hostedAuthIOS(String clientSecret) async {
if (!Platform.isIOS) {
throw PassageError(
code: PassageErrorCode.hostedAuthFinish,
message: 'Only supported on iOS. Use hostedAuthStart instead.');
}
try {
final magicLinkId = await methodChannel
.invokeMethod<String>('hostedAuthFinish', {'code': code, 'clientSecret': clientSecret, 'state': state});
return magicLinkId!;
final authResultWithIdToken = await methodChannel
.invokeMethod<AuthResultWithIdToken>('hostedAuth', {'clientSecret': clientSecret});
return authResultWithIdToken!;
} catch (e) {
throw PassageError.fromObject(object: e);
}
}

@override
Future<AuthResultWithIdToken> hostedAuthFinish(String code, String clientSecret, String state) async {
if (Platform.isIOS) {
throw PassageError(
code: PassageErrorCode.hostedAuthFinish,
message: 'Not supported on iOS. Use hostedAuthIOS instead.');
}
try {
final authResultWithIdToken = await methodChannel
.invokeMethod<AuthResultWithIdToken>('hostedAuthFinish', {'code': code, 'clientSecret': clientSecret, 'state': state});
return authResultWithIdToken!;
} catch (e) {
throw PassageError.fromObject(object: e);
}
Expand All @@ -361,4 +386,15 @@ class MethodChannelPassageFlutter extends PassageFlutterPlatform {
}
}

@override
Future<String> hostedLogoutWithIdToken(String idToken) async {
try {
final magicLinkId = await methodChannel
.invokeMethod<String>('hostedLogoutWithIdToken', {'idToken': idToken});
return magicLinkId!;
} catch (e) {
throw PassageError.fromObject(object: e);
}
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:plugin_platform_interface/plugin_platform_interface.dart';

import '../passage_flutter_models/auth_result_with_id_token.dart';
import '/passage_flutter_models/auth_result.dart';
import '/passage_flutter_models/authenticator_attachment.dart';
import '/passage_flutter_models/passage_app_info.dart';
Expand Down Expand Up @@ -158,15 +159,23 @@ abstract class PassageFlutterPlatform extends PlatformInterface {
throw UnimplementedError('changePhone() has not been implemented.');
}

Future<String> hostedAuthStart() {
Future<void> hostedAuthStart() {
throw UnimplementedError('hostedAuthStart() has not been implemented.');
}

Future<String> hostedAuthFinish(String code, String clientSecret, String state) {
Future<AuthResultWithIdToken> hostedAuthIOS(String clientSecret) {
throw UnimplementedError('hostedAuthStart() has not been implemented.');
}

Future<AuthResultWithIdToken> hostedAuthFinish(String code, String clientSecret, String state) {
throw UnimplementedError('hostedAuthFinish() has not been implemented.');
}

Future<String> hostedLogout() {
throw UnimplementedError('hostedLogout() has not been implemented.');
}

Future<String> hostedLogoutWithIdToken(String idToken) {
throw UnimplementedError('hostedLogoutWithIdToken() has not been implemented.');
}
}

0 comments on commit f6faae5

Please sign in to comment.