<commit_before># name: discourse-meteor
# about: Meteor's OAuth Plugin
# version: 0.1
# authors: <NAME>

require_dependency 'auth/oauth2_authenticator.rb'

class MeteorAuthenticator < ::Auth::OAuth2Authenticator
  def register_middleware(omniauth)
    omniauth.provider :oauth2,
                      :name => 'meteor',
                      :client_id => GlobalSetting.meteor_client_id,
                      :client_secret => GlobalSetting.meteor_client_secret,
                      :provider_ignores_state => true,
                      :client_options => {
                        :site => 'https://www.meteor.com',
                        :authorize_url => '/oauth2/authorize',
                        :token_url => '/oauth2/token'
                      }
  end<commit_msg>Retrieve email/username from Meteor<commit_after># name: discourse-meteor
# about: Meteor's OAuth Plugin
# version: 0.1
# authors: <NAME>

require_dependency 'auth/oauth2_authenticator.rb'

class MeteorAuthenticator < ::Auth::OAuth2Authenticator
  def register_middleware(omniauth)
    omniauth.provider :oauth2,
                      :name => 'meteor',
                      :client_id => GlobalSetting.meteor_client_id,
                      :client_secret => GlobalSetting.meteor_client_secret,
                      :provider_ignores_state => true,
                      :client_options => {
                        :site => 'https://www.meteor.com',
                        :authorize_url => '/oauth2/authorize',
                        :token_url => '/oauth2/token'
                      }
  end

  def after_authenticate(auth)
    result = Auth::Result.new
    token = URI.escape(auth['credentials']['token'])
    token.gsub!(/\+/, '%2B')

    user = JSON.parse(open("https://www.meteor.com/api/v1/identity", { "Authorization" => "Bearer #{token}" }).read)

    result.username = user['username']
    if user['emails'].present?
      email = user['emails'].find {|e| e['primary'] }
      if email.present?
        result.email = email['address']
        result.email_valid = email['verified']
      end
    end

    current_info = ::PluginStore.get("meteor", "meteor_user_#{user['id']}")
    if current_info
      result.user = User.where(id: current_info[:user_id]).first
    end
    result.extra_data = { meteor_user_id: user['id'] }
    result
  end

  def after_create_account(user, auth)
    ::PluginStore.set("meteor", "meteor_user_#{auth[:extra_data][:meteor_user_id]}", {user_id: user.id })
  end
{'fraction_non_alphanumeric': 0.13092369477911647, 'fraction_numerical': 0.00642570281124498, 'mean_word_length': 2.2690288713910762, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 22, 'https://': 3, 'lorem ipsum': 0, 'www.': 3, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '587393', 'n_tokens_mistral': 796, 'n_tokens_neox': 735, 'n_words': 126}