<commit_before>require 'kontena/client'
require_relative '../common'

module Kontena::Cli::Server
  class User
    include Kontena::Cli::Common

    def login(api_url = nil)
      until !api_url.nil? && !api_url.empty?
        api_url = ask('Kontena Master Node URL: ')
      end
      update_api_url(api_url)

      unless request_server_info
        print color('Could not connect to server', :red)
        return false
      end

      email = ask("Email: ")
      password = password("Password: ")
      response = do_login(email, password)

      if response
        update_access_token(response['access_token'])
        display_logo
        puts "Welcome #{response['user']['name'].green}"
        puts ''
        reset_client
        grid = client(require_token).get('grids')['grids'][0]
        if grid
          self.current_grid = grid
          puts "Using grid: #{grid['name'].cyan}"
        else
          clear_current_grid
        end
        true
      else
        print color('Login Failed', :red)
        false
      end
    end

    def logout
      settings['server'].delete('token')
      save_settings
    end

    def whoami
      require_api_url
      puts "Server: #{settings['server']['url']}"
      token = require_token
      response = client(token).get('user')
      puts "User: #{response['email']}"
    end

    def invite(email)
      require_api_url
      token = require_token
      data = { email: email }
      response = client(token).post('users', data)
      puts 'User invited' if response
    end

    def register(api_url = nil, options)
      auth_api_url = api_url || 'https://auth.kontena.io'
      if !auth_api_url.start_with?('http://') && !auth_api_url.start_with?('https://')
        auth_api_url = "https://#{auth_api_url}"
      end
      email = ask("Email: ")
      password = password("Password: ")
      password2 = password("<PASSWORD>: ")
      if password != <PASSWORD>
        raise ArgumentError.new("Passwords don't match")
      end
      params = {email: email, password: password}
      auth_client = Kontena::Client.new(auth_api_url)
      auth_client.post('users', params)
    end

    def verify_account(token)
      require_api_url

      params = {token: token}
      client.post('user/email_confirm', params)
      print color('Account verified', :green)
    end

    def request_password_reset(email)
      require_api_url

      params = {email: email}
      client.post('user/password_reset', params)
      puts 'Email with password reset instructions is sent to your email address. Please follow the instructions to change your password.'
    end

    def reset_password(token)
      require_api_url
      password = password("Password: ")
      password2 = password("<PASSWORD>: ")
      if password != <PASSWORD>
        raise ArgumentError.new("Passwords don't match")
      end
      params = {token: token, password: password}
      client.put('user/password_reset', params)
      puts 'Password is now changed. To login with the new password, please run: kontena login'
    end

    def add_registry
      default_url = 'https://index.docker.io/v1/'
      require_api_url
      username = ask("Username: ")
      password = password("Password: ")
      email = ask("Email: ")
      url = ask("URL [#{default_url}]: ")
      url = default_url if url.strip == ''
      data = { username: username, password: password, email: email, url: url }
      client(token).post("user/registries", data)
    end

    private

    def token
      @token ||= require_token
    end

    def do_login(email, password)
      params = {
          username: email,
          password: password,
          grant_type: 'password',
          scope: 'user'
      }
      client.post('auth', params)
    end

    def request_server_info
      valid = true
      begin
        client.get('ping') # test server connection
      rescue
        valid = false
      end
      valid
    end

    ##<commit_msg>show ssl error and possible workaround for user<commit_after>require 'kontena/client'
require_relative '../common'

module Kontena::Cli::Server
  class User
    include Kontena::Cli::Common

    def login(api_url = nil)
      until !api_url.nil? && !api_url.empty?
        api_url = ask('Kontena Master Node URL: ')
      end
      update_api_url(api_url)

      unless request_server_info
        print color('Could not connect to server', :red)
        return false
      end

      email = ask("Email: ")
      password = password("Password: ")
      response = do_login(email, password)

      if response
        update_access_token(response['access_token'])
        display_logo
        puts "Welcome #{response['user']['name'].green}"
        puts ''
        reset_client
        grid = client(require_token).get('grids')['grids'][0]
        if grid
          self.current_grid = grid
          puts "Using grid: #{grid['name'].cyan}"
        else
          clear_current_grid
        end
        true
      else
        print color('Login Failed', :red)
        false
      end
    end

    def logout
      settings['server'].delete('token')
      save_settings
    end

    def whoami
      require_api_url
      puts "Server: #{settings['server']['url']}"
      token = require_token
      response = client(token).get('user')
      puts "User: #{response['email']}"
    end

    def invite(email)
      require_api_url
      token = require_token
      data = { email: email }
      response = client(token).post('users', data)
      puts 'User invited' if response
    end

    def register(api_url = nil, options)
      auth_api_url = api_url || 'https://auth.kontena.io'
      if !auth_api_url.start_with?('http://') && !auth_api_url.start_with?('https://')
        auth_api_url = "https://#{auth_api_url}"
      end
      email = ask("Email: ")
      password = password("Password: ")
      password2 = password("<PASSWORD>: ")
      if password != password2
        raise ArgumentError.new("Passwords don't match")
      end
      params = {email: email, password: password}
      auth_client = Kontena::Client.new(auth_api_url)
      auth_client.post('users', params)
    end

    def verify_account(token)
      require_api_url

      params = {token: token}
      client.post('user/email_confirm', params)
      print color('Account verified', :green)
    end

    def request_password_reset(email)
      require_api_url

      params = {email: email}
      client.post('user/password_reset', params)
      puts 'Email with password reset instructions is sent to your email address. Please follow the instructions to change your password.'
    end

    def reset_password(token)
      require_api_url
      password = password("Password: ")
      password2 = password("<PASSWORD>: ")
      if password != password2
        raise ArgumentError.new("Passwords don't match")
      end
      params = {token: token, password: password}
      client.put('user/password_reset', params)
      puts 'Password is now changed. To login with the new password, please run: kontena login'
    end

    def add_registry
      default_url = 'https://index.docker.io/v1/'
      require_api_url
      username = ask("Username: ")
      password = password("Password: ")
      email = ask("Email: ")
      url = ask("URL [#{default_url}]: ")
      url = default_url if url.strip == ''
      data = { username: username, password: password, email: email, url: url }
      client(token).post("user/registries", data)
    end

    private

    def token
      @token ||= require_token
    end

    def do_login(email, password)
      params = {
          username: email,
          password: password,
          grant_type: 'password',
          scope: 'user'
      }
      client.post('auth', params)
    end

    def request_server_info
      valid = true
      begin
        client.get('ping') # test server connection
      rescue OpenSSL::SSL::SSLError => _
        raise 'Could not connect to server because of SSL problem. If you want to ignore SSL errors, set SSL_IGNORE_ERRORS=true environment variable'
      rescue => exc
        valid = false
      end
      valid
    end

    ##
{'fraction_non_alphanumeric': 0.11126104023552502, 'fraction_numerical': 0.0012266928361138372, 'mean_word_length': 2.4055973266499584, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 11, 'https://': 8, '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': '3378558', 'n_tokens_mistral': 2350, 'n_tokens_neox': 2323, 'n_words': 615}