Skip to content

Commit

Permalink
Rename userName variable to username in security-webauthn
Browse files Browse the repository at this point in the history
  • Loading branch information
ynojima committed Dec 17, 2024
1 parent 21da529 commit 8685dc0
Show file tree
Hide file tree
Showing 14 changed files with 68 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,15 @@ public Response login(@BeanParam WebAuthnLoginResponse webAuthnResponse,

try {
WebAuthnCredentialRecord credentialRecord = this.webAuthnSecurity.login(webAuthnResponse, ctx).await().indefinitely();
User user = User.findByUserName(credentialRecord.getUserName());
User user = User.findByUsername(credentialRecord.getUsername());
if(user == null) {
// Invalid user
return Response.status(Status.BAD_REQUEST).build();
}
// bump the auth counter
user.webAuthnCredential.counter = credentialRecord.getCounter();
// make a login cookie
this.webAuthnSecurity.rememberUser(credentialRecord.getUserName(), ctx);
this.webAuthnSecurity.rememberUser(credentialRecord.getUsername(), ctx);
return Response.ok().build();
} catch (Exception exception) {
// handle login failure - make a proper error response
Expand All @@ -52,29 +52,29 @@ public Response login(@BeanParam WebAuthnLoginResponse webAuthnResponse,
@Path("/register")
@POST
@Transactional
public Response register(@RestForm String userName,
public Response register(@RestForm String username,
@BeanParam WebAuthnRegisterResponse webAuthnResponse,
RoutingContext ctx) {
// Input validation
if(userName == null || userName.isEmpty() || !webAuthnResponse.isSet() || !webAuthnResponse.isValid()) {
if(username == null || username.isEmpty() || !webAuthnResponse.isSet() || !webAuthnResponse.isValid()) {
return Response.status(Status.BAD_REQUEST).build();
}

User user = User.findByUserName(userName);
User user = User.findByUsername(username);
if(user != null) {
// Duplicate user
return Response.status(Status.BAD_REQUEST).build();
}
try {
// store the user
WebAuthnCredentialRecord credentialRecord = this.webAuthnSecurity.register(userName, webAuthnResponse, ctx).await().indefinitely();
WebAuthnCredentialRecord credentialRecord = this.webAuthnSecurity.register(username, webAuthnResponse, ctx).await().indefinitely();
User newUser = new User();
newUser.userName = credentialRecord.getUserName();
newUser.username = credentialRecord.getUsername();
WebAuthnCredential credential = new WebAuthnCredential(credentialRecord, newUser);
credential.persist();
newUser.persist();
// make a login cookie
this.webAuthnSecurity.rememberUser(newUser.userName, ctx);
this.webAuthnSecurity.rememberUser(newUser.username, ctx);
return Response.ok().build();
} catch (Exception ignored) {
// handle login failure
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ public class MyWebAuthnSetup implements WebAuthnUserProvider {

@Transactional
@Override
public Uni<List<WebAuthnCredentialRecord>> findByUserName(String userId) {
return Uni.createFrom().item(WebAuthnCredential.findByUserName(userId).stream().map(WebAuthnCredential::toWebAuthnCredentialRecord).toList());
public Uni<List<WebAuthnCredentialRecord>> findByUsername(String userId) {
return Uni.createFrom().item(WebAuthnCredential.findByUsername(userId).stream().map(WebAuthnCredential::toWebAuthnCredentialRecord).toList());
}

@Transactional
Expand All @@ -34,7 +34,7 @@ public Uni<WebAuthnCredentialRecord> findByCredentialId(String credId) {
@Override
public Uni<Void> store(WebAuthnCredentialRecord credentialRecord) {
User newUser = new User();
newUser.userName = credentialRecord.getUserName();
newUser.username = credentialRecord.getUsername();
WebAuthnCredential credential = new WebAuthnCredential(credentialRecord, newUser);
credential.persist();
newUser.persist();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@
public class User extends PanacheEntity {

@Column(unique = true)
public String userName;
public String username;

// non-owning side, so we can add more credentials later
@OneToOne(mappedBy = "user")
public WebAuthnCredential webAuthnCredential;

public static User findByUserName(String userName) {
return User.find("userName", userName).firstResult();
public static User findByUsername(String username) {
return User.find("username", username).firstResult();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ public WebAuthnCredential(WebAuthnCredentialRecord credentialRecord, User user)
public WebAuthnCredentialRecord toWebAuthnCredentialRecord() {
return WebAuthnCredentialRecord
.fromRequiredPersistedData(
new RequiredPersistedData(user.userName, credentialId, aaguid, publicKey, publicKeyAlgorithm, counter));
new RequiredPersistedData(user.username, credentialId, aaguid, publicKey, publicKeyAlgorithm, counter));
}

public static List<WebAuthnCredential> findByUserName(String userName) {
return list("user.userName", userName);
public static List<WebAuthnCredential> findByUsername(String username) {
return list("user.username", username);
}

public static WebAuthnCredential findByCredentialId(String credentialId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ <h1>Login</h1>
<div class="item">
<h1>Register</h1>
<p>
<input id="userNameRegister" placeholder="User name"/><br/>
<input id="usernameRegister" placeholder="User name"/><br/>
<input id="firstName" placeholder="First name"/><br/>
<input id="lastName" placeholder="Last name"/><br/>
<button id="register">Register</button>
Expand All @@ -87,8 +87,8 @@ <h1>Register</h1>
webAuthn.login()
.then(x => fetch('/api/public/me'))
.then(response => response.text())
.then(userName => {
result.append("User: "+userName);
.then(username => {
result.append("User: "+username);
})
.catch(err => {
result.append("Login failed: "+err);
Expand All @@ -99,13 +99,13 @@ <h1>Register</h1>
const registerButton = document.getElementById('register');

registerButton.addEventListener("click", (e) => {
var userName = document.getElementById('userNameRegister').value;
var username = document.getElementById('usernameRegister').value;
var firstName = document.getElementById('firstName').value;
var lastName = document.getElementById('lastName').value;
result.replaceChildren();
webAuthn.register({ name: userName, displayName: firstName + " " + lastName })
webAuthn.register({ name: username, displayName: firstName + " " + lastName })
.then(body => {
result.append("User: "+userName);
result.append("User: "+username);
})
.catch(err => {
result.append("Registration failed: "+err);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class TestUserProvider extends MyWebAuthnSetup {
@Override
public Uni<Void> store(WebAuthnCredentialRecord credentialRecord) {
// this user is handled in the LoginResource endpoint manually
if (credentialRecord.getUserName().equals("scooby")) {
if (credentialRecord.getUsername().equals("scooby")) {
return Uni.createFrom().voidItem();
}
return super.store(credentialRecord);
Expand All @@ -27,7 +27,7 @@ public Uni<Void> store(WebAuthnCredentialRecord credentialRecord) {
public Uni<Void> update(String credentialId, long counter) {
WebAuthnCredential credential = WebAuthnCredential.findByCredentialId(credentialId);
// this user is handled in the LoginResource endpoint manually
if (credential.user.userName.equals("scooby")) {
if (credential.user.username.equals("scooby")) {
return Uni.createFrom().voidItem();
}
return super.update(credentialId, counter);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,26 +41,26 @@ public void testWebAuthnAdmin() {
testWebAuthn("admin", User.ADMIN, Endpoint.DEFAULT);
}

private void testWebAuthn(String userName, User user, Endpoint endpoint) {
private void testWebAuthn(String username, User user, Endpoint endpoint) {
Filter cookieFilter = new RenardeCookieFilter();
WebAuthnHardware token = new WebAuthnHardware(url);

verifyLoggedOut(cookieFilter);

// two-step registration
String challenge = WebAuthnEndpointHelper.obtainRegistrationChallenge(userName, cookieFilter);
String challenge = WebAuthnEndpointHelper.obtainRegistrationChallenge(username, cookieFilter);
JsonObject registrationJson = token.makeRegistrationJson(challenge);
if(endpoint == Endpoint.DEFAULT)
WebAuthnEndpointHelper.invokeRegistration(userName, registrationJson, cookieFilter);
WebAuthnEndpointHelper.invokeRegistration(username, registrationJson, cookieFilter);
else {
invokeCustomEndpoint("/register", cookieFilter, request -> {
WebAuthnEndpointHelper.addWebAuthnRegistrationFormParameters(request, registrationJson);
request.formParam("userName", userName);
request.formParam("username", username);
});
}

// verify that we can access logged-in endpoints
verifyLoggedIn(cookieFilter, userName, user);
verifyLoggedIn(cookieFilter, username, user);

// logout
WebAuthnEndpointHelper.invokeLogout(cookieFilter);
Expand All @@ -79,7 +79,7 @@ private void testWebAuthn(String userName, User user, Endpoint endpoint) {
}

// verify that we can access logged-in endpoints
verifyLoggedIn(cookieFilter, userName, user);
verifyLoggedIn(cookieFilter, username, user);

// logout
WebAuthnEndpointHelper.invokeLogout(cookieFilter);
Expand All @@ -103,7 +103,7 @@ private void invokeCustomEndpoint(String uri, Filter cookieFilter, Consumer<Requ
.cookie(WebAuthnEndpointHelper.getMainCookie(), Matchers.notNullValue());
}

private void verifyLoggedIn(Filter cookieFilter, String userName, User user) {
private void verifyLoggedIn(Filter cookieFilter, String username, User user) {
// public API still good
RestAssured.given().filter(cookieFilter)
.when()
Expand All @@ -117,15 +117,15 @@ private void verifyLoggedIn(Filter cookieFilter, String userName, User user) {
.get("/api/public/me")
.then()
.statusCode(200)
.body(Matchers.is(userName));
.body(Matchers.is(username));

// user API accessible
RestAssured.given().filter(cookieFilter)
.when()
.get("/api/users/me")
.then()
.statusCode(200)
.body(Matchers.is(userName));
.body(Matchers.is(username));

// admin API?
if(user == User.ADMIN) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public Uni<Response> login(@BeanParam WebAuthnLoginResponse webAuthnResponse,

return this.webAuthnSecurity.login(webAuthnResponse, ctx)
.flatMap(auth -> {
return User.findByUserName(auth.getUserName())
return User.findByUsername(auth.getUsername())
.map(user -> {
if(user == null) {
// Invalid user
Expand All @@ -44,7 +44,7 @@ public Uni<Response> login(@BeanParam WebAuthnLoginResponse webAuthnResponse,
// bump the auth counter
user.webAuthnCredential.counter = auth.getCounter();
// make a login cookie
this.webAuthnSecurity.rememberUser(auth.getUserName(), ctx);
this.webAuthnSecurity.rememberUser(auth.getUsername(), ctx);
return Response.ok().build();
});
})
Expand All @@ -58,37 +58,37 @@ public Uni<Response> login(@BeanParam WebAuthnLoginResponse webAuthnResponse,
@Path("/register")
@POST
@WithTransaction
public Uni<Response> register(@RestForm String userName,
public Uni<Response> register(@RestForm String username,
@BeanParam WebAuthnRegisterResponse webAuthnResponse,
RoutingContext ctx) {
// Input validation
if(userName == null || userName.isEmpty()
if(username == null || username.isEmpty()
|| !webAuthnResponse.isSet()
|| !webAuthnResponse.isValid()) {
return Uni.createFrom().item(Response.status(Status.BAD_REQUEST).build());
}

Uni<User> userUni = User.findByUserName(userName);
Uni<User> userUni = User.findByUsername(username);
return userUni.flatMap(user -> {
if(user != null) {
// Duplicate user
return Uni.createFrom().item(Response.status(Status.BAD_REQUEST).build());
}
Uni<WebAuthnCredentialRecord> credentialRecord = this.webAuthnSecurity.register(userName, webAuthnResponse, ctx);
Uni<WebAuthnCredentialRecord> credentialRecord = this.webAuthnSecurity.register(username, webAuthnResponse, ctx);

return credentialRecord
// store the user
.flatMap(auth -> {
User newUser = new User();
newUser.userName = auth.getUserName();
newUser.username = auth.getUsername();
WebAuthnCredential credential = new WebAuthnCredential(auth, newUser);
return credential.persist()
.flatMap(c -> newUser.<User>persist());

})
.map(newUser -> {
// make a login cookie
this.webAuthnSecurity.rememberUser(newUser.userName, ctx);
this.webAuthnSecurity.rememberUser(newUser.username, ctx);
return Response.ok().build();
})
// handle login failure
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ public class MyWebAuthnSetup implements WebAuthnUserProvider {

@WithTransaction
@Override
public Uni<List<WebAuthnCredentialRecord>> findByUserName(String userName) {
return WebAuthnCredential.findByUserName(userName)
public Uni<List<WebAuthnCredentialRecord>> findByUsername(String username) {
return WebAuthnCredential.findByUsername(username)
.map(list -> list.stream().map(WebAuthnCredential::toWebAuthnCredentialRecord).toList());
}

Expand All @@ -33,7 +33,7 @@ public Uni<WebAuthnCredentialRecord> findByCredentialId(String credentialId) {
@Override
public Uni<Void> store(WebAuthnCredentialRecord credentialRecord) {
User newUser = new User();
newUser.userName = credentialRecord.getUserName();
newUser.username = credentialRecord.getUsername();
WebAuthnCredential credential = new WebAuthnCredential(credentialRecord, newUser);
return credential.persist()
.flatMap(c -> newUser.persist())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@
public class User extends PanacheEntity {

@Column(unique = true)
public String userName;
public String username;

// non-owning side, so we can add more credentials later
@OneToOne(mappedBy = "user")
public WebAuthnCredential webAuthnCredential;

public static Uni<User> findByUserName(String userName) {
return find("userName", userName).firstResult();
public static Uni<User> findByUsername(String username) {
return find("username", username).firstResult();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ public WebAuthnCredential(WebAuthnCredentialRecord credentialRecord, User user)
public WebAuthnCredentialRecord toWebAuthnCredentialRecord() {
return WebAuthnCredentialRecord
.fromRequiredPersistedData(
new RequiredPersistedData(user.userName, credentialId, aaguid, publicKey, publicKeyAlgorithm, counter));
new RequiredPersistedData(user.username, credentialId, aaguid, publicKey, publicKeyAlgorithm, counter));
}

public static Uni<List<WebAuthnCredential>> findByUserName(String userName) {
return list("user.userName", userName);
public static Uni<List<WebAuthnCredential>> findByUsername(String username) {
return list("user.username", username);
}

public static Uni<WebAuthnCredential> findByCredentialId(String credentialId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ <h1>Login</h1>
<div class="item">
<h1>Register</h1>
<p>
<input id="userNameRegister" placeholder="User name"/><br/>
<input id="usernameRegister" placeholder="User name"/><br/>
<input id="firstName" placeholder="First name"/><br/>
<input id="lastName" placeholder="Last name"/><br/>
<button id="register">Register</button>
Expand All @@ -87,8 +87,8 @@ <h1>Register</h1>
webAuthn.login()
.then(x => fetch('/api/public/me'))
.then(response => response.text())
.then(userName => {
result.append("User: "+userName);
.then(username => {
result.append("User: "+username);
})
.catch(err => {
result.append("Login failed: "+err);
Expand All @@ -99,13 +99,13 @@ <h1>Register</h1>
const registerButton = document.getElementById('register');

registerButton.addEventListener("click", (e) => {
var userName = document.getElementById('userNameRegister').value;
var username = document.getElementById('usernameRegister').value;
var firstName = document.getElementById('firstName').value;
var lastName = document.getElementById('lastName').value;
result.replaceChildren();
webAuthn.register({ name: userName, displayName: firstName + " " + lastName })
webAuthn.register({ name: username, displayName: firstName + " " + lastName })
.then(body => {
result.append("User: "+userName);
result.append("User: "+username);
})
.catch(err => {
result.append("Registration failed: "+err);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class TestUserProvider extends MyWebAuthnSetup {
@Override
public Uni<Void> store(WebAuthnCredentialRecord credentialRecord) {
// this user is handled in the LoginResource endpoint manually
if (credentialRecord.getUserName().equals("scooby")) {
if (credentialRecord.getUsername().equals("scooby")) {
return Uni.createFrom().voidItem();
}
return super.store(credentialRecord);
Expand All @@ -28,7 +28,7 @@ public Uni<Void> update(String credentialId, long counter) {
return WebAuthnCredential.findByCredentialId(credentialId)
.flatMap(credential -> {
// this user is handled in the LoginResource endpoint manually
if (credential.user.userName.equals("scooby")) {
if (credential.user.username.equals("scooby")) {
return Uni.createFrom().voidItem();
}
return super.update(credentialId, counter);
Expand Down
Loading

0 comments on commit 8685dc0

Please sign in to comment.