redstone_jwt_plugin 0.0.2

  • README.md
  • Example
  • Installing
  • Versions
  • 0

redstone_jwt_plugin

example/main.dart

import "dart:async";
import "dart:io";
import 'package:redstone/redstone.dart' as Redstone;
import "package:di/di.dart";
import "package:redstone_jwt_plugin/redstone_jwt_plugin.dart";

main() {
  Redstone.showErrorPage = false;

  Redstone.addModule(new Module()..bind(AuthService));

  /// Add RedstoneJwtPlugin
  Redstone.addPlugin(RedstoneJwtPlugin(AuthService));

  Redstone.start();

}

const String BasicUser = "basic";
const String ProUser = "pro";

const List<String> Audience = const [ BasicUser, ProUser ];

List<User> users = <User>[];
int idGenerator = 0;

class User {
  String username;
  String password;
  List<String> rights;
  int id = idGenerator++;
}

@Redstone.Group("/auth")
class AuthService extends JwtProvider with AuthenticationProvider {

  /// Token Name in the request header
  @override
  String get TokenName => "MyToken";

  /// Use to salt the password of an User
  @override
  String get PasswordSalt => "MyPasswordSalt";

  @override
  Duration get TokenDuration => const Duration(days: 7);

  /// Use to encrypt the JwtToken
  @override
  String get TokenSecret => "MyTokenSecret";

  @Redstone.Route("/login", methods: const [ Redstone.POST])
  Future<Map<String, dynamic>> login(@Redstone.Body(Redstone.JSON) Map body) async {
    String username = body["username"];
    String password = hashPassword(body["password"]);

    User user = users.firstWhere((User usr) => usr.username == username && usr.password == password, orElse: () => null);
    if (user == null) {
      throw new Redstone.ErrorResponse(HttpStatus.UNAUTHORIZED, {"error": "Authentication error"});
    }
    return {"token": issueToken("issuer:RedstoneJwtPlugin", username, audience: user.rights)};
  }

  @Redstone.Route("/register", methods: const [ Redstone.POST])
  register(@Redstone.Body(Redstone.JSON) Map body) {
    String username = body["username"];
    String password = hashPassword(body["password"]);
    String right = body["right"];
    if (!Audience.contains(right)) {
      right = BasicUser;
    }

    User user = new User();
    user.username = username;
    user.password = password;
    user.rights = [ right ];

    User foundUser = users.firstWhere((User usr) => usr.username == username, orElse: () => null);
    if (foundUser != null) {
      throw new Redstone.ErrorResponse(HttpStatus.BAD_REQUEST, {"error": "Username already exist"});
    }
    users.add(user);
  }

  @AuthRequire(audience: Audience) /// Every auth User can access to this route
  @Redstone.Route('/connectedUser', methods: const [ Redstone.GET])
  Future<Map<String, dynamic>> connectedUser(@DecodeToken() Map token) async {
    User user = users.firstWhere((User usr) => usr.username == token["sub"], orElse: () => null);
    if (user == null) {
      throw new Redstone.ErrorResponse(HttpStatus.BAD_REQUEST, {"error": "User does not exist"});
    }
    return {
      "username": user.username,
      "id": user.id,
      "rights": user.rights
    };
  }

  @AuthRequire(audience: const [ ProUser ]) /// Every auth User can access to this route
  @Redstone.Route('/connectedProUser', methods: const [ Redstone.GET])
  Future<Map<String, dynamic>> connectedProUser(@DecodeToken() Map token) async {
    User user = users.firstWhere((User usr) => usr.username == token["sub"], orElse: () => null);
    if (user == null) {
      throw new Redstone.ErrorResponse(HttpStatus.BAD_REQUEST, {"error": "User does not exist"});
    }
    return {
      "username": user.username,
      "id": user.id,
      "rights": user.rights
    };
  }


}

Use this package as a library

1. Depend on it

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


dependencies:
  redstone_jwt_plugin: ^0.0.2

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:redstone_jwt_plugin/redstone_jwt_plugin.dart';
  
Version Uploaded Documentation Archive
0.0.2 Aug 8, 2016 Go to the documentation of redstone_jwt_plugin 0.0.2 Download redstone_jwt_plugin 0.0.2 archive
0.0.1 Jul 19, 2016 Go to the documentation of redstone_jwt_plugin 0.0.1 Download redstone_jwt_plugin 0.0.1 archive
Popularity:
Describes how popular the package is relative to other packages. [more]
0
Health:
Code health derived from static analysis. [more]
0
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
0
Overall:
Weighted score of the above. [more]
0
Learn more about scoring.

The package version is not analyzed, because it does not support Dart 2. Until this is resolved, the package will receive a health and maintenance score of 0.

Issues and suggestions

Support Dart 2 in pubspec.yaml.

The SDK constraint in pubspec.yaml doesn't allow the Dart 2.0.0 release. For information about upgrading it to be Dart 2 compatible, please see https://www.dartlang.org/dart-2#migration.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=1.16.0 <2.0.0