Skip to content

Commit

Permalink
Add tokenInit to import otpkey (#60)
Browse files Browse the repository at this point in the history
  • Loading branch information
scpcom authored Jul 26, 2024
1 parent 05d4623 commit 49cae9d
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/main/java/org/privacyidea/PIConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
28 changes: 28 additions & 0 deletions src/main/java/org/privacyidea/PrivacyIDEA.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<String, String> 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<String, String> params)
{
if (configuration.realm != null && !configuration.realm.isEmpty())
Expand Down

0 comments on commit 49cae9d

Please sign in to comment.