One message for every failure

A login form that says "no such user" tells attackers which accounts exist. A safe form gives the same message for every failure.

What to build

Check login attempts and answer with a single generic failure message.

Requirements

  • Line 1: N, then N accounts user password.
  • Each remaining line is an attempt user password.
  • Print welcome USER for a correct pair, and wrong user name or password for every other attempt, including unknown users.

Input

As described.

Output

One line per attempt.

Constraints

Up to 50 accounts and 50 attempts. Real systems store salted hashes; plain passwords keep this exercise short.

Example

Input:

1
rahim s3cret
rahim s3cret
rahim wrong

Output:

welcome rahim
wrong user name or password

Why it works

With one message for both kinds of failure, an attacker learns nothing about which user names exist, so they cannot build a list of real accounts to attack.

Examples

Example 1 · Input
1
rahim s3cret
rahim s3cret
rahim wrong
Expected output
welcome rahim
wrong user name or password
Example 2 · Input
1
anika pw
nobody pw
Expected output
wrong user name or password

Hints

Hint 1

Use a Map so a user name like constructor cannot match a built-in property.

Solution

Show one correct solution (try it yourself first)
const n = Number(prompt());
const accounts = new Map();
for (let k = 0; k < n; k++) {
  const [user, password] = prompt().trim().split(/\s+/);
  accounts.set(user, password);
}
let line = prompt();
while (line !== null) {
  if (line.trim()) {
    const [user, password] = line.trim().split(/\s+/);
    console.log(accounts.has(user) && accounts.get(user) === password ? `welcome ${user}` : "wrong user name or password");
  }
  line = prompt();
}

▶ Open this solution in the compiler, then press Visualize to watch it run step by step

Lesson: Broken authentication and sessions →