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 16 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
9 changes: 6 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,22 @@ x.x.x Release notes (yyyy-MM-dd)

**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.**

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

### Enhancements
* Support result value from write transaction callbacks ([#294](https://github.com/realm/realm-dart/pull/294/))

### Fixed
* None

### Compatibility
* Dart ^2.15 on Windows, MacOS and Linux

### Internal
* Added a command to deploy a MongoDB Realm app to `realm_dart`. Usage: `dart run realm_dart deploy-apps`. By default it will deploy apps to `http://localhost:9090` which is the endpoint of the local docker image. If `--atlas-cluster` is provided, it will authenticate, create an application and link the provided cluster to it. (PR [#309](https://github.com/realm/realm-dart/pull/309))
* Unit tests will now attempt to lookup and create if necessary MongoDB applications (similarly to the above mentioned command). See `test.dart/setupBaas()` for the environment variables that control the Url and Atlas Cluster that will be used. If the `BAAS_URL` environment variable is not set, no apps will be imported and sync tests will not run. (PR [#309](https://github.com/realm/realm-dart/pull/309))

### Compatibility
* Dart ^2.15 on Windows, MacOS and Linux

0.2.1+alpha Release notes (2022-03-20)
==============================================================

Expand Down
5 changes: 4 additions & 1 deletion dartdoc_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ dartdoc:
"Realm":
markdown: topic.md
name: Realm
categoryOrder: ["Realm", "Configuration", "Annotations"]
"Application":
markdown: topic.md
name: Application
categoryOrder: ["Realm", "Configuration", "Annotations", "Application"]
examplePathPrefix: 'example'
# nodoc: ['generator/flutter/ffigen/scripts/src/test/*.g.dart']
showUndocumentedCategories: true
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
49 changes: 49 additions & 0 deletions lib/src/app_credentials.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
////////////////////////////////////////////////////////////////////////////////
//
// 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 all authentication providers. These have to be enabled manually for the application before they can be used
/// your application before they can be used.
desistefanova 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,
}

/// A class, representing the credentials used for authenticating a [User]
/// {@category Application}
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 {
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, 16);
}

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);
});
}