Best place for storing user login credentials in Android

后端 未结 3 1212
南笙
南笙 2020-12-08 03:24

I am creating an android app, and i cannot(?) find any information on authenticating a user of the app on Google App Engine (without using the user\'s Google Account).Is it

相关标签:
3条回答
  • 2020-12-08 03:50

    You can save the user credentials in shared preference. You can use this preference value at any of your activity. The saved value will remains even after the exit from application. The sample code for shared preferences are here. It is recommended to keep your preferences code in your application utils class to keep your code organized.

      public static String KEY = "SESSION"; 
        public static void saveUserName(String userid, Context context) {
            Editor editor = context
                    .getSharedPreferences(KEY, Activity.MODE_PRIVATE).edit();
            editor.putString("username", userid);
            editor.commit();
        }
    
    public static String getUserName(Context context) {
        SharedPreferences savedSession = context.getSharedPreferences(KEY,
                Activity.MODE_PRIVATE);
        return savedSession.getString("username", "");
    }
    
    0 讨论(0)
  • 2020-12-08 03:52

    Consider SharedPreferences for this, like...

    public class PrefUtils {
        public static final String PREFS_LOGIN_USERNAME_KEY = "__USERNAME__" ;
        public static final String PREFS_LOGIN_PASSWORD_KEY = "__PASSWORD__" ;
    
        /**
         * Called to save supplied value in shared preferences against given key.
         * @param context Context of caller activity
         * @param key Key of value to save against
         * @param value Value to save
         */
        public static void saveToPrefs(Context context, String key, String value) {
            SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
            final SharedPreferences.Editor editor = prefs.edit();
            editor.putString(key,value);
            editor.commit();
        }
    
        /**
         * Called to retrieve required value from shared preferences, identified by given key.
         * Default value will be returned of no value found or error occurred.
         * @param context Context of caller activity
         * @param key Key to find value against
         * @param defaultValue Value to return if no data found against given key
         * @return Return the value found against given key, default if not found or any error occurs
         */
        public static String getFromPrefs(Context context, String key, String defaultValue) {
            SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(context);
            try {
                return sharedPrefs.getString(key, defaultValue);
            } catch (Exception e) {
                 e.printStackTrace();
                 return defaultValue;
            }
        }
    }
    

    Simply use these methods like,

    // Saving user credentials on successful login case
    PrefUtils.saveToPrefs(YourActivity.this, PREFS_LOGIN_USERNAME_KEY, username);
    PrefUtils.saveToPrefs(YourActivity.this, PREFS_LOGIN_PASSWORD_KEY, password);
    
    // To retrieve values back
    String loggedInUserName = PrefUtils.getFromPrefs(YourActivity.this, PREFS_LOGIN_USERNAME_KEY);
    String loggedInUserPassword = PrefUtils.getFromPrefs(YourActivity.this, PREFS_LOGIN_PASSWORD_KEY);
    

    I think its much clearer now...:)

    0 讨论(0)
  • 2020-12-08 04:10

    You should NOT use SharedPreferences, despite how secure Google will tell you they are, for the simple fact that rooting habits are widespread by both power and wannabe-power users.

    In rooted systems, authorized apps will be able to access the internal storage /system partition, thus access the root-owned encrypted XML files where Android stores SharedPreferences, WHICH BECOME CLEARTEXT FOR ROOT-AUTHORIZED APPS. So if a compromised phone happens to have an app that stores such data and the user has a habit of abusing same email/password for different services, the attack vector is pretty straightforward to compromise credentials for multiple services, despite whatever warnings the user dismissed when rooting his phone or giving the app such permissions, because, well, those warnings just aren't clear enough.

    Alternatives are manual encryption of persistent storage for credentials, using custom, variable seed, salted algorithms. secure-preferences lib is a great alternative which pretty much does everything for you in a transparent way, with the added benefit of implementing Android's SharedPreferences interface, keeping most functionality of default sprefs with minor tweaking (look at their samples).

    2016 Edit: I just felt I needed to come back to this question in 2016 and add a nice reference to the Apple vs FBI drama to alter the OS in the San Bernardino assailant's phone. So, imagine you have a phone that is easily rootable, like most Android's, and that might even be rooted by default or doesn't need to wipe data to root. The FBI doesn't have to demand anything from Google or your company if they wan't to get your credentials. By using stuff like secure-preferences, you place the same type of responsibility Apple decided to undertake by only making your own system (in this case, your app) able to access those credentials. You do get to be annoyed by the FBI if they so desire, but giving your users the sense that you, not the underlying OS, is the only authority having direct control over those credentials, is something I'd rather have in my product.

    0 讨论(0)
提交回复
热议问题