I am currently experimenting with OAuth2 to develop a mobile application built entirely in JavaScript that talks to a CakePHP API. Take a look at the following code to see h
It looks like you're using the Resource Owner Password Credentials OAuth 2.0 flow e.g. submitting username/pass to get back both an access token and refresh token.
With that background in mind, let me address your questions:
http://domain.com/api/oauth/token
, and receives both the access token and refresh token.Admittedly, this does violate the "JS-Only" constraint you were looking for. However, a) again you really should NOT have a refresh token in javascript and b) it requires pretty minimal server-side logic at login/logout and no persistent server-side storage.
Note on CSRF: As noted in the comments, this solution doesn't address Cross-site Request Forgery; see the OWASP CSRF Prevention Cheat Sheet for further ideas on addressing these forms of attacks.
Another alternative is simply to not request the refresh token at all (not sure if that's an option with the OAuth 2 implementation you're dealing with; the refresh token is optional per the spec) and continually re-authenticate when it expires.
Hope that helps!
The only way to be fully secure is to not store the access tokens client side. Anyone with (physical)access to your browser could obtain your token.
1) Your assessment of neither being a great solution is accurate.
2) Using expiration times would be your best if you are limited to only client side development. It wouldn't require your users to re-authenticate with Oauth as frequently, and guarantee that the token wouldn't live forever. Still not the most secure.
3) Getting a new token would require performing the Oauth workflow to obtain a fresh token. The client_id is tied to a specific domain for Oauth to function.
The most secure method for retaining Oauth tokens would be a server side implementation.
For pure client side only approach, if you have a chance, try to use "Implicit Flow" rather then "Resource owner flow". You do not receive refresh token as a part of the response.
In the above approach the access token should be long living (e.g. 1 year). If there is a concern with long living token you can use following trick.