From 8685dc067e42fe88fa3b823d0c4f97df99bc1a6b Mon Sep 17 00:00:00 2001 From: Yoshikazu Nojima Date: Wed, 18 Dec 2024 00:08:40 +0900 Subject: [PATCH] Rename userName variable to username in security-webauthn --- .../acme/security/webauthn/LoginResource.java | 16 ++++++++-------- .../security/webauthn/MyWebAuthnSetup.java | 6 +++--- .../java/org/acme/security/webauthn/User.java | 6 +++--- .../security/webauthn/WebAuthnCredential.java | 6 +++--- .../resources/META-INF/resources/index.html | 12 ++++++------ .../webauthn/test/TestUserProvider.java | 4 ++-- .../webauthn/test/WebAuthnResourceTest.java | 18 +++++++++--------- .../acme/security/webauthn/LoginResource.java | 16 ++++++++-------- .../security/webauthn/MyWebAuthnSetup.java | 6 +++--- .../java/org/acme/security/webauthn/User.java | 6 +++--- .../security/webauthn/WebAuthnCredential.java | 6 +++--- .../resources/META-INF/resources/index.html | 12 ++++++------ .../webauthn/test/TestUserProvider.java | 4 ++-- .../webauthn/test/WebAuthnResourceTest.java | 18 +++++++++--------- 14 files changed, 68 insertions(+), 68 deletions(-) diff --git a/security-webauthn-quickstart/src/main/java/org/acme/security/webauthn/LoginResource.java b/security-webauthn-quickstart/src/main/java/org/acme/security/webauthn/LoginResource.java index eb41e73573..8d918d452f 100644 --- a/security-webauthn-quickstart/src/main/java/org/acme/security/webauthn/LoginResource.java +++ b/security-webauthn-quickstart/src/main/java/org/acme/security/webauthn/LoginResource.java @@ -33,7 +33,7 @@ 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(); @@ -41,7 +41,7 @@ public Response login(@BeanParam WebAuthnLoginResponse webAuthnResponse, // 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 @@ -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 diff --git a/security-webauthn-quickstart/src/main/java/org/acme/security/webauthn/MyWebAuthnSetup.java b/security-webauthn-quickstart/src/main/java/org/acme/security/webauthn/MyWebAuthnSetup.java index 4c83484e4b..a499845829 100644 --- a/security-webauthn-quickstart/src/main/java/org/acme/security/webauthn/MyWebAuthnSetup.java +++ b/security-webauthn-quickstart/src/main/java/org/acme/security/webauthn/MyWebAuthnSetup.java @@ -17,8 +17,8 @@ public class MyWebAuthnSetup implements WebAuthnUserProvider { @Transactional @Override - public Uni> findByUserName(String userId) { - return Uni.createFrom().item(WebAuthnCredential.findByUserName(userId).stream().map(WebAuthnCredential::toWebAuthnCredentialRecord).toList()); + public Uni> findByUsername(String userId) { + return Uni.createFrom().item(WebAuthnCredential.findByUsername(userId).stream().map(WebAuthnCredential::toWebAuthnCredentialRecord).toList()); } @Transactional @@ -34,7 +34,7 @@ public Uni findByCredentialId(String credId) { @Override public Uni 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(); diff --git a/security-webauthn-quickstart/src/main/java/org/acme/security/webauthn/User.java b/security-webauthn-quickstart/src/main/java/org/acme/security/webauthn/User.java index dc0e4d94d1..554989ba6c 100644 --- a/security-webauthn-quickstart/src/main/java/org/acme/security/webauthn/User.java +++ b/security-webauthn-quickstart/src/main/java/org/acme/security/webauthn/User.java @@ -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(); } } diff --git a/security-webauthn-quickstart/src/main/java/org/acme/security/webauthn/WebAuthnCredential.java b/security-webauthn-quickstart/src/main/java/org/acme/security/webauthn/WebAuthnCredential.java index 47a303dad6..26811a0037 100644 --- a/security-webauthn-quickstart/src/main/java/org/acme/security/webauthn/WebAuthnCredential.java +++ b/security-webauthn-quickstart/src/main/java/org/acme/security/webauthn/WebAuthnCredential.java @@ -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 findByUserName(String userName) { - return list("user.userName", userName); + public static List findByUsername(String username) { + return list("user.username", username); } public static WebAuthnCredential findByCredentialId(String credentialId) { diff --git a/security-webauthn-quickstart/src/main/resources/META-INF/resources/index.html b/security-webauthn-quickstart/src/main/resources/META-INF/resources/index.html index 0729b0b5a0..6afa3ed663 100644 --- a/security-webauthn-quickstart/src/main/resources/META-INF/resources/index.html +++ b/security-webauthn-quickstart/src/main/resources/META-INF/resources/index.html @@ -64,7 +64,7 @@

Login

Register

-
+


@@ -87,8 +87,8 @@

Register

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); @@ -99,13 +99,13 @@

Register

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); diff --git a/security-webauthn-quickstart/src/test/java/org/acme/security/webauthn/test/TestUserProvider.java b/security-webauthn-quickstart/src/test/java/org/acme/security/webauthn/test/TestUserProvider.java index 36d948b96f..dabf4cf08b 100644 --- a/security-webauthn-quickstart/src/test/java/org/acme/security/webauthn/test/TestUserProvider.java +++ b/security-webauthn-quickstart/src/test/java/org/acme/security/webauthn/test/TestUserProvider.java @@ -16,7 +16,7 @@ public class TestUserProvider extends MyWebAuthnSetup { @Override public Uni 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); @@ -27,7 +27,7 @@ public Uni store(WebAuthnCredentialRecord credentialRecord) { public Uni 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); diff --git a/security-webauthn-quickstart/src/test/java/org/acme/security/webauthn/test/WebAuthnResourceTest.java b/security-webauthn-quickstart/src/test/java/org/acme/security/webauthn/test/WebAuthnResourceTest.java index d77fef6ca3..dc80f07ab0 100644 --- a/security-webauthn-quickstart/src/test/java/org/acme/security/webauthn/test/WebAuthnResourceTest.java +++ b/security-webauthn-quickstart/src/test/java/org/acme/security/webauthn/test/WebAuthnResourceTest.java @@ -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); @@ -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); @@ -103,7 +103,7 @@ private void invokeCustomEndpoint(String uri, Filter cookieFilter, Consumer 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 @@ -44,7 +44,7 @@ public Uni 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(); }); }) @@ -58,29 +58,29 @@ public Uni login(@BeanParam WebAuthnLoginResponse webAuthnResponse, @Path("/register") @POST @WithTransaction - public Uni register(@RestForm String userName, + public Uni 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 userUni = User.findByUserName(userName); + Uni userUni = User.findByUsername(username); return userUni.flatMap(user -> { if(user != null) { // Duplicate user return Uni.createFrom().item(Response.status(Status.BAD_REQUEST).build()); } - Uni credentialRecord = this.webAuthnSecurity.register(userName, webAuthnResponse, ctx); + Uni 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.persist()); @@ -88,7 +88,7 @@ public Uni register(@RestForm String userName, }) .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 diff --git a/security-webauthn-reactive-quickstart/src/main/java/org/acme/security/webauthn/MyWebAuthnSetup.java b/security-webauthn-reactive-quickstart/src/main/java/org/acme/security/webauthn/MyWebAuthnSetup.java index c627bf7376..b27aa09b2e 100644 --- a/security-webauthn-reactive-quickstart/src/main/java/org/acme/security/webauthn/MyWebAuthnSetup.java +++ b/security-webauthn-reactive-quickstart/src/main/java/org/acme/security/webauthn/MyWebAuthnSetup.java @@ -16,8 +16,8 @@ public class MyWebAuthnSetup implements WebAuthnUserProvider { @WithTransaction @Override - public Uni> findByUserName(String userName) { - return WebAuthnCredential.findByUserName(userName) + public Uni> findByUsername(String username) { + return WebAuthnCredential.findByUsername(username) .map(list -> list.stream().map(WebAuthnCredential::toWebAuthnCredentialRecord).toList()); } @@ -33,7 +33,7 @@ public Uni findByCredentialId(String credentialId) { @Override public Uni 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()) diff --git a/security-webauthn-reactive-quickstart/src/main/java/org/acme/security/webauthn/User.java b/security-webauthn-reactive-quickstart/src/main/java/org/acme/security/webauthn/User.java index c9493da625..f04bf73953 100644 --- a/security-webauthn-reactive-quickstart/src/main/java/org/acme/security/webauthn/User.java +++ b/security-webauthn-reactive-quickstart/src/main/java/org/acme/security/webauthn/User.java @@ -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 findByUserName(String userName) { - return find("userName", userName).firstResult(); + public static Uni findByUsername(String username) { + return find("username", username).firstResult(); } } diff --git a/security-webauthn-reactive-quickstart/src/main/java/org/acme/security/webauthn/WebAuthnCredential.java b/security-webauthn-reactive-quickstart/src/main/java/org/acme/security/webauthn/WebAuthnCredential.java index dbf060c460..ff438d0f7e 100644 --- a/security-webauthn-reactive-quickstart/src/main/java/org/acme/security/webauthn/WebAuthnCredential.java +++ b/security-webauthn-reactive-quickstart/src/main/java/org/acme/security/webauthn/WebAuthnCredential.java @@ -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> findByUserName(String userName) { - return list("user.userName", userName); + public static Uni> findByUsername(String username) { + return list("user.username", username); } public static Uni findByCredentialId(String credentialId) { diff --git a/security-webauthn-reactive-quickstart/src/main/resources/META-INF/resources/index.html b/security-webauthn-reactive-quickstart/src/main/resources/META-INF/resources/index.html index 0729b0b5a0..6afa3ed663 100644 --- a/security-webauthn-reactive-quickstart/src/main/resources/META-INF/resources/index.html +++ b/security-webauthn-reactive-quickstart/src/main/resources/META-INF/resources/index.html @@ -64,7 +64,7 @@

Login

Register

-
+


@@ -87,8 +87,8 @@

Register

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); @@ -99,13 +99,13 @@

Register

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); diff --git a/security-webauthn-reactive-quickstart/src/test/java/org/acme/security/webauthn/test/TestUserProvider.java b/security-webauthn-reactive-quickstart/src/test/java/org/acme/security/webauthn/test/TestUserProvider.java index b493f14c6c..5138943b2b 100644 --- a/security-webauthn-reactive-quickstart/src/test/java/org/acme/security/webauthn/test/TestUserProvider.java +++ b/security-webauthn-reactive-quickstart/src/test/java/org/acme/security/webauthn/test/TestUserProvider.java @@ -16,7 +16,7 @@ public class TestUserProvider extends MyWebAuthnSetup { @Override public Uni 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); @@ -28,7 +28,7 @@ public Uni 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); diff --git a/security-webauthn-reactive-quickstart/src/test/java/org/acme/security/webauthn/test/WebAuthnResourceTest.java b/security-webauthn-reactive-quickstart/src/test/java/org/acme/security/webauthn/test/WebAuthnResourceTest.java index d77fef6ca3..dc80f07ab0 100644 --- a/security-webauthn-reactive-quickstart/src/test/java/org/acme/security/webauthn/test/WebAuthnResourceTest.java +++ b/security-webauthn-reactive-quickstart/src/test/java/org/acme/security/webauthn/test/WebAuthnResourceTest.java @@ -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); @@ -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); @@ -103,7 +103,7 @@ private void invokeCustomEndpoint(String uri, Filter cookieFilter, Consumer