0

I'm using couchbase lite in an android app with sync gateway and couchbase in the server and it works great.

Now I want to make a request to node.js from the android app, and i want to use the same session to authorize the user in the node.js app.

Is it possible? Can I read the session in node and match it with couchbase?

Yaron Yosef
  • 447
  • 3
  • 12

2 Answers2

1

You're asking about authorization. This is tied to authentication, but you should make sure to distinguish between the two. It sounds like what you want is to authenticate the user, then have your node piece authorize access based on this.

Having said that, it depends some on the type of authentication you're using to establish your session.

Basic Authentication (when used directly with Sync Gateway) just passes a username and password that gets checked. I think you get a session cookie after that. It would be very difficult to use that to authenticate to anything else (as in, you'd have to modify Sync Gateway code yourself).

OpenID Connect, in the auth flow, might give a few ways to possibly do this.

One simpler way is to have the node app also authenticate the user. If this flowed through the same browser, the browser will often keep state that would allow the user to bypass re-authenticating. This could be a little clunky, because you'd have to pass things off to a browser (or a webview, but there are security issues with that, too), which the user would likely notice.

Another approach would be to be to do a sort of double redirect. (I think this would work, but I haven't tried it. I can't find documentation on whether an authorization code can be used twice.) In the authorization flow, have the redirect go to the node app. Then have the node app redirect again to Sync Gateway. Both apps can ask for the ID token.

Yet another way would be to have your Android app ask for the ID token directly and pass this in some protected way to your node app. As always, you'd have to protect against replay attacks, and I'm not sure what else, so this could be challenging.

In any case, Sync Gateway is built to request the ID token itself, so any approach will need to ask for the ID token twice.

Here are some references you can look at to investigate this further yourself.

http://connect2id.com/learn/openid-connect - A nice write-up of the OpenID Connect protocol. https://developers.google.com/identity/protocols/CrossClientAuth - Google Identity Provider documentation that addresses sharing authorization between a mobile app and a web app. http://www.thread-safe.com/2012/01/problem-with-oauth-for-authentication.html - A post describing the distinction between authentication and authorization, and why OpenID Connect (not OAuth) should be used when needing authentication.

Hod
  • 2,236
  • 1
  • 14
  • 22
0

Note: You can't use the Couchbase Node.js SDK on the bucket that is used by Sync Gateway otherwise it will mess up with the _sync metadata and documents won't sync properly; but you can query documents, create sessions, etc. using the Sync Gateway REST API.

You can refer to the documentation of the Sync Gateway REST API to get the list of available endpoints. And if you don't want to roll out your own HTTP wrapper, a JS library is available that runs on Node.js and in the browser: http://developer.couchbase.com/documentation/mobile/1.3/develop/guides/sync-gateway/rest-api-client/index.html.

jamiltz
  • 1,144
  • 8
  • 15
  • thanks for the answer, but thats not what i'm asking. i want to connect from my android device to node.js, let's say just to print "hello world". but i want only people that have a sync gateway valid session to print that. that way, i can reuse SG session to allow access to other components (elasticsearch for instance), without creating another session. hope it's more clear. – Yaron Yosef Aug 22 '16 at 17:48