[java-idp-plugin-totp] branch main updated: Draft of CLI.

Scott Cantor cantor.2 at osu.edu
Tue Aug 11 23:13:38 UTC 2020


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

scantor pushed a commit to branch main
in repository java-idp-plugin-totp.

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

The following commit(s) were added to refs/heads/main by this push:
       new  b8e8b7a   Draft of CLI.
b8e8b7a is described below

commit b8e8b7a6c22f30a42c1505870c103fcc768c0e26
Author: Scott Cantor <cantor.2 at osu.edu>
AuthorDate: Tue Aug 11 19:15:02 2020 -0400

    Draft of CLI.
---
 .../totp/impl/TOTPAuthenticatorArguments.java      | 113 ++++++++++++++++++++
 .../idp/plugin/totp/impl/TOTPAuthenticatorCLI.java | 115 +++++++++++++++++++++
 2 files changed, 228 insertions(+)

diff --git a/totp-impl/src/main/java/net/shibboleth/idp/plugin/totp/impl/TOTPAuthenticatorArguments.java b/totp-impl/src/main/java/net/shibboleth/idp/plugin/totp/impl/TOTPAuthenticatorArguments.java
new file mode 100644
index 0000000..c6eadef
--- /dev/null
+++ b/totp-impl/src/main/java/net/shibboleth/idp/plugin/totp/impl/TOTPAuthenticatorArguments.java
@@ -0,0 +1,113 @@
+/*
+ * Licensed to the University Corporation for Advanced Internet Development,
+ * Inc. (UCAID) under one or more contributor license agreements.  See the
+ * NOTICE file distributed with this work for additional information regarding
+ * copyright ownership. The UCAID licenses this file to You 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.totp.impl;
+
+import java.io.PrintStream;
+
+import javax.annotation.Nullable;
+
+import com.beust.jcommander.Parameter;
+
+import net.shibboleth.ext.spring.cli.AbstractCommandLineArguments;
+import net.shibboleth.utilities.java.support.annotation.constraint.NotEmpty;
+import net.shibboleth.utilities.java.support.codec.Base32Support;
+import net.shibboleth.utilities.java.support.codec.DecodingException;
+
+/**
+ * Arguments for DataSealer CLI.
+ */
+public class TOTPAuthenticatorArguments extends AbstractCommandLineArguments {
+
+    /**
+     * Name of a specific {@link TOTPAuthenticator}, if one has been requested.
+     */
+    @Parameter(names = "--authenticator")
+    @Nullable private String authenticatorName;
+
+    /** Raw token seed when verifying codes. */
+    @Nullable private byte[] seed;
+    
+    /** Token code to verify. */
+    @Nullable private Integer tokenCode; 
+    
+    /**
+     * Get name of {@link TOTPAuthenticator} bean to access.
+     * 
+     * @return bean name
+     */
+    @Nullable @NotEmpty public String getAuthenticatorName() {
+        return authenticatorName;
+    }
+
+    /**
+     * Get token seed when verifying codes.
+     * 
+     * @return raw seed
+     */
+    @Nullable @NotEmpty public byte[] getSeed() {
+        return seed;
+    }
+
+    /**
+     * Get token code to verify.
+     * 
+     * @return token code
+     */
+    @Nullable public Integer getTokenCode() {
+        return tokenCode;
+    }
+
+    /** {@inheritDoc} */
+    public void validate() throws IllegalArgumentException {
+        super.validate();
+        
+        if (getOtherArgs().size() < 1) {
+            throw new IllegalArgumentException("Missing one or more required arguments");
+        }
+        
+        if (getOtherArgs().size() == 3) {
+            try {
+                seed = Base32Support.decode(getOtherArgs().get(1));
+            } catch (final DecodingException e) {
+                throw new IllegalArgumentException(e);
+            }
+            tokenCode = Integer.valueOf(getOtherArgs().get(2));
+        } else {
+            throw new IllegalArgumentException(
+                    "Invalid operation requested, must have zero or two additional arguments");
+        }
+    }
+
+    /** {@inheritDoc} */
+    public void printHelp(final PrintStream out) {
+        out.println("TOTPAuthenticatorCLI");
+        out.println("Provides a command line interface for TOTPAuthenticator operations.");
+        out.println();
+        out.println("   TOTPAuthenticatorCLI [options] springConfiguration [seed] [tokencode]");
+        out.println();
+        out.println("      springConfiguration      name of Spring configuration resource to use");
+        out.println("      seed                     encoded seed (omit when generating a new credential)");
+        out.println("      tokencode                token code to validate (omit when generating a new credential)");
+        super.printHelp(out);
+        out.println();
+        out.println(String.format("  --%-20s %s", "authenticator",
+                "Specifies a non-default TOTPAuthenticator bean to use."));
+        out.println();
+    }
+
+}
\ No newline at end of file
diff --git a/totp-impl/src/main/java/net/shibboleth/idp/plugin/totp/impl/TOTPAuthenticatorCLI.java b/totp-impl/src/main/java/net/shibboleth/idp/plugin/totp/impl/TOTPAuthenticatorCLI.java
new file mode 100644
index 0000000..82bbf87
--- /dev/null
+++ b/totp-impl/src/main/java/net/shibboleth/idp/plugin/totp/impl/TOTPAuthenticatorCLI.java
@@ -0,0 +1,115 @@
+/*
+ * Licensed to the University Corporation for Advanced Internet Development,
+ * Inc. (UCAID) under one or more contributor license agreements.  See the
+ * NOTICE file distributed with this work for additional information regarding
+ * copyright ownership. The UCAID licenses this file to You 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.totp.impl;
+
+import javax.annotation.Nonnull;
+import javax.annotation.Nullable;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import net.shibboleth.ext.spring.cli.AbstractCommandLine;
+import net.shibboleth.idp.Version;
+import net.shibboleth.idp.plugin.totp.impl.TOTPAuthenticator.TOTPCredential;
+import net.shibboleth.utilities.java.support.annotation.constraint.NotEmpty;
+import net.shibboleth.utilities.java.support.codec.Base32Support;
+
+/**
+ * Command line utility for {@link TOTPAuthenticator}.
+ */
+public class TOTPAuthenticatorCLI extends AbstractCommandLine<TOTPAuthenticatorArguments> {
+
+    /** Class logger. */
+    @Nullable private Logger log;
+    
+    /** {@inheritDoc} */
+    @Override
+    @Nonnull protected Logger getLogger() {
+        if (log == null) {
+            log = LoggerFactory.getLogger(TOTPAuthenticatorCLI.class);
+        }
+        return log;
+    }
+    
+    /** {@inheritDoc} */
+    @Override
+    @Nonnull protected Class<TOTPAuthenticatorArguments> getArgumentClass() {
+        return TOTPAuthenticatorArguments.class;
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    @Nonnull @NotEmpty protected String getVersion() {
+        return Version.getVersion();
+    }
+    
+    /** {@inheritDoc} */
+    @Override
+    protected int doRun(@Nonnull final TOTPAuthenticatorArguments args) {
+        final int ret = super.doRun(args);
+        if (ret != RC_OK) {
+            return ret;
+        }
+        
+        try {
+            final TOTPAuthenticator authenticator;
+            if (args.getAuthenticatorName() != null) {
+                authenticator = getApplicationContext().getBean(args.getAuthenticatorName(), TOTPAuthenticator.class);
+            } else {
+                authenticator = getApplicationContext().getBean(TOTPAuthenticator.class);
+            }
+            
+            if (args.getSeed() != null && args.getTokenCode() != null) {
+                
+                if (authenticator.validate(args.getSeed(), args.getTokenCode())) {
+                    System.out.println("OK");
+                    return RC_OK;
+                }
+                
+                System.out.println("INVALID");
+                return RC_UNKNOWN;
+            }
+            
+            // Create a new token.
+            final TOTPCredential tc = authenticator.createCredential();
+            System.out.println("Seed: " + Base32Support.encode(tc.getKey(), false));
+            System.out.println("Scratch Codes:");
+            tc.getScratchCodes().forEach(c -> System.out.println(c));
+            
+        } catch (final Exception e) {
+            if (args.isVerboseOutput()) {
+                getLogger().error("Unable to access TOTPAuthenticator from Spring context", e);
+            } else {
+                getLogger().error("Unable to access TOTPAuthenticator from Spring context", e.getMessage());
+            }
+            return RC_UNKNOWN;
+        }
+        
+        return RC_OK;
+    }
+
+    /**
+     * CLI entry point.
+     * 
+     * @param args arguments
+     */
+    public static void main(@Nonnull final String[] args) {
+        System.exit(new TOTPAuthenticatorCLI().run(args));
+    }
+    
+}
\ No newline at end of file

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


More information about the commits mailing list