Skip to content

Commit 3551cfe

Browse files
committed
Add tokenInit to import otpkey
1 parent 7785dd5 commit 3551cfe

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

src/main/java/org/privacyidea/PIConstants.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ private PIConstants()
5959
public static final String REALM = "realm";
6060
public static final String REALMS = "realms";
6161
public static final String GENKEY = "genkey";
62+
public static final String OTPKEY = "otpkey";
6263
public static final String RESULT = "result";
6364
public static final String VALUE = "value";
6465
public static final String TOKENS = "tokens";

src/main/java/org/privacyidea/PrivacyIDEA.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import static org.privacyidea.PIConstants.GENKEY;
4242
import static org.privacyidea.PIConstants.GET;
4343
import static org.privacyidea.PIConstants.HEADER_ORIGIN;
44+
import static org.privacyidea.PIConstants.OTPKEY;
4445
import static org.privacyidea.PIConstants.PASS;
4546
import static org.privacyidea.PIConstants.PASSWORD;
4647
import static org.privacyidea.PIConstants.POST;
@@ -379,6 +380,33 @@ public RolloutInfo tokenRollout(String username, String typeToEnroll)
379380
return parser.parseRolloutInfo(response);
380381
}
381382

383+
/**
384+
* Init a new token of the specified type for the specified user.
385+
* This requires a service account to be set. Currently, only HOTP and TOTP type token are supported.
386+
*
387+
* @param username username
388+
* @param typeToEnroll token type to enroll
389+
* @param otpKey secret to import
390+
* @return RolloutInfo which contains all info for the token or null if error
391+
*/
392+
public RolloutInfo tokenInit(String username, String typeToEnroll, String otpKey)
393+
{
394+
if (!serviceAccountAvailable())
395+
{
396+
error("Cannot do rollout without service account!");
397+
return null;
398+
}
399+
400+
Map<String, String> params = new LinkedHashMap<>();
401+
params.put(USER, username);
402+
params.put(TYPE, typeToEnroll);
403+
params.put(OTPKEY, otpKey); // Import the secret
404+
405+
String response = runRequestAsync(ENDPOINT_TOKEN_INIT, params, new LinkedHashMap<>(), true, POST);
406+
407+
return parser.parseRolloutInfo(response);
408+
}
409+
382410
private void appendRealm(Map<String, String> params)
383411
{
384412
if (configuration.realm != null && !configuration.realm.isEmpty())

0 commit comments

Comments
 (0)