Search in sources :

Example 1 with AuthenticationManager

use of de.tum.in.tumcampusapp.api.app.AuthenticationManager in project TumCampusApp by TCA-Team.

the class AccessTokenManager method requestAccessToken.

/**
 * Internal method for setting a new token.
 * It uses the given lrzId to generate a new access token, which is saved to
 * shared preferences afterwards
 *
 * @param lrzId LRZ id
 * @return True if new access token has been set successfully
 */
public boolean requestAccessToken(Activity activity, String lrzId) {
    try {
        if (!NetUtils.isConnected(context)) {
            Utils.showToastOnUIThread(activity, R.string.no_internet_connection);
            return false;
        }
        // ok, do the request now
        String strAccessToken = this.generateAccessToken(lrzId);
        Utils.log("AcquiredAccessToken = " + strAccessToken);
        // save access token to preferences
        Utils.setSetting(context, Const.ACCESS_TOKEN, strAccessToken);
        // Upload the secret to this new generated token
        AuthenticationManager am = new AuthenticationManager(activity);
        try {
            am.uploadPublicKey();
        } catch (NoPublicKey noPublicKey) {
            Utils.log(noPublicKey);
        }
        return true;
    } catch (TUMOException ex) {
        String tokenError;
        if (ex.getMessage() == null || !ex.getMessage().isEmpty()) {
            if (ex.getMessage().startsWith("Token-Limit")) {
                tokenError = context.getString(R.string.token_limit_reached);
            } else {
                tokenError = ex.getMessage();
            }
        } else {
            // default message so we don't get an empty Toast
            tokenError = context.getString(R.string.access_token_wasnt_generated);
        }
        // set access token to null
        Utils.setSetting(context, Const.ACCESS_TOKEN, null);
        Utils.showToastOnUIThread(activity, tokenError);
    } catch (Exception ex) {
        // NOPMD
        Utils.log(ex, context.getString(R.string.access_token_wasnt_generated));
        // set access token to null
        Utils.setSetting(context, Const.ACCESS_TOKEN, null);
        Utils.showToastOnUIThread(activity, R.string.access_token_wasnt_generated);
    }
    return false;
}
Also used : AuthenticationManager(de.tum.in.tumcampusapp.api.app.AuthenticationManager) NoPublicKey(de.tum.in.tumcampusapp.api.app.exception.NoPublicKey)

Example 2 with AuthenticationManager

use of de.tum.in.tumcampusapp.api.app.AuthenticationManager in project TumCampusApp by TCA-Team.

the class StartupActivity method init.

private void init() {
    // Migrate all settings - we somehow ended up having two different shared prefs: join them back together
    Utils.migrateSharedPreferences(this.getApplicationContext());
    // Check that we have a private key setup in order to authenticate this device
    AuthenticationManager am = new AuthenticationManager(this);
    am.generatePrivateKey(null);
    // On first setup show remark that loading could last longer than normally
    boolean isSetup = Utils.getSettingBool(this, Const.EVERYTHING_SETUP, false);
    if (!isSetup) {
        this.runOnUiThread(() -> findViewById(R.id.startup_loading_first).setVisibility(View.VISIBLE));
    }
    // Register receiver for background service
    IntentFilter filter = new IntentFilter(DownloadService.BROADCAST_NAME);
    LocalBroadcastManager.getInstance(this).registerReceiver(receiver, filter);
    // Start background service and ensure cards are set
    Intent i = new Intent(this, StartSyncReceiver.class);
    i.putExtra(Const.APP_LAUNCHES, true);
    sendBroadcast(i);
    // Request Permissions for Android 6.0
    requestLocationPermission();
    setupNotificationChannels();
}
Also used : AuthenticationManager(de.tum.in.tumcampusapp.api.app.AuthenticationManager) IntentFilter(android.content.IntentFilter) Intent(android.content.Intent)

Example 3 with AuthenticationManager

use of de.tum.in.tumcampusapp.api.app.AuthenticationManager in project TumCampusApp by TCA-Team.

the class WizNavExtrasActivity method onCreate.

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    overridePendingTransition(R.anim.fadein, R.anim.fadeout);
    preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
    // If called because app version changed remove "Step 4" and close on back pressed
    Intent i = getIntent();
    if (i != null && i.hasExtra(Const.TOKEN_IS_SETUP)) {
        // If coming from a 6X version we need to upload the public key to TUMO
        AuthenticationManager am = new AuthenticationManager(this);
        try {
            am.uploadPublicKey();
        } catch (NoPublicKey noPublicKey) {
            Utils.log(noPublicKey);
        }
        // Remember that we are only running through a limited setup
        tokenSetup = i.getBooleanExtra(Const.TOKEN_IS_SETUP, false);
    }
    // Get handles to all UI elements
    checkSilentMode = findViewById(R.id.chk_silent_mode);
    bugReport = findViewById(R.id.chk_bug_reports);
    bugReport.setChecked(preferences.getBoolean(Const.BUG_REPORTS, true));
    // Otherwise the app cannot load lectures so silence service makes no sense
    if (new AccessTokenManager(this).hasValidAccessToken()) {
        checkSilentMode.setChecked(preferences.getBoolean(Const.SILENCE_SERVICE, false));
        checkSilentMode.setOnCheckedChangeListener((buttonView, isChecked) -> {
            if (checkSilentMode.isChecked() && !SilenceService.hasPermissions(WizNavExtrasActivity.this)) {
                SilenceService.requestPermissions(WizNavExtrasActivity.this);
                checkSilentMode.setChecked(false);
            }
        });
    } else {
        checkSilentMode.setChecked(false);
        checkSilentMode.setEnabled(false);
    }
    // Get handles to all UI elements
    groupChatMode = findViewById(R.id.chk_group_chat);
    if (new AccessTokenManager(this).hasValidAccessToken()) {
        groupChatMode.setChecked(preferences.getBoolean(Const.GROUP_CHAT_ENABLED, true));
    } else {
        groupChatMode.setChecked(false);
        groupChatMode.setEnabled(false);
    }
}
Also used : AuthenticationManager(de.tum.in.tumcampusapp.api.app.AuthenticationManager) NoPublicKey(de.tum.in.tumcampusapp.api.app.exception.NoPublicKey) AccessTokenManager(de.tum.in.tumcampusapp.api.tumonline.AccessTokenManager) Intent(android.content.Intent)

Example 4 with AuthenticationManager

use of de.tum.in.tumcampusapp.api.app.AuthenticationManager in project TumCampusApp by TCA-Team.

the class WizNavExtrasActivity method onLoadInBackground.

@Override
protected ChatMember onLoadInBackground(Void... arg) {
    if (!NetUtils.isConnected(this)) {
        showNoInternetLayout();
        return null;
    }
    // Get the users lrzId and initialise chat member
    ChatMember currentChatMember = new ChatMember(Utils.getSetting(this, Const.LRZ_ID, ""));
    currentChatMember.setDisplayName(Utils.getSetting(this, Const.CHAT_ROOM_DISPLAY_NAME, ""));
    if (currentChatMember.getLrzId().equals("")) {
        return currentChatMember;
    }
    // Tell the server the new member
    ChatMember member;
    try {
        // After the user has entered their display name, send a request to the server to create the new member
        member = TUMCabeClient.getInstance(this).createMember(currentChatMember);
    } catch (IOException e) {
        Utils.log(e);
        Utils.showToastOnUIThread(this, R.string.error_setup_chat_member);
        return null;
    }
    // Catch a possible error, when we didn't get something returned
    if (member == null || member.getLrzId() == null) {
        Utils.showToastOnUIThread(this, R.string.error_setup_chat_member);
        return null;
    }
    // Generate the private key and upload the public key to the server
    AuthenticationManager am = new AuthenticationManager(this);
    if (!am.generatePrivateKey(member)) {
        // We cannot continue if the upload of the Public Key does not work
        Utils.showToastOnUIThread(this, getString(R.string.failure_uploading_public_key));
        return null;
    }
    // Try to restore already joined chat rooms from server
    try {
        List<ChatRoom> rooms = TUMCabeClient.getInstance(this).getMemberRooms(member.getId(), ChatVerification.Companion.getChatVerification(this, member));
        new ChatRoomController(this).replaceIntoRooms(rooms);
        // Store that this key was activated
        Utils.setSetting(this, Const.PRIVATE_KEY_ACTIVE, true);
        return member;
    } catch (IOException | NoPrivateKey e) {
        Utils.log(e);
    }
    return null;
}
Also used : ChatMember(de.tum.in.tumcampusapp.component.ui.chat.model.ChatMember) AuthenticationManager(de.tum.in.tumcampusapp.api.app.AuthenticationManager) NoPrivateKey(de.tum.in.tumcampusapp.api.app.exception.NoPrivateKey) ChatRoom(de.tum.in.tumcampusapp.component.ui.chat.model.ChatRoom) IOException(java.io.IOException) ChatRoomController(de.tum.in.tumcampusapp.component.ui.chat.ChatRoomController)

Example 5 with AuthenticationManager

use of de.tum.in.tumcampusapp.api.app.AuthenticationManager in project TumCampusApp by TCA-Team.

the class WizNavStartActivity method onClick.

/**
 * Handle click in dialog buttons.
 *
 * @param dialog Dialog handle
 * @param which  Button clicked
 */
@Override
public void onClick(DialogInterface dialog, int which) {
    if (which == DialogInterface.BUTTON_POSITIVE) {
        AuthenticationManager am = new AuthenticationManager(this);
        am.clearKeys();
        am.generatePrivateKey(null);
        // create a new token
        startLoading(lrzId);
    } else if (which == DialogInterface.BUTTON_NEGATIVE) {
        onLoadFinished(true);
    }
}
Also used : AuthenticationManager(de.tum.in.tumcampusapp.api.app.AuthenticationManager)

Aggregations

AuthenticationManager (de.tum.in.tumcampusapp.api.app.AuthenticationManager)6 Intent (android.content.Intent)2 NoPrivateKey (de.tum.in.tumcampusapp.api.app.exception.NoPrivateKey)2 NoPublicKey (de.tum.in.tumcampusapp.api.app.exception.NoPublicKey)2 IntentFilter (android.content.IntentFilter)1 AccessTokenManager (de.tum.in.tumcampusapp.api.tumonline.AccessTokenManager)1 ChatMessageViewModel (de.tum.in.tumcampusapp.component.ui.chat.ChatMessageViewModel)1 ChatRoomController (de.tum.in.tumcampusapp.component.ui.chat.ChatRoomController)1 ChatMember (de.tum.in.tumcampusapp.component.ui.chat.model.ChatMember)1 ChatMessage (de.tum.in.tumcampusapp.component.ui.chat.model.ChatMessage)1 ChatRoom (de.tum.in.tumcampusapp.component.ui.chat.model.ChatRoom)1 ChatMessageLocalRepository (de.tum.in.tumcampusapp.component.ui.chat.repository.ChatMessageLocalRepository)1 ChatMessageRemoteRepository (de.tum.in.tumcampusapp.component.ui.chat.repository.ChatMessageRemoteRepository)1 TcaDb (de.tum.in.tumcampusapp.database.TcaDb)1 CompositeDisposable (io.reactivex.disposables.CompositeDisposable)1 IOException (java.io.IOException)1