Search in sources :

Example 1 with NoPublicKey

use of de.tum.in.tumcampusapp.api.app.exception.NoPublicKey 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 NoPublicKey

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

the class AuthenticationManager method generatePrivateKey.

/**
 * Gets private key from preferences or generates one.
 *
 * @return true if a private key is present
 */
public boolean generatePrivateKey(ChatMember member) {
    // Try to retrieve private key
    try {
        // Try to get the private key
        this.getPrivateKeyString();
        // Reupload it in the case it was not yet transmitted to the server
        this.uploadKey(this.getPublicKeyString(), member);
        // If we already have one don't create a new one
        return true;
    } catch (NoPrivateKey | NoPublicKey e) {
    // NOPMD
    // Otherwise catch a not existing private key exception and proceed generation
    }
    // Something went wrong, generate a new pair
    this.clearKeys();
    // If the key is not in shared preferences, a new generate key-pair
    KeyPair keyPair = generateKeyPair();
    // In order to store the preferences we need to encode them as base64 string
    String publicKeyString = keyToBase64(keyPair.getPublic().getEncoded());
    String privateKeyString = keyToBase64(keyPair.getPrivate().getEncoded());
    this.saveKeys(privateKeyString, publicKeyString);
    // New keys, need to re-upload
    this.uploadKey(publicKeyString, member);
    return true;
}
Also used : KeyPair(java.security.KeyPair) NoPublicKey(de.tum.in.tumcampusapp.api.app.exception.NoPublicKey) NoPrivateKey(de.tum.in.tumcampusapp.api.app.exception.NoPrivateKey)

Example 3 with NoPublicKey

use of de.tum.in.tumcampusapp.api.app.exception.NoPublicKey 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)

Aggregations

NoPublicKey (de.tum.in.tumcampusapp.api.app.exception.NoPublicKey)3 AuthenticationManager (de.tum.in.tumcampusapp.api.app.AuthenticationManager)2 Intent (android.content.Intent)1 NoPrivateKey (de.tum.in.tumcampusapp.api.app.exception.NoPrivateKey)1 AccessTokenManager (de.tum.in.tumcampusapp.api.tumonline.AccessTokenManager)1 KeyPair (java.security.KeyPair)1