在数据库中存储和维护JWT刷新令牌的最佳策略?

时间:2020-08-30 06:56:56

标签: authentication oauth jwt refresh-token

我有一个移动应用程序,该应用程序通过后端实现JWT身份验证。访问令牌是短暂的(1h),并且不存储在后端。现在获取刷新令牌:

  1. 如果刷新令牌到期,则意味着用户将被定期注销,从业务角度来看,这是非常不希望的,这可能会损害用户保留。有没有办法避免这种情况而不削弱安全性,例如使刷新令牌“永恒”?

  2. 什么是最好的存储和清理刷新令牌表的方法,以防止未使用的令牌积累?假设我具有以下表结构: import 'dart:math' as math; import 'package:flutter/material.dart'; const double degrees2Radians = math.pi / 180.0; void main() => runApp(MyApp()); class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( title: 'Flutter Demo', theme: ThemeData( primarySwatch: Colors.blue, ), home: Scaffold( body: SafeArea( child: MyHomePage(), ), ), ); } } class MyHomePage extends StatefulWidget { @override _MyHomePageState createState() => _MyHomePageState(); } class _MyHomePageState extends State<MyHomePage> { int _currentSliderValue = 2; List<Color> colors = [ Colors.blue, Colors.red, Colors.yellow, Colors.green, Colors.purple, Colors.orange, ]; @override Widget build(BuildContext context) { return Column( children: [ Expanded( child: Container( width: double.infinity, child: CustomPaint( painter: MyPainter( colors: colors.take(_currentSliderValue).toList(), ), ), ), ), Slider( value: _currentSliderValue.toDouble(), min: 2, max: 6, label: _currentSliderValue.toString(), onChanged: (value) { setState(() { _currentSliderValue = value.round(); }); }, ), ], ); } } class MyPainter extends CustomPainter { const MyPainter({ @required this.colors, }) : super(); final List<Color> colors; @override void paint(Canvas canvas, Size size) { var paths = <Color, Path>{}; colors.asMap().forEach((index, color) { var degree = 360 / colors.length * (index + 0.5); var radian = degree * degrees2Radians; var path = _onePath(radian, size); if (index > 0) { path = Path.combine( PathOperation.difference, path, paths[colors[index - 1]], ); } if (index == colors.length - 1) { paths[colors[0]] = Path.combine( PathOperation.reverseDifference, path, paths[colors[0]], ); } paths[color] = path; }); for (final color in colors) { var path = paths[color]; final fillPaint = Paint() ..color = color ..style = PaintingStyle.fill; canvas.drawPath(path, fillPaint); final strokePaint = Paint() ..color = Color(0xFFC4C4C4) ..style = PaintingStyle.stroke ..strokeCap = StrokeCap.butt ..strokeWidth = 10; canvas.drawPath(path, strokePaint); } } Path _onePath(double radian, Size size) { var circleSize = 150.0; var center = size.center(Offset.zero); var maxSize = math.min(size.height, size.width) / 4; var sin = math.sin(radian); var cos = math.cos(radian); var rect = Rect.fromLTWH( center.dx - circleSize / 2 + (sin * maxSize), center.dy - circleSize / 2 + (cos * maxSize), circleSize, circleSize, ); return Path() ..addRRect( RRect.fromRectAndRadius( rect, Radius.circular( rect.height / 2, ), ), ) ..close(); } @override bool shouldRepaint(CustomPainter oldDelegate) => true; } user_iddevice_id。如果策略是永不使刷新令牌失效,则使令牌失效的唯一方法是用户注销。但是,用户也可以出于任何原因删除应用程序,丢失或损坏设备,或更改其refresh_token。我能想到的一种解决方案是使用device_id时间戳记,该时间戳记允许在几个月不使用后使令牌失效。还有其他已知的把戏吗?

  3. 说在刷新访问令牌时除了刷新令牌以外,我还使用共享秘密字符串,我的理解是正确的,如果所有三个都被破坏(访问令牌,刷新令牌和共享密钥),我无能为力关于它? refreshed_at API调用的最佳做法是什么?

2 个答案:

答案 0 :(得分:1)

永久刷新令牌确实会增加风险。为了弥补这一点,您可以削弱刷新期间发出的令牌。为此,您可以在示波器上设置到期时间。然后,随着刷新的发生,新令牌的作用域越来越少。以后,如果用户尝试执行高风险操作,则他们将不得不再次登录并证明对原始凭据的控制权。我认为企业可能会忍受这一点。 (More on this idea可以在这里找到。)

您保留refreshed_at的想法可能是避免刷新令牌表永久增长的方法。但是,如果用户注销,您是否不能删除刷新令牌?将有一些注销端点,因此在此之后进行清理。这样,您的refreshed_at仅适用于在裂缝之间滑动的材料。

我看不到秘密字符串可以起到什么作用。从本质上讲,它是另一个“令牌”,并且都是不记名令牌,因此我将跳过这一点。有关保护威胁以防刷新流的更多信息,请签出section 4.5 of RFC 6819。另外,签出section 4.12 of the OAuth security BCP(这只是RFC)。

答案 1 :(得分:1)

基于过去与Ping Federate一起工作并使用了其实现的几点要补充:

  • 存储刷新令牌SHA256哈希而不是令牌本身,以便没有恶意的员工可以窃取和使用刷新令牌

  • 包括client_id和issue_at / expires_at字段。这使管理员可以按应用程序,用户和时间查找和撤消刷新令牌。

相关问题