diff --git a/docs-core/src/main/java/com/sismics/docs/core/model/jpa/AuthenticationToken.java b/docs-core/src/main/java/com/sismics/docs/core/model/jpa/AuthenticationToken.java index b14a7a83..7d87930c 100644 --- a/docs-core/src/main/java/com/sismics/docs/core/model/jpa/AuthenticationToken.java +++ b/docs-core/src/main/java/com/sismics/docs/core/model/jpa/AuthenticationToken.java @@ -29,6 +29,18 @@ public class AuthenticationToken { @Column(name = "AUT_IDUSER_C", nullable = false, length = 36) private String userId; + /** + * Login IP. + */ + @Column(name = "AUT_IP_C", nullable = true, length = 45) + private String ip; + + /** + * Login user agent. + */ + @Column(name = "AUT_UA_C", nullable = true, length = 1000) + private String userAgent; + /** * Remember the user next time (long lasted session). */ @@ -100,6 +112,38 @@ public class AuthenticationToken { public void setLongLasted(boolean longLasted) { this.longLasted = longLasted; } + + /** + * Getter of ip. + * @return ip + */ + public String getIp() { + return ip; + } + + /** + * Setter of ip. + * @param ip ip + */ + public void setIp(String ip) { + this.ip = ip; + } + + /** + * Getter of userAgent. + * @return userAgent + */ + public String getUserAgent() { + return userAgent; + } + + /** + * Setter of userAgent. + * @param userAgent userAgent + */ + public void setUserAgent(String userAgent) { + this.userAgent = userAgent; + } /** * Getter of creationDate. @@ -142,6 +186,8 @@ public class AuthenticationToken { return Objects.toStringHelper(this) .add("id", "**hidden**") .add("userId", userId) + .add("ip", ip) + .add("userAgent", userAgent) .add("longLasted", longLasted) .toString(); } diff --git a/docs-core/src/main/resources/config.properties b/docs-core/src/main/resources/config.properties index edf8e6a4..592e6288 100644 --- a/docs-core/src/main/resources/config.properties +++ b/docs-core/src/main/resources/config.properties @@ -1 +1 @@ -db.version=9 \ No newline at end of file +db.version=10 \ No newline at end of file diff --git a/docs-core/src/main/resources/db/update/dbupdate-010-0.sql b/docs-core/src/main/resources/db/update/dbupdate-010-0.sql new file mode 100644 index 00000000..78ee9cee --- /dev/null +++ b/docs-core/src/main/resources/db/update/dbupdate-010-0.sql @@ -0,0 +1,4 @@ +alter table T_FILE alter column FIL_IDUSER_C set not null; +alter table T_AUTHENTICATION_TOKEN add column AUT_IP_C varchar(45); +alter table T_AUTHENTICATION_TOKEN add column AUT_UA_C varchar(1000); +update T_CONFIG set CFG_VALUE_C='10' where CFG_ID_C='DB_VERSION'; \ No newline at end of file diff --git a/docs-web/src/dev/resources/config.properties b/docs-web/src/dev/resources/config.properties index 04b5153a..f935e8fa 100644 --- a/docs-web/src/dev/resources/config.properties +++ b/docs-web/src/dev/resources/config.properties @@ -1,3 +1,3 @@ api.current_version=${project.version} api.min_version=1.0 -db.version=9 \ No newline at end of file +db.version=10 \ No newline at end of file diff --git a/docs-web/src/main/java/com/sismics/docs/rest/resource/UserResource.java b/docs-web/src/main/java/com/sismics/docs/rest/resource/UserResource.java index 44939ef4..78ebe922 100644 --- a/docs-web/src/main/java/com/sismics/docs/rest/resource/UserResource.java +++ b/docs-web/src/main/java/com/sismics/docs/rest/resource/UserResource.java @@ -1,5 +1,6 @@ package com.sismics.docs.rest.resource; +import com.google.common.base.Strings; import com.sismics.docs.core.constant.Constants; import com.sismics.docs.core.dao.jpa.AuthenticationTokenDao; import com.sismics.docs.core.dao.jpa.RoleBaseFunctionDao; @@ -288,12 +289,20 @@ public class UserResource extends BaseResource { if (userId == null) { throw new ForbiddenClientException(); } - + + // Get the remote IP + String ip = request.getHeader("x-forwarded-for"); + if (Strings.isNullOrEmpty(ip)) { + ip = request.getRemoteAddr(); + } + // Create a new session token AuthenticationTokenDao authenticationTokenDao = new AuthenticationTokenDao(); AuthenticationToken authenticationToken = new AuthenticationToken(); authenticationToken.setUserId(userId); authenticationToken.setLongLasted(longLasted); + authenticationToken.setIp(ip); + authenticationToken.setUserAgent(StringUtils.abbreviate(request.getHeader("user-agent"), 1000)); String token = authenticationTokenDao.create(authenticationToken); // Cleanup old session tokens @@ -566,6 +575,8 @@ public class UserResource extends BaseResource { for (AuthenticationToken authenticationToken : authenticationTokenDao.getByUserId(principal.getId())) { JSONObject session = new JSONObject(); session.put("create_date", authenticationToken.getCreationDate().getTime()); + session.put("ip", authenticationToken.getIp()); + session.put("user_agent", authenticationToken.getUserAgent()); if (authenticationToken.getLastConnectionDate() != null) { session.put("last_connection_date", authenticationToken.getLastConnectionDate().getTime()); } diff --git a/docs-web/src/main/webapp/src/partial/docs/settings.session.html b/docs-web/src/main/webapp/src/partial/docs/settings.session.html index e2f1794d..8d32a86d 100644 --- a/docs-web/src/main/webapp/src/partial/docs/settings.session.html +++ b/docs-web/src/main/webapp/src/partial/docs/settings.session.html @@ -4,6 +4,7 @@ Created date Last connection date + From Current @@ -11,6 +12,7 @@ {{ session.create_date | date: 'yyyy-MM-dd HH:mm' }} {{ session.last_connection_date | date: 'yyyy-MM-dd HH:mm' }} + {{ session.ip }} diff --git a/docs-web/src/prod/resources/config.properties b/docs-web/src/prod/resources/config.properties index 04b5153a..f935e8fa 100644 --- a/docs-web/src/prod/resources/config.properties +++ b/docs-web/src/prod/resources/config.properties @@ -1,3 +1,3 @@ api.current_version=${project.version} api.min_version=1.0 -db.version=9 \ No newline at end of file +db.version=10 \ No newline at end of file diff --git a/docs-web/src/test/java/com/sismics/docs/rest/TestUserResource.java b/docs-web/src/test/java/com/sismics/docs/rest/TestUserResource.java index b36f3105..022f4b3f 100644 --- a/docs-web/src/test/java/com/sismics/docs/rest/TestUserResource.java +++ b/docs-web/src/test/java/com/sismics/docs/rest/TestUserResource.java @@ -144,6 +144,9 @@ public class TestUserResource extends BaseJerseyTest { Assert.assertEquals(Status.OK, Status.fromStatusCode(response.getStatus())); json = response.getEntity(JSONObject.class); Assert.assertTrue(json.getJSONArray("sessions").length() > 0); + JSONObject session = json.getJSONArray("sessions").getJSONObject(0); + Assert.assertEquals("127.0.0.1", session.getString("ip")); + Assert.assertTrue(session.getString("user_agent").startsWith("Java")); // Delete all sessions userResource = resource().path("/user/session");