[java-idp-plugin-webauthn] branch main updated: Add overloaded methods to the encoder to deal with custom date formats

Phil Smart philip.smart at jisc.ac.uk
Mon Feb 17 16:02:27 UTC 2025


This is an automated email from the git hooks/post-receive script.

philsmart pushed a commit to branch main
in repository java-idp-plugin-webauthn.

View the commit online:
http://git.shibboleth.net/view/?p=java-idp-plugin-webauthn.git;a=commit;h=71469f1ceac47c7705eb9ce9ccf9cc039bf927fd

The following commit(s) were added to refs/heads/main by this push:
     new 71469f1  Add overloaded methods to the encoder to deal with custom date formats
71469f1 is described below

commit 71469f1ceac47c7705eb9ce9ccf9cc039bf927fd
Author: Phil Smart <philip.smart at jisc.ac.uk>
AuthorDate: Mon Feb 17 16:02:23 2025 +0000

    Add overloaded methods to the encoder to deal with custom date formats
    
     - Also add a new method that picks the latest (chronologically) of two
    instants, formats it, and returns it. This helps support the 'Last Used'
    field.
---
 .../authn/webauthn/impl/WebAuthnEncoder.java       | 64 +++++++++++++++++++++-
 .../authn/webauthn/impl/WebAuthnEncoderTest.java   | 47 ++++++++++++++++
 2 files changed, 108 insertions(+), 3 deletions(-)

diff --git a/webauthn-impl/src/main/java/net/shibboleth/idp/plugin/authn/webauthn/impl/WebAuthnEncoder.java b/webauthn-impl/src/main/java/net/shibboleth/idp/plugin/authn/webauthn/impl/WebAuthnEncoder.java
index 7bfda9d..96fdb9f 100644
--- a/webauthn-impl/src/main/java/net/shibboleth/idp/plugin/authn/webauthn/impl/WebAuthnEncoder.java
+++ b/webauthn-impl/src/main/java/net/shibboleth/idp/plugin/authn/webauthn/impl/WebAuthnEncoder.java
@@ -98,18 +98,76 @@ public final class WebAuthnEncoder {
      * @return a formatted time string
      */
     @Nonnull @NotEmpty public static String formatInstant(@Nullable final Instant time) {
-        if (time == null) {
+        return formatInstant(time, DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mmXXX")
+                .withZone((ZoneId.of("UTC"))));
+    }
+    
+    /**
+     * Format the given instant into a display friendly string using the given pattern.
+     * 
+     * @param time the instant to encode
+     * @param pattern the date time pattern to use
+     * 
+     * @return a formatted time string
+     */
+    @Nonnull @NotEmpty public static String formatInstant(@Nullable final Instant time, 
+            @Nullable final String pattern) {
+        if (time == null || pattern == null) { 
+            return "";
+        }
+        return formatInstant(time, DateTimeFormatter.ofPattern(pattern).withZone((ZoneId.of("UTC"))));           
+    }
+    
+    /**
+     * Format the given instant into a display friendly string using the given formatter.
+     * 
+     * @param time the instant to encode
+     * @param pattern the date time pattern to use
+     * 
+     * @return a formatted time string
+     */
+    @Nonnull @NotEmpty public static String formatInstant(@Nullable final Instant time, 
+            @Nullable final DateTimeFormatter formatter) {
+        if (time == null || formatter == null) { 
             return "";
         }
         try {
-            final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'hh:mm")
-                    .withZone(ZoneId.systemDefault());
             final String isoFormat = formatter.format(time);
             assert isoFormat != null;
             return isoFormat; 
         } catch (final Exception e) {
+            LOG.error("Date time formatting error",e);
+            return "";
+        }
+    }
+    
+    
+    /**
+     * Choose the latest (on the time-line) instant, format it into a display friendly string, and return it.
+     *  
+     * @param first the first instant
+     * @param second the second instant
+     * @return the latest of the two, formatted.
+     * 
+     * @since 1.1.0
+     */
+    @Nonnull @NotEmpty public static String chooseLatestAndTransform(@Nullable final Instant first, 
+            @Nullable final Instant second) {
+        if (first == null && second == null) {
             return "";
         }
+        if (first == null && second != null) {
+            return formatInstant(second);
+        }
+        if (first != null && second == null) {
+            return formatInstant(first);
+        }
+        assert first != null;
+        assert second != null;
+        if (first.isAfter(second)) {
+            return formatInstant(first);
+        }
+        else return formatInstant(second);
     }
     
     /**
diff --git a/webauthn-impl/src/test/java/net/shibboleth/idp/plugin/authn/webauthn/impl/WebAuthnEncoderTest.java b/webauthn-impl/src/test/java/net/shibboleth/idp/plugin/authn/webauthn/impl/WebAuthnEncoderTest.java
new file mode 100644
index 0000000..b196f7a
--- /dev/null
+++ b/webauthn-impl/src/test/java/net/shibboleth/idp/plugin/authn/webauthn/impl/WebAuthnEncoderTest.java
@@ -0,0 +1,47 @@
+/*
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package net.shibboleth.idp.plugin.authn.webauthn.impl;
+
+import static org.testng.Assert.assertNotEquals;
+import static org.testng.Assert.assertNotNull;
+
+import java.time.Instant;
+
+import org.testng.annotations.Test;
+
+/**
+ * Tests for the {@link WebAuthnEncoder}
+ */
+public class WebAuthnEncoderTest {
+    
+    
+    @Test
+    public void testFormatTime() {
+        final String dateTime = WebAuthnEncoder.formatInstant(Instant.now());
+        assertNotNull(dateTime);
+        assertNotEquals(dateTime, "");
+    }
+    
+    @Test
+    public void testFormatTime_DifferentTimeZone() {
+        final Instant fixedInstant = Instant.parse("2025-06-17T14:00:00+01:00");
+        final String dateTime = WebAuthnEncoder.formatInstant(fixedInstant);
+        System.out.println("date:"+dateTime);
+        assertNotNull(dateTime);
+        assertNotEquals(dateTime, "");
+    }
+
+
+}

-- 
To stop receiving notification emails like this one, please contact
the administrator of this repository.


More information about the commits mailing list