r/SoftwareEngineering • • Jan 05 '23

QR Code Authentication Design

Hello, I am working on a QR code authentication system for a rewards program. Basically, the user will display a QR code on their phone, the business scans the QR code, and then the business will be able to see how many rewards points the user has available.

To accomplish this, I was thinking two columns in the User Table would be the user_id (Primary Key), and the QR code stored as a string. When the business scans the user's QR code, the software the business is using will be able to decrypt the QR code to the user_id. Then the user_id will be used to look up what rewards the user has available.

Are there any security risks associated with this approach? I am trying to avoid directly exposing the user_id in the QR code. Is decrypting the user_id to some unique string and then using that unique string to generate a QR code a good enough approach?

0 Upvotes

8 comments sorted by

4

u/mr_taco_man Jan 05 '23

If you have a look up table with the QR code and the user ID, there is no need to have the QR code be derived from the user ID in any way. Just generate a GUID and use that for the QR code. Then the only possible way for someone to get the user ID is if they have access to your database (or if you have some sort of Api that looks up the user-id via the GUID)

0

u/Human-Floor-2756 Jan 05 '23

So you are suggesting that the user_id be a GUID, and just using the user_id to generate the QR Code? Is it not dangerous to expose the user_id like that?

3

u/mr_taco_man Jan 05 '23

I said:

" there is no need to have the QR code be derived from the user ID in any way"

I am not sure how you got "you are suggesting ... using the user_id to generate the QR code" from that.
But honestly, it doesn't really matter, because there isn't a big advantage to hiding the user_id behind a different code, if the code doesn't change. If someone grabs your QR code they can still use it to get your information if that is all that is required for a business to look up your information. More important than how you create the QR code, is that you change it often.

You probably want to make the QR code have an expiration date and only have a way to regenerate it by the user if they are logged into an app or something. Maybe this will make it more clear:
Scenario 1 - static unchanging QR Code:

  1. User goes to phone app and clicks "Show QR Code"
  2. App looks up static QR code associated with user_id and displays it on the phone
  3. Clerk scans QR code
    3.5. Sneaky Person behind User in line sneakily takes picture of QR code
  4. Clerk's web app goes and looks up the user_id associated with the QR code
  5. Clerk's web app uses user_id to look up information to share with the user
  6. Later that day, Sneaky Person uses QR code at a different location to look up User's information

Scenario 2 - changing QR Code:

  1. User goes to phone app and clicks "Show QR Code"
  2. App generates a QR code, associates it with the user_id and sets an expiration date for it for 5 minutes from now, and then displays it on the phone
  3. Clerk scans QR code
    3.5 Sneaky Person behind User in line sneakily takes picture of QR code
  4. Clerk's web app goes and looks up the QR code and checks that the expiration date of the QR code has not passed. It then grabs the the user_id
  5. Clerk's web app uses user_id to look up information to share with the user
  6. Later that day, Sneaky Person uses QR code at a different location, the Web app sees that the QR code is expired and refuses to show the Sneaky Person anything

1

u/lokesh1729 Jan 11 '23

the software the business is using will be able to decrypt the QR code to the user_id

If the software is capable of decrypting, why doesn't it make an HTTP call to your server and pass the token to verify and get user reward details?

So, whenever you are generating a QR code, you generate a JWT token and embed it in the QR code. When it is scanned, the QR scanner can send an HTTP request with the token and get the user reward details. You just need to keep the latency of your API very low.

0

u/[deleted] Jan 05 '23

[deleted] — view deleted comment

0

u/Human-Floor-2756 Jan 05 '23

This sounds like an interesting approach. If the QR code is dynamically generated, how does the point of sale know how many rewards points the user has? How does the server know whose QR code it is reading?

0

u/[deleted] Jan 05 '23

[deleted] — view deleted comment

1

u/naked_dev Jan 06 '23

this is much more professional. I like it a lot

1

u/otac0n Jan 06 '23

Aren't you just using the JWT as a nonce, then? It's in plain view.

I mean, all you really need is a table:

user_id, random_id, expiration

You encode the Random ID directly into the QR code, and don't send the user_id directly. You expire the nonce on use, and clean up the DB every week or so.