Unable to Store IAuthenticationResult MSAL

45 Views Asked by At

I am Using Msal for Microsoft Signin

This is the Snippet i use when user Click on Signin with Microsoft Button

   if (mSingleAccountApp == null) {
                            return;
                        }
                        final SignInParameters signInParameters = SignInParameters.builder()
                                .withActivity(LoginActivity.this)
                                .withLoginHint(null)
                                .withScopes(Arrays.asList(getScopes()))
                                .withCallback(getAuthInteractiveCallback())
                                .build();
                        mSingleAccountApp.signIn(signInParameters);

In getAuthInteractiveCallback i am Saving the IAuthenticationResult in Singalton and run a WorkeManager which hits the acquireTokenSilent every 40 minutes

private AuthenticationCallback getAuthInteractiveCallback() { return new AuthenticationCallback() {

        @Override
        public void onSuccess(IAuthenticationResult authenticationResult) {

            /* Update account */
            mAccount = authenticationResult.getAccount();

            SharedPrefs.setMsalAuthority(LoginActivity.this,mAccount.getAuthority());

            SharedPrefs.setUsername(LoginActivity.this,authenticationResult.getAccount().getUsername());
            SharedPrefs.setPassword(LoginActivity.this,"Microsoft"+authenticationResult.getAccount().getIdToken());

           // MyAccountSerializer.saveAccountDetails(getApplicationContext(), mAccount);
            AccountSingleton.getInstance().setAccount(mAccount);




            Constraints constraints = new Constraints.Builder()
                    .setRequiredNetworkType(NetworkType.CONNECTED)
                    .build();
            // Schedule periodic token refresh
            PeriodicWorkRequest tokenRefreshRequest =
                    new PeriodicWorkRequest.Builder(TokenRefreshWorker.class, 40, TimeUnit.MINUTES)
                            .setConstraints(constraints)
                            .addTag("tokenRefreshTag")
                            .setInitialDelay(10, TimeUnit.SECONDS)
                            .setBackoffCriteria(
                                    BackoffPolicy.LINEAR,
                                    PeriodicWorkRequest.MIN_BACKOFF_MILLIS,
                                    TimeUnit.MILLISECONDS
                            )

                            .build();

            WorkManager.getInstance(getApplicationContext()).enqueue(tokenRefreshRequest);

            ((EditText) findViewById(R.id.input_username)).getText().toString().trim();
            mPassword=inputPassword.getText().toString().trim();

            ((EditText)findViewById(R.id.input_username)).setText( SharedPrefs.getUsername(LoginActivity.this));
                inputPassword.setText(SharedPrefs.getPassword(LoginActivity.this));

          dologinMSAL();

        }

        @Override
        public void onError(MsalException exception) {
            /* Failed to acquireToken */


            if (exception instanceof MsalClientException) {
                /* Exception inside MSAL, more info inside MsalError.java */
            } else if (exception instanceof MsalServiceException) {
                /* Exception when communicating with the STS, likely config issue */
            }
        }

        @Override
        public void onCancel() {
            /* User canceled the authentication */
            Log.d(TAG, "User cancelled login.");
        }
    };

In my Workmanager Class this is how i am using acquireTokenSilent Snippet

private void acquireTokenSilent() {
    final AcquireTokenSilentParameters silentParameters = new AcquireTokenSilentParameters.Builder()
            .fromAuthority(SharedPrefs.getMsalAuthority(ContextHolder.getInstance().getContext()))
            .forAccount(mAccount)
            .withScopes(Arrays.asList(getScopes()))
            .withCallback(getAuthSilentCallback())
            .build();

    mSingleAccountApp.acquireTokenSilentAsync(silentParameters);
}

Now the main issue is this all functionality wokes fine untill i didn't restart the app. The moment i start restarting the app i get maccount null in worker class.

Is there any way i can store complete authenticationResult in app something like shared prefrence

0

There are 0 best solutions below