changePassword method

Future<void> changePassword({
  1. required String oldPassword,
  2. required String newPassword,
})

Change the password for the connected user.

Updates the user's password on the Oracle server. The connection remains active with the new password.

Parameters:

  • oldPassword: Current password for verification
  • newPassword: New password to set

Example - Password Change:

try {
  await connection.changePassword(
    oldPassword: 'current_pass123',
    newPassword: 'new_secure_pass456!',
  );
  print('Password changed successfully');
} on OracleConnectionException catch (e) {
  if (e.errorMessage?.contains('ORA-28007') ?? false) {
    print('Password cannot be reused');
  } else if (e.errorMessage?.contains('ORA-28003') ?? false) {
    print('Old password is incorrect');
  } else {
    print('Password change failed: ${e.errorMessage}');
  }
}

Example - Forced Password Expiry:

// User's password has expired (ORA-28001)
try {
  connection = await OracleConnection.connect(...);
} on OracleConnectionException catch (e) {
  if (e.errorMessage?.contains('ORA-28001') ?? false) {
    // Connect with expired password flag and change it
    await connection.changePassword(
      oldPassword: expiredPassword,
      newPassword: newPassword,
    );
  }
}

Security Note: Ensure new passwords meet Oracle's password policy requirements (complexity, length, history, etc.).

See also:

Implementation

Future<void> changePassword({
  required String oldPassword,
  required String newPassword,
}) async {
  _ensureConnected();

  final (oldPwdNative, oldPwdLen) = StringUtils.toNativeUtf8WithLength(oldPassword, _memoryManager);
  final (newPwdNative, newPwdLen) = StringUtils.toNativeUtf8WithLength(newPassword, _memoryManager);
  final (usernameNative, usernameLen) = StringUtils.toNativeUtf8WithLength(_config.username, _memoryManager);

  final result = _dpiOracle.dpiConn_changePassword(
    _connectionPtr.value,
    usernameNative,
    usernameLen,
    oldPwdNative,
    oldPwdLen,
    newPwdNative,
    newPwdLen,
  );

  if (result == DPI_FAILURE) {
    final errorInfo = _memoryManager.allocate<dpiErrorInfo>(sizeOf<dpiErrorInfo>());
    _dpiOracle.dpiContext_getError(_context, errorInfo);
    final errorMsg = StringUtils.fromNativeUtf8(errorInfo.ref.message.cast<Char>());

    throw OracleConnectionException(
      'Failed to change password',
      errorMessage: errorMsg,
    );
  }
}