Skip to content

Commit

Permalink
fix(jans-auth-server): don't fail registration without custom script (#…
Browse files Browse the repository at this point in the history
…711)

If there no any custom script for dynamic registration we should not fail entire call (avoid npe).

#710
  • Loading branch information
yuriyzz committed Jan 26, 2022
1 parent fe07d76 commit 277be82
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,12 @@ public boolean executeExternalUpdateClientMethods(HttpServletRequest httpRequest
}

public JSONObject getSoftwareStatementJwks(HttpServletRequest httpRequest, JSONObject registerRequest, Jwt softwareStatement) {
if (defaultExternalCustomScript == null) {
return null;
}

try {
log.info("Executing python 'getSoftwareStatementJwks' method, script name:" + defaultExternalCustomScript.getName());
log.info("Executing python 'getSoftwareStatementJwks' method, script name: {}", defaultExternalCustomScript.getName());

DynamicClientRegistrationContext context = new DynamicClientRegistrationContext(httpRequest, registerRequest, defaultExternalCustomScript);
context.setSoftwareStatement(softwareStatement);
Expand All @@ -137,7 +141,7 @@ public JSONObject getSoftwareStatementJwks(HttpServletRequest httpRequest, JSONO
ClientRegistrationType externalType = (ClientRegistrationType) defaultExternalCustomScript.getExternalType();
final String result = externalType.getSoftwareStatementJwks(context);
context.throwWebApplicationExceptionIfSet();
log.info("Result of python 'getSoftwareStatementJwks' method: " + result);
log.info("Result of python 'getSoftwareStatementJwks' method: {}", result);
return new JSONObject(result);
} catch (WebApplicationException e) {
throw e;
Expand All @@ -149,6 +153,10 @@ public JSONObject getSoftwareStatementJwks(HttpServletRequest httpRequest, JSONO
}

public String getSoftwareStatementHmacSecret(HttpServletRequest httpRequest, JSONObject registerRequest, Jwt softwareStatement) {
if (defaultExternalCustomScript == null) {
return "";
}

try {
log.trace("Executing python 'getSoftwareStatementHmacSecret' method");

Expand All @@ -159,7 +167,7 @@ public String getSoftwareStatementHmacSecret(HttpServletRequest httpRequest, JSO
ClientRegistrationType externalType = (ClientRegistrationType) defaultExternalCustomScript.getExternalType();
final String result = externalType.getSoftwareStatementHmacSecret(context);
context.throwWebApplicationExceptionIfSet();
log.trace("Result of python 'getSoftwareStatementHmacSecret' method: " + result);
log.trace("Result of python 'getSoftwareStatementHmacSecret' method: {}", result);
return result;
} catch (WebApplicationException e) {
throw e;
Expand All @@ -171,6 +179,10 @@ public String getSoftwareStatementHmacSecret(HttpServletRequest httpRequest, JSO
}

public JSONObject getDcrJwks(HttpServletRequest httpRequest, Jwt dcr) {
if (defaultExternalCustomScript == null) {
return null;
}

try {
log.trace("Executing python 'getDcrJwks' method");

Expand All @@ -181,7 +193,7 @@ public JSONObject getDcrJwks(HttpServletRequest httpRequest, Jwt dcr) {
ClientRegistrationType externalType = (ClientRegistrationType) defaultExternalCustomScript.getExternalType();
final String result = externalType.getDcrJwks(context);
context.throwWebApplicationExceptionIfSet();
log.trace("Result of python 'getDcrJwks' method: " + result);
log.trace("Result of python 'getDcrJwks' method: {}", result);
return new JSONObject(result);
} catch (WebApplicationException e) {
throw e;
Expand All @@ -193,6 +205,10 @@ public JSONObject getDcrJwks(HttpServletRequest httpRequest, Jwt dcr) {
}

public String getDcrHmacSecret(HttpServletRequest httpRequest, Jwt dcr) {
if (defaultExternalCustomScript == null) {
return "";
}

try {
log.trace("Executing python 'getDcrHmacSecret' method");

Expand All @@ -203,7 +219,7 @@ public String getDcrHmacSecret(HttpServletRequest httpRequest, Jwt dcr) {
ClientRegistrationType externalType = (ClientRegistrationType) defaultExternalCustomScript.getExternalType();
final String result = externalType.getDcrHmacSecret(context);
context.throwWebApplicationExceptionIfSet();
log.trace("Result of python 'getDcrHmacSecret' method: " + result);
log.trace("Result of python 'getDcrHmacSecret' method: {}", result);
return result;
} catch (WebApplicationException e) {
throw e;
Expand All @@ -215,14 +231,18 @@ public String getDcrHmacSecret(HttpServletRequest httpRequest, Jwt dcr) {
}

public boolean isCertValidForClient(X509Certificate cert, DynamicClientRegistrationContext context) {
if (defaultExternalCustomScript == null) {
return true;
}

try {
log.trace("Executing python 'isCertValidForClient' method");
context.setScript(defaultExternalCustomScript);
context.setErrorResponseFactory(errorResponseFactory);
ClientRegistrationType externalType = (ClientRegistrationType) defaultExternalCustomScript.getExternalType();
final boolean result = externalType.isCertValidForClient(cert, context);
context.throwWebApplicationExceptionIfSet();
log.trace("Result of python 'isCertValidForClient' method: " + result);
log.trace("Result of python 'isCertValidForClient' method: {}", result);
return result;
} catch (WebApplicationException e) {
throw e;
Expand All @@ -234,6 +254,10 @@ public boolean isCertValidForClient(X509Certificate cert, DynamicClientRegistrat
}

public boolean modifyPostResponse(JSONObject responseAsJsonObject, ExecutionContext context) {
if (defaultExternalCustomScript == null) {
return false;
}

CustomScriptConfiguration script = defaultExternalCustomScript;

try {
Expand All @@ -257,6 +281,10 @@ public boolean modifyPostResponse(JSONObject responseAsJsonObject, ExecutionCont
}

public boolean modifyPutResponse(JSONObject responseAsJsonObject, ExecutionContext context) {
if (defaultExternalCustomScript == null) {
return false;
}

CustomScriptConfiguration script = defaultExternalCustomScript;

try {
Expand All @@ -280,6 +308,10 @@ public boolean modifyPutResponse(JSONObject responseAsJsonObject, ExecutionConte
}

public boolean modifyReadResponse(JSONObject responseAsJsonObject, ExecutionContext context) {
if (defaultExternalCustomScript == null) {
return false;
}

CustomScriptConfiguration script = defaultExternalCustomScript;

try {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package io.jans.as.server.service.external;

import io.jans.as.model.configuration.AppConfiguration;
import io.jans.as.model.error.ErrorResponseFactory;
import io.jans.as.model.jwt.Jwt;
import io.jans.as.server.model.common.ExecutionContext;
import org.json.JSONObject;
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 static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertNull;
import static org.testng.Assert.assertTrue;

/**
* @author Yuriy Zabrovarnyy
*/
@Listeners(MockitoTestNGListener.class)
public class ExternalDynamicClientRegistrationServiceTest {

@InjectMocks
private ExternalDynamicClientRegistrationService externalDynamicClientRegistrationService;

@Mock
private Logger log;

@Mock
private AppConfiguration appConfiguration;

@Mock
private ErrorResponseFactory errorResponseFactory;

@Test
public void modifyPostResponse_whenDefaultExternalCustomScriptIsNull_shouldReturnFalseWithoutNpe() {
final boolean result = externalDynamicClientRegistrationService.modifyPostResponse(new JSONObject(), new ExecutionContext());
assertFalse(result);
}

@Test
public void modifyPutResponse_whenDefaultExternalCustomScriptIsNull_shouldReturnFalseWithoutNpe() {
final boolean result = externalDynamicClientRegistrationService.modifyPutResponse(new JSONObject(), new ExecutionContext());
assertFalse(result);
}

@Test
public void modifyReadResponse_whenDefaultExternalCustomScriptIsNull_shouldReturnFalseWithoutNpe() {
final boolean result = externalDynamicClientRegistrationService.modifyReadResponse(new JSONObject(), new ExecutionContext());
assertFalse(result);
}

@Test
public void isCertValidForClient_whenDefaultExternalCustomScriptIsNull_shouldReturnTrueWithoutNpe() {
final boolean result = externalDynamicClientRegistrationService.isCertValidForClient(null, null);
assertTrue(result);
}

@Test
public void getDcrHmacSecret_whenDefaultExternalCustomScriptIsNull_shouldReturnEmptyStringWithoutNpe() {
final String result = externalDynamicClientRegistrationService.getDcrHmacSecret(null, new Jwt());
assertEquals(result, "");
}

@Test
public void getDcrJwks_whenDefaultExternalCustomScriptIsNull_shouldReturnNullWithoutNpe() {
JSONObject result = externalDynamicClientRegistrationService.getDcrJwks(null, new Jwt());
assertNull(result);
}

@Test
public void getSoftwareStatementHmacSecret_whenDefaultExternalCustomScriptIsNull_shouldReturnEmptyStringWithoutNpe() {
String result = externalDynamicClientRegistrationService.getSoftwareStatementHmacSecret(null, new JSONObject(), new Jwt());
assertEquals(result, "");
}

@Test
public void getSoftwareStatementJwks_whenDefaultExternalCustomScriptIsNull_shouldReturnNullWithoutNpe() {
JSONObject result = externalDynamicClientRegistrationService.getSoftwareStatementJwks(null, new JSONObject(), new Jwt());
assertNull(result);
}
}

0 comments on commit 277be82

Please sign in to comment.