shelf_enforces_ssl 1.2.1 copy "shelf_enforces_ssl: ^1.2.1" to clipboard
shelf_enforces_ssl: ^1.2.1 copied to clipboard

Enforces that users can only make API requests over HTTPS (SSL).

example/README.md

Example #

Add the Middleware your Shelf API #

by simply adding:

.addMiddleware(enforceSSL()),
copied to clipboard

you can define a custom error Response by setting the errorResponse parameter:

.addMiddleware(
    enforceSSL(
        errorResponse: Response(
            403,
            body: 'Only use HTTPS when you try to send data to this API',
        ),
    ),
),
copied to clipboard

you can also add use the Middleware in DartFrog by doing the following:

Handler maxContentLengthValidator(Handler handler) {
    return handler.use(fromShelfMiddleware(enforceSSl()));
}
copied to clipboard

Full example #

import 'package:content_length_validator/content_length_validator.dart';
import 'package:shelf/shelf.dart';
import 'package:shelf/shelf_io.dart' as shelf_io;

void main() async {
  final handler = const Pipeline()
      .addMiddleware(
        enforceSSL(
            errorResponse: Response(
                403,
                body: 'Only use HTTPS when you try to send data to this API',
            ),
        ),
    ),
    .addMiddleware(logRequests())
    .addHandler(_echoRequest);

  final server = await shelf_io.serve(handler, 'localhost', 8080);

  // Enable content compression
  server.autoCompress = true;

  print('Serving at http://${server.address.host}:${server.port}');
}

Response _echoRequest(Request request) => Response.ok('Request for "${request.url}"');
copied to clipboard
9
likes
160
points
97
downloads

Publisher

verified publisherjxstxn.dev

Weekly Downloads

2024.08.26 - 2025.03.10

Enforces that users can only make API requests over HTTPS (SSL).

Homepage
Repository (GitHub)
View/report issues

Topics

#shelf #dart-frog #middleware #ssl #https

Documentation

API reference

License

Apache-2.0 (license)

Dependencies

shelf

More

Packages that depend on shelf_enforces_ssl