@@ -14,23 +14,25 @@ class AuthenticationApi < Grape::API
1414 helpers AuthorisationHelpers
1515
1616 #
17- # Sign in - only mounted if AAF and SAML auth is NOT used
17+ # Sign in - only mounted if AAF and SAML auth is NOT used (database auth)
1818 #
1919 if !AuthenticationHelpers . aaf_auth? && !AuthenticationHelpers . saml_auth?
2020 desc 'Sign in'
2121 params do
2222 requires :username , type : String , desc : 'User username'
23- requires :password , type : String , desc : 'User\'s password'
23+ optional :password , type : String , desc : 'User\'s password'
24+ optional :auth_token , type : String , desc : 'User\'s auth token'
2425 optional :remember , type : Boolean , desc : 'User has requested to remember login' , default : false
2526 end
2627 post '/auth' do
2728 username = params [ :username ]
2829 password = params [ :password ]
30+ auth_token = params [ :auth_token ]
2931 remember = params [ :remember ]
3032 logger . info "Authenticate #{ username } from #{ request . ip } "
3133
3234 # No provided credentials
33- if username . nil? || password . nil?
35+ if username . nil? || ( password . nil? && auth_token . nil? )
3436 error! ( { error : 'The request must contain the user username and password.' } , 400 )
3537 end
3638
@@ -46,10 +48,13 @@ class AuthenticationApi < Grape::API
4648 new_user . login_id = username
4749 end
4850
49- # Try to authenticate
50- unless user . authenticate? ( password )
51+ # Try to authenticate with password
52+ if password . present? && ! user . authenticate? ( password )
5153 error! ( { error : 'Invalid email or password.' } , 401 )
5254 return
55+ elsif auth_token . present? && !authenticated? ( :login )
56+ error! ( { error : 'Invalid user or auth token.' } , 401 )
57+ return
5358 end
5459
5560 # Create user if they are a new record
@@ -66,9 +71,15 @@ class AuthenticationApi < Grape::API
6671
6772 logger . info "Login #{ username } from #{ request . ip } "
6873
74+ user = User . find_by ( username : params [ :username ] )
75+ token = user &.token_for_text? ( params [ :auth_token ] , :login )
76+
77+ token &.destroy!
78+ token = user . generate_authentication_token!
79+
6980 # Return user details
7081 present :user , user , with : Entities ::UserEntity
71- present :auth_token , user . generate_authentication_token! . authentication_token
82+ present :auth_token , token . authentication_token
7283 add_refresh_cookie_to_response ( remember )
7384 end
7485 end
0 commit comments