Skip to content

Commit e71415b

Browse files
authored
fix(cloud_auth): Session duration on Web (#372)
- Fixes logic for determining the cookie expiration on Web. - Updates celest_auth example - Adds smoke test for verifying behavior on browsers
1 parent f00effa commit e71415b

File tree

15 files changed

+335
-144
lines changed

15 files changed

+335
-144
lines changed

.github/workflows/celest_auth.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ on:
44
paths:
55
- ".github/workflows/celest_auth.yaml"
66
- "packages/celest_auth/**"
7+
- "services/celest_cloud_auth/**"
78

89
# Prevent duplicate runs due to Graphite
910
# https://graphite.dev/docs/troubleshooting#why-are-my-actions-running-twice

.github/workflows/celest_cloud_auth.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,9 @@ jobs:
4747
- name: Test
4848
working-directory: services/celest_cloud_auth
4949
run: dart test
50+
- name: Test (Web/JS)
51+
working-directory: services/celest_cloud_auth
52+
run: dart test -p chrome test/web
53+
- name: Test (Web/WASM)
54+
working-directory: services/celest_cloud_auth
55+
run: dart test -p chrome -c dart2wasm test/web

packages/celest_auth/example/celest/client/lib/celest_auth_example_client.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ enum CelestEnvironment {
2727

2828
Uri get baseUri => switch (this) {
2929
local => _$celest.kIsWeb || !Platform.isAndroid
30-
? Uri.parse('http://localhost:61552')
31-
: Uri.parse('http://10.0.2.2:61552'),
30+
? Uri.parse('http://localhost:7777')
31+
: Uri.parse('http://10.0.2.2:7777'),
3232
};
3333
}
3434

packages/celest_auth/example/celest/lib/src/generated/auth.celest.dart

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@
44

55
library; // ignore_for_file: no_leading_underscores_for_library_prefixes
66

7-
import 'package:celest/celest.dart' as _$celest;
87
import 'package:celest/src/core/context.dart' as _$celest;
9-
import 'package:celest/src/runtime/data/connect.dart' as _$celest;
8+
import 'package:celest_backend/src/generated/cloud.celest.dart';
109
import 'package:celest_cloud_auth/celest_cloud_auth.dart' as _$celest;
1110

1211
/// The auth service for the Celest backend.
@@ -18,14 +17,8 @@ class CelestAuth {
1817

1918
/// Initializes the Celest Auth service in the given [context].
2019
static Future<void> init(_$celest.Context context) async {
21-
final database = await _$celest.connect(
22-
context,
23-
name: 'CelestAuthDatabase',
24-
factory: _$celest.AuthDatabase.new,
25-
hostnameVariable: const _$celest.env('CELEST_AUTH_DATABASE_HOST'),
26-
tokenSecret: const _$celest.secret('CELEST_AUTH_DATABASE_TOKEN'),
27-
);
28-
final service = await _$celest.CelestCloudAuth.create(database: database);
20+
final service =
21+
await _$celest.CelestCloudAuth.create(database: celest.data.cloudAuth);
2922
context.router.mount(
3023
'/v1alpha1/auth/',
3124
service.handler,

packages/celest_auth/example/celest/lib/src/generated/cloud.celest.dart

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ library; // ignore_for_file: no_leading_underscores_for_library_prefixes
66

77
import 'package:celest/src/core/context.dart' as _$celest;
88
import 'package:celest_backend/src/generated/config.celest.dart';
9+
import 'package:celest_backend/src/generated/data.celest.dart';
910

1011
/// The interface to your Celest backend.
1112
///
@@ -38,6 +39,12 @@ class CelestCloud {
3839
/// This class provides access to the values configured for the
3940
/// [currentEnvironment].
4041
CelestVariables get variables => const CelestVariables();
42+
43+
/// The data services for the Celest backend.
44+
///
45+
/// This class provides access to the databases that are configured
46+
/// for the [currentEnvironment].
47+
CelestData get data => const CelestData();
4148
}
4249

4350
/// A per-request context object which propogates request information and common
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Generated by Celest. This file should not be modified manually, but
2+
// it can be checked into version control.
3+
// ignore_for_file: type=lint, unused_local_variable, unnecessary_cast, unnecessary_import, deprecated_member_use, invalid_use_of_internal_member
4+
5+
library; // ignore_for_file: no_leading_underscores_for_library_prefixes
6+
7+
import 'package:celest/celest.dart' as _$celest;
8+
import 'package:celest/src/core/context.dart' as _$celest;
9+
import 'package:celest/src/runtime/data/connect.dart' as _$celest;
10+
import 'package:celest_cloud_auth/src/database/auth_database.dart' as _$celest;
11+
12+
/// The data services for the Celest backend.
13+
///
14+
/// This class provides access to the databases that are configured
15+
/// for the current [CelestEnvironment].
16+
class CelestData {
17+
const CelestData();
18+
19+
/// Initializes the databases attached to this project in the given [context].
20+
static Future<void> init(_$celest.Context context) async {
21+
context.put(
22+
_cloudAuthKey,
23+
await _$celest.connect(
24+
context,
25+
name: 'CloudAuthDatabase',
26+
factory: _$celest.CloudAuthDatabase.new,
27+
hostnameVariable: const _$celest.env('CLOUD_AUTH_DATABASE_HOST'),
28+
tokenSecret: const _$celest.secret('CLOUD_AUTH_DATABASE_TOKEN'),
29+
),
30+
);
31+
}
32+
33+
/// The `CloudAuthDatabase` instance for this project.
34+
_$celest.CloudAuthDatabase get cloudAuth =>
35+
_$celest.Context.current.expect(_cloudAuthKey);
36+
37+
/// The context key for the [cloudAuth] instance.
38+
static _$celest.ContextKey<_$celest.CloudAuthDatabase> get _cloudAuthKey =>
39+
const _$celest.ContextKey('CloudAuthDatabase');
40+
}

services/celest_cloud_auth/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.3.2+1
2+
3+
- fix: Session duration on Web
4+
15
## 0.3.2
26

37
- fix: Properly set/check session duration

services/celest_cloud_auth/bin/server.dart

Lines changed: 0 additions & 33 deletions
This file was deleted.
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import 'dart:io';
2+
3+
import 'package:cedar/cedar.dart';
4+
import 'package:celest/http.dart';
5+
import 'package:celest_ast/celest_ast.dart';
6+
import 'package:celest_cloud_auth/celest_cloud_auth.dart';
7+
import 'package:celest_cloud_auth/src/context.dart';
8+
import 'package:pub_semver/pub_semver.dart';
9+
import 'package:shelf/shelf.dart';
10+
import 'package:shelf/shelf_io.dart';
11+
import 'package:shelf_router/shelf_router.dart';
12+
13+
Future<void> main() async {
14+
context.put(ContextKey.project, exampleProject);
15+
16+
final database = CloudAuthDatabase.memory();
17+
final cloudAuth = await CelestCloudAuth.create(database: database);
18+
19+
final router = Router()
20+
..get('/test/hello', () => Response.ok('Hello, world!'))
21+
..mount('/v1alpha1/auth', cloudAuth.handler);
22+
final handler = const Pipeline()
23+
.addMiddleware(logRequests())
24+
.addMiddleware(cloudAuth.middleware.call)
25+
.addHandler(router.call);
26+
27+
final server = await serve(handler, 'localhost', 8080);
28+
print('Serving at http://${server.address.host}:${server.port}');
29+
30+
await ProcessSignal.sigint.watch().first;
31+
await server.close();
32+
print('Server closed');
33+
}
34+
35+
final exampleProject = ResolvedProject(
36+
projectId: 'test',
37+
environmentId: 'production',
38+
sdkConfig: SdkConfiguration(
39+
celest: Version(1, 0, 0),
40+
dart: Sdk(
41+
type: SdkType.dart,
42+
version: Version(3, 5, 0),
43+
),
44+
),
45+
apis: {
46+
AuthenticationService.api.apiId: AuthenticationService.api,
47+
UsersService.api.apiId: UsersService.api,
48+
'test': ResolvedApi(
49+
apiId: 'test',
50+
functions: {
51+
'hello': ResolvedCloudFunction(
52+
apiId: 'test',
53+
functionId: 'hello',
54+
httpConfig: ResolvedHttpConfig(
55+
route: ResolvedHttpRoute(
56+
method: HttpMethod.get,
57+
path: '/test/hello',
58+
),
59+
),
60+
policySet: PolicySet(
61+
templateLinks: const [
62+
TemplateLink(
63+
templateId: 'cloud.functions.authenticated',
64+
newId: 'test/hello',
65+
values: {
66+
SlotId.resource: EntityUid.of(
67+
'Celest::Function',
68+
'test/hello',
69+
),
70+
},
71+
),
72+
],
73+
),
74+
),
75+
},
76+
),
77+
},
78+
);

services/celest_cloud_auth/lib/src/http/cookie_cork.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ extension type Corkie._(Cookie cookie) implements Cookie {
1313
cork.toString(),
1414
expiration: switch (cork.claims?.attributes['expireTime']) {
1515
LongValue(:final value) =>
16-
DateTime.fromMillisecondsSinceEpoch(value.toInt()),
16+
DateTime.fromMillisecondsSinceEpoch(value.toInt() * 1000),
1717
_ => clock.now().add(SessionsRepository.postAuthSessionDuration),
1818
},
1919
);

0 commit comments

Comments
 (0)