1616import java .util .Objects ;
1717
1818
19+ import com .staffbase .plugins .sdk .RemoteCall .DeleteInstanceCallHandlerInterface ;
20+ import com .staffbase .plugins .sdk .RemoteCall .RemoteCallInterface ;
1921import org .apache .logging .log4j .LogManager ;
2022import org .apache .logging .log4j .Logger ;
2123
@@ -67,6 +69,11 @@ public static SSOFacade create(final RSAPublicKey rsaPublicKey) {
6769 */
6870 private JwtConsumer jwtConsumer ;
6971
72+ /**
73+ * An instance handling remote calls
74+ */
75+ private RemoteCallInterface remoteCallHandler ;
76+
7077
7178 /**********************************************
7279 * Constructors
@@ -83,15 +90,19 @@ public static SSOFacade create(final RSAPublicKey rsaPublicKey) {
8390 * Initialization
8491 **********************************************/
8592
93+ SSOFacade initialize (final RSAPublicKey rsaPublicKey ) {
94+ return this .initialize (rsaPublicKey , null );
95+ }
96+
8697 /**
8798 * Initialize this component by building up the consumer for JWT using the
8899 * pre-configured secret
89100 *
90101 * @param rsaPublicKey the RSA public key to be used for verification.
91- *
102+ * @param remoteCallHandler a class handling remote calls
92103 * @return Fluent interface.
93104 */
94- SSOFacade initialize (final RSAPublicKey rsaPublicKey ) {
105+ SSOFacade initialize (final RSAPublicKey rsaPublicKey , final RemoteCallInterface remoteCallHandler ) {
95106
96107 if (logger .isDebugEnabled ()) {
97108 logger .debug ("Initializing single-sign-on manager SSOFacade. " );
@@ -112,6 +123,8 @@ SSOFacade initialize(final RSAPublicKey rsaPublicKey) {
112123 .setRequireIssuedAt ()
113124 .build ();
114125
126+ this .remoteCallHandler = remoteCallHandler ;
127+
115128 return this ;
116129 }
117130
@@ -152,7 +165,7 @@ public SSOData verify(final String raw) throws SSOException {
152165 + "[instance_id=" + instanceId + "]" );
153166 }
154167
155- throw new SSOException ("Missing or malformed instnance_id ." );
168+ throw new SSOException ("Missing or malformed instance_id ." );
156169 }
157170
158171 if (logger .isDebugEnabled ()) {
@@ -162,7 +175,23 @@ public SSOData verify(final String raw) throws SSOException {
162175 }
163176
164177 // Parse and return the container data.
165- return new SSOData (jwtClaims );
178+ SSOData ssoData = new SSOData (jwtClaims );
179+ if (ssoData .isDeleteInstanceCall () && this .remoteCallHandler != null ) {
180+ boolean result = true ;
181+ if (this .remoteCallHandler instanceof DeleteInstanceCallHandlerInterface ){
182+ result = ((DeleteInstanceCallHandlerInterface ) this .remoteCallHandler ).deleteInstance (instanceId );
183+ } else {
184+ logger .warn ("Warning: An instance deletion call for instance $instanceId was not handled." );
185+ }
186+
187+ if (result ){
188+ this .remoteCallHandler .exitSuccess ();
189+ } else {
190+ this .remoteCallHandler .exitFailure ();
191+ }
192+ exitRemoteCall ();
193+ }
194+ return ssoData ;
166195 } catch (final MalformedClaimException malformationException ) {
167196 if (logger .isFatalEnabled ()) {
168197 logger .fatal ("Encountered malformed sso attempt." , malformationException );
@@ -177,4 +206,14 @@ public SSOData verify(final String raw) throws SSOException {
177206 throw new SSOException (invalidJwtException .getMessage (), invalidJwtException );
178207 }
179208 }
209+
210+
211+ /**
212+ * @throws SSOException if a remote call was not handled by the user
213+ */
214+ private void exitRemoteCall () throws SSOException {
215+ String message = "Warning: The exit procedure for a remote call was not properly handled." ;
216+ logger .warn (message );
217+ throw new SSOException (message );
218+ }
180219}
0 commit comments