Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google API: Authorized JavaScript Origins

I'm implementing a Google+ Sign-In for our web service, and stumbled on "Authorized JavaScript Origins". Our clients have web addresses either as a sub-domain of our main domain, or as a custom domain name. Since the login page is under that sub-domain (or custom domain), and in order to make the Google+ Sing-In button work, that custom domain/sub-domain should be (manually) entered in the "Authorized JavaScript Origins" list (with both http and https).

Does anybody know a way to do that automatically (through some API maybe)? If not, then how do you do it?

like image 649
Ivaylo Avatar asked Jun 11 '14 14:06

Ivaylo


3 Answers

Not sure if there is an API for this. At first glance I don't see one. The alternative (aside from manually adding domains all the time) is to use a hidden iframe on each site - this iframe would come from your domain and would be the only thing that calls google services. The main sites would communicate with the iframe (postMessage) to tell it what to send google. This of course, opens up a security risk (anybody could load your iframe into their page and do bad things on your behalf) so you'll want to make sure that the iframe code refuses to do anything unless it's running within a page on a known-good domain.

like image 57
Robert Levy Avatar answered Nov 11 '22 21:11

Robert Levy


You can also have a common URL which all subdomains point to when trying to log in with Google. Then have this URL redirect to your actual Google login path. Beats having to deal with an iframe this way.

like image 20
Swaathi Kakarla Avatar answered Nov 11 '22 22:11

Swaathi Kakarla


Finally I made it to work, however there may be some fixes to apply.

So a server is host for many domain and subdomains (childs) which all of them needs google sign-in and there is a main domain (parent).

I implemented a general login page on parent which childs open this page via window.open() as popup. As client is in a popup, it is very likely that auth2 cannot open another popup, so the parent will do the google auth with {ux_mode: 'redirect'} parameter as gapi.auth2.SignInOptions.

Process will continue to your callback page which you provided as another gapi.auth2.SignInOptions parameter which is redirect_uri and is on parent.

On this page google may have provided you the golden id_token which you must authenticate this token on your server. And this was the main twist which you should use this information to create a token on your server which parent asked server to create, but send it to child on client side (for example via query parameter) to use it for later usage.

I will happily take any advice for security leaks or any comment which may ease the process just a little.

like image 43
Rafe Avatar answered Nov 11 '22 20:11

Rafe