Skip to content

Commit

Permalink
chore: add example to frontier and publish frontier package
Browse files Browse the repository at this point in the history
  • Loading branch information
francescovallone committed Feb 11, 2025
1 parent d2da441 commit 243a249
Show file tree
Hide file tree
Showing 13 changed files with 543 additions and 29 deletions.
4 changes: 4 additions & 0 deletions packages/frontier/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 1.0.0

- First stable release, providing a clean API for using authentication and authorization strategies in Dart built with frontier_strategy.

## 0.0.1

- Initial version.
20 changes: 9 additions & 11 deletions packages/frontier/bin/frontier.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@ final class HeaderOptions extends StrategyOptions {
final String key;
final String value;

HeaderOptions(
{required this.key, required this.value});
HeaderOptions({required this.key, required this.value});
}

class HeaderStrategy extends Strategy<HeaderOptions> {

HeaderStrategy(super.options, super.callback);

@override
Expand All @@ -24,20 +22,20 @@ class HeaderStrategy extends Strategy<HeaderOptions> {

void main(List<String> arguments) {
Frontier frontier = Frontier();
frontier.use(HeaderStrategy(HeaderOptions(key: 'auth', value: 'admin'), (options, result, done) async {
done(result);
}));
frontier.use(HeaderStrategy(HeaderOptions(key: 'auth', value: 'admin'),
(options, result, done) async {
done(result);
}));
HttpServer.bind(InternetAddress.loopbackIPv4, 8080).then((server) {
server.listen((HttpRequest request) {
final headers = <String, String>{};
request.headers.forEach((key, values) {
headers[key] = values.join(',');
});
frontier
.authenticate(
'Header',
StrategyRequest(headers: headers),
);
frontier.authenticate(
'Header',
StrategyRequest(headers: headers),
);
});
});
}
3 changes: 3 additions & 0 deletions packages/frontier/example/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# https://dart.dev/guides/libraries/private-files
# Created by `dart pub`
.dart_tool/
3 changes: 3 additions & 0 deletions packages/frontier/example/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## 1.0.0

- Initial version.
2 changes: 2 additions & 0 deletions packages/frontier/example/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
A sample command-line application with an entrypoint in `bin/`, library code
in `lib/`, and example unit test in `test/`.
30 changes: 30 additions & 0 deletions packages/frontier/example/analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# This file configures the static analysis results for your project (errors,
# warnings, and lints).
#
# This enables the 'recommended' set of lints from `package:lints`.
# This set helps identify many issues that may lead to problems when running
# or consuming Dart code, and enforces writing Dart using a single, idiomatic
# style and format.
#
# If you want a smaller set of lints you can change this to specify
# 'package:lints/core.yaml'. These are just the most critical lints
# (the recommended set includes the core lints).
# The core lints are also what is used by pub.dev for scoring packages.

include: package:lints/recommended.yaml

# Uncomment the following section to specify additional rules.

# linter:
# rules:
# - camel_case_types

# analyzer:
# exclude:
# - path/to/excluded/files/**

# For more information about the core and recommended set of lints, see
# https://dart.dev/go/core-lints

# For additional information about configuring this file, see
# https://dart.dev/guides/language/analysis-options
52 changes: 52 additions & 0 deletions packages/frontier/example/lib/example.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import 'dart:io';

import 'package:frontier/frontier.dart';

final class HeaderOptions extends StrategyOptions {
final String key;
final String value;

HeaderOptions({required this.key, required this.value});
}

class HeaderStrategy extends Strategy<HeaderOptions> {
HeaderStrategy(super.options, super.callback);

@override
String get name => 'Header';

@override
Future<void> authenticate(StrategyRequest request) async {
callback.call(options, request.headers[options.key] == options.value, done);
}
}

void main() {
final frontier = Frontier();

frontier.use(HeaderStrategy(HeaderOptions(key: 'auth', value: 'admin'),
(options, result, done) async {
done(result);
}));

final server = HttpServer.bind(InternetAddress.loopbackIPv4, 8080);

server.then((server) {
server.listen((HttpRequest request) async {
final headers = <String, String>{};
request.headers.forEach((key, values) {
headers[key] = values.join(',');
});
final result = await frontier.authenticate(
'Header',
StrategyRequest(headers: headers),
);
if (result) {
request.response.write('Authenticated');
} else {
request.response.statusCode = HttpStatus.unauthorized;
request.response.write('Not Authenticated');
}
});
});
}
Loading

0 comments on commit 243a249

Please sign in to comment.