shelf_auth_session 0.4.5

shelf_auth_session #

Build Status Pub Version

Session support for shelf_auth

TODO: ideally this would be merged or at least share code with shelf_simple_session

Usage #

Coming ...

Features and bugs #

Please file feature requests and bugs at the issue tracker.

Changelog #

0.4.0 #

  • upgrade option, shelf and shelf_auth versions

0.3.0 #

  • require shelf_auth 0.5.0

0.2.0 #

  • upgrade shelf_auth dependency

0.0.1 #

  • Initial version, created by Stagehand.


// Copyright (c) 2014, Anders Holmgren. All rights reserved. Use of this source code
// is governed by a BSD-style license that can be found in the LICENSE file.

library shelf_auth_session.example;

import 'package:shelf/shelf.dart';
import 'package:shelf/shelf_io.dart' as io;
import 'package:shelf_auth/shelf_auth.dart';
import 'package:shelf_exception_handler/shelf_exception_handler.dart';
import 'dart:async';
import 'package:option/option.dart';
import 'package:logging/logging.dart';
import 'package:shelf_auth_session/shelf_auth_session.dart';
import 'package:uuid/uuid.dart';
import 'package:http_exception/http_exception.dart';

void main() {
  Logger.root.level = Level.FINER;
  Logger.root.onRecord.listen((lr) {
    print('${lr.time} ${lr.level} ${lr.message}');

  var authMiddleware = (builder()
      .authenticator(new RandomAuthenticator())
      .jwtSession('moi', 'ssh', testLookup)..allowHttp = true).build();

  var handler = const Pipeline()
      .addMiddleware(sessionMiddleware(new InMemorySessionRepository()))

  io.serve(handler, 'localhost', 8080).then((server) {
    print('Serving at http://${}:${server.port}');

Response _fooHandler(Request request) {
  final s = session(request).get();
  final currCount = s['count'];
  final count = currCount == null ? 0 : currCount + 1;
  s['count'] = count;
  return new Response.ok("I'm in with "
      "${authenticatedContext().map((ac) =>}: "
      "count is $count\n");

class RandomAuthenticator extends Authenticator {
  bool approve = true;
  bool readsBody = false;

  Future<Option<AuthenticatedContext>> authenticate(Request request) async {
    approve = !approve;
    final now = new;

    if (approve) {
      return new Some(new SessionAuthenticatedContext(new Principal("fred"),
          new Uuid().v4(), now, now, now.add(const Duration(days: 10))));
    } else {
      throw new UnauthorizedException();

Future<Option<Principal>> testLookup(String username) {
  return new Future.value(new Some(new Principal(username)));

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:

  shelf_auth_session: ^0.4.5

2. Install it

You can install packages from the command line:

with pub:

$ pub get

Alternatively, your editor might support pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:

import 'package:shelf_auth_session/shelf_auth_session.dart';
Awaiting analysis to complete.