Right now, my company runs a java app that uses IMAP and SMTP to read/send e-mails without user interaction. The authentication protocol we use is Basic Authentication.
Microsoft has announces End-of-support for Basic authentication, which will be replaced by Oauth2. Unfortunately, they did not provide a clear solution for applications without user interaction.
https://learn.microsoft.com/en-gb/exchange/client-developer/legacy-protocols/how-to-authenticate-an-imap-pop-smtp-application-by-using-oauth
One of my ideas is to have users authenticate themselves (using Oauth2 authorization code flow) just once, the first time they start the service. Then, refresh tokens will be used to get access tokens without user interaction. It is very important that the connection is not broken because of short expiration dates or revocations. There will probably be nobody around to re-authenticate if the app will be unable to get new access tokens.
I cannot seem to find any information about expiry dates for these tokens (if it is possible to set it). The ideal would be that they never expire...
Does anyone know more about this? It would help me a lot to know whether my idea is a viable approach.
Access tokens can be automatically refreshed. But to me more provider specific I did find some info on refresh token expiry here
https://learn.microsoft.com/en-us/linkedin/shared/authentication/programmatic-refresh-tokens
According to this link LinkedIn tokens access and refresh expire after a max of 365 days.
Also if you see this link it says that the refresh token expires in 90 days if inactive but doesn’t say anywhere that it will expire if active.
https://learn.microsoft.com/en-us/office365/enterprise/session-timeouts
Can you provide a reference to the API that you are trying to use so that I find some more info on it (maybe)
Related
Background
We've developed a CMS in ASP.Net Core 2.2 a few years ago for company internal day-to-day stuff. It holds several hundreds of users and I'm considering adding an android app to it. The android app should mainly serve as a front end to existing logic in the ASP.Net controllers. The benefits of the Android app I expect to have are:
Better UX
User not having to type the password for each session (the ASP.Net page logs them out after 20 or 30 minutes of inactivity)
Push notifications etc.
Existing infrastructure
The CMS has already an API (JWT) which is used by several backend services and could easily be expanded to be used by every user through the android app. Unfortunately I'm not a professional developer in general and are quite new to Java especially, so please bare some patience with me as I'm asking for a general advise of how to handle the authentication in the android app (Java).
I read about curity.id and stuff like this, but IMO this would be kind of overkill to the whole project and would also require major changes to the already working fine ASP.Net website.
Now my question is whether the following would be an acceptable practice to do:
When the user opens the app for the first time, I ask him for his username and password (and optionally 2nd factor).
I make an http request to the API of the CMS in order to authenticate the user and get back the JWT token on successful authentication.
The JWT token gets stored in some way (e.g. SQLite DB) and is being used for subsequent requests being made to the API.
Since the the lifetime of the JWT token is also limited to a few hours and the goal is to keep the app running without prompting the user for his credentials all the time, I need to store the password also in some way in the app or a DB on the android device. This is actually the part which confuses me the most and I have no clue how I would store the password in a safe way, so it may be used again for authentication requests down the road. (I'm aware that if the user uses 2nd factor there is no way around but to ask for the 2nd factor every time the token expires or to exclude the device or so, but this should not be within the scope of this question)
So I would be grateful to get some judgements about this approach, if it is acceptable in general and what would be the best way to do about point #4.
Thanks a lot
Do not store password
If store (and frequently send) passwords you defeat the entire purpose of a token. The token itself exists as a disposable means to substitute the primary authentication (yet is cryptographically sufficiently strong to replace it without reducing system security).
SIMPLE: Use a long token
Create a new authentication API (or add some parameters to the existing one) to enable the mobile APP to require a long-lasting token (for example 6 months). If you then store this token in the SecureStorage of the device you can be quite confident it can not be extracted from there.
Remember that with this solution, you lose control over the authentication. This is not the best practice but is the easiest solution to adopt in the case of existing JWT infrastructures that use only one token.
RECCOMENDED: Create a renew token system
Once you issued a token you cannot revoke it. Hence the main issue introduced by the 'long token' solution is that if a device gets lost it can not be de-authenticated. This can be solved by a renew token:
You create an authentication API that after the confirmed authentication it creates a unique sessionId, stores it in the DB, and issues a special token that contains the sessionId among other information. This is called the renew token. This special token can be used only for the exchange API
You then create an exchange API, that expects a renew token in exchange for a normal token (that can have the same properties your current token has). This API will always check if the sessionId present in the renew token is still marked as active in the DB otherwise will not issue the normal token.
This approach allows you to have very long expiring tokens but still have control to remotely log out the app (by masking the sessionId as deactivated in the DB) and even monitor how frequently the app is used.
recently I've started my project in SpringBoot and Kotlin and I wanted to create user management system. After a few searches, I've looked at this video regarding the implementation of JWT tokens for the authentication process.
Everything seems to work perfectly, besides the fact that currently, I need to pass the username and password in each authentication - which means that the client calling the API needs to either store it or ask for it each time.
After looking in this article it seems that option 3 is actually what I'm looking for. From what I'm understanding the process of authentication should be as follow:
a client calls the authentication method
2 tokens are returned - 1 with a short period which is the authentication token, and one with a longer period which allows renewal of the authentication token.
Once the authentication token is expired, the client will as to renew the expired token using only those tokens without the original details of the user.
I have 2 questions in this regards:
From the security perspective - what will happen if a hacker "catches" those 2 tokens? that basically means that the hacker can authenticate as this user from now on without any problem - isn't it?
More or implementation question, from what I could see, jjwt does not support this functionality (as I can read the information from the token as long as the token did not expire). From my search, I couldn't find something that gives it "out of the box" - is there a standard that to implement this functionally?
I am building my own authentication microservice and although I have the main setup in place (generating access tokens etc.), I am a bit lost when it comes to refresh tokens.
I feel there are a lot of different way to handle this.
You can either store them in Redis or in the database.
You can use a whitelist or a blacklist them
Right now, my idea is to add another database table that links a valid refresh token to a user entity. When a user hits the logout endpoint, the refresh token gets destroyed.
I was wondering if this was a good solution and otherwise, if there are other possible solutions to consider. I have seen a number of articles when googling but they stem from anywhere between 2015 and 2019, and they all have different approaches.
The issue with refresh tokens is not so much where or how you store them on the server side, as well if and how you store them on the client side.
It all depends on whether you can trust your client (software using the token) to keep secrets. You only want to issue refresh tokens to a client you can trust to keep these tokens secure. Typically, this means only issue refresh tokens to confidential clients, i.e. web applications that run on a web server. These clients can also have their own (client) credentials to authenticate themselves with when using the refresh token.
For public (non-confidential) clients, like Single Page Applications, some OAuth2 libraries use an hidden IFRAME and a cookie session with the authorization server to issue new access tokens.
So, the answer to your question depends on what kind of clients you will be using.
i am working on a java/j2ee based web application where i have one module called leave management in which when the employee request for the leave the manager approves or reject leave by log in to the application,Here log in into the application creates an extra overhead for the user , so i am trying to implement a feature where the manager can approve or reject the from his/her mail itself as mail is sent to the manager every time the employee request for leave based on the parameters in the url in the link like this
without log in to the application
https://my.xyz.com//LEAVE#LeaveReq#123#1545#State
So my question is
Is it possible to achieve this without log in to the application and saving values to the database and without breaching the security.
2.If yes, how can i implement this?
Yes you could. Just pass in some other parameters like say we call it token which would contain a "random" string which is currently in your database.
Each request that doesn't require authentication should also pass in this token. You check the token in your database if it is there then do the request, if not, you know what to do. If a token is consumed, you could either delete it from your DB (meaning each token is one-time use only).
EDIT: Regarding whether the correct manager approves/reject, well that's difficult without something like authentication to identity the manager.
We just rely on the fact that that URL can only be used once, and that it can only be seen on the email of the appropriate person.
is it possible? Certainly.
Is it desirable? Maybe not. What prevents someone else from sending that confirmation email? You need to find a way to secure it all.
How to do it? A REST webservice comes to mind, with the manager just clicking a link in that automatically generated email which launches a webservice request.
Or you set up a system where the server can receive emails, and the manager can then just forward the leave request to either of 2 email addresses.
There should be at least single authentication like a pin code or password before manager approve or disapprove the leave. Don't think about overhead, for such circumstances security is equally important.
Is it possible to achieve this without log in to the application and
saving values to the database and without breaching the security.
When the manager gets an email & he clicks on the approve. Ideally a request is fired from manager's system carrying an authentication token or something like a remember me token. In this case the application won't prompt the manager to login. It will tally the request token with token stored in the db & would let manager in.
Check how remember me works in web applications. You can create a remember me token for manager for an indefinite amount of time. This way the app will never prompt the manager for a login. But there is a caveat, this approach is a little vulnerable for attack. If there is a man in middle attack the hacker will get hold of the manager's client side cookies & would always login with ease. The auth token should be changed periodically. You can always implement remember me, it just depends on what level of security you require.
Bottom line: Yes it is possible, generate a remember me token which will always be included in the manager's email request for approving employee's leave request. Just follow the best practices to implement a remember me token.
Another way is
If your application is implementing security using filters. You can bypass the request for leave approval. Just put a secure none attribute for that particular request in ant style say "yourapp/approval" resource. Now the server side code would let all the requests in this pattern pass without security check. But this approach is a strict No No. If this happens a hacker which intercepts the leave request from the employee's browser can approve his leave by himself. No need of manager's authentication.
If yes, how can i implement this?
Implementing remember-me for a website
You can also integrate Spring Security with your code. It's hell of a framework, one stop solution for all the security related features which your app would require. You don't have to write the login for implementing auth token & stuff from scratch.
Is it possible? Certainly.
Is it desirable? Yes
Use a one time hash(sha1 hash may be) in the link to approve and after approving the email, send an email to the person who approved the email saying that "He/She has approved a leave for this particular person"
So the manager/authorizer is aware about it.
How password rest with an email link works could be a good analogy.
I think login is not critical for this scenario assuming that cancelling a approved leave is not that critical.(It happens usually in many companies)
My company is building a RESTful API that will return moderately sensitive information (i.e. financial information, but not account numbers). I have control over the RESTful API code/server and also am building the Android app. I've setup the API to use OAuth 2 with authorization code grant flow (with client ID and secret), and I auto-approve users without them having to approve the client since we own both client and provider. We use CAS for SSO and I am using this for the Authorization server as part of the OAuth 2 process when the user logs in to retrieve the token.
I am contemplating various ways to secure the data on the Android app. I've concluded that storing the client id and secret on the device is definitely not going to happen, but am thinking that storing the auth token might work, since it is only risk to the individual user (and really only if they happen to have a rooted phone).
Here are two options I have thought of. They both require me to have a sort of proxy server that is CAS protected, does the dance with the API server, and returns the auth token. This gets rid of the need for storing the client id and secret in the app code.
Here are what I've come up with:
1) Require the user to enter their password to access data each time they startup the App. This is definitely the most foolproof method. If this were done, I'd probably want to save the userID for convenience, but in that case couldn't use the CAS login (since it's web-based). I might be able to use a headless browser on the backend to log the user into CAS and retrieve the token based on what they enter in the Android form, but this seems hacky. Saving the userID is similar to what the Chase app does (if you happen to use this one) - it saves the userID but not your password between sessions.
2) Store the auth token on the Android device. This is a little less secure, but almost foolproof. When the user starts the app for the first time, open the webpage to the CAS login of the proxy server that returns the token (similar to https://developers.google.com/accounts/docs/MobileApps). After the user logs in and the token is returned to the app, encrypt it and store it private to the application. Also, use ProGuard to obfuscate the code, making the encryption algorithm more difficult to reverse engineer. I could also work in a token refresh, but I think this would be more of a false sense of security.
3) Don't use CAS but come up with another way to get an auth token for the service.
Any advice of how others have implemented similar scenarios (if it's been done)?
Thanks.
Well the reason why standards like OAuth are developed is that not everyone has to rethink the same attack vectors again and again. So most often it is your best choice to stick to something already available instead of baking your own thing.
The first problem with clients that are not capable of secretly storing data is that the user's data could be accessed by some attacker. As it is technically not possible to prevent this (code obfuscation won't help you against an expert attacker), the access token in OAuth 2 typically expires after short time and doesn't give an attacker full access (bounded by scope). Certainly you shouldn't store any refresh token on such a device.
The second problem is client impersonation. An attacker could steal your client secret and access your API in his own (maybe malicious) app. The user would still have to login there himself. The OAuth draft there requires the server to do everything it can to prevent this, but it is really hard.
The authorization server MUST authenticate the client whenever possible. If the authorization server cannot authenticate the client due to the client's nature, the authorization server MUST require the registration of any redirection URI used for receiving authorization responses, and SHOULD utilize other means to protect resource owners from such potentially malicious clients. For example, the authorization server can engage the resource owner to assist in identifying the client and its origin.
I think Google are the first to try another approach to authenticate a client on such devices, by checking the signature of the application, but they are not yet ready for prime time. If you want more insight into that approach, see my answer here.
For now, your best bet is to stay on the OAuth way, i.e. having the access token, client ID and client secrect (when using the authorization code grant flow) on the device, and configure your server to do additional checks. If you feel more secure obfuscating these, just do it, but always think of it as if these values were publicly available.