encoders = new HashMap<>();
encoders.put(ENCODING_ID, new BCryptPasswordEncoder(12));
diff --git a/wise-api/src/main/java/com/wisemapping/security/GoogleAuthenticationProvider.java b/wise-api/src/main/java/com/wisemapping/security/GoogleAuthenticationProvider.java
new file mode 100644
index 00000000..2fc33fda
--- /dev/null
+++ b/wise-api/src/main/java/com/wisemapping/security/GoogleAuthenticationProvider.java
@@ -0,0 +1,59 @@
+package com.wisemapping.security;
+
+import org.jetbrains.annotations.NotNull;
+import org.springframework.security.authentication.BadCredentialsException;
+import org.springframework.security.core.Authentication;
+import org.springframework.security.core.AuthenticationException;
+import org.springframework.security.web.authentication.preauth.PreAuthenticatedAuthenticationToken;
+
+import com.wisemapping.model.Account;
+
+public class GoogleAuthenticationProvider implements org.springframework.security.authentication.AuthenticationProvider {
+
+ private UserDetailsService userDetailsService;
+
+ public GoogleAuthenticationProvider(@NotNull UserDetailsService userDetailsService) {
+ this.userDetailsService = userDetailsService;
+ }
+
+ /**
+ * Authenticate the given PreAuthenticatedAuthenticationToken.
+ *
+ * If the principal contained in the authentication object is null, the request will
+ * be ignored to allow other providers to authenticate it.
+ */
+ @Override
+ public Authentication authenticate(Authentication inputToken) throws AuthenticationException {
+ if (!supports(inputToken.getClass())) {
+ return null;
+ }
+ if (inputToken.getPrincipal() == null) {
+ throw new BadCredentialsException("No pre-authenticated principal found in request.");
+ }
+ UserDetails userDetails = userDetailsService.loadUserByUsername(inputToken.getName());
+ final Account user = userDetails.getUser();
+
+ if (!user.isActive()) {
+ throw new BadCredentialsException("User has been disabled for login " + inputToken.getName());
+ }
+
+ PreAuthenticatedAuthenticationToken resultToken = new PreAuthenticatedAuthenticationToken(userDetails,
+ inputToken.getCredentials(), userDetails.getAuthorities());
+ resultToken.setDetails(userDetails);
+
+ userDetailsService.getUserService().auditLogin(user);
+
+ return resultToken;
+ }
+
+ /**
+ * Indicate that this provider only supports PreAuthenticatedAuthenticationToken
+ * (sub)classes.
+ */
+ @Override
+ public final boolean supports(Class> authentication) {
+ return PreAuthenticatedAuthenticationToken.class.isAssignableFrom(authentication);
+ }
+
+
+}
diff --git a/wise-api/src/main/java/com/wisemapping/security/JwtTokenUtil.java b/wise-api/src/main/java/com/wisemapping/security/JwtTokenUtil.java
new file mode 100644
index 00000000..20841163
--- /dev/null
+++ b/wise-api/src/main/java/com/wisemapping/security/JwtTokenUtil.java
@@ -0,0 +1,87 @@
+package com.wisemapping.security;
+
+import io.jsonwebtoken.*;
+import io.jsonwebtoken.io.Decoders;
+import io.jsonwebtoken.security.Keys;
+import jakarta.servlet.http.HttpServletResponse;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.http.HttpHeaders;
+import org.springframework.security.core.userdetails.UserDetails;
+import org.springframework.stereotype.Component;
+
+import java.io.Serializable;
+import java.security.Key;
+import java.util.Date;
+
+@Component
+public class JwtTokenUtil implements Serializable {
+ final private Logger logger = LogManager.getLogger();
+ public final static String BEARER_TOKEN_PREFIX = "Bearer ";
+
+
+ @Value("${app.jwt.secret}")
+ private String jwtSecret;
+
+ @Value("${app.jwt.expirationMin}")
+ private int jwtExpirationMin;
+
+ @Autowired
+ private UserDetailsService userDetailsService;
+
+
+ public String generateJwtToken(@NotNull final UserDetails user) {
+ return Jwts.builder()
+ .setSubject((user.getUsername()))
+ .setIssuedAt(new Date())
+ .setExpiration(new Date((new Date()).getTime() + jwtExpirationMin * 1000L * 60))
+ .signWith(key(), SignatureAlgorithm.HS256)
+ .compact();
+ }
+
+ private Key key() {
+ return Keys.hmacShaKeyFor(Decoders.BASE64.decode(jwtSecret));
+ }
+
+
+ @Nullable
+ public String extractFromJwtToken(String token) {
+ return Jwts.parserBuilder().setSigningKey(key()).build()
+ .parseClaimsJws(token).getBody().getSubject();
+ }
+
+ public boolean validateJwtToken(@NotNull String authToken) {
+ boolean result = false;
+ try {
+ Jwts.parserBuilder().setSigningKey(key()).build().parse(authToken);
+ result = true;
+ } catch (MalformedJwtException e) {
+ logger.error("Invalid JWT token: {}", e.getMessage());
+ } catch (ExpiredJwtException e) {
+ logger.error("JWT token is expired: {}", e.getMessage());
+ } catch (UnsupportedJwtException e) {
+ logger.error("JWT token is unsupported: {}", e.getMessage());
+ } catch (IllegalArgumentException e) {
+ logger.error("JWT claims string is empty: {}", e.getMessage());
+ }
+
+ logger.trace("Is JWT token valid:" + result);
+ return result;
+ }
+
+ @NotNull
+ public String doLogin(@NotNull HttpServletResponse response, @NotNull String email) {
+ logger.debug("Performing login:" + email);
+ final UserDetails userDetails = userDetailsService.loadUserByUsername(email);
+
+ // Add JWT in the HTTP header ...
+ final String token = generateJwtToken(userDetails);
+ response.addHeader(HttpHeaders.AUTHORIZATION, BEARER_TOKEN_PREFIX + token);
+
+ return token;
+ }
+}
\ No newline at end of file
diff --git a/wise-webapp/src/main/java/com/wisemapping/security/LegacyPasswordEncoder.java b/wise-api/src/main/java/com/wisemapping/security/LegacyPasswordEncoder.java
similarity index 100%
rename from wise-webapp/src/main/java/com/wisemapping/security/LegacyPasswordEncoder.java
rename to wise-api/src/main/java/com/wisemapping/security/LegacyPasswordEncoder.java
diff --git a/wise-webapp/src/main/java/com/wisemapping/security/MapAccessPermission.java b/wise-api/src/main/java/com/wisemapping/security/MapAccessPermission.java
similarity index 100%
rename from wise-webapp/src/main/java/com/wisemapping/security/MapAccessPermission.java
rename to wise-api/src/main/java/com/wisemapping/security/MapAccessPermission.java
diff --git a/wise-webapp/src/main/java/com/wisemapping/security/MapAccessPermissionEvaluation.java b/wise-api/src/main/java/com/wisemapping/security/MapAccessPermissionEvaluation.java
similarity index 95%
rename from wise-webapp/src/main/java/com/wisemapping/security/MapAccessPermissionEvaluation.java
rename to wise-api/src/main/java/com/wisemapping/security/MapAccessPermissionEvaluation.java
index 868d5bf5..3fb3fd69 100644
--- a/wise-webapp/src/main/java/com/wisemapping/security/MapAccessPermissionEvaluation.java
+++ b/wise-api/src/main/java/com/wisemapping/security/MapAccessPermissionEvaluation.java
@@ -2,7 +2,7 @@ package com.wisemapping.security;
import com.wisemapping.model.Collaborator;
import com.wisemapping.model.Mindmap;
-import com.wisemapping.model.User;
+import com.wisemapping.model.Account;
import jakarta.validation.constraints.NotNull;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager;
@@ -36,7 +36,7 @@ public class MapAccessPermissionEvaluation implements PermissionEvaluator {
}
boolean result;
- final User user = Utils.getUser();
+ final Account user = Utils.getUser();
final MapAccessPermission perm = MapAccessPermission.valueOf((permission.toString().toUpperCase()));
if (targetDomainObject instanceof Integer) {
// Checking permissions by mapId ...
@@ -69,7 +69,7 @@ public class MapAccessPermissionEvaluation implements PermissionEvaluator {
private boolean hasPrivilege(@NotNull int mapId, @NotNull MapAccessPermission permission) {
boolean result;
- final User user = Utils.getUser();
+ final Account user = Utils.getUser();
if (MapAccessPermission.READ == permission) {
result = readAdvice.isAllowed(user, mapId);
} else {
@@ -80,7 +80,7 @@ public class MapAccessPermissionEvaluation implements PermissionEvaluator {
private boolean hasPrivilege(@NotNull Mindmap map, @NotNull MapAccessPermission permission) {
boolean result;
- final User user = Utils.getUser();
+ final Account user = Utils.getUser();
if (MapAccessPermission.READ == permission) {
result = readAdvice.isAllowed(user, map);
} else {
diff --git a/wise-webapp/src/main/java/com/wisemapping/security/MapPermissionsSecurityAdvice.java b/wise-api/src/main/java/com/wisemapping/security/MapPermissionsSecurityAdvice.java
similarity index 85%
rename from wise-webapp/src/main/java/com/wisemapping/security/MapPermissionsSecurityAdvice.java
rename to wise-api/src/main/java/com/wisemapping/security/MapPermissionsSecurityAdvice.java
index 33a986a5..242e2a0a 100755
--- a/wise-webapp/src/main/java/com/wisemapping/security/MapPermissionsSecurityAdvice.java
+++ b/wise-api/src/main/java/com/wisemapping/security/MapPermissionsSecurityAdvice.java
@@ -19,7 +19,7 @@
package com.wisemapping.security;
import com.wisemapping.model.Mindmap;
-import com.wisemapping.model.User;
+import com.wisemapping.model.Account;
import com.wisemapping.service.MindmapService;
import org.jetbrains.annotations.Nullable;
import org.springframework.beans.factory.annotation.Autowired;
@@ -27,9 +27,9 @@ import org.springframework.beans.factory.annotation.Autowired;
public abstract class MapPermissionsSecurityAdvice {
@Autowired private MindmapService mindmapService;
- protected abstract boolean isAllowed(@Nullable User user, Mindmap map);
+ protected abstract boolean isAllowed(@Nullable Account user, Mindmap map);
- protected abstract boolean isAllowed(@Nullable User user, int mapId);
+ protected abstract boolean isAllowed(@Nullable Account user, int mapId);
protected MindmapService getMindmapService() {
return mindmapService;
diff --git a/wise-webapp/src/main/java/com/wisemapping/security/ReadSecurityAdvise.java b/wise-api/src/main/java/com/wisemapping/security/ReadSecurityAdvise.java
similarity index 87%
rename from wise-webapp/src/main/java/com/wisemapping/security/ReadSecurityAdvise.java
rename to wise-api/src/main/java/com/wisemapping/security/ReadSecurityAdvise.java
index db545fa6..fd7b0c63 100755
--- a/wise-webapp/src/main/java/com/wisemapping/security/ReadSecurityAdvise.java
+++ b/wise-api/src/main/java/com/wisemapping/security/ReadSecurityAdvise.java
@@ -20,7 +20,7 @@ package com.wisemapping.security;
import com.wisemapping.model.CollaborationRole;
import com.wisemapping.model.Mindmap;
-import com.wisemapping.model.User;
+import com.wisemapping.model.Account;
import org.jetbrains.annotations.Nullable;
import org.springframework.stereotype.Component;
@@ -28,11 +28,11 @@ import org.springframework.stereotype.Component;
public class ReadSecurityAdvise
extends MapPermissionsSecurityAdvice {
- protected boolean isAllowed(@Nullable User user, Mindmap map) {
+ protected boolean isAllowed(@Nullable Account user, Mindmap map) {
return getMindmapService().hasPermissions(user, map, CollaborationRole.VIEWER);
}
- protected boolean isAllowed(@Nullable User user, int mapId) {
+ protected boolean isAllowed(@Nullable Account user, int mapId) {
return getMindmapService().hasPermissions(user, mapId, CollaborationRole.VIEWER);
}
}
diff --git a/wise-webapp/src/main/java/com/wisemapping/security/UpdateSecurityAdvise.java b/wise-api/src/main/java/com/wisemapping/security/UpdateSecurityAdvise.java
similarity index 89%
rename from wise-webapp/src/main/java/com/wisemapping/security/UpdateSecurityAdvise.java
rename to wise-api/src/main/java/com/wisemapping/security/UpdateSecurityAdvise.java
index ecac9c41..9bd4ea22 100755
--- a/wise-webapp/src/main/java/com/wisemapping/security/UpdateSecurityAdvise.java
+++ b/wise-api/src/main/java/com/wisemapping/security/UpdateSecurityAdvise.java
@@ -20,7 +20,7 @@ package com.wisemapping.security;
import com.wisemapping.model.CollaborationRole;
import com.wisemapping.model.Mindmap;
-import com.wisemapping.model.User;
+import com.wisemapping.model.Account;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.springframework.stereotype.Component;
@@ -30,7 +30,7 @@ public class UpdateSecurityAdvise
extends MapPermissionsSecurityAdvice {
@Override
- protected boolean isAllowed(@Nullable User user, @NotNull Mindmap map) {
+ protected boolean isAllowed(@Nullable Account user, @NotNull Mindmap map) {
boolean result;
if (map.getCreator() == null) {
// This means that the map is new and is an add operation.
@@ -42,7 +42,7 @@ public class UpdateSecurityAdvise
}
@Override
- protected boolean isAllowed(@Nullable User user, int mapId) {
+ protected boolean isAllowed(@Nullable Account user, int mapId) {
return getMindmapService().hasPermissions(user, mapId, CollaborationRole.EDITOR);
}
}
diff --git a/wise-webapp/src/main/java/com/wisemapping/security/UserDetails.java b/wise-api/src/main/java/com/wisemapping/security/UserDetails.java
similarity index 91%
rename from wise-webapp/src/main/java/com/wisemapping/security/UserDetails.java
rename to wise-api/src/main/java/com/wisemapping/security/UserDetails.java
index 8f4b2d38..842992d8 100644
--- a/wise-webapp/src/main/java/com/wisemapping/security/UserDetails.java
+++ b/wise-api/src/main/java/com/wisemapping/security/UserDetails.java
@@ -18,7 +18,7 @@
package com.wisemapping.security;
-import com.wisemapping.model.User;
+import com.wisemapping.model.Account;
import org.jetbrains.annotations.NotNull;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
@@ -27,10 +27,10 @@ import java.util.ArrayList;
import java.util.Collection;
public class UserDetails implements org.springframework.security.core.userdetails.UserDetails {
- private final com.wisemapping.model.User user;
+ private final Account user;
private final boolean isAdmin;
- public UserDetails(@NotNull final com.wisemapping.model.User user, boolean isAdmin) {
+ public UserDetails(@NotNull final Account user, boolean isAdmin) {
this.user = user;
this.isAdmin = isAdmin;
}
@@ -77,7 +77,7 @@ public class UserDetails implements org.springframework.security.core.userdetail
}
- public User getUser() {
+ public Account getUser() {
return user;
}
}
diff --git a/wise-webapp/src/main/java/com/wisemapping/security/UserDetailsService.java b/wise-api/src/main/java/com/wisemapping/security/UserDetailsService.java
similarity index 92%
rename from wise-webapp/src/main/java/com/wisemapping/security/UserDetailsService.java
rename to wise-api/src/main/java/com/wisemapping/security/UserDetailsService.java
index 42725a89..be4c95ca 100644
--- a/wise-webapp/src/main/java/com/wisemapping/security/UserDetailsService.java
+++ b/wise-api/src/main/java/com/wisemapping/security/UserDetailsService.java
@@ -19,11 +19,12 @@
package com.wisemapping.security;
-import com.wisemapping.model.User;
+import com.wisemapping.model.Account;
import com.wisemapping.service.UserService;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Value;
import org.springframework.dao.DataAccessException;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
@@ -34,11 +35,13 @@ public class UserDetailsService
implements org.springframework.security.core.userdetails.UserDetailsService {
@Autowired
private UserService userService;
+
+ @Value("${app.admin.user}")
private String adminUser;
@Override
public UserDetails loadUserByUsername(@NotNull String email) throws UsernameNotFoundException, DataAccessException {
- final User user = userService.getUserBy(email);
+ final Account user = userService.getUserBy(email);
if (user != null) {
return new UserDetails(user, isAdmin(email));
diff --git a/wise-webapp/src/main/java/com/wisemapping/security/Utils.java b/wise-api/src/main/java/com/wisemapping/security/Utils.java
similarity index 88%
rename from wise-webapp/src/main/java/com/wisemapping/security/Utils.java
rename to wise-api/src/main/java/com/wisemapping/security/Utils.java
index f5c68587..a8b4d8fb 100644
--- a/wise-webapp/src/main/java/com/wisemapping/security/Utils.java
+++ b/wise-api/src/main/java/com/wisemapping/security/Utils.java
@@ -18,8 +18,7 @@
package com.wisemapping.security;
-import com.wisemapping.model.User;
-import org.jetbrains.annotations.NotNull;
+import com.wisemapping.model.Account;
import org.jetbrains.annotations.Nullable;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
@@ -30,13 +29,12 @@ final public class Utils {
@SuppressWarnings({"ConstantConditions"})
@Nullable
- public static User getUser() {
+ public static Account getUser() {
return getUser(false);
}
- @NotNull
- public static User getUser(boolean forceCheck) {
- User result = null;
+ public static Account getUser(boolean forceCheck) {
+ Account result = null;
final Authentication auth = SecurityContextHolder.getContext().getAuthentication();
if (auth != null && auth.getDetails() != null)
{
diff --git a/wise-webapp/src/main/java/com/wisemapping/service/CollaborationException.java b/wise-api/src/main/java/com/wisemapping/service/CollaborationException.java
similarity index 100%
rename from wise-webapp/src/main/java/com/wisemapping/service/CollaborationException.java
rename to wise-api/src/main/java/com/wisemapping/service/CollaborationException.java
diff --git a/wise-webapp/src/main/java/com/wisemapping/service/HibernateUtil.java b/wise-api/src/main/java/com/wisemapping/service/HibernateUtil.java
similarity index 97%
rename from wise-webapp/src/main/java/com/wisemapping/service/HibernateUtil.java
rename to wise-api/src/main/java/com/wisemapping/service/HibernateUtil.java
index 600b3305..92e969e5 100755
--- a/wise-webapp/src/main/java/com/wisemapping/service/HibernateUtil.java
+++ b/wise-api/src/main/java/com/wisemapping/service/HibernateUtil.java
@@ -22,7 +22,6 @@ import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateUtil {
-
private static final SessionFactory sessionFactory;
static {
diff --git a/wise-webapp/src/main/java/com/wisemapping/service/InvalidActivationCodeException.java b/wise-api/src/main/java/com/wisemapping/service/InvalidActivationCodeException.java
similarity index 100%
rename from wise-webapp/src/main/java/com/wisemapping/service/InvalidActivationCodeException.java
rename to wise-api/src/main/java/com/wisemapping/service/InvalidActivationCodeException.java
diff --git a/wise-webapp/src/main/java/com/wisemapping/service/InvalidAuthSchemaException.java b/wise-api/src/main/java/com/wisemapping/service/InvalidAuthSchemaException.java
similarity index 100%
rename from wise-webapp/src/main/java/com/wisemapping/service/InvalidAuthSchemaException.java
rename to wise-api/src/main/java/com/wisemapping/service/InvalidAuthSchemaException.java
diff --git a/wise-webapp/src/main/java/com/wisemapping/service/InvalidUserEmailException.java b/wise-api/src/main/java/com/wisemapping/service/InvalidUserEmailException.java
similarity index 100%
rename from wise-webapp/src/main/java/com/wisemapping/service/InvalidUserEmailException.java
rename to wise-api/src/main/java/com/wisemapping/service/InvalidUserEmailException.java
diff --git a/wise-webapp/src/main/java/com/wisemapping/service/LabelService.java b/wise-api/src/main/java/com/wisemapping/service/LabelService.java
similarity index 65%
rename from wise-webapp/src/main/java/com/wisemapping/service/LabelService.java
rename to wise-api/src/main/java/com/wisemapping/service/LabelService.java
index e2ea8ec2..7687d22c 100644
--- a/wise-webapp/src/main/java/com/wisemapping/service/LabelService.java
+++ b/wise-api/src/main/java/com/wisemapping/service/LabelService.java
@@ -18,8 +18,8 @@
package com.wisemapping.service;
import com.wisemapping.exceptions.WiseMappingException;
-import com.wisemapping.model.Label;
-import com.wisemapping.model.User;
+import com.wisemapping.model.MindmapLabel;
+import com.wisemapping.model.Account;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -27,14 +27,14 @@ import java.util.List;
public interface LabelService {
- void addLabel(@NotNull final Label label, @NotNull final User user) throws WiseMappingException;
+ void addLabel(@NotNull final MindmapLabel label, @NotNull final Account user) throws WiseMappingException;
- @NotNull List
- The WiseMapping Team
+ The WiseMapping Team