jaguar_auth_mongo 2.1.4

  • Readme
  • Changelog
  • Example
  • Installing
  • 53

jaguar_auth_mongo #

Jaguar AuthModelManager for MongoDb backend

Usage #

User model that implements AuthorizationUser.

class User implements AuthorizationUser {
  String id;

  String username;

  String password;

  User();

  User.make(this.id, this.username, this.password);

  String get authorizationId => id;
}

Mongo model manager to fetch and authenticate the user model.

final MgoUserManager<User> modelManager =
    new MgoUserManager<User>(userMgoSerializer);

Interface containing all the MongoDb and Authorizer interceptor creators.

abstract class BaseApi {
  /// Interceptor creator for authorization
  Authorizer authorizer(Context ctx) => new Authorizer(modelManager);

  /// Interceptor creator to connect to MongoDb
  MongoDb mongoDb(Context ctx) => new MongoDb('mongodb://localhost:27017/test');
}

Authorization routes.

/// This route group contains login and logout routes
@Api(path: '/auth')
@WrapOne(#mongoDb)
class AuthRoutes extends BaseApi {
  @Post(path: '/login')
  Future login(Context ctx) async {
    await BasicAuth.authenticate(ctx, modelManager);
  }

  @Post(path: '/logout')
  Future logout(Context ctx) async {
    // Clear session
    (await ctx.session).clear();
  }
}

Authorized routes.

/// Collection of routes students can also access
@Api(path: '/book')
@Wrap(const [#mongoDb, #authorizer])
class StudentRoutes extends BaseApi {
  @Get(path: '/all')
  Response<String> getAllBooks(Context ctx) {
    List<Map> ret =
        _books.values.map((Book book) => bookSerializer.toMap(book)).toList();
    return Response.json(ret);
  }

  @Get(path: '/:id')
  Response<String> getBook(Context ctx) {
    final String id = ctx.pathParams.get('id');
    final Book book = _books[id];
    return Response.json(bookSerializer.toMap(book));
  }
}

Changelog #

2.1.1 #

  • Architecture
  • Introducing MgoUserManager

example/example.dart

library example.basic_auth.server;

import 'dart:async';
import 'package:jaguar/jaguar.dart';
import 'package:jaguar_reflect/jaguar_reflect.dart';
import 'package:jaguar_auth/jaguar_auth.dart';
import 'package:jaguar_auth_mongo/jaguar_auth_mongo.dart';
import 'package:jaguar_mongo/jaguar_mongo.dart';

import 'model/model.dart';

final Map<String, Book> _books = {
  '0': new Book.make('0', 'Book0', 'Author0'),
  '1': new Book.make('1', 'Book1', 'Author1'),
  '2': new Book.make('2', 'Book2', 'Author2'),
};

// Mongo pool
final mongoPool = MongoPool('mongodb://localhost:27017/test');

/// Interceptor creator to connect to MongoDb
Future<void> mongoInterceptor(Context ctx) => mongoPool.injectInterceptor(ctx);

/// This route group contains login and logout routes
@Controller(path: '/auth')
@Intercept([mongoInterceptor])
class AuthRoutes {
  @Post(path: '/login')
  Future login(Context ctx) async {
    await BasicAuth.authenticate<User>(ctx);
  }

  @Post(path: '/logout')
  Future logout(Context ctx) async {
    await Authorizer.authorize<User>(ctx);
    // Clear session
    (await ctx.session).clear();
  }
}

/// Collection of routes students can also access
@Controller(path: '/book')
@Intercept([mongoInterceptor, Authorizer<User>()])
class StudentRoutes {
  @Get(path: '/all')
  Response<String> getAllBooks(Context ctx) {
    List<Map> ret =
        _books.values.map((Book book) => bookSerializer.toMap(book)).toList();
    return Response.json(ret);
  }
}

@Controller(path: '/api')
class LibraryApi {
  @IncludeHandler()
  final auth = AuthRoutes();

  @IncludeHandler()
  final student = StudentRoutes();
}

main() async {
  final server = new Jaguar(port: 10000);
  server.userFetchers[User] = MgoUserManager<User>(userMgoSerializer);
  server.add(reflect(LibraryApi()));
  server.log.onRecord.listen(print);
  await server.serve(logRequests: true);
}

Use this package as a library

1. Depend on it

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


dependencies:
  jaguar_auth_mongo: ^2.1.4

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter pub get

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

3. Import it

Now in your Dart code, you can use:


import 'package:jaguar_auth_mongo/jaguar_auth_mongo.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
20
Health:
Code health derived from static analysis. [more]
99
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
69
Overall:
Weighted score of the above. [more]
53
Learn more about scoring.

We analyzed this package on Oct 8, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.5.1
  • pana: 0.12.21

Platforms

Detected platforms: Flutter, other

Primary library: package:jaguar_auth_mongo/jaguar_auth_mongo.dart with components: io.

Health suggestions

Fix lib/src/auth_mongo.dart. (-1 points)

Analysis of lib/src/auth_mongo.dart reported 2 hints:

line 21 col 23: Use = to separate a named parameter from its default value.

line 21 col 48: Use = to separate a named parameter from its default value.

Maintenance issues and suggestions

Support latest dependencies. (-10 points)

The version constraint in pubspec.yaml does not support the latest published versions for 1 dependency (mongo_dart).

Package is getting outdated. (-16.44 points)

The package was last published 60 weeks ago.

Homepage URL is insecure. (-5 points)

Update the homepage field and use a secure (https) URL.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0 <3.0.0
auth_header ^2.1.1 2.1.4
jaguar ^2.1.37 2.4.36
jaguar_auth ^2.1.2 2.4.4
jaguar_common ^2.1.1 2.1.4
jaguar_mongo ^2.1.1 2.4.1
jaguar_serializer ^2.2.0 2.2.12
mongo_dart ^0.3.2 0.3.6 0.4.0-dev.1
Transitive dependencies
async 2.4.0
bson 0.3.2
charcode 1.1.2
collection 1.14.12
conn_pool 2.1.9
convert 2.1.1
crypto 2.1.3
fixnum 0.10.9
http_server 0.9.8+3
logging 0.11.3+2
meta 1.1.7
mime 0.9.6+3
mongo_dart_query 0.3.2 0.4.1
more 1.18.0
path 1.6.4
path_tree 2.2.2
pool 1.4.0
stack_trace 1.9.3
typed_data 1.1.6
uuid 2.0.2
Dev dependencies
build_runner ^0.10.0
jaguar_reflect ^2.1.6
jaguar_serializer_cli ^2.2.0
jaguar_serializer_mongo ^2.1.1
test ^1.3.0