Express JS Routes - javascript

Beginner question here. I’m wanting to build an app that will be an intranet of sorts for a 70 unit restaurant chain. What I’m needing is for each physical restaurant location to see data posted to a MongoDB DB and show on a page in the store within a screen hanging on the wall. This data will be specific to each location such as announcements, catering orders that need to be filled by that store, etc... my question is, how do I create routes so that each individual store will see only their data without having to create 70 different routes or use authentication to utilize user IDs?
Trying to avoid user logins and:
“/store1”
“/store2”
“/store3”
Etc...
Seems like I could query a site ID or something like that
Hope this make since.

If you're talking about one centrally located server that is accessed by all 70 restaurants and you want to have the same routes for all, then you need to decipher some other clue from the request to figure out which restaurant it is that is making the request.
Since you have nixed authentication, I will assume that there are no security concerns here - that this is on an intranet only accessible by restaurant employees.
So, here are the clues you could get from any given request to decide which restaurant's data to show:
Cookie that contains desired restaurant ID.
Query parameter that contains desired restaurant ID.
IP address of request to discern which restaurant the request is coming from.
Separate sub-domain for each restaurant (all pointing to the same host).
Here's more info on each option:
Cookie
In this implementation, if a client arrives with no cookie set for the restaurant ID, then you take them to an opening page where they select which restaurant they want to see the data for and you ask the user to manually select the desired restaurant. Then, you set a long lasting cookie in that browser and from then on (as long as the cookie is not removed), your server can default to showing them info only for their restaurant.
Query Parameter
In this implementation, you use some initial restaurant specification like in the cookie option and then you generate pages where all the links have a query parameter specified in them that lists the restaurant ID (likely using a template system). This is generally not favored these days over a plain cookie system.
IP address
If all your restaurants have their own sub-network (presumably some sort of broadband connection connecting to a central server over some sort of private connection, then you could perhaps build a small table of restaurant source IP addresses so you could identify which restaurant was making the request purely by the IP address of the request. This may or may not be feasible depending upon your network topology. This would be much more automatic than any of the other options.
Separate sub-domain
This would work somewhat like the cookie-based option, but instead of setting a cookie, once the desired restaurant was selected, you would redirect to a subdomain specific to that restaurant. All sub-domains would still point to the same host, but the host could identify the desired restaurant from the sub-domain making the request. Similar to the query parameter option, you'd some template system on the server side to rewrite all links to include the proper sub-domain so you'd stay on the same sub-domain as you navigate.
Of all of these options, the IP address scheme is the only one that would be completely automatic, but may or may not be feasible depending upon your network topology. Of the other choices, the cookie is the most straightforward to implement because the page links themselves don't have to be rewritten.
Enterprise Configuration
In an enterprise scenario where you "manage" any browser that is approved for use, you may also be able to pre-configure the browser in each restaurant to automatically provide some identification (either a custom header or a cookie) and your server could use that identification info to determine which restaurant was making the choice. This would require accessing your site only through pre-configured and approved and properly maintained browsers (which may be more pain than it's worth).
For example, here's how you would configure a custom header on all requests with Microsoft Edge.

Related

How to get device id in react js based web application?

I'm developing a reactjs based application. I want to limit the login from multiple devices at one time to an account. So I need to have an ID that is unique and fixed on device.
I also want the user to stay logged in on one device, so I can not use IP address.
Also, I've already limited access from several browsers.
Thanks a lot. Also, sorry for my English.
This check can't be implemented on client side of the application.
On the server end, you can get IP address from requested payload. You can store the IP address against the session in In-memory databases like redis or memcache. Now, create an intermediate check at login controller where you can block the user if he/she has logged in more than certain number of IP addresses.

Add Remember Functionality in Website to load same page when url is loaded

I am making an app with local Html files inside the assets folder in android studio. When opened it first open index.html files as specified. As it is routine application, I was wondering if I could add Remember this functionality, such that when user loads application, they'll get automatically redirects to the same page they have checked and also provides Reset in case they want to reset their choice in future.
Any Help would be greatly appreciated.
Improved Persistent Login Cookie Best Practice
You could use this strategy described here as best practice (2006) or an updated strategy described here (2015):
When the user successfully logs in with Remember Me checked, a login cookie is issued in addition to the standard session management cookie.
The login cookie contains a series identifier and a token. The series and token are unguessable random numbers from a suitably large space. Both are stored together in a database table, the token is hashed (sha256 is fine).
When a non-logged-in user visits the site and presents a login cookie, the series identifier is looked up in the database.
If the series identifier is present and the hash of the token matches the hash for that series identifier, the user is considered authenticated. A new token is generated, a new hash for the token is stored over the old record, and a new login cookie is issued to the user (it's okay to re-use the series identifier).
If the series is present but the token does not match, a theft is assumed. The user receives a strongly worded warning and all of the user's remembered sessions are deleted.
If the username and series are not present, the login cookie is ignored.
This approach provides defense-in-depth. If someone manages to leak the database table, it does not give an attacker an open door for impersonating users.

Routing to subdomains using Sails.js

I'm attempting to create an application in Sails.js where users are routed to a specific subdomain based on a company attribute in their profile.
For example, Jack Smith belongs to company "pluto". If Jack logs in to the application at myapp.com , he would be redirected to the dashboard at pluto.myapp.com.
There seems to be a lot of resources on this for express.js but I can't seem to find a straight answer on how to apply those principles in Sails (see this). I'm a novice in Sails/NodeJs so this is MORE than likely a fault on my part :)
Any help is greatly appreciated.
A lot of articles you find on express will apply to sails, but they can over engineer the problem since you are using authentication.
Your dashboards will change based on user or company settings. Since your dashboard is behind a login, then the sub domain is irrelevant to achieve your goal since the variable that adjusts the data being shown on your dashboard is just as easily found via a session or token depending on how you authenticate.
The simplest answer I can give you to your problem assumes you already have your sub domains routed correctly to your main domain. Then all you have to do is log them in and direct them to the correct sub-domain via res.redirect(subDomain+'.mysite.com'). The dashboard can then be displayed based on their login credentials without really caring what the sub-domain is. In this instance, the sub-domain is merely for decoration.
Now if you want to enforce and make sure the correct sub domain is always displayed for a person's credentials, you can always verify that in a policy.

Is a cookie the only way for jQuery to open a popup only once per user or can jQuery handle IP validation?

There are already numerous questions about jQuery cookie popups, so no need to go into any detail about that functionality. What I want to know is if this is the only way jQuery can handle popups only once per user or a time-based popup per user. I.e. the popup loads when the user first comes to the site and then doesn't load again for a week.
I know there is IP validation, but can jQuery do IP validation and if so, can you point me to a resource? I've searched here for jQuery popup IP validation and so far I haven't turned up anything.
I've said it countless times before and I'll say it again: IPs are a data delivery mechanism, not an identification or authentication tool. IPs are not guaranteed to be unique or stable in any way and are entirely unsuitable for identifying individual users at one machine.
You need to store an identifier on the user's machine somehow to remember for that particular user what he did or didn't do. A cookie is the most obvious answer, local storage, WebDBs, Flash cookies etc. are alternative fallbacks.
I must agree with deceze, considering a company for example could use a single IP while 10+ computers are connected to the web through this IP-address. So if an employee would visit your website the other 9 won't get to see your pop-up because someone with the same IP already visited your website.
But regardless of this being the right approach or not, to get back at you´re question, I came across this SO answer:
How to get client IP address using jQuery
It looks relative easy to get a vistors IP address, even though you should consider you're dependent on a third party this way.
This has more to do with an understanding of how web browsers work and less to do with JQuery. What you need is a way to identify a user. The easiest and most accurate way is a cookie. Something that only the user will have. Plus, you're trying to trying to do this on the client side. The only way for javascript to persist data across requests is via cookies, and for some browsers an Html 5 data persistance structure like IndexedDB.
If you can program on the server you can try to do IP lookups but you potentially get one identifier for multiple devices which can be multiple users. This is because it is common for multiple people to use the same IP (think about how many devices are connected to your home router). You can get more accurate by using the http header value for user-agent. These values are not accessible via javascript.
It may help you to get a better understanding of how http requests and responses.

SINGLE SIGN ON SECURITY THREAT! FACEBOOK access_token broadcast in the open/clear

02/20/2011:
It was confirmed by Facebook today that indeed there is one call in which the access_token is broadcast in the open . . . it just happens to be one call I use to make sure that the USER is still logged in before saving to my application database. Their recommendation was to use the SSL option provided as of last month for canvase and facebook as a whole. For the most part the Auth and Auth are secure.
Findings:
Subsequent to my posting there was a remark made that this was not really a question but I thought I did indeed postulate one. So that there is no ambiquity here is the question with a lead in:
Since there is no data sent from Facebook during the Canvas Load process that is not at some point divulged, including the access_token, session and other data that could uniquely identify a user, does any one see any other way other than adding one more layer, i.e., a password, sent over the wire via HTTPS along with the access_toekn, that will insure unique untampered with security by the user?
Using Wireshark I captured the local broadcast while loading my Canvas Application page. I was hugely surprised to see the access_token broadcast in the open, viewable for any one to see. This access_token is appended to any https call to the Facebook OpenGraph API.
Using facebook as a single click log on has now raised huge concerns for me. It is stored in a session object in memory and the cookie is cleared upon app termination and after reviewing the FB.Init calls I saw a lot of HTTPS calls so I assumed the access_token was always encrypted.
But last night I saw in the status bar a call from what was simply an http call that included the App ID so I felt I should sniff the Application Canvas load sequence.
Today I did sniff the broadcast and in the attached image you can see that there are http calls with the access_token being broadcast in the open and clear for anyone to gain access to.
Am I missing something, is what I am seeing and my interpretation really correct. If any one can sniff and get the access_token they can theorically make calls to the Graph API via https, even though the call back would still need to be the site established in Facebook's application set up.
But what is truly a security threat is anyone using the access_token for access to their own site. I do not see the value of a single sign on via Facebook if the only thing that was established as secure was the access_token - becuase for what I can see it clearly is not secure. Access tokens that never have an expire date do not change. Access_tokens are different for every user, to access to another site could be held tight to just a single user, but compromising even a single user's data is unacceptable.
http://www.creatingstory.com/images/InTheOpen.png
Went back and did more research on this:
FINDINGS:
Went back an re ran the canvas application to verify that it was not any of my code that was not broadcasting.
In this call: HTTP GET /connect.php/en_US/js/CacheData HTTP/1.1
The USER ID is clearly visible in the cookie. So USER_ID's are fully visible, but they are already. Anyone can go to pretty much any ones page and hover over the image and see the USER ID. So no big threat. APP_ID are also easily obtainable - but . . .
http://www.creatingstory.com/images/InTheOpen2.png
The above file clearly shows the FULL ACCESS TOKEN clearly in the OPEN via a Facebook initiated call.
Am I wrong. TELL ME I AM WRONG because I want to be wrong about this.
I have since reset my app secret so I am showing the real sniff of the Canvas Page being loaded.
Additional data 02/20/2011:
#ifaour - I appreciate the time you took to compile your response.
I am pretty familiar with the OAuth process and have a pretty solid understanding of the signed_request unpacking and utilization of the access_token. I perform a substantial amount of my processing on the server and my Facebook server side flows are all complete and function without any flaw that I know of. The application secret is secure and never passed to the front end application and is also changed regularly. I am being as fanatical about security as I can be, knowing there is so much I don’t know that could come back and bite me.
Two huge access_token issues:
The issues concern the possible utilization of the access_token from the USER AGENT (browser). During the FB.INIT() process of the Facebook JavaScript SDK, a cookie is created as well as an object in memory called a session object. This object, along with the cookie contain the access_token, session, a secret, and uid and status of the connection. The session object is structured such that is supports both the new OAuth and the legacy flows. With OAuth, the access_token and status are pretty much al that is used in the session object.
The first issue is that the access_token is used to make HTTPS calls to the GRAPH API. If you had the access_token, you could do this from any browser:
https://graph.facebook.com/220439?access_token=...
and it will return a ton of information about the user. So any one with the access token can gain access to a Facebook account. You can also make additional calls to any info the user has granted access to the application tied to the access_token. At first I thought that a call into the GRAPH had to have a Callback to the URL established in the App Setup, but I tested it as mentioned below and it will return info back right into the browser. Adding that callback feature would be a good idea I think, tightens things up a bit.
The second issue is utilization of some unique private secured data that identifies the user to the third party data base, i.e., like in my case, I would use a single sign on to populate user information into my database using this unique secured data item (i.e., access_token which contains the APP ID, the USER ID, and a hashed with secret sequence). None of this is a problem on the server side. You get a signed_request, you unpack it with secret, make HTTPS calls, get HTTPS responses back. When a user has information entered via the USER AGENT(browser) that must be stored via a POST, this unique secured data element would be sent via HTTPS such that they are validated prior to data base insertion.
However, If there is NO secured piece of unique data that is supplied via the single sign on process, then there is no way to guarantee unauthorized access. The access_token is the one piece of data that is utilized by Facebook to make the HTTPS calls into the GRAPH API. it is considered unique in regards to BOTH the USER and the APPLICATION and is initially secure via the signed_request packaging. If however, it is subsequently transmitted in the clear and if I can sniff the wire and obtain the access_token, then I can pretend to be the application and gain the information they have authorized the application to see. I tried the above example from a Safari and IE browser and it returned all of my information to me in the browser.
In conclusion, the access_token is part of the signed_request and that is how the application initially obtains it. After OAuth authentication and authorization, i.e., the USER has logged into Facebook and then runs your app, the access_token is stored as mentioned above and I have sniffed it such that I see it stored in a Cookie that is transmitted over the wire, resulting in there being NO UNIQUE SECURED IDENTIFIABLE piece of information that can be used to support interaction with the database, or in other words, unless there were one more piece of secure data sent along with the access_token to my database, i.e., a password, I would not be able to discern if it is a legitimate call. Luckily I utilized secure AJAX via POST and the call has to come from the same domain, but I am sure there is a way to hijack that.
I am totally open to any ideas on this topic on how to uniquely identify my USERS other than adding another layer (password) via this single sign on process or if someone would just share with me that I read and analyzed my data incorrectly and that the access_token is always secure over the wire.
Mahalo nui loa in advance.
I am not terribly familiar with Facebook's authentication/authorization methods, but I do believe that they implement oauth (or something close to it) for delegation, distributed authorization, and "single sign-on".
OAuth is described by RFC-5849
EDIT: Facebook Uses OAuth 2.0 which is still in working draft.
In OAuth, and similar systems, the "access_token" is only part of the picture. There is also typically a secret key, which is known only by the service provider (facebook) and the client application (your app). The secret key is the only part that is expected to stay secret - and that part is never sent over the wire (after it's initial issuance).
In the case of Facebook, I think the secret key is assigned to you when you register your application to use their API, and the 'access_token' is returned to you for a given user, whenever the user agrees to allow your app to access their info.
Messages are sent in the clear, including the user's username, and the relevant "access_token"; However, each message must also include a valid signature in order to be accepted by the server. The signature is a cryptographically computed string, that is created using a technique called HMAC.
Computing the HMAC signature requires both the token and the secret, and includes other key parts of the message as well. Each signature is unique for the given message contents; and each message uses a nonce to ensure that no two messages can ever be exactly identical.
When the server receives a signed message, it starts by extracting the access_token (clear-text), and determining which app the token was issued for. It then retrieves the matching secret from its own local database (the secret is not contained in the message). Finally, the server uses the clear-text message, the clear-text access_token, and the secret to compute the expected HMAC signature for the message. If the computed signature matches the signature on the received message, then the message must have been sent by someone who knows the same secret (i.e. your application).
Have a look at Section 3.1 of RFC-5849 for an OAuth specific example, and further elaboration on the details.
Incidentally, the same approach is used by Amazon to control access to S3 and EC2, as well as most other service providers that offer API access with long-term authorization. Suffice it to say - this approach is secure. It might be a little counter-intuitive at first, but it makes sense once you think it through.
Adding a few links and quotes from Facebook Documentation:
Facebook is indeed using the HMAC-SHA256 algorithm. Registration document (PHP Example reading signed_request section).
Always verify the signed_request:
If you are unable to validate the
signed_request because you can't embed
your application secret (e.g. in
javascript or a desktop application)
then you MUST only use one piece of
information from the payload, the
oauth_token.
The Authentication Document contains a lot of useful info about the different flows you may use to authenticate a user. Also read the Security Considerations section at the bottom of the page:
Cross site request forgery is an
attack in which an trusted
(authenticated and authorized) user
unknowingly performs an action on
website. To prevent this attack, you
should pass an identifier in the state
parameter, and then validate the state
parameter matches on the response. We
strongly recommend that any app
implementing Facebook user login
implement CSRF protection using this
mechanism.
It was confirmed by Facebook that indeed there is one call in which the access_token is broadcast in the open - it just happens to be one call I use to make sure that the user is still logged in before saving to my application database. Their recommendation was to use the SSL option provided as of last month for canvas and Facebook as a whole. For the most part the Auth and Auth are secure.
To ensure a secure interface between a third party application and a Facebook application or even any website that uses Facebook Single Sign on, an identity question would provide the extra layer when used in conjunction with the access_token.
Either that or require your users to use Facebook with the new SSL feature of Facebook and Facebook Canvas Applications. If the access_token is broadcast in the open it cannot be used to uniquely identify anyone in your third party database when needing to have a confirmed identity before database interactions.

Categories