56 lines
1.6 KiB
Dart
56 lines
1.6 KiB
Dart
import 'package:backend/database.dart';
|
|
import 'package:backend/service/db_access.dart';
|
|
import 'package:backend/utils/environment.dart';
|
|
import 'package:dart_jsonwebtoken/dart_jsonwebtoken.dart';
|
|
import 'package:logging/logging.dart';
|
|
import 'package:shared_models/jwt.dart';
|
|
import 'package:shared_models/user.dart';
|
|
|
|
final jwtSecret = getJWTSecret();
|
|
const expTimeSecs = 3600;
|
|
|
|
final log = Logger('Authenticator');
|
|
|
|
enum JWTTokenStatus {
|
|
valid,
|
|
expired,
|
|
invalid,
|
|
}
|
|
|
|
class Authenticator {
|
|
Future<(String?, User?)> generateToken(CreateUserRequest req) async {
|
|
final newUser = await Db.createUser(username: req.username, roomCode: req.roomCode);
|
|
if (newUser == null) return (null, null);
|
|
|
|
final iat = DateTime.now().millisecondsSinceEpoch ~/ 1000;
|
|
final jwt = JWT(
|
|
JWTBody(uuid: newUser.uuid, roomUuid: newUser.gameRoomUuid, iat: iat, exp: iat + expTimeSecs).toJson(),
|
|
);
|
|
|
|
return (jwt.sign(SecretKey(jwtSecret!)), newUser);
|
|
}
|
|
|
|
Future<(User?, JWTTokenStatus)> verifyToken(
|
|
String token,
|
|
) async {
|
|
try {
|
|
log.info('Verifying jwt: ${token.substring(0, 10)}...${token.substring(token.length - 10)}');
|
|
final payload = JWT.verify(
|
|
token,
|
|
SecretKey(jwtSecret!),
|
|
);
|
|
|
|
final jwt = JWTBody.fromJson(payload.payload as Map<String, dynamic>);
|
|
|
|
if (jwt.iat + expTimeSecs != jwt.exp || DateTime.now().millisecondsSinceEpoch ~/ 1000 > jwt.exp) {
|
|
return (null, JWTTokenStatus.expired);
|
|
}
|
|
|
|
return (await Db.getUserById(jwt.uuid), JWTTokenStatus.valid);
|
|
} catch (e) {
|
|
log.fine('Error verifying token', e);
|
|
return (null, JWTTokenStatus.invalid);
|
|
}
|
|
}
|
|
}
|