|
1 | 1 | # frozen_string_literal: true |
2 | 2 |
|
| 3 | +require 'rest-client' |
| 4 | + |
3 | 5 | class User < ApplicationRecord |
4 | 6 | include Comparable |
5 | 7 | include Gravtastic |
@@ -151,20 +153,27 @@ def self.authenticate(login, submitted_password) |
151 | 153 | end |
152 | 154 |
|
153 | 155 | 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) |
| 156 | + auth_url = SiteSetting.value('courses_mooc_fi_auth_url') |
| 157 | + response = RestClient.post( |
| 158 | + auth_url, |
| 159 | + { email: email, password: submitted_password }.to_json, |
| 160 | + { content_type: :json, accept: :json } |
| 161 | + ) |
162 | 162 |
|
163 | 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 |
| 164 | + unless data["authenticated"] == true |
| 165 | + raise "Authentication via courses.mooc.fi failed for #{email}" |
| 166 | + end |
| 167 | + |
| 168 | + true |
| 169 | + rescue RestClient::Unauthorized, RestClient::Forbidden |
| 170 | + raise "Authentication rejected by courses.mooc.fi for #{email}" |
| 171 | + rescue RestClient::ExceptionWithResponse => e |
| 172 | + Rails.logger.error("Authentication via courses.mooc.fi error: #{e.response}") |
| 173 | + raise "Authentication via courses.mooc.fi failed: #{e.message}" |
| 174 | + rescue => e |
| 175 | + Rails.logger.error("Unexpected error during authentication via courses.mooc.fi: #{e.message}") |
| 176 | + raise "Unexpected error while authenticating via courses.mooc.fi: #{e.message}" |
168 | 177 | end |
169 | 178 |
|
170 | 179 | def password_reset_key |
|
0 commit comments