-
Notifications
You must be signed in to change notification settings - Fork 101
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(rest): Added basic username and password based authentication
Signed-off-by: Smruti Prakash Sahoo <[email protected]>
- Loading branch information
Showing
20 changed files
with
380 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,10 @@ | |
|
||
|
||
public class UserHandlerTest { | ||
private static final String DUMMY_LASTNAME = "Dummy Lastname"; | ||
|
||
private static final String DUMMY_GIVENNAME = "Dummy Givenname"; | ||
|
||
private static final String dbName = DatabaseSettingsTest.COUCH_DB_USERS; | ||
|
||
private static final String DUMMY_EMAIL_ADDRESS_1 = "[email protected]"; | ||
|
@@ -47,7 +51,7 @@ public void tearDown() throws Exception { | |
|
||
@Test | ||
public void testAddUser() throws Exception { | ||
User userWithComment = new User().setEmail(DUMMY_EMAIL_ADDRESS_1).setCommentMadeDuringModerationRequest(DUMMY_COMMENT); | ||
User userWithComment = new User().setEmail(DUMMY_EMAIL_ADDRESS_1).setCommentMadeDuringModerationRequest(DUMMY_COMMENT).setGivenname(DUMMY_GIVENNAME).setLastname(DUMMY_LASTNAME).setDepartment(DUMMY_DEPARTMENT); | ||
|
||
handler.addUser(userWithComment); | ||
|
||
|
@@ -58,7 +62,7 @@ public void testAddUser() throws Exception { | |
|
||
@Test | ||
public void testUpdateUser() throws Exception { | ||
User userWithoutComment = new User().setEmail(DUMMY_EMAIL_ADDRESS_2); | ||
User userWithoutComment = new User().setEmail(DUMMY_EMAIL_ADDRESS_2).setGivenname(DUMMY_GIVENNAME).setLastname(DUMMY_LASTNAME).setDepartment(DUMMY_DEPARTMENT); | ||
|
||
handler.addUser(userWithoutComment); // does not contain a comment | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
...ce-server/src/main/java/org/eclipse/sw360/rest/resourceserver/filter/EndpointsFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
SPDX-FileCopyrightText: © 2022 Siemens AG | ||
SPDX-License-Identifier: EPL-2.0 | ||
*/ | ||
package org.eclipse.sw360.rest.resourceserver.filter; | ||
|
||
import java.io.IOException; | ||
|
||
import javax.servlet.FilterChain; | ||
import javax.servlet.ServletException; | ||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
|
||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.filter.OncePerRequestFilter; | ||
|
||
@Component | ||
public class EndpointsFilter extends OncePerRequestFilter { | ||
|
||
private static final String ERROR_MESSAGE = "Service is disabled"; | ||
|
||
private static final String CREATE_USER_ENDPOINT = "/resource/api/users"; | ||
|
||
@Value("${sw360.rest.api.createuser.disabled}") | ||
boolean disabledUsrCreation; | ||
|
||
@Override | ||
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) | ||
throws ServletException, IOException { | ||
String requestURI = request.getRequestURI(); | ||
String method = request.getMethod(); | ||
|
||
if (disabledUsrCreation && requestURI.equalsIgnoreCase(CREATE_USER_ENDPOINT) && method.equals("POST")) { | ||
response.sendError(HttpStatus.SERVICE_UNAVAILABLE.value(), ERROR_MESSAGE); | ||
} else { | ||
filterChain.doFilter(request, response); | ||
} | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
...a/org/eclipse/sw360/rest/resourceserver/security/basic/Sw360CustomUserDetailsService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
SPDX-FileCopyrightText: © 2022 Siemens AG | ||
SPDX-License-Identifier: EPL-2.0 | ||
*/ | ||
package org.eclipse.sw360.rest.resourceserver.security.basic; | ||
|
||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
import org.eclipse.sw360.datahandler.thrift.users.User; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.context.annotation.Profile; | ||
import org.springframework.security.core.userdetails.UserDetails; | ||
import org.springframework.security.core.userdetails.UserDetailsService; | ||
import org.springframework.stereotype.Component; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
|
||
@Profile("!SECURITY_MOCK") | ||
@Component | ||
@RequiredArgsConstructor(onConstructor = @__(@Autowired)) | ||
public class Sw360CustomUserDetailsService implements UserDetailsService { | ||
|
||
private static final Logger log = LogManager.getLogger(Sw360CustomUserDetailsService.class); | ||
|
||
@Autowired | ||
Sw360UserDetailsProvider sw360UserDetailsProvider; | ||
|
||
@Override | ||
public UserDetails loadUserByUsername(String userid) { | ||
log.info("Authenticating for the user with username {}", userid); | ||
User user = sw360UserDetailsProvider.provideUserDetails(userid, null); | ||
return new org.springframework.security.core.userdetails.User(user.getEmail(), user.getPassword(), | ||
Sw360GrantedAuthoritiesCalculator.generateFromUser(user)); | ||
} | ||
} |
Oops, something went wrong.