Class FailedSignInStore

java.lang.Object
com.iizix.server.user.FailedSignInStore

public final class FailedSignInStore extends Object
The failed attempts to sign in to a known account (CC-SIGNIN-FAILED-ATTEMPTS, order 561): one row per attempt that named an existing user and was refused - at the password, at the second factor, at a passkey, at a recovery code, by a policy or by the license - with the time, the client address and its place, the device and the application, and what the server mailed the user about it. The security events of the same attempts (SecurityEventStore) are the server's audit and stay; this table is the USER'S list: shown on the account page and in ServerAdmin's "Devices and security", cleared by the user, by an administrator, or at a successful sign-in when the user chose so, and gone with the user (cascade).

The mail bookkeeping. mail_kind says what became of an attempt: NULL while a mail about it is still due (collected for a batch, or held past the hourly cap of the warning mails), MAIL_NOTICE when mailed at once, MAIL_BATCH when listed in a batched mail, MAIL_WARNING when mailed as a warning at once, MAIL_SUMMARY when listed in the summary that follows the cap, MAIL_NONE when no mail is wanted or possible. mailed_at is when. The throttle and the cap are computed from these columns (FailedSignInNotifier), so they survive a restart of the server: nothing about them is kept in memory only. urgent marks the attempts that are mailed every time (past the password, or on a sensitive account).

The table IZS.FAILED_SIGNINS is created here when it is absent, as DeviceTokenStore creates its own. Where it cannot be created the store is unavailable: nothing is kept and nothing is mailed, the log line of the attempt is written anyway, and the sign-in is as before. Rows go after $signIn.signInRecordDays days (purge(int)).

SQL-92, Derby, PostgreSQL and MySQL. Thread-safe: every method takes its own connection.

Author:
Christopher Mindus
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static final record 
    One attempt, as written and as read.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final String
    What became of the mail about an attempt (mail_kind); NULL while a mail is due.
    static final String
    What became of the mail about an attempt (mail_kind); NULL while a mail is due.
    static final String
    What became of the mail about an attempt (mail_kind); NULL while a mail is due.
    static final String
    What became of the mail about an attempt (mail_kind); NULL while a mail is due.
    static final String
    What became of the mail about an attempt (mail_kind); NULL while a mail is due.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Constructs the store on the system clock.
    Constructs the store on a clock.
  • Method Summary

    Modifier and Type
    Method
    Description
    int
    clear(long uid)
    Deletes every attempt of a user: the user's "Clear", the administrator's, and the clearing at a successful sign-in.
    int
    count(long uid)
    Counts the attempts of a user.
    boolean
    Creates the table when it is absent.
    long
    lastMailedAt(long uid, String... kinds)
    Gets when a user was last mailed with a mail of some kinds.
    list(long uid, int limit)
    Lists the attempts of a user, the newest first.
    mailedSince(long uid, long since, String... kinds)
    Lists when a user was mailed with a mail of some kinds AFTER a time, the oldest first: the cap of the warning mails is counted on it (a mail at exactly the time is outside the window, so a window that ends where a mail was sent frees the mail's slot).
    int
    markMailed(List<Long> ids, String mailKind, long mailedAt)
    Records what became of the mail about attempts: the kind and the time.
    pending(long uid, boolean urgent)
    Lists the attempts of a user about which a mail is still due, the oldest first.
    Lists the users about whom a mail is still due: after a restart, what the notifier takes up.
    int
    purge(int days)
    Deletes the attempts older than a number of days: the daily purge.
    Writes an attempt.
  • Field Details

    • MAIL_NOTICE

      public static final String MAIL_NOTICE
      What became of the mail about an attempt (mail_kind); NULL while a mail is due.
      See Also:
    • MAIL_BATCH

      public static final String MAIL_BATCH
      What became of the mail about an attempt (mail_kind); NULL while a mail is due.
      See Also:
    • MAIL_WARNING

      public static final String MAIL_WARNING
      What became of the mail about an attempt (mail_kind); NULL while a mail is due.
      See Also:
    • MAIL_SUMMARY

      public static final String MAIL_SUMMARY
      What became of the mail about an attempt (mail_kind); NULL while a mail is due.
      See Also:
    • MAIL_NONE

      public static final String MAIL_NONE
      What became of the mail about an attempt (mail_kind); NULL while a mail is due.
      See Also:
  • Constructor Details

    • FailedSignInStore

      public FailedSignInStore(ConnectionSource connections)
      Constructs the store on the system clock.
      Parameters:
      connections - The connection source, in the server DBUtilities::getPooledConnection.
    • FailedSignInStore

      public FailedSignInStore(ConnectionSource connections, LongSupplier clock)
      Constructs the store on a clock.
      Parameters:
      connections - The connection source.
      clock - The clock: epoch milliseconds.
  • Method Details

    • ensureSchema

      public boolean ensureSchema() throws SQLException
      Creates the table when it is absent.
      Returns:
      true if the table was created here, false if it existed or another server made it.
      Throws:
      SQLException - For SQL errors: the table is absent and cannot be created.
    • write

      Writes an attempt. The texts are cut to their columns; the identifier is made here when the attempt's is zero.
      Parameters:
      a - The attempt.
      Returns:
      The attempt as written (its identifier set), null when the store is unavailable.
      Throws:
      NullPointerException - If the attempt or its step is null.
      SQLException - For SQL errors.
    • markMailed

      public int markMailed(List<Long> ids, String mailKind, long mailedAt) throws SQLException
      Records what became of the mail about attempts: the kind and the time.
      Parameters:
      ids - The attempts.
      mailKind - One of the MAIL_ constants.
      mailedAt - When, epoch milliseconds; for MAIL_NONE the time of the decision.
      Returns:
      The rows updated, 0 when the store is unavailable.
      Throws:
      SQLException - For SQL errors.
    • clear

      public int clear(long uid) throws SQLException
      Deletes every attempt of a user: the user's "Clear", the administrator's, and the clearing at a successful sign-in.
      Parameters:
      uid - The user.
      Returns:
      The rows deleted, 0 when the store is unavailable.
      Throws:
      SQLException - For SQL errors.
    • purge

      public int purge(int days) throws SQLException
      Deletes the attempts older than a number of days: the daily purge.
      Parameters:
      days - The days kept, positive.
      Returns:
      The number of rows deleted.
      Throws:
      IllegalArgumentException - If days is not positive.
      SQLException - For SQL errors.
    • list

      public List<FailedSignInStore.Attempt> list(long uid, int limit) throws SQLException
      Lists the attempts of a user, the newest first.
      Parameters:
      uid - The user.
      limit - The most rows, positive.
      Returns:
      The attempts, possibly none; none when the store is unavailable.
      Throws:
      SQLException - For SQL errors.
    • pending

      public List<FailedSignInStore.Attempt> pending(long uid, boolean urgent) throws SQLException
      Lists the attempts of a user about which a mail is still due, the oldest first.
      Parameters:
      uid - The user.
      urgent - true for the attempts held past the cap of the warning mails, false for the attempts collected for a batch.
      Returns:
      The attempts, possibly none; none when the store is unavailable.
      Throws:
      SQLException - For SQL errors.
    • pendingUsers

      public List<Long> pendingUsers() throws SQLException
      Lists the users about whom a mail is still due: after a restart, what the notifier takes up.
      Returns:
      The user IDs, possibly none; none when the store is unavailable.
      Throws:
      SQLException - For SQL errors.
    • lastMailedAt

      public long lastMailedAt(long uid, String... kinds) throws SQLException
      Gets when a user was last mailed with a mail of some kinds.
      Parameters:
      uid - The user.
      kinds - The MAIL_ constants looked at, at least one.
      Returns:
      The time, epoch milliseconds; Long.MIN_VALUE for never, and when the store is unavailable.
      Throws:
      SQLException - For SQL errors.
    • mailedSince

      public List<Long> mailedSince(long uid, long since, String... kinds) throws SQLException
      Lists when a user was mailed with a mail of some kinds AFTER a time, the oldest first: the cap of the warning mails is counted on it (a mail at exactly the time is outside the window, so a window that ends where a mail was sent frees the mail's slot).
      Parameters:
      uid - The user.
      since - The time, epoch milliseconds; mails after it are listed.
      kinds - The MAIL_ constants looked at, at least one.
      Returns:
      The times; none when the store is unavailable.
      Throws:
      SQLException - For SQL errors.
    • count

      public int count(long uid) throws SQLException
      Counts the attempts of a user.
      Parameters:
      uid - The user.
      Returns:
      The count, 0 when the store is unavailable.
      Throws:
      SQLException - For SQL errors.