about summary refs log tree commit diff
path: root/app/controllers/matrix/identity/v1/check_credentials_controller.rb
blob: 1770c6767b7b0f9d39bce2bd6ccb402b6d6aeedf (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# frozen_string_literal: true

class Matrix::Identity::V1::CheckCredentialsController < Matrix::BaseController
  def create
    matrix_profile = matrix_profile_json
    return render json: fail_json if matrix_profile.blank?

    render json: matrix_profile
  rescue ActionController::ParameterMissing, ActiveRecord::RecordNotFound
    render json: fail_json
  end

  private

  def resource_params
    params.require(:user).permit(:id, :password)
  end

  def matrix_domains
    ENV.fetch('MATRIX_AUTH_DOMAINS', '').delete(',').split.to_set
  end

  def matrix_profile_json
    user_params = resource_params
    return unless user_params[:id].present? && user_params[:password].present? && user_params[:id][0] == '@'

    (username, domain) = user_params[:id].downcase.split(':', 2)
    return unless matrix_domains.include?(domain)

    user = User.find_by_lower_username!(username[1..-1])
    return unless user.valid_password?(user_params[:password])

    {
      auth: {
        success: true,
        mxid: "@#{username}:#{domain}",
        profile: {
          display_name: user.account.display_name.presence || user.username,
          three_pids: [
            {
              medium: 'email',
              address: user.email,
            },
          ]
        }
      }
    }
  end

  def fail_json
    { auth: { success: false } }
  end
end