From 8c5a43bf687b4bf079fad145b4ac958950987771 Mon Sep 17 00:00:00 2001 From: Dillon Nys Date: Thu, 7 Mar 2024 16:22:44 -0800 Subject: [PATCH] Add back Auth provider types --- packages/celest/lib/celest.dart | 8 ++++ .../celest/lib/src/auth/auth_provider.dart | 10 ++--- packages/celest/lib/src/core/context.dart | 30 ++++++++++++++ packages/celest/lib/src/core/project.dart | 6 --- packages/celest/lib/src/grants/grants.dart | 40 +++++++++++++++++++ 5 files changed, 83 insertions(+), 11 deletions(-) create mode 100644 packages/celest/lib/src/core/context.dart create mode 100644 packages/celest/lib/src/grants/grants.dart diff --git a/packages/celest/lib/celest.dart b/packages/celest/lib/celest.dart index d242310f..51f01b06 100644 --- a/packages/celest/lib/celest.dart +++ b/packages/celest/lib/celest.dart @@ -3,13 +3,21 @@ library celest; export 'package:celest_core/celest_core.dart'; +/// Auth +export 'src/auth/auth.dart'; +export 'src/auth/auth_provider.dart'; + /// Config export 'src/config/env.dart'; /// Core export 'src/core/cloud_widget.dart'; +export 'src/core/context.dart'; export 'src/core/project.dart'; /// Functions export 'src/functions/cloud_api.dart'; export 'src/functions/cloud_function.dart'; + +/// Grants +export 'src/grants/grants.dart'; diff --git a/packages/celest/lib/src/auth/auth_provider.dart b/packages/celest/lib/src/auth/auth_provider.dart index 54c5b624..39d60488 100644 --- a/packages/celest/lib/src/auth/auth_provider.dart +++ b/packages/celest/lib/src/auth/auth_provider.dart @@ -2,15 +2,15 @@ /// An authentication provider which can be used to sign in to Celest. /// /// Currently, Celest supports the following authentication methods: -/// - [AuthProvider.google] Sign in with Google +/// - [AuthProvider.email] Email sign-in with OTP codes. /// {@endtemplate} sealed class AuthProvider { const AuthProvider(); - /// A provider which enables Sign in with Google. - const factory AuthProvider.google() = _GoogleAuthProvider; + /// A provider which enables email sign-in with OTP codes. + const factory AuthProvider.email() = _EmailAuthProvider; } -final class _GoogleAuthProvider extends AuthProvider { - const _GoogleAuthProvider(); +final class _EmailAuthProvider extends AuthProvider { + const _EmailAuthProvider(); } diff --git a/packages/celest/lib/src/core/context.dart b/packages/celest/lib/src/core/context.dart new file mode 100644 index 00000000..a0cfff41 --- /dev/null +++ b/packages/celest/lib/src/core/context.dart @@ -0,0 +1,30 @@ +import 'package:celest/celest.dart'; + +/// The context of a [CloudFunction] invocation. +abstract final class Context { + const Context._(); + + /// A context reference to the [User] invoking a [CloudFunction]. + /// + /// ## Example + /// + /// To inject a user into an `@authenticated` function: + /// + /// ```dart + /// @authenticated + /// Future sayHello({ + /// @Context.user() required User user, + /// }) async { + /// print('Hello, ${user.displayName}!'); + /// } + /// ``` + const factory Context.user() = _ContextKey.user; +} + +final class _ContextKey extends Context { + const _ContextKey._(this.key) : super._(); + + const _ContextKey.user() : this._('user'); + + final String key; +} diff --git a/packages/celest/lib/src/core/project.dart b/packages/celest/lib/src/core/project.dart index 0f140eee..a66a9c4a 100644 --- a/packages/celest/lib/src/core/project.dart +++ b/packages/celest/lib/src/core/project.dart @@ -11,14 +11,8 @@ class Project implements CloudWidget { /// {@macro celest.core.project} const Project({ required this.name, - this.logoUrl, }); /// The name of the project as its identified in your Celest backend. final String name; - - /// The hosted URL of your project's logo. - /// - /// This is used by widgets like [Auth] to craft a custom login page. - final String? logoUrl; } diff --git a/packages/celest/lib/src/grants/grants.dart b/packages/celest/lib/src/grants/grants.dart new file mode 100644 index 00000000..8b484505 --- /dev/null +++ b/packages/celest/lib/src/grants/grants.dart @@ -0,0 +1,40 @@ +import 'package:celest/src/core/entity.dart'; + +/// A grant which provides access to all authenticated users. +/// +/// ## Example +/// +/// ```dart +/// import 'package:celest/celest.dart'; +/// +/// @authenticated +/// Future sayHello() async { +/// print('User is authenticated!'); +/// } +/// ``` +const authenticated = _Authenticated(); + +final class _Role implements Entity { + const _Role({ + required this.name, + }); + + static const _Role authenticated = _Role(name: r'$authenticated'); + + final String name; +} + +/// An assignment which grants a set of permissions to a specific group of +/// [Entity]s. +final class _Grant { + const _Grant({ + required this.to, + }); + + /// The group of [Entity] which are granted access. + final List to; +} + +final class _Authenticated extends _Grant { + const _Authenticated() : super(to: const [_Role.authenticated]); +}