From 49cae9d4deb46c11cb4d06094a4248b09eb01041 Mon Sep 17 00:00:00 2001 From: scpcom Date: Fri, 26 Jul 2024 12:29:24 +0200 Subject: [PATCH] Add tokenInit to import otpkey (#60) --- .../java/org/privacyidea/PIConstants.java | 1 + .../java/org/privacyidea/PrivacyIDEA.java | 28 +++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/src/main/java/org/privacyidea/PIConstants.java b/src/main/java/org/privacyidea/PIConstants.java index b667fd1..8fe17cd 100644 --- a/src/main/java/org/privacyidea/PIConstants.java +++ b/src/main/java/org/privacyidea/PIConstants.java @@ -59,6 +59,7 @@ private PIConstants() public static final String REALM = "realm"; public static final String REALMS = "realms"; public static final String GENKEY = "genkey"; + public static final String OTPKEY = "otpkey"; public static final String RESULT = "result"; public static final String VALUE = "value"; public static final String TOKENS = "tokens"; diff --git a/src/main/java/org/privacyidea/PrivacyIDEA.java b/src/main/java/org/privacyidea/PrivacyIDEA.java index 4081a0a..62d736e 100644 --- a/src/main/java/org/privacyidea/PrivacyIDEA.java +++ b/src/main/java/org/privacyidea/PrivacyIDEA.java @@ -41,6 +41,7 @@ import static org.privacyidea.PIConstants.GENKEY; import static org.privacyidea.PIConstants.GET; import static org.privacyidea.PIConstants.HEADER_ORIGIN; +import static org.privacyidea.PIConstants.OTPKEY; import static org.privacyidea.PIConstants.PASS; import static org.privacyidea.PIConstants.PASSWORD; import static org.privacyidea.PIConstants.POST; @@ -379,6 +380,33 @@ public RolloutInfo tokenRollout(String username, String typeToEnroll) return parser.parseRolloutInfo(response); } + /** + * Init a new token of the specified type for the specified user. + * This requires a service account to be set. Currently, only HOTP and TOTP type token are supported. + * + * @param username username + * @param typeToEnroll token type to enroll + * @param otpKey secret to import + * @return RolloutInfo which contains all info for the token or null if error + */ + public RolloutInfo tokenInit(String username, String typeToEnroll, String otpKey) + { + if (!serviceAccountAvailable()) + { + error("Cannot do rollout without service account!"); + return null; + } + + Map params = new LinkedHashMap<>(); + params.put(USER, username); + params.put(TYPE, typeToEnroll); + params.put(OTPKEY, otpKey); // Import the secret + + String response = runRequestAsync(ENDPOINT_TOKEN_INIT, params, new LinkedHashMap<>(), true, POST); + + return parser.parseRolloutInfo(response); + } + private void appendRealm(Map params) { if (configuration.realm != null && !configuration.realm.isEmpty())