Skip to content

Commit

Permalink
feat: Add remaining auth providers and secrets (#190)
Browse files Browse the repository at this point in the history
- Adds support for remaining auth providers
- Adds support for secrets
  • Loading branch information
dnys1 authored Oct 6, 2024
1 parent cf1d09f commit c74e324
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 9 deletions.
63 changes: 63 additions & 0 deletions packages/celest/lib/src/auth/auth_provider.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'package:celest/src/config/env.dart';

/// {@template celest.auth.auth_provider}
/// An authentication provider which can be used to sign in to Celest.
///
Expand All @@ -14,6 +16,33 @@ sealed class AuthProvider {

/// A provider which enables SMS sign-in with OTP codes.
const factory AuthProvider.sms() = _SmsAuthProvider;

/// A provider which enables GitHub sign-in.
///
/// [clientId] and [clientsecret] are required to authenticate with GitHub.
const factory AuthProvider.gitHub({
required secret clientId,
required secret clientsecret,
}) = _GitHubAuthProvider;

/// A provider which enables Google sign-in.
///
/// [clientId] and [clientsecret] are required to authenticate with Google.
const factory AuthProvider.google({
required secret clientId,
required secret clientsecret,
}) = _GoogleAuthProvider;

/// A provider which enables Sign In with Apple.
///
/// [clientId], [teamId], [keyId], and [key] are required to authenticate
/// with Apple.
const factory AuthProvider.apple({
required secret clientId,
required secret teamId,
required secret keyId,
required secret key,
}) = _AppleAuthProvider;
}

final class _EmailAuthProvider extends AuthProvider {
Expand All @@ -23,3 +52,37 @@ final class _EmailAuthProvider extends AuthProvider {
final class _SmsAuthProvider extends AuthProvider {
const _SmsAuthProvider();
}

final class _GitHubAuthProvider extends AuthProvider {
const _GitHubAuthProvider({
required this.clientId,
required this.clientsecret,
});

final secret clientId;
final secret clientsecret;
}

final class _GoogleAuthProvider extends AuthProvider {
const _GoogleAuthProvider({
required this.clientId,
required this.clientsecret,
});

final secret clientId;
final secret clientsecret;
}

final class _AppleAuthProvider extends AuthProvider {
const _AppleAuthProvider({
required this.clientId,
required this.teamId,
required this.keyId,
required this.key,
});

final secret clientId;
final secret teamId;
final secret keyId;
final secret key;
}
43 changes: 36 additions & 7 deletions packages/celest/lib/src/config/env.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,22 @@
import 'package:celest/src/core/cloud_widget.dart';
import 'package:celest/src/core/context.dart';

/// A configuration value that can be used to configure a Celest backend.
sealed class ConfigurationValue implements ContextKey<String> {
const ConfigurationValue._(this.name);

/// The name of the configuration value in the environment.
final String name;

@override
String? read(Context context) {
return context.platform.environment[name];
}

@override
void set(Context context, String? value) {
throw UnsupportedError('Cannot set a configuration value at runtime.');
}
}

/// {@template celest.config.environment_variable}
/// A reference to an environment variable within a Celest backend.
Expand All @@ -8,12 +26,23 @@ import 'package:celest/src/core/cloud_widget.dart';
/// configuration to your backend. They allow you to keep configuration values
/// separate from your codebase, improving flexibility when running in different
/// environments.
final class EnvironmentVariable extends CloudWidget {
final class env extends ConfigurationValue {
/// {@macro celest.config.environment_variable}
const EnvironmentVariable({
required this.name,
});
const env(super.name) : super._();

/// The name of the environment variable as it appears in its environment.
final String name;
/// The active Celest environment.
///
/// For example, `production`.
static const env environment = env('CELEST_ENVIRONMENT');
}

/// {@template celest.config.secret}
/// A secret value that should be kept confidential within a Celest backend.
///
/// Secrets behave much like environment variables except that their values
/// are encrypted in the transit and only exposed to the backend itself.
/// {@endtemplate}
final class secret extends ConfigurationValue {
/// {@macro celest.config.secret}
const secret(super.name) : super._();
}
11 changes: 9 additions & 2 deletions packages/celest/lib/src/core/context.dart
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import 'dart:async';
import 'dart:io';

import 'package:celest/src/config/env.dart';
import 'package:celest/src/core/environment.dart';
import 'package:celest/src/runtime/gcp/gcp.dart';
import 'package:celest_core/_internal.dart';
// ignore: implementation_imports
import 'package:celest_core/src/auth/user.dart';
import 'package:cloud_http/cloud_http.dart';
import 'package:meta/meta.dart';
import 'package:platform/platform.dart';
import 'package:shelf/shelf.dart' as shelf;

/// The [Context] for the current request.
Expand Down Expand Up @@ -37,6 +38,9 @@ final class Context {
/// The [Context] for the current execution scope.
static Context get current => Context.of(Zone.current);

/// The platform of the current context.
final Platform platform = const LocalPlatform();

/// Context-specific values.
final Map<ContextKey<Object>, Object> _values = {};

Expand Down Expand Up @@ -80,7 +84,10 @@ final class Context {

/// The Celest [Environment] of the running service.
Environment get environment {
return Environment(Platform.environment['ENV']!);
return switch (get(env.environment)) {
final env? => Environment(env),
_ => throw StateError('No environment set in context'),
};
}

(Context, V)? _get<V extends Object>(ContextKey<V> key) {
Expand Down
1 change: 1 addition & 0 deletions packages/celest/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ dependencies:
http_sfv: ^0.1.0
logging: ^1.2.0
meta: ^1.11.0
platform: ^3.1.5
shelf: ^1.4.1
shelf_router: ^1.1.4
shelf_web_socket: ^2.0.0
Expand Down

0 comments on commit c74e324

Please sign in to comment.