Commit 8f7a8b73 authored by ls1947's avatar ls1947
Browse files

add om classes for totp authenticator token init

parent 9f51b775
......@@ -33,7 +33,8 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import edu.kit.scc.webreg.entity.UserEntity;
import edu.kit.scc.webreg.service.twofa.linotp.LinotpResponse;
import edu.kit.scc.webreg.service.twofa.linotp.LinotpInitAuthenticatorTokenResponse;
import edu.kit.scc.webreg.service.twofa.linotp.LinotpShowUserResponse;
public class LinotpConnection {
......@@ -92,7 +93,45 @@ public class LinotpConnection {
httpClient = HttpClients.custom().setDefaultRequestConfig(config).build();
}
public LinotpResponse getTokenList(UserEntity user) throws TwoFaException {
public LinotpInitAuthenticatorTokenResponse createAuthenticatorToken(UserEntity user) throws TwoFaException {
try {
HttpPost httpPost = new HttpPost(configMap.get("url") + "/admin/init");
List<NameValuePair> nvps = new ArrayList <NameValuePair>();
nvps.add(new BasicNameValuePair("session", adminSession));
nvps.add(new BasicNameValuePair("type", "totp"));
nvps.add(new BasicNameValuePair("otplen", "6"));
nvps.add(new BasicNameValuePair("genkey", "1"));
nvps.add(new BasicNameValuePair("hashlib", "sha256"));
nvps.add(new BasicNameValuePair("timeStep", "30"));
nvps.add(new BasicNameValuePair("description", "This is a description"));
if (configMap.containsKey("userId"))
nvps.add(new BasicNameValuePair("user", configMap.get("userId")));
else
nvps.add(new BasicNameValuePair("user", user.getEppn()));
if (configMap.containsKey("realm"))
nvps.add(new BasicNameValuePair("realm", configMap.get("realm")));
httpPost.setEntity(new UrlEncodedFormEntity(nvps));
CloseableHttpResponse response = httpClient.execute(targetHost, httpPost, context);
try {
HttpEntity entity = response.getEntity();
String responseString = EntityUtils.toString(entity);
logger.debug(responseString);
return resultParser.parseInitAuthenticatorTokenResponse(responseString);
} finally {
response.close();
}
} catch (ParseException | IOException e) {
throw new TwoFaException(e);
}
}
public LinotpShowUserResponse getTokenList(UserEntity user) throws TwoFaException {
try {
HttpPost httpPost = new HttpPost(configMap.get("url") + "/admin/show");
......@@ -112,8 +151,7 @@ public class LinotpConnection {
String responseString = EntityUtils.toString(entity);
logger.debug(responseString);
resultParser.parseResult(responseString);
return resultParser.getResponse();
return resultParser.parseShowUserResponse(responseString);
} finally {
response.close();
......
......@@ -5,27 +5,35 @@ import java.io.IOException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import edu.kit.scc.webreg.service.twofa.linotp.LinotpResponse;
import edu.kit.scc.webreg.service.twofa.linotp.LinotpInitAuthenticatorTokenResponse;
import edu.kit.scc.webreg.service.twofa.linotp.LinotpShowUserResponse;
public class LinotpResultParser {
private ObjectMapper om;
private LinotpResponse response;
public LinotpResultParser() {
om = new ObjectMapper();
om.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
}
public void parseResult(String responseString) throws TwoFaException {
public LinotpShowUserResponse parseShowUserResponse(String responseString) throws TwoFaException {
try {
response = om.readValue(responseString, LinotpResponse.class);
LinotpShowUserResponse response = om.readValue(responseString, LinotpShowUserResponse.class);
return response;
} catch (IOException e) {
throw new TwoFaException(e);
}
}
public LinotpResponse getResponse() {
return response;
public LinotpInitAuthenticatorTokenResponse parseInitAuthenticatorTokenResponse(String responseString) throws TwoFaException {
try {
LinotpInitAuthenticatorTokenResponse response =
om.readValue(responseString, LinotpInitAuthenticatorTokenResponse.class);
return response;
} catch (IOException e) {
throw new TwoFaException(e);
}
}
}
package edu.kit.scc.webreg.service.twofa;
import edu.kit.scc.webreg.service.twofa.linotp.LinotpInitAuthenticatorTokenResponse;
public interface TwoFaService {
LinotpTokenResultList findByUserId(Long userId) throws TwoFaException;
LinotpInitAuthenticatorTokenResponse createAuthenticatorToken(Long userId) throws TwoFaException;
}
......@@ -9,7 +9,8 @@ import org.slf4j.Logger;
import edu.kit.scc.webreg.dao.UserDao;
import edu.kit.scc.webreg.entity.UserEntity;
import edu.kit.scc.webreg.service.twofa.linotp.LinotpResponse;
import edu.kit.scc.webreg.service.twofa.linotp.LinotpInitAuthenticatorTokenResponse;
import edu.kit.scc.webreg.service.twofa.linotp.LinotpShowUserResponse;
@Stateless
public class TwoFaServiceImpl implements TwoFaService {
......@@ -32,7 +33,7 @@ public class TwoFaServiceImpl implements TwoFaService {
LinotpConnection linotpConnection = new LinotpConnection(configMap);
linotpConnection.requestAdminSession();
LinotpResponse response = linotpConnection.getTokenList(user);
LinotpShowUserResponse response = linotpConnection.getTokenList(user);
LinotpTokenResultList resultList = new LinotpTokenResultList();
if (response.getResult() != null && response.getResult().getValue() != null &&
response.getResult().getValue().getData() !=null) {
......@@ -53,6 +54,18 @@ public class TwoFaServiceImpl implements TwoFaService {
return resultList;
}
@Override
public LinotpInitAuthenticatorTokenResponse createAuthenticatorToken(Long userId) throws TwoFaException {
UserEntity user = userDao.findById(userId);
Map<String, String> configMap = configResolver.resolveConfig(user);
LinotpConnection linotpConnection = new LinotpConnection(configMap);
linotpConnection.requestAdminSession();
LinotpInitAuthenticatorTokenResponse response = linotpConnection.createAuthenticatorToken(user);
return response;
}
}
package edu.kit.scc.webreg.service.twofa.linotp;
import java.io.Serializable;
public class LinotpInitAuthenticatorTokenDetail implements Serializable {
private static final long serialVersionUID = 1L;
private LinotpInitAuthenticatorTokenOtpKey googleurl;
private String serial;
private LinotpInitAuthenticatorTokenOtpKey otpkey;
public LinotpInitAuthenticatorTokenOtpKey getGoogleurl() {
return googleurl;
}
public void setGoogleurl(LinotpInitAuthenticatorTokenOtpKey googleurl) {
this.googleurl = googleurl;
}
public String getSerial() {
return serial;
}
public void setSerial(String serial) {
this.serial = serial;
}
public LinotpInitAuthenticatorTokenOtpKey getOtpkey() {
return otpkey;
}
public void setOtpkey(LinotpInitAuthenticatorTokenOtpKey otpkey) {
this.otpkey = otpkey;
}
}
package edu.kit.scc.webreg.service.twofa.linotp;
import java.io.Serializable;
public class LinotpInitAuthenticatorTokenOtpKey implements Serializable {
private static final long serialVersionUID = 1L;
private String img;
private String order;
private String value;
private String description;
public String getImg() {
return img;
}
public void setImg(String img) {
this.img = img;
}
public String getOrder() {
return order;
}
public void setOrder(String order) {
this.order = order;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
package edu.kit.scc.webreg.service.twofa.linotp;
import java.io.Serializable;
import com.fasterxml.jackson.annotation.JsonProperty;
public class LinotpInitAuthenticatorTokenResponse implements Serializable {
private static final long serialVersionUID = 1L;
private String version;
@JsonProperty("jsonrpc")
private String jsonRpc;
private LinotpResult result;
private LinotpInitAuthenticatorTokenDetail detail;
private Integer id;
public String getVersion() {
return version;
}
public void setVersion(String version) {
this.version = version;
}
public String getJsonRpc() {
return jsonRpc;
}
public void setJsonRpc(String jsonRpc) {
this.jsonRpc = jsonRpc;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public LinotpResult getResult() {
return result;
}
public void setResult(LinotpResult result) {
this.result = result;
}
public LinotpInitAuthenticatorTokenDetail getDetail() {
return detail;
}
public void setDetail(LinotpInitAuthenticatorTokenDetail detail) {
this.detail = detail;
}
}
......@@ -3,9 +3,8 @@ package edu.kit.scc.webreg.service.twofa.linotp;
public class LinotpResult {
private boolean status;
private boolean value;
private LinotpValue value;
public boolean isStatus() {
return status;
}
......@@ -14,13 +13,12 @@ public class LinotpResult {
this.status = status;
}
public LinotpValue getValue() {
public boolean isValue() {
return value;
}
public void setValue(LinotpValue value) {
public void setValue(boolean value) {
this.value = value;
}
}
......@@ -4,7 +4,7 @@ import java.io.Serializable;
import com.fasterxml.jackson.annotation.JsonProperty;
public class LinotpResponse implements Serializable {
public class LinotpShowUserResponse implements Serializable {
private static final long serialVersionUID = 1L;
......@@ -13,7 +13,7 @@ public class LinotpResponse implements Serializable {
@JsonProperty("jsonrpc")
private String jsonRpc;
private LinotpResult result;
private LinotpValueResult result;
private Integer id;
......@@ -33,11 +33,11 @@ public class LinotpResponse implements Serializable {
this.jsonRpc = jsonRpc;
}
public LinotpResult getResult() {
public LinotpValueResult getResult() {
return result;
}
public void setResult(LinotpResult result) {
public void setResult(LinotpValueResult result) {
this.result = result;
}
......
package edu.kit.scc.webreg.service.twofa.linotp;
public class LinotpValueResult {
private boolean status;
private LinotpValue value;
public boolean isStatus() {
return status;
}
public void setStatus(boolean status) {
this.status = status;
}
public LinotpValue getValue() {
return value;
}
public void setValue(LinotpValue value) {
this.value = value;
}
}
......@@ -24,6 +24,7 @@ import edu.kit.scc.webreg.service.UserService;
import edu.kit.scc.webreg.service.twofa.LinotpTokenResultList;
import edu.kit.scc.webreg.service.twofa.TwoFaException;
import edu.kit.scc.webreg.service.twofa.TwoFaService;
import edu.kit.scc.webreg.service.twofa.linotp.LinotpInitAuthenticatorTokenResponse;
import edu.kit.scc.webreg.session.SessionManager;
import edu.kit.scc.webreg.util.FacesMessageGenerator;
......@@ -33,7 +34,6 @@ public class TwoFaUserBean implements Serializable {
private static final long serialVersionUID = 1L;
@Inject
private Logger logger;
......@@ -51,6 +51,7 @@ public class TwoFaUserBean implements Serializable {
private UserEntity user;
private LinotpTokenResultList tokenList;
private LinotpInitAuthenticatorTokenResponse createTokenResponse;
public void preRenderView(ComponentSystemEvent ev) {
if (user == null) {
......@@ -64,6 +65,14 @@ public class TwoFaUserBean implements Serializable {
}
}
public void createAuthenticatorToken() {
try {
createTokenResponse = twoFaService.createAuthenticatorToken(user.getId());
} catch (TwoFaException e) {
logger.warn("TwoFaException", e);
}
}
public Boolean getReadOnly() {
return tokenList.getReadOnly();
}
......@@ -80,4 +89,8 @@ public class TwoFaUserBean implements Serializable {
return user;
}
public LinotpInitAuthenticatorTokenResponse getCreateTokenResponse() {
return createTokenResponse;
}
}
......@@ -37,9 +37,17 @@
</p:panel>
<p:panel header="#{messages.twofa_list}" rendered="#{! twoFaUserBean.readOnly}">
<p:repeat var="token" value="#{twoFaUserBean.tokenList}">
<div><h:outputText value="#{token.serial}" /></div>
</p:repeat>
<p:panelGrid columns="2">
<p:repeat var="token" value="#{twoFaUserBean.tokenList}">
<p:panel>
<div><h:outputText value="#{token.serial}" /></div>
<div><h:outputText value="#{token.tokenType}" /></div>
<div><h:outputText value="#{token.isactive}" /></div>
</p:panel>
</p:repeat>
</p:panelGrid>
<p:commandButton action="#{twoFaUserBean.createAuthenticatorToken()}" value="Create"></p:commandButton>
</p:panel>
</h:form>
</ui:define>
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment