<commit_before># frozen_string_literal: true
require 'base64'
require 'rack'
require 'openssl'

module DiscourseApi
  class SingleSignOn
    ACCESSORS = [:nonce, :name, :username, :email, :avatar_url, :avatar_force_update, :require_activation,
                 :bio, :external_id, :return_sso_url, :admin, :moderator, :suppress_welcome_message, :title,
                 :add_groups, :remove_groups, :groups]
    FIXNUMS = []
    BOOLS = [:avatar_force_update, :admin, :moderator, :require_activation, :suppress_welcome_message]
    ARRAYS = [:groups]
    #NONCE_EXPIRY_TIME = 10.minutes # minutes is a rails method and is causing an error. Is this needed in the api?

    attr_accessor(*ACCESSORS)
    attr_writer :sso_secret, :sso_url

    def self.sso_secret
      raise RuntimeError, "sso_secret not implemented on class, be sure to set it on instance"
    end

    def self.sso_url
      raise RuntimeError, "sso_url not implemented on class, be sure to set it on instance"
    end

    def self.parse(payload, sso_secret = nil)
      sso = new
      sso.sso_secret = sso_secret if sso_secret

      parsed = Rack::Utils.parse_query(payload)
      if sso.sign(parsed["sso"]) != parsed["sig"]
        diags = "\n\nsso: #{parsed["sso"]}\n\nsig: #{parsed["sig"]}\n\nexpected sig: #{sso.sign(parsed["sso"])}"
        if parsed["sso"] =~ /[^a-zA-Z0-9=\r\n\/+]/m
          raise RuntimeError, "The SSO field should be Base64 encoded, using only A-Z, a-z, 0-9, +, /, and = characters. Your input contains characters we don't understand as Base64, see http://en.wikipedia.org/wiki/Base64 #{diags}"
        else
          raise RuntimeError, "Bad signature for payload #{diags}"
        end
      end<commit_msg>Allow to set locale in SingleSignOn (#186)<commit_after># frozen_string_literal: true
require 'base64'
require 'rack'
require 'openssl'

module DiscourseApi
  class SingleSignOn
    ACCESSORS = [:nonce, :name, :username, :email, :avatar_url, :avatar_force_update, :require_activation,
                 :bio, :external_id, :return_sso_url, :admin, :moderator, :suppress_welcome_message, :title,
                 :add_groups, :remove_groups, :groups, :locale, :locale_force_update]
    FIXNUMS = []
    BOOLS = [:avatar_force_update, :admin, :moderator, :require_activation, :suppress_welcome_message,
      :locale_force_update]
    ARRAYS = [:groups]
    #NONCE_EXPIRY_TIME = 10.minutes # minutes is a rails method and is causing an error. Is this needed in the api?

    attr_accessor(*ACCESSORS)
    attr_writer :sso_secret, :sso_url

    def self.sso_secret
      raise RuntimeError, "sso_secret not implemented on class, be sure to set it on instance"
    end

    def self.sso_url
      raise RuntimeError, "sso_url not implemented on class, be sure to set it on instance"
    end

    def self.parse(payload, sso_secret = nil)
      sso = new
      sso.sso_secret = sso_secret if sso_secret

      parsed = Rack::Utils.parse_query(payload)
      if sso.sign(parsed["sso"]) != parsed["sig"]
        diags = "\n\nsso: #{parsed["sso"]}\n\nsig: #{parsed["sig"]}\n\nexpected sig: #{sso.sign(parsed["sso"])}"
        if parsed["sso"] =~ /[^a-zA-Z0-9=\r\n\/+]/m
          raise RuntimeError, "The SSO field should be Base64 encoded, using only A-Z, a-z, 0-9, +, /, and = characters. Your input contains characters we don't understand as Base64, see http://en.wikipedia.org/wiki/Base64 #{diags}"
        else
          raise RuntimeError, "Bad signature for payload #{diags}"
        end
      end
{'fraction_non_alphanumeric': 0.11964591661907481, 'fraction_numerical': 0.00885208452312964, 'mean_word_length': 3.3678304239401498, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '7131714', 'n_tokens_mistral': 1205, 'n_tokens_neox': 1160, 'n_words': 324}