-
Notifications
You must be signed in to change notification settings - Fork 155
feat(jans-auth-server): introducing interception script for tx_tokens #8376 #12596
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
yuriyz
wants to merge
10
commits into
main
Choose a base branch
from
jans-auth-server-8376
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+493
−7
Draft
Changes from 5 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
2d15532
feat(jans-auth-server): introducing interception script for tx_tokens…
yuriyz fb85fef
Merge branch 'main' into jans-auth-server-8376
yuriyz fa430aa
feat(jans-auth-server): added external service for tx_token scripts #…
yuriyz cd2e9ec
feat(jans-auth-server): injected script service into existing tx toke…
yuriyz d194763
feat(jans-auth-server): added methods to tx_token script for jwt payl…
yuriyz e3eeb23
fixes
yuriyz 2ec0422
added tx token script to client
yuriyz 138eff1
added tx token script to client registration
yuriyz 68f3a1b
added sample tx token script
yuriyz bd0fe49
docs: updated docs with tx token sample script
yuriyz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
153 changes: 153 additions & 0 deletions
153
...erver/server/src/main/java/io/jans/as/server/service/external/ExternalTxTokenService.java
This file contains hidden or 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,153 @@ | ||
| package io.jans.as.server.service.external; | ||
|
|
||
| import com.google.common.collect.Lists; | ||
| import io.jans.as.model.token.JsonWebResponse; | ||
| import io.jans.as.server.model.common.ExecutionContext; | ||
| import io.jans.as.server.service.external.context.ExternalScriptContext; | ||
| import io.jans.model.custom.script.CustomScriptType; | ||
| import io.jans.model.custom.script.conf.CustomScriptConfiguration; | ||
| import io.jans.model.custom.script.type.token.TxTokenType; | ||
| import io.jans.service.custom.script.ExternalScriptService; | ||
| import jakarta.enterprise.context.ApplicationScoped; | ||
| import jakarta.ws.rs.WebApplicationException; | ||
| import org.jetbrains.annotations.NotNull; | ||
| import org.json.JSONObject; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| @ApplicationScoped | ||
| public class ExternalTxTokenService extends ExternalScriptService { | ||
|
|
||
| public ExternalTxTokenService() { | ||
| super(CustomScriptType.TX_TOKEN); | ||
| } | ||
|
|
||
| public boolean modifyTokenPayload(CustomScriptConfiguration script, JsonWebResponse jsonWebResponse, ExternalScriptContext context) { | ||
| try { | ||
| log.trace("Executing 'modifyTokenPayload' method, script name: {}, jsonWebResponse: {}, context: {}", script.getName(), jsonWebResponse, context); | ||
| context.getExecutionContext().setScript(script); | ||
|
|
||
| TxTokenType scriptType = (TxTokenType) script.getExternalType(); | ||
| final boolean result = scriptType.modifyTokenPayload(jsonWebResponse, context); | ||
| log.trace("Finished 'modifyTokenPayload' method, script name: {}, jsonWebResponse: {}, context: {}, result: {}", script.getName(), jsonWebResponse, context, result); | ||
|
|
||
| context.throwWebApplicationExceptionIfSet(); | ||
| return result; | ||
| } catch (WebApplicationException e) { | ||
| throw e; | ||
| } catch (Exception ex) { | ||
| log.error(ex.getMessage(), ex); | ||
| saveScriptError(script.getCustomScript(), ex); | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| public boolean modifyTokenPayload(JsonWebResponse jsonWebResponse, ExternalScriptContext context) { | ||
| List<CustomScriptConfiguration> scripts = getScripts(context.getExecutionContext()); | ||
| if (scripts.isEmpty()) { | ||
| return false; | ||
| } | ||
| log.trace("Executing {} 'modifyTokenPayload' scripts.", scripts.size()); | ||
|
|
||
| for (CustomScriptConfiguration script : scripts) { | ||
| if (!modifyTokenPayload(script, jsonWebResponse, context)) { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| public boolean modifyResponse(CustomScriptConfiguration script, JSONObject response, ExternalScriptContext context) { | ||
| try { | ||
| log.trace("Executing 'modifyResponse' method, script name: {}, response: {}, context: {}", script.getName(), response, context); | ||
| context.getExecutionContext().setScript(script); | ||
|
|
||
| TxTokenType scriptType = (TxTokenType) script.getExternalType(); | ||
| final boolean result = scriptType.modifyResponse(response, context); | ||
| log.trace("Finished 'modifyResponse' method, script name: {}, response: {}, context: {}, result: {}", script.getName(), response, context, result); | ||
|
|
||
| context.throwWebApplicationExceptionIfSet(); | ||
| return result; | ||
| } catch (WebApplicationException e) { | ||
| throw e; | ||
| } catch (Exception ex) { | ||
| log.error(ex.getMessage(), ex); | ||
| saveScriptError(script.getCustomScript(), ex); | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| public boolean modifyResponse(JSONObject response, ExternalScriptContext context) { | ||
| List<CustomScriptConfiguration> scripts = getScripts(context.getExecutionContext()); | ||
| if (scripts.isEmpty()) { | ||
| log.trace("No TxTokenType scripts found."); | ||
| return false; | ||
| } | ||
|
|
||
| log.trace("Executing {} 'modifyResponse' scripts.", scripts.size()); | ||
|
|
||
| for (CustomScriptConfiguration script : scripts) { | ||
| if (!modifyResponse(script, response, context)) { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| public int getTxTokenLifetimeInSeconds(CustomScriptConfiguration script, ExternalScriptContext context) { | ||
| try { | ||
| log.trace("Executing 'getTxTokenLifetimeInSeconds' method, script name: {}, context: {}", script.getName(), context); | ||
| context.getExecutionContext().setScript(script); | ||
|
|
||
| TxTokenType txTokenType = (TxTokenType) script.getExternalType(); | ||
| final int result = txTokenType.getTxTokenLifetimeInSeconds(context); | ||
| log.trace("Finished 'getTxTokenLifetimeInSeconds' method, script name: {}, context: {}, result: {}", script.getName(), context, result); | ||
|
|
||
| context.throwWebApplicationExceptionIfSet(); | ||
| return result; | ||
| } catch (WebApplicationException e) { | ||
| throw e; | ||
| } catch (Exception ex) { | ||
| log.error(ex.getMessage(), ex); | ||
| saveScriptError(script.getCustomScript(), ex); | ||
| } | ||
| return 0; | ||
| } | ||
|
|
||
| public int getTxTokenLifetimeInSeconds(ExternalScriptContext context) { | ||
| List<CustomScriptConfiguration> scripts = getScripts(context.getExecutionContext()); | ||
| if (scripts.isEmpty()) { | ||
| return 0; | ||
| } | ||
| log.trace("Executing {} 'getTxTokenLifetimeInSeconds' scripts.", scripts.size()); | ||
|
|
||
| for (CustomScriptConfiguration script : scripts) { | ||
| final int lifetime = getTxTokenLifetimeInSeconds(script, context); | ||
| if (lifetime > 0) { | ||
| log.trace("Finished 'getTxTokenLifetimeInSeconds' methods, lifetime: {}", lifetime); | ||
| return lifetime; | ||
| } | ||
| } | ||
| return 0; | ||
| } | ||
|
|
||
| @NotNull | ||
| private List<CustomScriptConfiguration> getScripts(@NotNull ExecutionContext context) { | ||
| if (customScriptConfigurations == null || customScriptConfigurations.isEmpty() || context.getClient() == null) { | ||
| log.trace("No TxToken scripts or client is null."); | ||
| return Lists.newArrayList(); | ||
| } | ||
|
|
||
| final List<CustomScriptConfiguration> scripts = getCustomScriptConfigurationsByDns(context.getClient().getAttributes().getTxTokenScriptDns()); | ||
| if (!scripts.isEmpty()) { | ||
| return scripts; | ||
| } | ||
|
|
||
| log.trace("No TxToken scripts associated with client {}", context.getClient().getClientId()); | ||
| return Lists.newArrayList(); | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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
45 changes: 45 additions & 0 deletions
45
jans-core/script/src/main/java/io/jans/model/custom/script/type/token/DummyTxTokenType.java
This file contains hidden or 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,45 @@ | ||
| package io.jans.model.custom.script.type.token; | ||
|
|
||
| import io.jans.model.SimpleCustomProperty; | ||
| import io.jans.model.custom.script.model.CustomScript; | ||
|
|
||
| import java.util.Map; | ||
|
|
||
| public class DummyTxTokenType implements TxTokenType { | ||
|
|
||
|
|
||
| @Override | ||
| public boolean init(Map<String, SimpleCustomProperty> configurationAttributes) { | ||
| return true; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean init(CustomScript customScript, Map<String, SimpleCustomProperty> configurationAttributes) { | ||
| return true; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean destroy(Map<String, SimpleCustomProperty> configurationAttributes) { | ||
| return true; | ||
| } | ||
|
|
||
| @Override | ||
| public int getApiVersion() { | ||
| return 1; | ||
| } | ||
|
|
||
| @Override | ||
| public int getTxTokenLifetimeInSeconds(Object context) { | ||
| return 0; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean modifyTokenPayload(Object jsonWebResponse, Object context) { | ||
| return true; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean modifyResponse(Object responseAsJsonObject, Object context) { | ||
| return true; | ||
| } | ||
| } |
12 changes: 12 additions & 0 deletions
12
jans-core/script/src/main/java/io/jans/model/custom/script/type/token/TxTokenType.java
This file contains hidden or 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,12 @@ | ||
| package io.jans.model.custom.script.type.token; | ||
|
|
||
| import io.jans.model.custom.script.type.BaseExternalType; | ||
|
|
||
| public interface TxTokenType extends BaseExternalType { | ||
|
|
||
| int getTxTokenLifetimeInSeconds(Object context); | ||
|
|
||
| boolean modifyTokenPayload(Object jsonWebResponse, Object context); | ||
|
|
||
| boolean modifyResponse(Object responseAsJsonObject, Object context); | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.