-
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 authenticarion
Signed-off-by: Smruti Prakash Sahoo <[email protected]>
- Loading branch information
Showing
17 changed files
with
321 additions
and
12 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
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
38 changes: 38 additions & 0 deletions
38
...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,38 @@ | ||
/* | ||
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.Authentication; | ||
import org.springframework.security.core.context.SecurityContextHolder; | ||
import org.springframework.security.core.userdetails.UserDetails; | ||
import org.springframework.security.core.userdetails.UserDetailsService; | ||
import org.springframework.security.core.userdetails.UsernameNotFoundException; | ||
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)); | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
...g/eclipse/sw360/rest/resourceserver/security/basic/Sw360GrantedAuthoritiesCalculator.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,37 @@ | ||
/* | ||
SPDX-FileCopyrightText: © 2022 Siemens AG | ||
SPDX-License-Identifier: EPL-2.0 | ||
*/ | ||
package org.eclipse.sw360.rest.resourceserver.security.basic; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import org.eclipse.sw360.datahandler.permissions.PermissionUtils; | ||
import org.eclipse.sw360.datahandler.thrift.users.User; | ||
import org.eclipse.sw360.rest.resourceserver.Sw360ResourceServer; | ||
import org.springframework.security.core.GrantedAuthority; | ||
import org.springframework.security.core.authority.SimpleGrantedAuthority; | ||
|
||
/** | ||
* This class offer helper methods to calculate the {@GrantedAuthority} for a | ||
* user. | ||
*/ | ||
public class Sw360GrantedAuthoritiesCalculator { | ||
|
||
public static List<GrantedAuthority> generateFromUser(User user) { | ||
List<GrantedAuthority> grantedAuthorities = new ArrayList<>(); | ||
|
||
grantedAuthorities.add(new SimpleGrantedAuthority(Sw360GrantedAuthority.READ.getAuthority())); | ||
if (user != null) { | ||
if (PermissionUtils.isUserAtLeast(Sw360ResourceServer.CONFIG_WRITE_ACCESS_USERGROUP, user)) { | ||
grantedAuthorities.add(new SimpleGrantedAuthority(Sw360GrantedAuthority.WRITE.getAuthority())); | ||
} | ||
if (PermissionUtils.isUserAtLeast(Sw360ResourceServer.CONFIG_ADMIN_ACCESS_USERGROUP, user)) { | ||
grantedAuthorities.add(new SimpleGrantedAuthority(Sw360GrantedAuthority.ADMIN.getAuthority())); | ||
} | ||
} | ||
|
||
return grantedAuthorities; | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
...main/java/org/eclipse/sw360/rest/resourceserver/security/basic/Sw360GrantedAuthority.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,17 @@ | ||
/* | ||
SPDX-FileCopyrightText: © 2022 Siemens AG | ||
SPDX-License-Identifier: EPL-2.0 | ||
*/ | ||
package org.eclipse.sw360.rest.resourceserver.security.basic; | ||
|
||
import org.springframework.security.core.GrantedAuthority; | ||
|
||
public enum Sw360GrantedAuthority implements GrantedAuthority { | ||
|
||
BASIC, READ, WRITE, ADMIN; | ||
|
||
@Override | ||
public String getAuthority() { | ||
return toString(); | ||
} | ||
} |
Oops, something went wrong.