开发者

Design approach for login without auth token

开发者 https://www.devze.com 2023-02-28 22:25 出处:网络
I am writing an Android 2.2 app for my company.The app simply sends http get/put/post requests to perform certain operations.There is no real login procedure as the username and password have to be in

I am writing an Android 2.2 app for my company. The app simply sends http get/put/post requests to perform certain operations. There is no real login procedure as the username and password have to be included in each http request.

I could see that there is AccountManager in Android. But since the username and password (instead of some auth token) are needed for each http request, how can it fit in? Obviously, I want to make it similar to other Android apps so that the user only needs to login once for the very first time and it won't prompt for username/password again when the app is re-launched.

Any sugge开发者_高级运维stion is appreciated. Thanks!


I have developed an application like that, so here is how I solved it, in psuedocode.

But since the username and password (instead of some auth token) are needed for each http request, how can it fit in?

1#: Make a first page, a login page. Let this View include two EditTexts (username and password) and one Button (login button).

2#: Make a login request on the Button click to see if you're getting a correct Cookie with HTTP header names that is corresponding with the values you are getting when you're logged in. Locate valid information via a network tool, like WireShark. For more information about the login procedure, check out my other answer here.

3#: If the username and password resulted in correct Cookie information, save the username and password in a SharedPreferences and make their values available through your application by assigning it into an Application class, read this for more info regarding global variables. If the values were incorrect and you did not get a valid Cookie, show it to the user via a message (Toast?).

4#: When you are trying to reach the authorized information, make a request by using the saved information in the Application class.

5#: Next time you're starting your application, make a check in onCreate() where you are checking if SharedPreferences contains user information, if so: see step 6#, otherwise wait for the user to start entering information.

6#: If the login page has determined user information, assign the SharedPreferences to the global Application state, finish the login Activity and start the authorized Activity instead. This will happen very fast, so the user wouldn't notice that the "login page" was displayed.

7# (extra step): In the authorized Activity, make sure to grab the user information from the Application instance. When doing the first request towards authorized content, validate the task as you did in step #3 in order to control if the user has changed the password on the website. If the user hasn't changed any information, start grabbing the response and you are free to do whatever you want.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号