-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
57a2ea5
commit e5ac522
Showing
9 changed files
with
180 additions
and
74 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,60 +6,57 @@ | |
import org.springframework.security.authentication.BadCredentialsException; | ||
import org.springframework.security.core.Authentication; | ||
import org.springframework.security.core.AuthenticationException; | ||
import org.springframework.security.core.userdetails.UserDetails; | ||
import org.springframework.security.core.userdetails.UsernameNotFoundException; | ||
import org.springframework.security.provisioning.UserDetailsManager; | ||
|
||
import java.util.Arrays; | ||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class AppSecAuthenticationProvider implements AuthenticationProvider { | ||
|
||
private static final Map<String, AppSecUser> USERS = new HashMap<>(); | ||
private final UserDetailsManager userDetailsManager; | ||
|
||
static { | ||
Arrays.asList( | ||
new AppSecUser("social-security-id", "test", "1234", "[email protected]"), | ||
new AppSecUser("591dc126-8431-4d0f-9509-b23318d3dce4", "testuuid", "1234", "[email protected]") | ||
).forEach(user -> USERS.put(user.getUsername(), user)); | ||
public AppSecAuthenticationProvider(final UserDetailsManager userDetailsManager) { | ||
this.userDetailsManager = userDetailsManager; | ||
} | ||
|
||
@Override | ||
public Authentication authenticate(Authentication authentication) throws AuthenticationException { | ||
AppSecSdkToken token = (AppSecSdkToken) authentication; | ||
AppSecToken token = (AppSecToken) authentication; | ||
if (token.getSdkEvent() == null) { | ||
return loginUserPassword(token); | ||
} else { | ||
return loginSdk(token); | ||
} | ||
} | ||
|
||
private Authentication loginUserPassword(final AppSecSdkToken auth) { | ||
private Authentication loginUserPassword(final AppSecToken auth) { | ||
String username = auth.getName(); | ||
if (!USERS.containsKey(username)) { | ||
if (!userDetailsManager.userExists(username)) { | ||
throw new UsernameNotFoundException(username); | ||
} | ||
final AppSecUser user = USERS.get(username); | ||
final AppSecUser user = (AppSecUser) userDetailsManager.loadUserByUsername(username); | ||
if (!user.getPassword().equals(auth.getCredentials())) { | ||
throw new BadCredentialsException(username); | ||
} | ||
return new AppSecSdkToken(new AppSecUser(user), auth.getCredentials(), Collections.emptyList()); | ||
return new AppSecToken(new AppSecUser(user), auth.getCredentials(), Collections.emptyList()); | ||
} | ||
|
||
private Authentication loginSdk(final AppSecSdkToken auth) { | ||
String username = auth.getSdkUser(); | ||
private Authentication loginSdk(final AppSecToken auth) { | ||
Map<String, String> metadata = new HashMap<>(); | ||
EventTracker tracker = GlobalTracer.getEventTracker(); | ||
switch (auth.getSdkEvent()) { | ||
case "success": | ||
tracker.trackLoginSuccessEvent(username, metadata); | ||
return new AppSecSdkToken(username, auth.getCredentials(), Collections.emptyList()); | ||
tracker.trackLoginSuccessEvent(auth.getSdkUser(), metadata); | ||
return new AppSecToken(auth.getName(), auth.getCredentials(), Collections.emptyList()); | ||
case "failure": | ||
tracker.trackLoginFailureEvent(username, auth.isSdkUserExists(), metadata); | ||
tracker.trackLoginFailureEvent(auth.getSdkUser(), auth.isSdkUserExists(), metadata); | ||
if (auth.isSdkUserExists()) { | ||
throw new BadCredentialsException(username); | ||
throw new BadCredentialsException(auth.getSdkUser()); | ||
} else { | ||
throw new UsernameNotFoundException(username); | ||
throw new UsernameNotFoundException(auth.getSdkUser()); | ||
} | ||
default: | ||
throw new IllegalArgumentException("Invalid SDK event: " + auth.getSdkEvent()); | ||
|
@@ -69,7 +66,7 @@ private Authentication loginSdk(final AppSecSdkToken auth) { | |
|
||
@Override | ||
public boolean supports(Class<?> authentication) { | ||
return AppSecSdkToken.class.isAssignableFrom(authentication); | ||
return AppSecToken.class.isAssignableFrom(authentication); | ||
} | ||
|
||
|
||
|
24 changes: 24 additions & 0 deletions
24
...rc/main/java/com/datadoghq/system_tests/springboot/security/AppSecSecurityController.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,24 @@ | ||
package com.datadoghq.system_tests.springboot.security; | ||
|
||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.security.core.userdetails.User; | ||
import org.springframework.security.provisioning.UserDetailsManager; | ||
import org.springframework.stereotype.Controller; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
|
||
@Controller | ||
public class AppSecSecurityController { | ||
|
||
private final UserDetailsManager userDetailsManager; | ||
|
||
public AppSecSecurityController(final UserDetailsManager userDetailsManager) { | ||
this.userDetailsManager = userDetailsManager; | ||
} | ||
|
||
@PostMapping("/signup") | ||
public ResponseEntity<String> signUp(@RequestParam String username, @RequestParam String password) { | ||
userDetailsManager.createUser(User.withUsername(username).password(password).roles("USER").build()); | ||
return ResponseEntity.ok("Signup successful"); | ||
} | ||
} |
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
Oops, something went wrong.