@@ -45,6 +45,8 @@ public class OPAAuthorizationManager
4545 // If opaPath is null, then we assume the user wants to use the default path.
4646 private String opaPath ;
4747
48+ private String reasonKey ;
49+
4850 private ContextDataProvider ctxProvider ;
4951
5052 private OPAClient opa ;
@@ -64,6 +66,7 @@ public OPAAuthorizationManager() {
6466 OPAClient opac = new OPAClient (opaURL );
6567 this .opa = opac ;
6668 this .opaPath = null ;
69+ this .reasonKey = "en" ;
6770 }
6871
6972 /**
@@ -76,6 +79,7 @@ public OPAAuthorizationManager() {
7679 public OPAAuthorizationManager (OPAClient opa ) {
7780 this .opa = opa ;
7881 this .opaPath = null ;
82+ this .reasonKey = "en" ;
7983 }
8084
8185 /**
@@ -90,6 +94,7 @@ public OPAAuthorizationManager(OPAClient opa) {
9094 public OPAAuthorizationManager (OPAClient opa , String newOpaPath ) {
9195 this .opa = opa ;
9296 this .opaPath = newOpaPath ;
97+ this .reasonKey = "en" ;
9398 }
9499
95100 /**
@@ -104,6 +109,7 @@ public OPAAuthorizationManager(OPAClient opa, String newOpaPath) {
104109 public OPAAuthorizationManager (OPAClient opa , ContextDataProvider newProvider ) {
105110 this .opa = opa ;
106111 this .ctxProvider = newProvider ;
112+ this .reasonKey = "en" ;
107113 }
108114
109115 /**
@@ -119,6 +125,23 @@ public OPAAuthorizationManager(OPAClient opa, String newOpaPath, ContextDataProv
119125 this .opa = opa ;
120126 this .opaPath = newOpaPath ;
121127 this .ctxProvider = newProvider ;
128+ this .reasonKey = "en" ;
129+ }
130+
131+ public String getReasonKey () {
132+ return this .reasonKey ;
133+ }
134+
135+ /**
136+ * Changes the "preferred" key where the access decision reason should be
137+ * searched for in the OPAResponse object. A default value of 'en' is used.
138+ * If the selected key is not present in the response, the key which sorts
139+ * lexicographically first is used instead.
140+ *
141+ * @param newReasonKey
142+ */
143+ public void setReasonKey (String newReasonKey ) {
144+ this .reasonKey = newReasonKey ;
122145 }
123146
124147 private Map <String , Object > makeRequestInput (
@@ -250,9 +273,9 @@ public void verify(
250273 }
251274
252275 boolean allow = resp .getDecision ();
253- String reason = "access denied by policy" ;
254- if (resp . getReasonForDecision () ! = null ) {
255- reason = resp . getReasonForDecision () ;
276+ String reason = resp . getReasonForDecision ( this . reasonKey ) ;
277+ if (reason = = null ) {
278+ reason = "access denied by policy" ;
256279 }
257280
258281 if (allow ) {
0 commit comments