Results 1 to 6 of 6

Thread: [SOLVED] MySQL - Check credentials?

  1. #1
    Join Date
    Oct 2012
    Posts
    13
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Thumbs up [SOLVED] MySQL - Check credentials?

    I've done this in PHP, but for QT i'm not finding much documentation. I compiled the QMYSQL driver (finally), I'm connected to my local DB and all I need to do now is to compare the user supplied username and password against the database. I have already hashed & salted the password.

    This is what I currently have, which is not working. More specifically, it is now saying that anything I enter is valid. Please note, QSqlQuery.lastError() is returning a null string.

    I echoed the credentials just before the query is executed, everything is proper. I can't think of what it could be.

    Qt Code:
    1. query.prepare("SELECT UID, PWD FROM login WHERE UID=? AND PWD=?");
    2. query.addBindValue(username);
    3. query.addBindValue(password);
    4.  
    5. if(!query.exec())
    6. {
    7. msg.setText(query.lastError().text());
    8. msg.exec();
    9. }
    10.  
    11. QSqlRecord record = query.record();
    12. int cols = record.count();
    13.  
    14. if(cols > 0)
    15. {
    16. msg.setText("Good");
    17. return true;
    18. }
    19. else
    20. {
    21. msg.setText("Bad");
    22. return false;
    23. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by xyz247; 28th October 2012 at 04:34.

  2. #2
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: MySQL - Check credentials?

    Quote Originally Posted by xyz247 View Post
    it is now saying that anything I enter is valid.
    From the QSqlQuery::record() docs:
    An empty record is returned when there is no active query (isActive() returns false)
    So you will always get the correct column count whether you have a valid user_id/password or not.

    Try testing for query.size() > 0

  3. The following user says thank you to norobro for this useful post:

    xyz247 (28th October 2012)

  4. #3
    Join Date
    Oct 2012
    Posts
    13
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: MySQL - Check credentials?

    Okay, yeah I had added that soon after and got it working. Thank you.

    Is it safe to compare locally, the results from the database against what the user entered? For example:

    Qt Code:
    1. // username: The local username that the user entered
    2. // remoteUsername: The username retrieved from the database
    3. // Is it safe to compare these locally on the client's computer?
    4. if(username != remoteUsername || password != remotePassword)
    5. {
    6. // Login failed
    7. invalidCredentialsMsg.exec();
    8. }
    To copy to clipboard, switch view to plain text mode 

  5. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: MySQL - Check credentials?

    I'd be inclined to do this:
    Qt Code:
    1. query.prepare("SELECT 1 FROM login WHERE UID=? AND PWD=?");
    2. ...
    3. query.addBindValue(username);
    4. query.addBindValue(password);
    5. if (query.exec() && query.next()) {
    6. //successful
    7. }
    8. else {
    9. // unsuccessful either no match or bad SQL
    10. }
    To copy to clipboard, switch view to plain text mode 
    and rely on the presence of a row as the indicator of success. I don't retrieve the uid and pwd because I don't need them.

  6. The following user says thank you to ChrisW67 for this useful post:

    xyz247 (28th October 2012)

  7. #5
    Join Date
    Oct 2012
    Posts
    13
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: MySQL - Check credentials?

    Chris, you're both a gentleman and a scholar (:

    That is way more convenient! Thank you both, this is solved by both of you.

    Does anyone know of any decent articles or books on developing a secure authentication client? For example, encrypting/decrypting SQL query strings, max password attempts, etc.? I can't afford for an exploit to be discovered.

  8. #6
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: MySQL - Check credentials?

    If you cannot afford an exploit then you have a lot of learning to do: doing this safely is a tricky proposition. There is no iron-clad guarantee.

    If the SQL server is remote then you must use an SSL encrypted connection at the very least. If the traffic in this example is sniffed then they can login simply by replaying the correct hashed password. You could investigate ways to use public key crypto to verify identity without exposing passwords on the wire at all.

Similar Threads

  1. Check credentials using QSql
    By death_star in forum Qt Programming
    Replies: 6
    Last Post: 26th January 2011, 06:16
  2. How to save credentials in local keychain
    By kalos80 in forum Qt Programming
    Replies: 4
    Last Post: 9th November 2010, 11:08
  3. I need MySQL driver for Qt 4.6, WindowsXP(32), MySQL 5.1
    By User_3 in forum Installation and Deployment
    Replies: 7
    Last Post: 15th October 2010, 15:19
  4. Check if a table exists in a mysql database
    By graciano in forum Qt Programming
    Replies: 8
    Last Post: 5th November 2009, 02:44
  5. MYSQL 5 Table qt model as small Mysql admin
    By patrik08 in forum Qt-based Software
    Replies: 0
    Last Post: 1st May 2007, 09:43

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.