Skip to content

Commit

Permalink
Merge pull request #460 from ably/enable_authorise
Browse files Browse the repository at this point in the history
Enable authorise
  • Loading branch information
ikbalkaya authored Apr 28, 2023
2 parents 9c53093 + ee442fb commit ff9e0a5
Show file tree
Hide file tree
Showing 37 changed files with 2,091 additions and 1,058 deletions.
2 changes: 1 addition & 1 deletion android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ apply plugin: 'com.android.library'

dependencies {
// https://github.com/ably/ably-java/
implementation 'io.ably:ably-android:1.2.15'
implementation 'io.ably:ably-android:1.2.27'

// https://firebase.google.com/docs/cloud-messaging/android/client
implementation 'com.google.firebase:firebase-messaging:23.0.4'
Expand Down
1,785 changes: 925 additions & 860 deletions android/src/main/java/io/ably/flutter/plugin/AblyMessageCodec.java

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

import io.ably.flutter.plugin.generated.PlatformConstants;
import io.ably.flutter.plugin.push.PushActivationEventHandlers;
import io.ably.flutter.plugin.push.PushMessagingEventHandlers;
import io.ably.flutter.plugin.types.PlatformClientOptions;
import io.ably.flutter.plugin.util.BiConsumer;
import io.ably.lib.realtime.AblyRealtime;
Expand Down Expand Up @@ -49,6 +48,7 @@ public class AblyMethodCallHandler implements MethodChannel.MethodCallHandler {
private final StreamsChannel streamsChannel;
private final Map<String, BiConsumer<MethodCall, MethodChannel.Result>> _map;
private final AblyInstanceStore instanceStore;
private final AuthMethodHandler authMethodHandler;
@Nullable
private RemoteMessage remoteMessageFromUserTapLaunchesApp;

Expand All @@ -59,6 +59,7 @@ public AblyMethodCallHandler(final MethodChannel methodChannel,
this.streamsChannel = streamsChannel;
this.applicationContext = applicationContext;
this.instanceStore = AblyInstanceStore.getInstance();
authMethodHandler = new AuthMethodHandler(this.instanceStore);
_map = new HashMap<>();
_map.put(PlatformConstants.PlatformMethod.getPlatformVersion, this::getPlatformVersion);
_map.put(PlatformConstants.PlatformMethod.getVersion, this::getVersion);
Expand Down Expand Up @@ -91,6 +92,26 @@ public AblyMethodCallHandler(final MethodChannel methodChannel,
_map.put(PlatformConstants.PlatformMethod.realtimeTime, this::realtimeTime);
_map.put(PlatformConstants.PlatformMethod.restTime, this::restTime);

//authorizations
_map.put(PlatformConstants.PlatformMethod.realtimeAuthAuthorize,
(methodCall, result) -> authMethodHandler.authorize(methodCall, result, AuthMethodHandler.Type.Realtime));
_map.put(PlatformConstants.PlatformMethod.realtimeAuthRequestToken,
(methodCall, result) -> authMethodHandler.requestToken(methodCall, result, AuthMethodHandler.Type.Realtime));
_map.put(PlatformConstants.PlatformMethod.realtimeAuthCreateTokenRequest,
(methodCall, result) -> authMethodHandler.createTokenRequest(methodCall, result, AuthMethodHandler.Type.Realtime));
_map.put(PlatformConstants.PlatformMethod.realtimeAuthGetClientId,
(methodCall, result) -> authMethodHandler.clientId(methodCall, result, AuthMethodHandler.Type.Realtime));

_map.put(PlatformConstants.PlatformMethod.restAuthAuthorize,
(methodCall, result) -> authMethodHandler.authorize(methodCall, result, AuthMethodHandler.Type.Rest));
_map.put(PlatformConstants.PlatformMethod.restAuthRequestToken,
(methodCall, result) -> authMethodHandler.requestToken(methodCall, result, AuthMethodHandler.Type.Rest));
_map.put(PlatformConstants.PlatformMethod.restAuthCreateTokenRequest,
(methodCall, result) -> authMethodHandler.createTokenRequest(methodCall, result,
AuthMethodHandler.Type.Rest));
_map.put(PlatformConstants.PlatformMethod.restAuthGetClientId,
(methodCall, result) -> authMethodHandler.clientId(methodCall, result, AuthMethodHandler.Type.Rest));

// Push Notifications
_map.put(PlatformConstants.PlatformMethod.pushActivate, this::pushActivate);
_map.put(PlatformConstants.PlatformMethod.pushDeactivate, this::pushDeactivate);
Expand Down Expand Up @@ -559,7 +580,10 @@ private void realtimeTime(@NonNull MethodCall methodCall, @NonNull MethodChannel
time(result, instanceStore.getRealtime(ablyMessage.handle));
}

private void restTime(@NonNull MethodCall methodCall, @NonNull MethodChannel.Result result) {



private void restTime(@NonNull MethodCall methodCall, @NonNull MethodChannel.Result result) {
final AblyFlutterMessage ablyMessage = (AblyFlutterMessage) methodCall.arguments;
time(result, instanceStore.getRest(ablyMessage.handle));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package io.ably.flutter.plugin;

import android.os.Handler;
import android.os.Looper;

import androidx.annotation.NonNull;

import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import io.ably.flutter.plugin.generated.PlatformConstants;
import io.ably.lib.rest.Auth;
import io.ably.lib.types.AblyException;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;

class AuthMethodHandler {
enum Type{Realtime,Rest}

private final AblyInstanceStore instanceStore;
private final ExecutorService executor = Executors.newSingleThreadExecutor();

public AuthMethodHandler(AblyInstanceStore instanceStore) {
this.instanceStore = instanceStore;

}

void authorize(@NonNull MethodCall methodCall, @NonNull MethodChannel.Result result, Type type) {
final AblyFlutterMessage<Map<String, Object>> ablyMessage = (AblyFlutterMessage) methodCall.arguments;
final Auth.TokenParams tokenParams =
(Auth.TokenParams) ablyMessage.message.get(PlatformConstants.TxTransportKeys.params);

final Auth.AuthOptions options =
(Auth.AuthOptions) ablyMessage.message.get(PlatformConstants.TxTransportKeys.options);

executor.execute(() -> {
try {
final Auth.TokenDetails tokenDetails = getAuth(ablyMessage, type)
.authorize(tokenParams, options);
result.success(tokenDetails);
} catch (AblyException e) {
result.error(String.valueOf(e.errorInfo.code), e.errorInfo.message, e);
}
});

}

private Auth getAuth(AblyFlutterMessage<Map<String, Object>> ablyMessage, Type type) {
if (type == Type.Realtime) {
return instanceStore.getRealtime(ablyMessage.handle).auth;
}
return instanceStore.getRest(ablyMessage.handle).auth;
}

void requestToken(@NonNull MethodCall methodCall, @NonNull MethodChannel.Result result, Type type) {
final AblyFlutterMessage<Map<String, Object>> ablyMessage = (AblyFlutterMessage) methodCall.arguments;
final Auth.TokenParams tokenParams =
(Auth.TokenParams) ablyMessage.message.get(PlatformConstants.TxTransportKeys.params);

final Auth.AuthOptions options =
(Auth.AuthOptions) ablyMessage.message.get(PlatformConstants.TxTransportKeys.options);

executor.execute(() -> {
try {
final Auth.TokenDetails tokenDetails = getAuth(ablyMessage, type)
.requestToken(tokenParams, options);
result.success(tokenDetails);
} catch (AblyException e) {
result.error(String.valueOf(e.errorInfo.code), e.errorInfo.message, e);
}
});
}

void createTokenRequest(@NonNull MethodCall methodCall, @NonNull MethodChannel.Result result, Type type) {
final AblyFlutterMessage<Map<String, Object>> ablyMessage = (AblyFlutterMessage) methodCall.arguments;
final Auth.TokenParams tokenParams =
(Auth.TokenParams) ablyMessage.message.get(PlatformConstants.TxTransportKeys.params);

final Auth.AuthOptions options =
(Auth.AuthOptions) ablyMessage.message.get(PlatformConstants.TxTransportKeys.options);

executor.execute(() -> {
try {
final Auth.TokenRequest tokenRequest = getAuth(ablyMessage, type)
.createTokenRequest(tokenParams, options);
result.success(tokenRequest);
} catch (AblyException e) {
result.error(String.valueOf(e.errorInfo.code), e.errorInfo.message, e);
}
});
}

public void clientId(MethodCall methodCall, MethodChannel.Result result, Type type) {
String clientId = getAuth((AblyFlutterMessage) methodCall.arguments, type).clientId;
result.success(clientId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,31 @@ static final public class CodecTypes {
public static final byte ablyMessage = (byte) 128;
public static final byte ablyEventMessage = (byte) 129;
public static final byte clientOptions = (byte) 130;
public static final byte messageData = (byte) 131;
public static final byte messageExtras = (byte) 132;
public static final byte message = (byte) 133;
public static final byte tokenParams = (byte) 134;
public static final byte tokenDetails = (byte) 135;
public static final byte tokenRequest = (byte) 136;
public static final byte restChannelOptions = (byte) 137;
public static final byte realtimeChannelOptions = (byte) 138;
public static final byte paginatedResult = (byte) 139;
public static final byte restHistoryParams = (byte) 140;
public static final byte realtimeHistoryParams = (byte) 141;
public static final byte restPresenceParams = (byte) 142;
public static final byte presenceMessage = (byte) 143;
public static final byte realtimePresenceParams = (byte) 144;
public static final byte deviceDetails = (byte) 145;
public static final byte localDevice = (byte) 146;
public static final byte pushChannelSubscription = (byte) 147;
public static final byte unNotificationSettings = (byte) 148;
public static final byte remoteMessage = (byte) 149;
public static final byte errorInfo = (byte) 150;
public static final byte logLevel = (byte) 151;
public static final byte connectionStateChange = (byte) 152;
public static final byte channelStateChange = (byte) 153;
public static final byte cipherParams = (byte) 154;
public static final byte authOptions = (byte) 131;
public static final byte messageData = (byte) 132;
public static final byte messageExtras = (byte) 133;
public static final byte message = (byte) 134;
public static final byte tokenParams = (byte) 135;
public static final byte tokenDetails = (byte) 136;
public static final byte tokenRequest = (byte) 137;
public static final byte restChannelOptions = (byte) 138;
public static final byte realtimeChannelOptions = (byte) 139;
public static final byte paginatedResult = (byte) 140;
public static final byte restHistoryParams = (byte) 141;
public static final byte realtimeHistoryParams = (byte) 142;
public static final byte restPresenceParams = (byte) 143;
public static final byte presenceMessage = (byte) 144;
public static final byte realtimePresenceParams = (byte) 145;
public static final byte deviceDetails = (byte) 146;
public static final byte localDevice = (byte) 147;
public static final byte pushChannelSubscription = (byte) 148;
public static final byte unNotificationSettings = (byte) 149;
public static final byte remoteMessage = (byte) 150;
public static final byte errorInfo = (byte) 151;
public static final byte logLevel = (byte) 152;
public static final byte connectionStateChange = (byte) 153;
public static final byte channelStateChange = (byte) 154;
public static final byte cipherParams = (byte) 155;
}

static final public class PlatformMethod {
Expand All @@ -51,6 +52,10 @@ static final public class PlatformMethod {
public static final String restPresenceGet = "restPresenceGet";
public static final String restPresenceHistory = "restPresenceHistory";
public static final String releaseRestChannel = "releaseRestChannel";
public static final String restAuthAuthorize = "restAuthAuthorize";
public static final String restAuthCreateTokenRequest = "restAuthCreateTokenRequest";
public static final String restAuthRequestToken = "restAuthRequestToken";
public static final String restAuthGetClientId = "restAuthGetClientId";
public static final String createRealtime = "createRealtime";
public static final String connectRealtime = "connectRealtime";
public static final String closeRealtime = "closeRealtime";
Expand All @@ -68,6 +73,10 @@ static final public class PlatformMethod {
public static final String realtimeHistory = "realtimeHistory";
public static final String realtimeTime = "realtimeTime";
public static final String restTime = "restTime";
public static final String realtimeAuthAuthorize = "realtimeAuthAuthorize";
public static final String realtimeAuthCreateTokenRequest = "realtimeAuthCreateTokenRequest";
public static final String realtimeAuthRequestToken = "realtimeAuthRequestToken";
public static final String realtimeAuthGetClientId = "realtimeAuthGetClientId";
public static final String pushActivate = "pushActivate";
public static final String pushDeactivate = "pushDeactivate";
public static final String pushReset = "pushReset";
Expand Down Expand Up @@ -204,6 +213,19 @@ static final public class TxTokenDetails {
public static final String clientId = "clientId";
}

static final public class TxAuthOptions {
public static final String authCallback = "authCallback";
public static final String authUrl = "authUrl";
public static final String authMethod = "authMethod";
public static final String key = "key";
public static final String token = "token";
public static final String tokenDetails = "tokenDetails";
public static final String authHeaders = "authHeaders";
public static final String authParams = "authParams";
public static final String queryTime = "queryTime";
public static final String useTokenAuth = "useTokenAuth";
}

static final public class TxTokenParams {
public static final String capability = "capability";
public static final String clientId = "clientId";
Expand Down
27 changes: 27 additions & 0 deletions bin/codegen_context.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Iterable<Map<String, dynamic>> get _types sync* {

//Other ably objects
'clientOptions',
'authOptions',
'messageData',
'messageExtras',
'message',
Expand Down Expand Up @@ -74,6 +75,10 @@ const List<Map<String, dynamic>> _platformMethods = [
{'name': 'restPresenceGet', 'value': 'restPresenceGet'},
{'name': 'restPresenceHistory', 'value': 'restPresenceHistory'},
{'name': 'releaseRestChannel', 'value': 'releaseRestChannel'},
{'name': 'restAuthAuthorize', 'value': 'restAuthAuthorize'},
{'name': 'restAuthCreateTokenRequest', 'value': 'restAuthCreateTokenRequest'},
{'name': 'restAuthRequestToken', 'value': 'restAuthRequestToken'},
{'name': 'restAuthGetClientId', 'value': 'restAuthGetClientId'},

// Realtime
{'name': 'createRealtime', 'value': 'createRealtime'},
Expand All @@ -96,6 +101,13 @@ const List<Map<String, dynamic>> _platformMethods = [
{'name': 'realtimeHistory', 'value': 'realtimeHistory'},
{'name': 'realtimeTime', 'value': 'realtimeTime'},
{'name': 'restTime', 'value': 'restTime'},
{'name': 'realtimeAuthAuthorize', 'value': 'realtimeAuthAuthorize'},
{
'name': 'realtimeAuthCreateTokenRequest',
'value': 'realtimeAuthCreateTokenRequest'
},
{'name': 'realtimeAuthRequestToken', 'value': 'realtimeAuthRequestToken'},
{'name': 'realtimeAuthGetClientId', 'value': 'realtimeAuthGetClientId'},

// Push Notifications
{'name': 'pushActivate', 'value': 'pushActivate'},
Expand Down Expand Up @@ -267,6 +279,21 @@ const List<Map<String, dynamic>> _objects = [
'clientId'
]
},
{
'name': 'AuthOptions',
'properties': <String>[
'authCallback',
'authUrl',
'authMethod',
'key',
'token',
'tokenDetails',
'authHeaders',
'authParams',
'queryTime',
'useTokenAuth'
]
},
{
'name': 'TokenParams',
'properties': <String>[
Expand Down
4 changes: 2 additions & 2 deletions example/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@ SPEC CHECKSUMS:
ably_flutter: bba4fea46a2a3269fae98b67bd5c0c75cf3f0132
AblyDeltaCodec: 783d017270de70bbbc0a84e4235297b225d33636
device_info_plus: e5c5da33f982a436e103237c0c85f9031142abed
Flutter: 50d75fe2f02b26cc09d224853bb45737f8b3214a
Flutter: f04841e97a9d0b0a8025694d0796dd46242b2854
flutter_local_notifications: 0c0b1ae97e741e1521e4c1629a459d04b9aec743
fluttertoast: 6122fa75143e992b1d3470f61000f591a798cc58
msgpack: c85f6251873059738472ae136951cec5f30f3251
Toast: 91b396c56ee72a5790816f40d3a94dd357abc196

PODFILE CHECKSUM: fe0e1ee7f3d1f7d00b11b474b62dd62134535aea

COCOAPODS: 1.11.3
COCOAPODS: 1.12.1
Loading

0 comments on commit ff9e0a5

Please sign in to comment.