jaguar_jwt 0.1.0 jaguar_jwt: ^0.1.0 copied to clipboard
JWT interceptor and utilities for Jaguar
example/jaguar_jwt_example.dart
// Copyright (c) 2016, Ravi Teja Gudapati. All rights reserved. Use of this source code
// is governed by a BSD-style license that can be found in the LICENSE file.
library jwt_example;
import 'dart:async';
import 'dart:io';
import 'package:jaguar/jaguar.dart';
import 'package:dart_jwt/dart_jwt.dart';
import 'package:jaguar_jwt/jaguar_jwt.dart';
import 'custom_payload.dart';
part 'jaguar_jwt_example.g.dart';
const String kSecretToken = "myScret";
@Api()
class ExampleJwt extends _$JaguarExampleJwt implements RequestHandler {
// This route will generate basic token with an issuer, a subject and an audience
@Get(path: '/token')
Future<String> token() async {
JwtManager jwtHelper = new JwtManager(
new JwtInfo(kSecretToken, tokenDuration: new Duration(seconds: 30)));
String token = jwtHelper.issueToken('example', "toto",
audience: <String>['department_of_finance']);
return token;
}
// This route will raised an Exception if the token is not valid
// else it will response "you are authenticate"
// with all the information about you token
@Post(path: '/needAuth')
@JwtOnAuthHeader(
const JwtInfo(kSecretToken), const <String>['department_of_finance'])
@Input(JwtOnAuthHeader)
Future<String> needAuth(Map jwtInfo) async {
return "you are authenticate $jwtInfo";
}
@Get(path: '/customToken')
Future<String> customToken() async {
JwtHelper jwtHelper = new JwtHelper(
new JwtInfo(kSecretToken, tokenDuration: new Duration(seconds: 30)));
String token = jwtHelper.issuerCustomToken(new CustomPayloadClaimSet(
'toto', 'tata', const <String>['DG'], "Hello DG"));
return token;
}
@Post(path: '/needCustomAuth')
@InputHeader(HttpHeaders.AUTHORIZATION)
Future<String> needCustomAuth(String auth) async {
final CustomPayloadValidationContext customPayload =
new CustomPayloadValidationContext(
kSecretToken,
new CustomPayloadClaimSetValidationContext(
'tata', const <String>['DG']));
try {
JsonWebToken jwt = new JwtInJws<CustomPayloadClaimSet>.decode(
auth, customPayload, customPayloadClaimSetParser);
return "you are authenticate ! payload is ${jwt.claimSet.payload}";
} on ConstraintViolations catch (e) {
return "Error: ${e.summaryMessage}";
}
}
}
main() {
ExampleJwt exampleJwt = new ExampleJwt();
Configuration config = new Configuration();
config.addApi(exampleJwt);
serve(config);
}