forked from getsentry/sentry-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
6 changed files
with
209 additions
and
3 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
76 changes: 76 additions & 0 deletions
76
...-spring-boot-starter/src/main/java/io/sentry/spring/boot/SentrySpringJwtUserProvider.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,76 @@ | ||
package io.sentry.spring.boot; | ||
|
||
import io.sentry.SentryOptions; | ||
import io.sentry.protocol.User; | ||
import io.sentry.spring.SentryUserProvider; | ||
import io.sentry.util.Objects; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
import org.springframework.security.core.context.SecurityContextHolder; | ||
import org.springframework.security.oauth2.jwt.Jwt; | ||
|
||
public final class SentrySpringJwtUserProvider implements SentryUserProvider { | ||
private final @NotNull SentryOptions options; | ||
|
||
public SentrySpringJwtUserProvider(final @NotNull SentryOptions options) { | ||
this.options = Objects.requireNonNull(options, "options is required"); | ||
} | ||
|
||
@Override | ||
public @Nullable User provideUser() { | ||
if (options.isSendDefaultPii()) { | ||
final Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal(); | ||
if (principal instanceof Jwt) { | ||
return extraJwtUser((Jwt) principal); | ||
} else { | ||
return null; | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
private User extraJwtUser(Jwt jwt) { | ||
final User user = new User(); | ||
user.setId(jwt.getSubject()); | ||
user.setEmail(jwt.getClaimAsString("email")); | ||
user.setUsername(findJwtUsername(jwt)); | ||
user.setOthers(findJwtInfo(jwt)); | ||
return user; | ||
} | ||
|
||
private @Nullable String findJwtUsername(Jwt jwt) { | ||
if (jwt.hasClaim("username")) { | ||
return jwt.getClaimAsString("username"); | ||
} else if (jwt.hasClaim("preferred_username")) { | ||
return jwt.getClaimAsString("preferred_username"); | ||
} else if (jwt.hasClaim("name")) { | ||
return jwt.getClaimAsString("name"); | ||
} else if (jwt.hasClaim("email")) { | ||
return jwt.getClaimAsString("email"); | ||
} else { | ||
return null; | ||
} | ||
} | ||
|
||
private Map<String, String> findJwtInfo(Jwt jwt) { | ||
final Map<String, String> info = new HashMap<>(); | ||
if (jwt.hasClaim("iss")) { | ||
info.put("iss", jwt.getClaimAsString("iss")); | ||
} | ||
if (jwt.hasClaim("aud")) { | ||
info.put("aud", jwt.getClaimAsString("aud")); | ||
} | ||
if (jwt.hasClaim("exp")) { | ||
info.put("exp", jwt.getClaimAsString("exp")); | ||
} | ||
if (jwt.hasClaim("iat")) { | ||
info.put("iat", jwt.getClaimAsString("iat")); | ||
} | ||
if (jwt.hasClaim("scope")) { | ||
info.put("scope", jwt.getClaimAsString("scope")); | ||
} | ||
return info; | ||
} | ||
} |
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
89 changes: 89 additions & 0 deletions
89
...ing-boot-starter/src/test/kotlin/io/sentry/spring/boot/SentrySpringJwtUserProviderTest.kt
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,89 @@ | ||
package io.sentry.spring.boot | ||
|
||
import com.nhaarman.mockitokotlin2.mock | ||
import com.nhaarman.mockitokotlin2.whenever | ||
import io.sentry.SentryOptions | ||
import org.junit.jupiter.api.Test | ||
import org.springframework.security.core.Authentication | ||
import org.springframework.security.core.context.SecurityContext | ||
import org.springframework.security.core.context.SecurityContextHolder | ||
import org.springframework.security.oauth2.jwt.Jwt | ||
import java.time.Instant | ||
import kotlin.test.assertEquals | ||
import kotlin.test.assertNotNull | ||
import kotlin.test.assertNull | ||
|
||
class SentrySpringJwtUserProviderTest { | ||
|
||
class Fixture { | ||
fun getSut(isSendDefaultPii: Boolean = true, jwt: Jwt? = null): SentrySpringJwtUserProvider { | ||
val options = SentryOptions().apply { | ||
this.isSendDefaultPii = isSendDefaultPii | ||
} | ||
val securityContext = mock<SecurityContext>() | ||
if (jwt != null) { | ||
val authentication = mock<Authentication>() | ||
whenever(securityContext.authentication).thenReturn(authentication) | ||
whenever(authentication.principal).thenReturn(jwt) | ||
} else { | ||
whenever(securityContext.authentication).thenReturn(null) | ||
} | ||
SecurityContextHolder.setContext(securityContext) | ||
return SentrySpringJwtUserProvider(options) | ||
} | ||
|
||
fun getJwt(tokenValue: String, subject: String, issuer: String, issuedAt: Instant, expiresAt: Instant, audience: Collection<String>, headers: Map<String, Any>, claims: Map<String, Any>): Jwt { | ||
return Jwt.withTokenValue(tokenValue) | ||
.issuedAt(issuedAt) | ||
.expiresAt(expiresAt) | ||
.subject(subject) | ||
.issuer(issuer) | ||
.audience(audience) | ||
.claims { | ||
it.putAll(claims) | ||
} | ||
.headers { | ||
it.putAll(headers) | ||
} | ||
.build() | ||
} | ||
} | ||
|
||
private val fixture = Fixture() | ||
|
||
@Test | ||
fun `when send default pii is set to true, returns fully populated user`() { | ||
val jwt = fixture.getJwt( | ||
tokenValue = "token", | ||
subject = "subject", | ||
issuer = "issuer", | ||
issuedAt = Instant.MIN, | ||
expiresAt = Instant.MAX, | ||
audience = listOf("audience"), | ||
headers = mapOf("header" to "value"), | ||
claims = mapOf("email" to "info@sentry.io") | ||
) | ||
val provider = fixture.getSut(true, jwt) | ||
val user = provider.provideUser() | ||
assertNotNull(user) { | ||
assertEquals("subject", it.username) | ||
assertEquals("email", "info@sentry.io") | ||
assertEquals("subject", it.id) | ||
assertEquals(jwt.claims, it.others) | ||
} | ||
} | ||
|
||
@Test | ||
fun `when send default pii is set to false, returns null`() { | ||
val provider = fixture.getSut(false) | ||
val user = provider.provideUser() | ||
assertNull(user) | ||
} | ||
|
||
@Test | ||
fun `when send default pii is set to true and security context is not set, returns null`() { | ||
val provider = fixture.getSut(true) | ||
val user = provider.provideUser() | ||
assertNull(user) | ||
} | ||
} |