-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(jans-auth-server): added externalUriWhiteList configuration prop…
…erty before call external uri from AS #3130
- Loading branch information
Showing
8 changed files
with
152 additions
and
8 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
48 changes: 48 additions & 0 deletions
48
jans-auth-server/server/src/main/java/io/jans/as/server/service/net/UriService.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,48 @@ | ||
package io.jans.as.server.service.net; | ||
|
||
import io.jans.as.model.configuration.AppConfiguration; | ||
import io.jans.as.model.util.JwtUtil; | ||
import io.jans.as.model.util.URLPatternList; | ||
import jakarta.ejb.Stateless; | ||
import jakarta.inject.Inject; | ||
import jakarta.inject.Named; | ||
import org.apache.tika.utils.StringUtils; | ||
import org.json.JSONObject; | ||
import org.slf4j.Logger; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* @author Yuriy Z | ||
*/ | ||
@Stateless | ||
@Named | ||
public class UriService { | ||
|
||
@Inject | ||
private Logger log; | ||
|
||
@Inject | ||
private AppConfiguration appConfiguration; | ||
|
||
public boolean canCall(String uri) { | ||
if (StringUtils.isBlank(uri)) { | ||
return false; | ||
} | ||
|
||
final List<String> externalUriWhiteList = appConfiguration.getExternalUriWhiteList(); | ||
if (externalUriWhiteList == null || externalUriWhiteList.isEmpty()) { | ||
return true; | ||
} | ||
|
||
return new URLPatternList(externalUriWhiteList).isUrlListed(uri); | ||
} | ||
|
||
public JSONObject loadJson(String uri) { | ||
if (!canCall(uri)) { | ||
log.debug("Unable to call external uri: {}, externalUriWhiteList: {}", uri, appConfiguration.getExternalUriWhiteList()); | ||
return null; | ||
} | ||
return JwtUtil.getJSONWebKeys(uri); | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
jans-auth-server/server/src/test/java/io/jans/as/server/service/net/UriServiceTest.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,53 @@ | ||
package io.jans.as.server.service.net; | ||
|
||
import io.jans.as.model.configuration.AppConfiguration; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.mockito.testng.MockitoTestNGListener; | ||
import org.slf4j.Logger; | ||
import org.testng.annotations.Listeners; | ||
import org.testng.annotations.Test; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
|
||
import static org.mockito.Mockito.when; | ||
import static org.testng.Assert.assertTrue; | ||
import static org.testng.Assert.assertFalse; | ||
|
||
/** | ||
* @author Yuriy Z | ||
*/ | ||
@Listeners(MockitoTestNGListener.class) | ||
public class UriServiceTest { | ||
|
||
@InjectMocks | ||
private UriService uriService; | ||
|
||
@Mock | ||
private Logger log; | ||
|
||
@Mock | ||
private AppConfiguration appConfiguration; | ||
|
||
@Test | ||
public void canCall_whenExternalUriWhiteListIsBlank_shouldReturnTrue() { | ||
when(appConfiguration.getExternalUriWhiteList()).thenReturn(new ArrayList<>()); | ||
|
||
assertTrue(uriService.canCall("http://example.com")); | ||
} | ||
|
||
@Test | ||
public void canCall_whenUriAllowedByExternalUriWhiteList_shouldReturnTrue() { | ||
when(appConfiguration.getExternalUriWhiteList()).thenReturn(Collections.singletonList("example.com")); | ||
|
||
assertTrue(uriService.canCall("http://example.com")); | ||
} | ||
|
||
@Test | ||
public void canCall_whenUriNotAllowedByExternalUriWhiteList_shouldReturnFalse() { | ||
when(appConfiguration.getExternalUriWhiteList()).thenReturn(Collections.singletonList("my.com")); | ||
|
||
assertFalse(uriService.canCall("http://example.com")); | ||
} | ||
} |
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