diff options
author | Rémy Coutable <remy@gitlab.com> | 2016-09-19 13:04:04 +0000 |
---|---|---|
committer | Rémy Coutable <remy@gitlab.com> | 2016-09-19 13:04:04 +0000 |
commit | ba7c36852528822daeb3b40e9519251c80ef120f (patch) | |
tree | 09ab887b8630e235b9c56303ceb4828d386312f0 /lib/api/helpers.rb | |
parent | 187dd50f881ffced6484ba1b7ffd419b92cda151 (diff) | |
parent | 5d1b616eba6040a9a51fee057c2edc24a5be9832 (diff) | |
download | gitlab-ce-ba7c36852528822daeb3b40e9519251c80ef120f.tar.gz |
Merge branch '18302-use-rails-cookie-in-api' into 'master'
Allow the Rails cookie to be used for API authentication
Makes the Rails cookie into a valid authentication token for the Grape
API, and uses it instead of token authentication in frontend code that
uses the API.
Rendering the private token into client-side javascript is a security
risk; it may be stolen through XSS or other attacks. In general,
re-using API code in the frontend is more desirable than implementing
endless actions that return JSON.
Closes #18302
See merge request !1995
Diffstat (limited to 'lib/api/helpers.rb')
-rw-r--r-- | lib/api/helpers.rb | 23 |
1 files changed, 20 insertions, 3 deletions
diff --git a/lib/api/helpers.rb b/lib/api/helpers.rb index 150875ed4f0..714d4ea3dc6 100644 --- a/lib/api/helpers.rb +++ b/lib/api/helpers.rb @@ -12,13 +12,30 @@ module API nil end + def private_token + params[PRIVATE_TOKEN_PARAM] || env[PRIVATE_TOKEN_HEADER] + end + + def warden + env['warden'] + end + + # Check the Rails session for valid authentication details + def find_user_from_warden + warden ? warden.authenticate : nil + end + def find_user_by_private_token - token_string = (params[PRIVATE_TOKEN_PARAM] || env[PRIVATE_TOKEN_HEADER]).to_s - User.find_by_authentication_token(token_string) || User.find_by_personal_access_token(token_string) + token = private_token + return nil unless token.present? + + User.find_by_authentication_token(token) || User.find_by_personal_access_token(token) end def current_user - @current_user ||= (find_user_by_private_token || doorkeeper_guard) + @current_user ||= find_user_by_private_token + @current_user ||= doorkeeper_guard + @current_user ||= find_user_from_warden unless @current_user && Gitlab::UserAccess.new(@current_user).allowed? return nil |