redstone_jwt_plugin 0.0.2

  • README.md
  • Example
  • Installing
  • Versions
  • 36

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
    };
  }


}

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

Analysis

This feature is new.
We welcome feedback.
More details: scoring.

We analyzed this package, and provided a score, details, and suggestions below.

  • completed on Feb 3, 2018
  • Dart: 2.0.0-dev.20.0
  • pana: 0.10.1

Scores

Popularity:
Describes how popular the package is relative to other packages. [more]
0 / 100
Health:
Code health derived from static analysis. [more]
95 / 100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
39 / 100
Overall score:
Weighted score of the above. [more]
36

Platforms

Detected platforms: other

Primary library: package:redstone_jwt_plugin/redstone_jwt_plugin.dart with components: io, mirrors.

Suggestions

  • Maintain CHANGELOG.md.

    Changelog entries help clients to follow the progress in your code.

  • Use constrained dependencies.

    The pubspec.yaml contains 1 dependency without version constraints. Specify version ranges for the following dependencies: dart_jwt.

  • The description is too short.

    Add more detail about the package, what it does and what is its target use case. Try to write at least 60 characters.

  • Package is pre-v1 release.

    While there is nothing inherently wrong with versions of 0.*.*, it usually means that the author is still experimenting with the general direction API.

  • Use analysis_options.yaml.

    Rename old .analysis_options file to analysis_options.yaml.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=1.16.0 <2.0.0
dart_jwt any 0.6.0
redstone ^0.6.7 0.6.8
Transitive dependencies
analyzer 0.27.2 0.31.0+1
ansicolor 0.0.9
args 0.13.7 1.3.0
async 1.13.3 2.0.3
barback 0.15.2+14
bignum 0.0.7 0.1.0
boolean_selector 1.0.2
charcode 1.1.1
cipher 0.7.1
cli_util 0.0.1+2 0.1.2+1
code_transformers 0.3.1 0.5.1+4
collection 1.14.5
convert 2.0.1
crypto 2.0.2+1
csslib 0.13.7+1 0.14.1
di 3.3.10
fixnum 0.9.1+2 0.10.6
glob 1.1.5
grinder 0.8.0+3 0.8.1
html 0.12.2+2 0.13.2+2
http 0.11.3+16
http_multi_server 2.0.4
http_parser 3.1.1
http_server 0.9.7
io 0.3.2+1
js 0.6.1
logging 0.11.3+1
matcher 0.12.1+4
meta 1.1.2
mime 0.9.6
mockable_filesystem 0.0.3
multi_server_socket 1.0.1
node_preamble 1.4.0
package_config 0.1.5 1.0.3
package_resolver 1.0.2
path 1.5.1
plugin 0.1.0 0.2.0+2
pool 1.3.4
pub_semver 1.3.2
route_hierarchical 0.7.0
shelf 0.6.8 0.7.2
shelf_packages_handler 1.0.3
shelf_static 0.2.7
shelf_web_socket 0.2.2
source_map_stack_trace 1.1.4
source_maps 0.10.4
source_span 1.4.0
stack_trace 1.9.1
stream_channel 1.6.3
string_scanner 1.0.2
supports_color 0.1.1
term_glyph 1.0.0
test 0.12.30+1
typed_data 1.1.5
unscripted 0.6.2
utf 0.9.0+4
watcher 0.9.7+7
web_socket_channel 1.0.6
when 0.2.0
which 0.1.3
yaml 2.1.13