1
0
mirror of https://github.com/sismics/docs.git synced 2026-04-13 04:18:49 +00:00

#84: Generate TOTP secret key

This commit is contained in:
jendib
2016-03-22 01:18:18 +01:00
parent 5de77e35dc
commit 718728a672
15 changed files with 69 additions and 29 deletions

View File

@@ -55,6 +55,8 @@ import com.sismics.rest.util.JsonUtil;
import com.sismics.rest.util.ValidationUtil;
import com.sismics.security.UserPrincipal;
import com.sismics.util.filter.TokenBasedSecurityFilter;
import com.warrenstrange.googleauth.GoogleAuthenticator;
import com.warrenstrange.googleauth.GoogleAuthenticatorKey;
/**
* User REST resources.
@@ -639,6 +641,29 @@ public class UserResource extends BaseResource {
return Response.ok().entity(response.build()).build();
}
@POST
@Path("enable_totp")
public Response enableTotp() {
if (!authenticate()) {
throw new ForbiddenClientException();
}
// Create a new TOTP key and scratch codes
// TODO Copy library sources here to scrap useless dependencies and make verification code generation public for testing
GoogleAuthenticator gAuth = new GoogleAuthenticator();
final GoogleAuthenticatorKey key = gAuth.createCredentials();
JsonArrayBuilder scratchCodes = Json.createArrayBuilder();
for (int scratchCode : key.getScratchCodes()) {
scratchCodes.add(scratchCode);
}
JsonObjectBuilder response = Json.createObjectBuilder()
.add("secret", key.getKey())
.add("scratch_codes", scratchCodes);
return Response.ok().entity(response.build()).build();
}
/**
* Returns the authentication token value.
*