44

I'm a PHP programmer by profession. So, I don't have any idea about iOS and Android coding.

The scenario is there is one website developed using a Social Networking PHP software titled "PHPFox".

Now there are two similar mobile apps which exactly replicates the functionality of this website. One mobile app is in iOS and another is in Android.

So, I've written a set of RESTful APIs where I'm accepting the request from mobile app, parse the request, pass the request parameters to the function which does the same job for website, get the response from this function, convert it into JSON format and sent it back to mobile app. For iOS and Android app I'm using the same set of REST API files.

When user logs in, the REST API for login gets called. Eventually the PHPFox function for authentication gets called, a security token is generated along with some other user data. With every login the different security token is generated by PHPFox. This data is set into the session. Now every time I call any of the functions through any REST API file the security token generated at the time of login is verified and only upon successful verification of token the PHPFox function gets called. This verification process is done internally by PHPFox. So no need to pass the security token explicitly or implicitly to any REST API call.

Till now everything works absolutely fine.

My doubt starts from here. I don't know whether the session is maintained in iOS/Android app. So, if session on server i.e. PHPFox gets timed out then what will happen to the app? Will it crash? Will the user have to login again? If user kills the app on the device and again comes to the app, does he/she have to do the login process again?

There are too many doubts in my mind. I get totally confused with these things.

Can someone please put more focus on the issue I'm facing? It would be really great if you could explain in detail.

Thanks.

PHPLover
  • 1
  • 51
  • 158
  • 311

7 Answers7

56

REST is sessionless for its nature. You need to generate a token when user logged in. You must save this token on your mobile client. For every request, you need to attach a valid token in request header and check it at server side. If token expires, the token stored on a client is not valid. So, you need to login again because of 401 response. If token it's not correct you need to responde 400. I hope that I'm helpful for you.

valeriocomo
  • 746
  • 4
  • 7
  • 3
    The token you are describing sounds exactly the same as a session ID. – malhal Aug 08 '15 at 01:34
  • 2
    yea but just tell me one thing you can check on most of the android apps that they never ask for login again now does they not set any expiry time or they do what ?? – Sudhanshu Gaur Jan 13 '16 at 20:32
  • 2
    Maybe they could have develop a system that understand if a token is expired and then give a token back to its app. Or the token never expire. I suggest you to read something about oauth. – valeriocomo Jan 15 '16 at 08:25
  • That's the beauty of using Tokens! Unlike sessions, they never expires implicitly unless one kills them explicitly. – aB9 Jun 22 '16 at 17:01
  • 1
    It's true! But it's a good pratice that a token will expire after a period. JSON Web Token is the way! – valeriocomo Jun 23 '16 at 13:58
21

Unlike web browsers, iOS and android apps cannot maintain sessions. Usually, once a user has logged in (login credentials verified from server), its login credentials are saved on client side. Then the app gets data from server using session less REST api calls. This is how mostly it is done in mobile applications.

However, if you want the server session and mobile app go hand in hand (which i don't think is a good idea), the way is

1) When the user logs in, a security token is generated on the server side and saved on both server and client side.

2) The mobile app will be able to communicate with the server as long as the security token is valid.

3) When the session expires, the security token becomes invalid. Now there must be an understanding between server and client about the response when the session is expired. Now the mobile app must redirect the user to login page again. The user will login again and then communicate with the server. This should happen every time the session is expired.

Fawad Masud
  • 12,219
  • 3
  • 25
  • 34
  • Thanks for your answer but I think you didn't understand my point completely. When user logs in from the device through iOS/Android app the session on server get started, the PHPFox generates security token which is set into this session. Upon each subsequent request from app this security token is verified by PHPFox internally to secure the functionality. My doubt is if user doesn't log out from the app and the session on server gets timed out i.e. PHPFox session gets tie out then what should I do? For this I think the session needs to be maintained on app side too. – PHPLover Feb 22 '15 at 07:08
  • And the session from app should be in sync with session from server. Only then the issue will not raise. In short I think both the sessions should work hand in hand. Am I correct? – PHPLover Feb 22 '15 at 07:09
18

If your are using Oauth 2 for athentication, here is the common setup:

  • User logs in on mobile app
  • If the credentials are ok, the server returns the access token, a refresh token and the token's lifetime
  • The mobile app stores those values + current timestamp
  • On the server's side, a garbage collector is configured to clear expired tokens
  • Before making any api call, the mobile app checks if the token is about to expire (with the help of the stored values). If the token is about to expire, the app sends the refresh token which instructs the server to generate a new access token
  • If you want users to stay connected, the app can be configured to check the access token periodically and request a new one if it's stale

Hope this helps.

Cheers

Valéry
  • 344
  • 2
  • 6
  • 7
    what is the use of refresh token if someone can steal your token then he/she can also steal your refresh token ?? – aman verma Jan 10 '16 at 18:58
  • @amanverma refresh tokens can be revoked. These are usually paired with short lived access tokens like self encoded tokens (JWTs) that cannot be revoked manually. – georaldc Jul 16 '19 at 18:52
8

Your server should be completely stateless, and so no session should be stored.. a REST API is effectively just a data abstraction layer with optional security (through token)

So you API expose an authentication service, which will respond with an Authorization token to be used on subsequent requests as a header, this token should be a 1to1 relation with each user, and Universally Unique. It should also have an expire time, at which point your server responds with appropriate error response requesting your app to refresh the token, which can be done either via a separate refresh token system, or requesting that the user logs in again to refresh the token.

It is the APP which should maintain the state, not the server. The server is merely there for data purposes, and so should not rely on any kind of session based authentication.

RaggaMuffin-420
  • 1,762
  • 1
  • 10
  • 14
  • The downside to this approach is the server can't expire the token if it isn't storing it. – malhal Aug 08 '15 at 01:38
  • Ofcourse the server is storing the token, the server needs to store the token to authenticate the client against... but thats all its storing, it shouldn't be storing any other session details. – RaggaMuffin-420 Aug 10 '15 at 08:11
  • Sorry I thought you meant completely stateless like JWT authentication. But you do mean keep state of what client has what token just in a database not via a traditional session. – malhal Aug 11 '15 at 00:38
4

You should not worry about the session from the mobile development side.in Android we use SharedPrefrence and NSUserDefaults (Flag which maintains the session locally).

Ali A. Jalil
  • 873
  • 11
  • 25
Puneet Ahuja
  • 147
  • 6
4

A session is "something" that lives on the server. It can be an object storing details about the user (for instance session id, username, email address...) or any other data that will be required to process future requests (such as shopping cart details, delivery address...).

That "something" is typically an object, which can be stored in memory, in a database or even serialized and saved to the file system (I believe this is the default in PHP).

So when you say "I don't know whether the session is maintained in iOS/Android app", I'm afraid that doesn't make sense. Only the server can maintain sessions.

Typically, the only thing that the client would know (web browser or mobile app) is the session id (in the form of a token or GUID). That is the only thing the client/app needs to remember and it needs to be sent alongside any request to the server.

It could be stored as a cookie and/or sent to the server as a request header.

Then the server will read the session id/token from the cookies or header and will retrieve the session details from the place where it stores sessions (file system, memory or database). That is what happens behind the scene when you call session_start().

To read more about session handling and how to create custom session handler (which might be required in your case to get a token from the request headers):
http://php.net/manual/en/function.session-start.php

June7
  • 19,874
  • 8
  • 24
  • 34
Alex Sanséau
  • 8,250
  • 5
  • 20
  • 25
3

I dont have any experience working with PHPFox but this is how a mobile frontend should ideally handle the issues:

Case 1: Mobile app actively talking to server:

  • Session timeout stamp keeps bumping up and session stays alive.

Case 2: Mobile app active without any server communication (e.g. incoming phone call, moving between apps etc.):

  • Server session may or may not timeout.
  • If it times out, next query to server will fail auth and return an error.
  • App consumes this error and gracefully redirects to login screen with a message toast urging the user to login. (This happens in my banking app)

Case 3: User kills the app on device and relaunches it:

  • The app should store the token either in sqllite or shared preferences. (Always logged in apps take this approach)
  • Upon relaunch, app can query the server with the presistent token.
  • If session is alive, communication goes through and user can continue. If not, user goes to login screen as in Case 2.
Sanand Sule
  • 123
  • 1
  • 5