11package io .jans .as .server .service .external ;
22
33import com .google .common .collect .Lists ;
4+ import io .jans .as .model .token .JsonWebResponse ;
45import io .jans .as .server .model .common .ExecutionContext ;
56import io .jans .as .server .service .external .context .ExternalScriptContext ;
67import io .jans .model .custom .script .CustomScriptType ;
1011import jakarta .enterprise .context .ApplicationScoped ;
1112import jakarta .ws .rs .WebApplicationException ;
1213import org .jetbrains .annotations .NotNull ;
14+ import org .json .JSONObject ;
1315
1416import java .util .List ;
1517
@@ -20,6 +22,82 @@ public ExternalTxTokenService() {
2022 super (CustomScriptType .TX_TOKEN );
2123 }
2224
25+ public boolean modifyTokenPayload (CustomScriptConfiguration script , JsonWebResponse jsonWebResponse , ExternalScriptContext context ) {
26+ try {
27+ log .trace ("Executing 'modifyTokenPayload' method, script name: {}, jsonWebResponse: {}, context: {}" , script .getName (), jsonWebResponse , context );
28+ context .getExecutionContext ().setScript (script );
29+
30+ TxTokenType scriptType = (TxTokenType ) script .getExternalType ();
31+ final boolean result = scriptType .modifyTokenPayload (jsonWebResponse , context );
32+ log .trace ("Finished 'modifyTokenPayload' method, script name: {}, jsonWebResponse: {}, context: {}, result: {}" , script .getName (), jsonWebResponse , context , result );
33+
34+ context .throwWebApplicationExceptionIfSet ();
35+ return result ;
36+ } catch (WebApplicationException e ) {
37+ throw e ;
38+ } catch (Exception ex ) {
39+ log .error (ex .getMessage (), ex );
40+ saveScriptError (script .getCustomScript (), ex );
41+ }
42+
43+ return false ;
44+ }
45+
46+ public boolean modifyTokenPayload (JsonWebResponse jsonWebResponse , ExternalScriptContext context ) {
47+ List <CustomScriptConfiguration > scripts = getScripts (context .getExecutionContext ());
48+ if (scripts .isEmpty ()) {
49+ return false ;
50+ }
51+ log .trace ("Executing {} 'modifyTokenPayload' scripts." , scripts .size ());
52+
53+ for (CustomScriptConfiguration script : scripts ) {
54+ if (!modifyTokenPayload (script , jsonWebResponse , context )) {
55+ return false ;
56+ }
57+ }
58+
59+ return true ;
60+ }
61+
62+ public boolean modifyResponse (CustomScriptConfiguration script , JSONObject response , ExternalScriptContext context ) {
63+ try {
64+ log .trace ("Executing 'modifyResponse' method, script name: {}, response: {}, context: {}" , script .getName (), response , context );
65+ context .getExecutionContext ().setScript (script );
66+
67+ TxTokenType scriptType = (TxTokenType ) script .getExternalType ();
68+ final boolean result = scriptType .modifyResponse (response , context );
69+ log .trace ("Finished 'modifyResponse' method, script name: {}, response: {}, context: {}, result: {}" , script .getName (), response , context , result );
70+
71+ context .throwWebApplicationExceptionIfSet ();
72+ return result ;
73+ } catch (WebApplicationException e ) {
74+ throw e ;
75+ } catch (Exception ex ) {
76+ log .error (ex .getMessage (), ex );
77+ saveScriptError (script .getCustomScript (), ex );
78+ }
79+
80+ return false ;
81+ }
82+
83+ public boolean modifyResponse (JSONObject response , ExternalScriptContext context ) {
84+ List <CustomScriptConfiguration > scripts = getScripts (context .getExecutionContext ());
85+ if (scripts .isEmpty ()) {
86+ log .trace ("No TxTokenType scripts found." );
87+ return false ;
88+ }
89+
90+ log .trace ("Executing {} 'modifyResponse' scripts." , scripts .size ());
91+
92+ for (CustomScriptConfiguration script : scripts ) {
93+ if (!modifyResponse (script , response , context )) {
94+ return false ;
95+ }
96+ }
97+
98+ return true ;
99+ }
100+
23101 public int getTxTokenLifetimeInSeconds (CustomScriptConfiguration script , ExternalScriptContext context ) {
24102 try {
25103 log .trace ("Executing 'getTxTokenLifetimeInSeconds' method, script name: {}, context: {}" , script .getName (), context );
0 commit comments