createTokens static method

Future<TokenPair> createTokens(
  1. Session session, {
  2. required UuidValue authUserId,
  3. required Set<Scope> scopes,
  4. Map<String, dynamic>? extraClaims,
  5. Transaction? transaction,
})

Creates a new token pair for the given auth user.

This is akin to creating a new session, and should be used after a successful login or registration.

Implementation

static Future<TokenPair> createTokens(
  final Session session, {
  required final UuidValue authUserId,
  required final Set<Scope> scopes,

  /// Extra claims to be added to the JWT.
  ///
  /// These are added on the top level of the paylaod, so be sure not to conflict with the [registered claims](https://datatracker.ietf.org/doc/html/rfc7519#section-4.1),
  /// as those will always overwrite any custom claims given here.
  ///
  /// These claims will be embedded in every access token (also across rotations) and then sent along with any request. This should be taken into account with regard to the total size of the added claims.
  final Map<String, dynamic>? extraClaims,
  final Transaction? transaction,
}) async {
  final secret = _generateRefreshTokenRotatingSecret();
  final newHash = await _refreshTokenSecretHash.createHash(secret: secret);

  final refreshToken = await RefreshToken.db.insertRow(
    session,
    RefreshToken(
      authUserId: authUserId,
      fixedSecret: ByteData.sublistView(_generateRefreshTokenFixedSecret()),
      rotatingSecretHash: ByteData.sublistView(newHash.hash),
      rotatingSecretSalt: ByteData.sublistView(newHash.salt),
      scopeNames: scopes.names,
      extraClaims: extraClaims != null ? jsonEncode(extraClaims) : null,
      createdAt: clock.now(),
      lastUpdatedAt: clock.now(),
    ),
    transaction: transaction,
  );

  return TokenPair(
    refreshToken: RefreshTokenString.buildRefreshTokenString(
      refreshToken: refreshToken,
      rotatingSecret: secret,
    ),
    accessToken: _jwtUtil.createJwt(refreshToken),
  );
}