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

application credentials anonymous #443

Merged
merged 20 commits into from
Apr 1, 2022
Merged
Show file tree
Hide file tree
Changes from 7 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
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
x.x.x Release notes (yyyy-MM-dd)
==============================================================

**This project is in the Beta stage. All API's might change without warning and no guarantees are given about stability. Do not use it in production.**

### Enhancements
* Support anonymouse application credentials ([#443](https://github.com/realm/realm-dart/pull/443/))
desistefanova marked this conversation as resolved.
Show resolved Hide resolved

x.x.x Release notes (yyyy-MM-dd)
desistefanova marked this conversation as resolved.
Show resolved Hide resolved
==============================================================

**This project is in the Alpha stage. All API's might change without warning and no guarantees are given about stability. Do not use it in production.**

### Enhancements
Expand Down
1 change: 1 addition & 0 deletions flutter/realm_flutter/ios/Classes/RealmPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ void dummy(void) {
realm_list_size(NULL, 0);
realm_dart_results_add_notification_callback(NULL, NULL, NULL, NULL);
realm_results_snapshot(NULL);
realm_app_credentials_new_anonymous();
}

@end
2 changes: 2 additions & 0 deletions flutter/realm_flutter/tests/test_driver/realm_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import '../test/realm_test.dart' as realm_tests;
import '../test/realm_object_test.dart' as realm_object_tests;
import '../test/list_test.dart' as list_tests;
import '../test/results_test.dart' as results_tests;
import '../test/app_credentials_test.dart' as app_credentials;

Future<String> main(List<String> args) async {
final Completer<String> completer = Completer<String>();
Expand All @@ -21,6 +22,7 @@ Future<String> main(List<String> args) async {
await realm_object_tests.main(args);
await list_tests.main(args);
await results_tests.main(args);
await app_credentials.main(args);

tearDown(() {
if (Invoker.current?.liveTest.state.result == test_api.Result.error || Invoker.current?.liveTest.state.result == test_api.Result.failure) {
Expand Down
55 changes: 55 additions & 0 deletions lib/src/app_credentials.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
////////////////////////////////////////////////////////////////////////////////
//
// Copyright 2022 Realm Inc.
//
// 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 'native/realm_core.dart';

/// An enum containing the possible authentication providers. These have to manually be enabled for
desistefanova marked this conversation as resolved.
Show resolved Hide resolved
/// your app before they can be used.
blagoev marked this conversation as resolved.
Show resolved Hide resolved
/// [Authentication Providers Docs](https://docs.mongodb.com/realm/authentication/providers/)
/// {@category Application}
enum AuthProvider {
/// Mechanism for authenticating without credentials.
anonymous,

/// Mechanism for authenticating with an email and a password.
emailPassword,

/// Allow users to log in with JWT-based credentials generated by a service external to Realm.
jwt,
blagoev marked this conversation as resolved.
Show resolved Hide resolved
desistefanova marked this conversation as resolved.
Show resolved Hide resolved

/// Mechanism for logging in with API keys generated by the client SDK.
apiKey,
blagoev marked this conversation as resolved.
Show resolved Hide resolved
}

/// A class, representing the credentials used for authenticating a [User]
class Credentials {
late final RealmAppCredentialsHandle _handle;

final AuthProvider provider;

/// Returns a Credentials object that can be used to authenticate an anonymous user.
/// [Anonymous Authentication Docs]("https://docs.mongodb.com/realm/authentication/anonymous/")
Credentials.anonymous()
: _handle = realmCore.createAppCredentialsAnonymous(),
provider = AuthProvider.anonymous;
}

/// @nodoc
extension CredentialsInternal on Credentials {
///@nodoc
blagoev marked this conversation as resolved.
Show resolved Hide resolved
RealmAppCredentialsHandle get handle => _handle;
}
8 changes: 8 additions & 0 deletions lib/src/native/realm_core.dart
Original file line number Diff line number Diff line change
Expand Up @@ -633,6 +633,10 @@ class _RealmCore {
return out_modified.asTypedList(count).toList();
});
}

RealmAppCredentialsHandle createAppCredentialsAnonymous() {
return RealmAppCredentialsHandle._(_realmLib.realm_app_credentials_new_anonymous());
}
}

class LastError {
Expand Down Expand Up @@ -725,6 +729,10 @@ class RealmObjectChangesHandle extends Handle<realm_object_changes> {
RealmObjectChangesHandle._(Pointer<realm_object_changes> pointer) : super(pointer, 256);
}

class RealmAppCredentialsHandle extends Handle<realm_app_credentials> {
RealmAppCredentialsHandle._(Pointer<realm_app_credentials> pointer) : super(pointer, 256); // TODO: What should hint be?
blagoev marked this conversation as resolved.
Show resolved Hide resolved
}

extension _StringEx on String {
Pointer<Int8> toUtf8Ptr(Allocator allocator) {
final units = utf8.encode(this);
Expand Down
3 changes: 2 additions & 1 deletion lib/src/realm_class.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export 'list.dart' show RealmList, RealmListOfObject, RealmListChanges;
export 'realm_object.dart' show RealmEntity, RealmException, RealmObject, RealmObjectChanges;
export 'realm_property.dart';
export 'results.dart' show RealmResults, RealmResultsChanges;
export 'app_credentials.dart' show Credentials, AuthProvider;

/// A [Realm] instance represents a `Realm` database.
///
Expand Down Expand Up @@ -70,7 +71,7 @@ class Realm {
_scheduler.stop();
rethrow;
}
_config.isInUse = true;
_config.isInUse = true;
}

/// Deletes all files associated with a `Realm` located at given [path]
Expand Down
2 changes: 1 addition & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ endif()
option(REALM_BUILD_CORE_FROM_SOURCE "Build Realm Core from source" ON)
if(REALM_BUILD_CORE_FROM_SOURCE)
set(REALM_BUILD_LIB_ONLY ON)
set(REALM_ENABLE_SYNC OFF)
set(REALM_ENABLE_SYNC ON)

if(ANDROID)
message ("Realm Flutter Android build enabled")
Expand Down
1 change: 1 addition & 0 deletions src/realm_dart.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ void dummy(void) {
realm_list_size(nullptr, 0);
realm_results_add_notification_callback(nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr);
realm_results_snapshot(nullptr);
realm_app_credentials_new_anonymous();
#if (ANDROID)
realm_android_dummy();
#endif
Expand Down
34 changes: 34 additions & 0 deletions test/app_credentials_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
////////////////////////////////////////////////////////////////////////////////
//
// Copyright 2022 Realm Inc.
//
// 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 'dart:io';

import 'package:test/test.dart' hide test, throws;
import '../lib/realm.dart';
import 'test.dart';

Future<void> main([List<String>? args]) async {
print("Current PID $pid");

setupTests(args);

test('ApplicationCredentials anonymous', () {
final credentials = Credentials.anonymous();
expect(credentials, isNotNull);
});
}