File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -146,9 +146,27 @@ def self.authenticate(login, submitted_password)
146146 user = find_by ( login : login )
147147 user ||= find_by ( 'lower(email) = ?' , login . downcase )
148148 return nil if user . nil?
149+ user if user . password_managed_by_courses_mooc_fi && authenticate_via_courses_mooc_fi ( user . email , submitted_password )
149150 user if user . has_password? ( submitted_password )
150151 end
151152
153+ def authenticate_via_courses_mooc_fi ( email , submitted_password )
154+ uri = URI . parse ( 'https://courses.mooc.fi/api/v0/tmc-server/auth' )
155+ http = Net ::HTTP . new ( uri . host , uri . port )
156+ http . use_ssl = ( uri . scheme == 'https' )
157+ request = Net ::HTTP ::Post . new ( uri . path , { 'Content-Type' => 'application/json' } )
158+ request . body = { email : email , password : submitted_password } . to_json
159+
160+ response = http . request ( request )
161+ return false unless response . is_a? ( Net ::HTTPSuccess )
162+
163+ data = JSON . parse ( response . body )
164+ data [ 'authenticated' ] == true
165+ rescue StandardError => e
166+ Rails . logger . error ( "MOOC.fi authentication failed: #{ e } " )
167+ false
168+ end
169+
152170 def password_reset_key
153171 action_tokens . find { |t | t . action == 'reset_password' }
154172 end
You can’t perform that action at this time.
0 commit comments