QtPass 2.0.0
Developer documentation
Loading...
Searching...
No Matches
FileContent Class Reference

Represents the parsed contents of a password file. More...

#include <filecontent.h>

Collaboration diagram for FileContent:

Public Member Functions

auto getPassword () const -> QString
auto getPasswordForDisplay () const -> QString
 Like getPassword but empty when the password line is itself an otpauth URI.
auto getNamedValues () const -> NamedValues
auto getRemainingData () const -> QString
auto getRemainingDataForDisplay () const -> QString
 Like getRemainingData but without data that should not be displayed (like a TOTP secret).
auto getRemainingLineCount () const -> qsizetype
 How many lines getRemainingData() holds.
auto getOtpUri () const -> QString
 The entry's raw one-time password configuration, if it has one.

Static Public Member Functions

static auto boundedForDisplay (const QString &text) -> QString
 text cut to kMaxDisplayLines lines, as getRemainingDataForDisplay() is: for showing an entry as it is (displayAsIs).
static auto parse (const QString &fileContent, const QStringList &templateFields, bool allFields, const std::function< bool(const QString &)> &alsoField={}) -> FileContent
 parse parses the given fileContent in a FileContent object. The password is accessible through getPassword. The named value pairs (name: value) are parsed and depending on the templateFields and allFields parameters accessible through getNamedValues, getRemainingData or getRemainingDataForDisplay.
static auto isOtpFieldName (const QString &name) -> bool
 Whether a field name designates the one-time password field.
static auto isSecretFieldName (const QString &name, const QStringList &secrets) -> bool
 Whether a field name names a secret: one of secrets occurs in it as whole words, compared case-insensitively ("key" matches "API key" and "Recovery key", not "keyboard layout").
static auto isOtpUriValue (const QString &value) -> bool
 Whether a field value is an otpauth URI, and therefore a secret.

Static Public Attributes

static constexpr int kMaxNamedFields = 1000
 Most named fields parse() takes from one entry.
static constexpr int kMaxDisplayLines = 10000
 Most lines of remaining text getRemainingDataForDisplay() gives.

Private Member Functions

 FileContent (QString password, NamedValues namedValues, QString remainingData, QString remainingDataDisplay, QString otpUri, qsizetype remainingLines)
 Assemble a parsed entry; only parse() calls this.

Private Attributes

QString password
 The first line of the entry, verbatim.
NamedValues namedValues
 Fields in order of appearance, template fields first.
QString remainingData
 Body lines that are not fields, joined with newlines; complete.
QString remainingDataDisplay
QString otpUri
 Raw OTP configuration (URI or bare base32), or empty.
qsizetype remainingLines = 0
 Number of lines in remainingData.

Detailed Description

Represents the parsed contents of a password file.

Splits the file into a password line, named key/value fields, and remaining data. Use FileContent::parse to construct an instance.

Definition at line 92 of file filecontent.h.

Constructor & Destructor Documentation

◆ FileContent()

FileContent::FileContent ( QString password,
NamedValues namedValues,
QString remainingData,
QString remainingDataDisplay,
QString otpUri,
qsizetype remainingLines )
private

Assemble a parsed entry; only parse() calls this.

Parameters
passwordThe first line.
namedValuesThe fields promoted out of the body.
remainingDataThe body lines that did not become fields.
remainingDataDisplayremainingData without secret-bearing lines, cut at kMaxDisplayLines.
otpUriThe OTP configuration found, or empty.
remainingLinesLine count of remainingData.

Definition at line 289 of file filecontent.cpp.

Here is the caller graph for this function:

Member Function Documentation

◆ boundedForDisplay()

auto FileContent::boundedForDisplay ( const QString & text) -> QString
staticnodiscard

text cut to kMaxDisplayLines lines, as getRemainingDataForDisplay() is: for showing an entry as it is (displayAsIs).

Parameters
textThe text to show.
Returns
text when it is short enough, else its first kMaxDisplayLines lines and one saying how many more there are.

Definition at line 297 of file filecontent.cpp.

Here is the caller graph for this function:

◆ getNamedValues()

auto FileContent::getNamedValues ( ) const -> NamedValues
nodiscard
Returns
the named values in the file in the order of appearance, with template values first.

Definition at line 275 of file filecontent.cpp.

Here is the caller graph for this function:

◆ getOtpUri()

auto FileContent::getOtpUri ( ) const -> QString
nodiscard

The entry's raw one-time password configuration, if it has one.

Looked up in this order:

  1. the value of the first field whose name satisfies isOtpFieldName();
  2. the first bare otpauth:// line in the body, which is how the pass-otp extension stores it;
  3. the value of the first OTP: line that was not promoted to a named value, which is what happens when the template does not list OTP.

The string is returned verbatim and is not validated here: it may be an otpauth:// URI or a bare base32 secret. Totp::parse() does the validation.

Returns
The OTP configuration string, or an empty string when the entry has none.

Definition at line 287 of file filecontent.cpp.

Here is the caller graph for this function:

◆ getPassword()

auto FileContent::getPassword ( ) const -> QString
nodiscard
Returns
the password from the parsed file.

Definition at line 269 of file filecontent.cpp.

Here is the caller graph for this function:

◆ getPasswordForDisplay()

auto FileContent::getPasswordForDisplay ( ) const -> QString
nodiscard

Like getPassword but empty when the password line is itself an otpauth URI.

pass otp insert writes the URI as the entry's only line, so it lands in the password position. It is a shared secret, not a password: it must never be rendered or placed on the clipboard. getPassword() still returns it so the edit dialog round-trips the file unchanged.

Returns
The password, or an empty string when it is an otpauth URI.

Definition at line 271 of file filecontent.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ getRemainingData()

auto FileContent::getRemainingData ( ) const -> QString
nodiscard
Returns
the data that is not the password and not in getNamedValues.

Definition at line 279 of file filecontent.cpp.

Here is the caller graph for this function:

◆ getRemainingDataForDisplay()

auto FileContent::getRemainingDataForDisplay ( ) const -> QString
nodiscard

Like getRemainingData but without data that should not be displayed (like a TOTP secret).

Returns
Remaining data string safe for display.

Definition at line 283 of file filecontent.cpp.

Here is the caller graph for this function:

◆ getRemainingLineCount()

auto FileContent::getRemainingLineCount ( ) const -> qsizetype
nodiscard

How many lines getRemainingData() holds.

Returns
The line count; 0 when there is no remaining text.

Definition at line 311 of file filecontent.cpp.

◆ isOtpFieldName()

auto FileContent::isOtpFieldName ( const QString & name) -> bool
staticnodiscard

Whether a field name designates the one-time password field.

Parameters
nameField name as written in the file; surrounding whitespace is ignored.
Returns
true for "OTP" or "TOTP", compared case-insensitively.

Definition at line 217 of file filecontent.cpp.

Here is the caller graph for this function:

◆ isOtpUriValue()

auto FileContent::isOtpUriValue ( const QString & value) -> bool
staticnodiscard

Whether a field value is an otpauth URI, and therefore a secret.

A URI is a shared secret whatever the field is called, so both the display-suppression and the OTP lookup key off the value as well as the name; suppressing only OTP:/TOTP: leaked 2fa: and friends.

Parameters
valueField value; surrounding whitespace is ignored.
Returns
true when it starts with "otpauth://", compared case-insensitively.

Definition at line 264 of file filecontent.cpp.

Here is the caller graph for this function:

◆ isSecretFieldName()

auto FileContent::isSecretFieldName ( const QString & name,
const QStringList & secrets ) -> bool
staticnodiscard

Whether a field name names a secret: one of secrets occurs in it as whole words, compared case-insensitively ("key" matches "API key" and "Recovery key", not "keyboard layout").

Parameters
nameField name as written in the file.
secretsThe secret field names (AppSettings::secretFieldNames).
Returns
true when one of them occurs in name.

Definition at line 223 of file filecontent.cpp.

Here is the caller graph for this function:

◆ parse()

auto FileContent::parse ( const QString & fileContent,
const QStringList & templateFields,
bool allFields,
const std::function< bool(const QString &)> & alsoField = {} ) -> FileContent
static

parse parses the given fileContent in a FileContent object. The password is accessible through getPassword. The named value pairs (name: value) are parsed and depending on the templateFields and allFields parameters accessible through getNamedValues, getRemainingData or getRemainingDataForDisplay.

Parameters
fileContentthe file content to parse.
templateFieldsthe fields in the template. Fields in the template will always be in getNamedValues() at the beginning of the list in the same order.
allFieldswhether all fields should be considered as named values. If set to false only templateFields are returned in getNamedValues().
alsoFielda field besides those, by its trimmed name: a secret the panel hides, which must not be left in the remaining text in plain view. Empty: none.
Returns
A FileContent instance with password, named values, and remaining data populated from the parsed input.

Definition at line 132 of file filecontent.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ kMaxDisplayLines

int FileContent::kMaxDisplayLines = 10000
staticconstexpr

Most lines of remaining text getRemainingDataForDisplay() gives.

The text browser makes a block, with its layout, per line. Past this many the display text ends with one line saying how many more there are; getRemainingData() keeps them all, so saving loses nothing. The edit dialog uses the same number for the whole entry (password and fields included) and does not load one with more lines.

Definition at line 114 of file filecontent.h.

◆ kMaxNamedFields

int FileContent::kMaxNamedFields = 1000
staticconstexpr

Most named fields parse() takes from one entry.

Every field becomes a row of widgets in the display panel. An entry is compressed before it is encrypted, so a small one in a shared store could decompress into millions of "a: b" lines within the plaintext limit; lines past this many fields stay in the remaining text. They are still looked at for an OTP configuration, as fields would be.

Definition at line 103 of file filecontent.h.

◆ namedValues

NamedValues FileContent::namedValues
private

Fields in order of appearance, template fields first.

Definition at line 262 of file filecontent.h.

◆ otpUri

QString FileContent::otpUri
private

Raw OTP configuration (URI or bare base32), or empty.

Definition at line 268 of file filecontent.h.

◆ password

QString FileContent::password
private

The first line of the entry, verbatim.

Definition at line 260 of file filecontent.h.

◆ remainingData

QString FileContent::remainingData
private

Body lines that are not fields, joined with newlines; complete.

Definition at line 264 of file filecontent.h.

◆ remainingDataDisplay

QString FileContent::remainingDataDisplay
private

remainingData for the panel: OTP-bearing lines and hidden secrets left out, at most kMaxDisplayLines lines plus a "more" line.

Definition at line 264 of file filecontent.h.

◆ remainingLines

qsizetype FileContent::remainingLines = 0
private

Number of lines in remainingData.

Definition at line 270 of file filecontent.h.


The documentation for this class was generated from the following files: