lundi 18 janvier 2021

RubyOnRails : read parameter from the database once

How does this happen in RubyOnRails:
The parameter is stored in the database.
The program needs to read it from the database once upon user login and save them in a global variable.
While the user is working, he uses only this variable, and the program does not read parameter again from the DB, only the next time the user logs on to the system.
How it is implemented.

Devise: Log user out after the confirmation period ends

I have a setup for logging the user out of the application once the confirmation period ends. Also, I need the user to be only logged out at night at 2 AM.

For this, I overrode the following -

def confirmation_period_valid?
  confirmation_time = confirmation_sent_at.in_time_zone(time_zone).end_of_day + 2.hours
  unconfirmed_access_time = self.class.allow_unconfirmed_access_for.ago.in_time_zone(time_zone)
  confirmation_sent_at && confirmation_time >= unconfirmed_access_time
end

But when I check after the expiration period, I am still able to use the application. The user is not logged out.

Checking the rails console shows that the confirmation period is not valid for the user so that works.

jeudi 14 janvier 2021

Rack::OAuth2::Client::Error in rails 3 app after authenticating user

In my rails project, I am using the oauth2 gem for authentication. In the configuration, I have configured authentication, token, and user info URL correctly. When the user is login and redirected back to the callback URL I am getting the following error and I am not able to figure out what's wrong in the code.

Rack::OAuth2::Client::Error
Invalid authentication method for accessing this endpoint.

rack-oauth2 (1.0.8) lib/rack/oauth2/client.rb:119:in `handle_error_response'
rack-oauth2 (1.0.8) lib/rack/oauth2/client.rb:96:in `handle_response'
rack-oauth2 (1.0.8) lib/rack/oauth2/client.rb:70:in `access_token!'
omniauth-openid-connect (0.1.0) lib/omniauth/strategies/openid_connect.rb:83:in `access_token'
omniauth-openid-connect (0.1.0) lib/omniauth/strategies/openid_connect.rb:68:in `callback_phase'
omniauth (1.2.2) lib/omniauth/strategy.rb:227:in `callback_call'
omniauth (1.2.2) lib/omniauth/strategy.rb:184:in `call!'
omniauth (1.2.2) lib/omniauth/strategy.rb:164:in `call'
omniauth (1.2.2) lib/omniauth/builder.rb:59:in `call'
meta_request (0.2.1) lib/meta_request/middlewares/app_request_handler.rb:11:in `call'
rack-contrib (1.2.0) lib/rack/contrib/response_headers.rb:17:in `call'
meta_request (0.2.1) lib/meta_request/middlewares/headers.rb:16:in `call'
meta_request (0.2.1) lib/meta_request/middlewares/meta_request_handler.rb:13:in `call'
rack-openid (1.3.1) lib/rack/openid.rb:98:in `call'
actionpack (3.2.21) lib/action_dispatch/middleware/best_standards_support.rb:17:in `call'
rack (1.4.7) lib/rack/etag.rb:23:in `call'
rack (1.4.7) lib/rack/conditionalget.rb:25:in `call'
actionpack (3.2.21) lib/action_dispatch/middleware/head.rb:14:in `call'
actionpack (3.2.21) lib/action_dispatch/middleware/params_parser.rb:21:in `call'
actionpack (3.2.21) lib/action_dispatch/middleware/flash.rb:242:in `call'
rack (1.4.7) lib/rack/session/abstract/id.rb:210:in `context'
rack (1.4.7) lib/rack/session/abstract/id.rb:205:in `call'
actionpack (3.2.21) lib/action_dispatch/middleware/cookies.rb:341:in `call'
activerecord (3.2.21) lib/active_record/query_cache.rb:64:in `call'
activerecord (3.2.21) lib/active_record/connection_adapters/abstract/connection_pool.rb:479:in `call'
actionpack (3.2.21) lib/action_dispatch/middleware/callbacks.rb:28:in `block in call'
activesupport (3.2.21) lib/active_support/callbacks.rb:405:in `_run__2379598500685443395__call__5000626384124356__callbacks'
activesupport (3.2.21) lib/active_support/callbacks.rb:405:in `__run_callback'
activesupport (3.2.21) lib/active_support/callbacks.rb:385:in `_run_call_callbacks'
activesupport (3.2.21) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (3.2.21) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (3.2.21) lib/action_dispatch/middleware/reloader.rb:65:in `call'
actionpack (3.2.21) lib/action_dispatch/middleware/remote_ip.rb:31:in `call'
actionpack (3.2.21) lib/action_dispatch/middleware/debug_exceptions.rb:16:in `call'
actionpack (3.2.21) lib/action_dispatch/middleware/show_exceptions.rb:56:in `call'
railties (3.2.21) lib/rails/rack/logger.rb:32:in `call_app'
railties (3.2.21) lib/rails/rack/logger.rb:16:in `block in call'
activesupport (3.2.21) lib/active_support/tagged_logging.rb:22:in `tagged'
railties (3.2.21) lib/rails/rack/logger.rb:16:in `call'
actionpack (3.2.21) lib/action_dispatch/middleware/request_id.rb:22:in `call'
rack (1.4.7) lib/rack/methodoverride.rb:21:in `call'
rack (1.4.7) lib/rack/runtime.rb:17:in `call'
rack (1.4.7) lib/rack/lock.rb:15:in `call'
actionpack (3.2.21) lib/action_dispatch/middleware/static.rb:83:in `call'
rack-cors (0.4.0) lib/rack/cors.rb:80:in `call'
utf8-cleaner (0.2.3) lib/utf8-cleaner/middleware.rb:21:in `call'
railties (3.2.21) lib/rails/engine.rb:484:in `call'
railties (3.2.21) lib/rails/application.rb:231:in `call'
railties (3.2.21) lib/rails/railtie/configurable.rb:30:in `method_missing'
utf8-cleaner (0.2.3) lib/utf8-cleaner/middleware.rb:21:in `call'
passenger (4.0.29) lib/phusion_passenger/rack/thread_handler_extension.rb:77:in `process_request'
passenger (4.0.29) lib/phusion_passenger/request_handler/thread_handler.rb:140:in `accept_and_process_next_request'
passenger (4.0.29) lib/phusion_passenger/request_handler/thread_handler.rb:108:in `main_loop'
passenger (4.0.29) lib/phusion_passenger/request_handler.rb:441:in `block (3 levels) in start_threads'
Request
Parameters:

{"code"=>"ND_VQvqLqGVCPXBxxvYf6gP43kg",
 "iss"=>"https://dev-sso.sungardas.com/am/oauth2/realms/root/realms/SungardAS",
 "client_id"=>"viewpoint"}

lundi 11 janvier 2021

How to update related attributes while saving objects in Rails Active Record

My current logic is, taking two inputs from form where one is primary book and one is secondary book and save it to database with is_primary check being true for primary book and false for secondary. We can have multiple secondary books.

I have a table books: Table name: books

  • book_id
  • book_name

And I have another table authors: Table name: authors

  • author_id
  • author_name

And I have relation table as below: Table name: author_book

  • author_book_id
  • book_id
  • author_id
  • is_primary (Bool)

Code for form:

  <div class="elem-set">
    <div class="elem-label">
      <label>Author</label>
    </div>

    <%= f.select :associated_author,
    Author.sort_by_name.map{|t| ["#{t.id}: #{t.name}", t.name]},
    { include_blank: "No Author" },
    { class: 'chzn-select', multiple: false }
    %><br /><br />
  </div>


  <div class="elem-set">
    <div class="elem-label">
      <label>Primary Book</label>
    </div>

    <%= f.select :associated_primary_book,
    Book.sort_by_name.map{|n| ["#{n.book_name}: #{n.book_id}", n.book_name]},
    { include_blank: "No Book" },
    { class: 'chzn-select', multiple: false }
    %><br /><br />
  </div>


  <div class="elem-set">
    <div class="elem-label">
      <label>Secondary Book</label>
    </div>

    <%= f.select :associated_secondary_book,
    Book.sort_by_name.map{|n| ["#{n.book_name}: #{n.book_id}", n.book_name]},
    { include_blank: "No Book" },
    { class: 'chzn-select', multiple: true }
    %><br /><br />
  </div>

MODEL Book class:

class Book < ActiveRecord::Base
    has_many :author_book, dependent: :destroy
    has_many :authors, through: :author_book

    scope :sort_by_name, -> { select([:book_id, :book_name]).order('book_name ASC') }
end

Authors class:

class Author < ActiveRecord::Base
  has_many :author_book, dependent: :destroy
  has_many :books, through: :author_book
  def associated_primary_books=(fetched_books)
    self.books.clear
    if !fetched_books.empty?
        self.books << Book.find_by_name(fetched_books)
        #is_primary=> true
    end
  end


  def associated_secondary_books=(fetched_books)
    fetched_books = fetched_books.delete_if { |x| x.empty? }
    if !fetched_books.empty?
      fetched_books.each do |book|
        self.books << Book.find_by_name(book)
      end
    end
  end
  
end

AuthorBook class:

class AuthorBook < ActiveRecord::Base
  belongs_to :authors
  belongs_to :books
  
end

Controller Author controller

AuthorController < ApplicationController
  def create
    @Author = Author.new(params[:author])

    respond_to do |format|
      begin
        retries ||= 0
        if @author.save
          flash[:notice] = 'Author created.'
          Event.init(user: current_user.id, type: "Create Author", details: {
            :author => @author.id
          })
          format.html { redirect_to action: 'show',
                :id => @author.id
          }
        end
      rescue ActiveRecord::StatementInvalid => e
        flash[:error] = 'error'
        redirect_to action: 'new'
      end
    end
  end

ISSUE: What I want to achieve is whenever I save book object, I also somehow save the is_primary as true or false which is present in author_book table.

  1. When I try to add book to author, it gets saved successfully. But I am unable to update the is_primary attribute resulting in all books as secondary. I get undefined method error if I try to update the is_primary attribute. How do I access the is_primary attribute which is present in relation table and solve this cleanly?
  2. If I try to update the author_book table after adding Book, it returns empty array because everything is saved after Author is created.
  3. This is stub code with example I created and not the complete code.

We are using Rails 3.

vendredi 8 janvier 2021

You have to wait 2 minutes to post. Multiple validates

The User has to wait 10 minutes to post again IF he:

  • PREVIOUSLY posted less then 2 minutes ago or
  • PREVIOUSLY posted a short comment (less then 20 characters) or
  • PREVIOUSLY posted the same text or
  • PREVIOUSLY posted a link.

in my models/comment.rb I added this

  scope :time_interval, -> { where('created_at > ?', 2.minutes.ago) }
  validate :check_probably_spam!

  def check_probably_spam!
    errors.add(
      :base, :check_probably_spam
    ) if (user.comments.time_interval.any?) || (...) ||  (...) ||  (...)
  end

I was able to made only the first task. Any tips to complete it?

The hardest part is to know how to have the previous comment.

Please, help me

mercredi 6 janvier 2021

How to remove header of the table in rails?

https://i.stack.imgur.com/19Xjt.png

I created this table in rails and when I press the delete all button it deletes all of the items in the table. But it leaves the header of the table. How can I delete it together with other items when pressing the delete all button?

vendredi 1 janvier 2021

When I start Metasploit comes up an error, how do I fix this?

I tried to start metasploit but then came this:

msfconsole WARN: Unresolved or ambiguous specs during Gem::Specification.reset: reline (>= 0) Available/installed versions of this gem: - 0.1.5 - 0.1.3 WARN: Clearing out unresolved specs. Try 'gem cleanup ' Please report a bug if this causes problems. Traceback (most recent call last): 13: from /usr/bin/msfconsole:23:in <main>' 12: from /usr/share/metasploit-framework/lib/metasploit/framework/command/base.rb:81:in start'

11: from /usr/share/metasploit-framework/lib/metasploit/framework/command/base.rb:55:in `require_environment!'

10: from /usr/share/metasploit-framework/lib/metasploit/framework/command/base.rb:55:in `require'

9: from /usr/share/metasploit-framework/config/application.rb:10:in `<top (required)>'

8: from /usr/share/rubygems-integration/all/gems/bundler-2.1.4/lib/bundler.rb:174:in `require'

7: from /usr/share/rubygems-integration/all/gems/bundler-2.1.4/lib/bundler/runtime.rb:58:in `require'

6: from /usr/share/rubygems-integration/all/gems/bundler-2.1.4/lib/bundler/runtime.rb:58:in `each'

5: from /usr/share/rubygems-integration/all/gems/bundler-2.1.4/lib/bundler/runtime.rb:69:in `block in require'

4: from /usr/share/rubygems-integration/all/gems/bundler-2.1.4/lib/bundler/runtime.rb:69:in `each'

3: from /usr/share/rubygems-integration/all/gems/bundler-2.1.4/lib/bundler/runtime.rb:74:in `block (2 levels) in require'

2: from /usr/share/rubygems-integration/all/gems/bundler-2.1.4/lib/bundler/runtime.rb:74:in `require'

1: from /usr/share/metasploit-framework/vendor/bundle/ruby/2.7.0/gems/sqlite3-1.3.13/lib/sqlite3.rb:4:in `<top (required)>'

/usr/share/metasploit-framework/vendor/bundle/ruby/2.7.0/gems/sqlite3-1.3.13/lib/sqlite3.rb:4:in `require': cannot load such file -- sqlite3/2.7/sqlite3_native (LoadError)

14: from /usr/bin/msfconsole:23:in `'

13: from /usr/share/metasploit-framework/lib/metasploit/framework/command/base.rb:81:in `start'

12: from /usr/share/metasploit-framework/lib/metasploit/framework/command/base.rb:55:in `require_environment!'

11: from /usr/share/metasploit-framework/lib/metasploit/framework/command/base.rb:55:in `require'

10: from /usr/share/metasploit-framework/config/application.rb:10:in `<top (required)>'

9: from /usr/share/rubygems-integration/all/gems/bundler-2.1.4/lib/bundler.rb:174:in `require'

8: from /usr/share/rubygems-integration/all/gems/bundler-2.1.4/lib/bundler/runtime.rb:58:in `require'

7: from /usr/share/rubygems-integration/all/gems/bundler-2.1.4/lib/bundler/runtime.rb:58:in `each'

6: from /usr/share/rubygems-integration/all/gems/bundler-2.1.4/lib/bundler/runtime.rb:69:in `block in require'

5: from /usr/share/rubygems-integration/all/gems/bundler-2.1.4/lib/bundler/runtime.rb:69:in `each'

4: from /usr/share/rubygems-integration/all/gems/bundler-2.1.4/lib/bundler/runtime.rb:74:in `block (2 levels) in require'

3: from /usr/share/rubygems-integration/all/gems/bundler-2.1.4/lib/bundler/runtime.rb:74:in `require'

2: from /usr/share/metasploit-framework/vendor/bundle/ruby/2.7.0/gems/sqlite3-1.3.13/lib/sqlite3.rb:2:in `<top (required)>'

1: from /usr/share/metasploit-framework/vendor/bundle/ruby/2.7.0/gems/sqlite3-1.3.13/lib/sqlite3.rb:6:in `rescue in <top (required)>'

/usr/share/metasploit-framework/vendor/bundle/ruby/2.7.0/gems/sqlite3-1.3.13

/lib/sqlite3.rb:6:in `require': /usr/share/metasploit-framework/vendor/bundle/ruby/2.7.0

/gems/sqlite3-1.3.13/lib/sqlite3/sqlite3_native.so: undefined symbol: sqlite3_column_database_name - /usr/share/metasploit-framework/vendor/bundle/ruby/2.7.0/gems/sqlite3-1.3.13/lib/sqlite3/sqlite3_native.so (LoadError)

I do not know if the error is related to the fact that I installed something with sqlite3.

my Os:

lsb_release -a

No LSB modules are available.

Distributor ID: Parrot

Description: Parrot GNU/Linux 4.10

Release: 4.10

Codename: n/a

my gems:

gem list

*** LOCAL GEMS ***

actioncable (6.0.3.4)

actionmailbox (6.0.3.4)

actionmailer (6.0.3.4)

actionpack (6.0.3.4)

actiontext (6.0.3.4)

actionview (6.0.3.4)

activejob (6.0.3.4)

activemodel (6.0.3.4)

activerecord (6.0.3.4)

activestorage (6.0.3.4)

activesupport (6.0.3.4)

addressable (2.7.0)

ansi (1.5.0)

async (1.26.2)

async-dns (1.2.5)

async-http (0.52.5)

async-io (1.30.1)

async-pool (0.3.2)

atomic (1.1.16)

bcrypt_pbkdf (1.0.1)

benchmark (default: 0.1.0)

bigdecimal (default: 2.0.0)

bindex (0.5.0)

bootsnap (1.4.6)

buftok (0.2.0)

builder (3.2.4)

bundler (2.2.3, 2.1.4, 1.17.3)

byebug (11.1.3)

capybara (3.12.0)

celluloid (0.16.0)

celluloid-io (0.16.2)

cgi (default: 0.1.0)

childprocess (4.0.0)

cms_scanner (0.12.1)

coffee-rails (5.0.0)

coffee-script (2.4.1)

coffee-script-source (1.12.2)

colorize (0.8.1)

columnize (0.9.0)

concurrent-ruby (1.1.6)

concurrent-ruby-edge (0.6.0)

connection_pool (2.2.2)

console (1.8.2)

crass (1.0.2)

csv (default: 3.1.2)

daemons (1.1.9)

date (default: 3.0.0)

dbm (default: 1.1.0)

delegate (default: 0.1.0)

did_you_mean (default: 1.4.0)

domain_name (0.5.20160216)

ed25519 (1.2.4)

em-websocket (0.5.1)

equalizer (0.0.11)

erubi (1.10.0, 1.9.0)

erubis (2.7.0)

espeak-ruby (1.0.4)

etc (default: 1.1.0)

ethon (0.9.0)

eventmachine (1.3.0.dev.1)

evil-winrm (2.3)

excon (0.72.0)

execjs (2.7.0)

fcntl (default: 1.0.0)

ffi (1.12.2)

ffi-compiler (1.0.1)

fiddle (default: 1.0.0)

fileutils (default: 1.4.1)

fog-core (2.1.0)

fog-json (1.2.0)

fog-libvirt (0.6.0)

fog-xml (0.1.1)

formatador (0.2.5)

forwardable (default: 1.3.1)

gdbm (default: 2.1.0)

get_process_mem (0.2.5)

getoptlong (default: 0.1.0)

globalid (0.3.6)

gssapi (1.3.1)

gyoku (1.3.1)

hashie (3.5.5)

hashie-forbidden_attributes (0.1.1)

hike (1.2.1)

hitimes (1.2.1)

http (4.4.1)

http-cookie (1.0.3)

http-form_data (2.2.0)

http-parser (1.2.1)

http_parser.rb (0.6.0)

httpclient (2.8.3)

i18n (1.8.5)

io-console (default: 0.5.6)

ipaddr (default: 1.2.2)

ipaddress (0.8.3)

irb (default: 1.2.6)

jbuilder (2.10.0)

json (default: 2.3.0)

listen (3.2.1)

little-plugger (1.1.4)

log4r (1.1.10)

logger (default: 1.4.2)

logging (2.3.0)

loofah (2.7.0)

mail (2.7.1)

marcel (0.3.2)

matrix (default: 0.2.0)

maxmind-db (1.0.0)

memoizable (0.4.2)

method_source (1.0.0)

mime (0.4.4)

mime-types (3.3.1)

mime-types-data (3.2015.1120)

mimemagic (0.3.5)

mini_exiftool (2.9.0)

mini_mime (1.0.2)

minitest (5.14.0, 5.13.0)

mojo_magick (0.5.6)

molinillo (0.6.4)

msfrpc-client (1.1.2)

msgpack (1.1.0)

multi_json (1.14.1)

multipart-post (2.0.0)

mustermann (1.1.1)

mutex_m (default: 0.1.0)

naught (1.1.0)

net-http-digest_auth (1.4)

net-http-persistent (3.1.0)

net-pop (default: 0.1.0)

net-scp (3.0.0)

net-sftp (2.1.2)

net-smtp (default: 0.1.0)

net-ssh (6.1.0)

net-telnet (0.1.1)

netrc (0.11.0)

nio4r (2.3.1)

nokogiri (1.10.9)

nori (2.6.0)

observer (default: 0.1.0)

oj (3.10.13)

open3 (default: 0.1.0)

openssl (default: 2.1.2)

opt_parse_validator (1.9.2)

ostruct (default: 0.2.0)

otr-activerecord (1.4.1)

parseconfig (1.0.8)

pkg-config (1.4.4)

power_assert (1.1.7)

prime (default: 0.1.1)

protocol-hpack (1.4.2)

protocol-http (0.20.0)

protocol-http1 (0.13.1)

protocol-http2 (0.14.0)

pstore (default: 0.1.0)

psych (default: 3.1.0)

public_suffix (4.0.3)

puma (4.3.6)

qr4r (0.4.1)

racc (default: 1.4.16)

rack (2.1.1)

rack-protection (2.0.8.1)

rack-proxy (0.6.1)

rack-test (0.7.0)

rails (6.0.3.4)

rails-deprecated_sanitizer (1.0.3)

rails-dom-testing (2.0.3)

rails-html-sanitizer (1.3.0)

railties (6.0.3.4)

rake (13.0.1)

rb-inotify (0.9.10)

rchardet (1.8.0)

rdoc (default: 6.2.1)

readline (default: 0.0.2)

readline-ext (default: 0.1.0)

regexp_parser (1.7.1)

reline (0.2.0, default: 0.1.5)

rest-client (2.0.2)

rexml (default: 3.2.3)

rqrcode_core (0.1.2)

rss (default: 0.2.8)

ruby (0.1.0)

ruby-libvirt (0.7.1)

ruby-progressbar (1.10.1)

ruby2_keywords (0.0.2)

rubydns (1.0.3)

rubyntlm (0.6.2)

rubyzip (2.0.0)

rushover (0.3.0)

sass (3.7.4)

sass-rails (6.0.0)

sassc (2.2.1)

sassc-rails (2.1.2)

sdbm (default: 1.0.0)

selenium-webdriver (3.142.7)

simple_oauth (0.3.1)

sinatra (2.0.8.1)

singleton (default: 0.1.0)

slack-notifier (1.5.1)

snmp (1.3.1)

spider (0.42)

spring (2.1.0)

spring-watcher-listen (2.0.1)

sprockets (3.7.2)

sprockets-rails (3.2.1)

stringio (default: 0.1.0)

strscan (default: 1.0.3)

sync (0.5.0)

term-ansicolor (1.3.0)

test-unit (3.3.5)

thin (1.7.2)

thor (0.20.3)

thread_safe (0.3.6)

tilt (2.0.10)

timeout (default: 0.1.0)

timers (4.1.1)

tins (1.1.0)

tracer (default: 0.1.0)

turbolinks (5.1.1)

turbolinks-source (5.1.0)

twitter (7.0.0)

typhoeus (1.4.0)

tzinfo (1.2.7, 1.2.6)

uglifier (2.7.2)

unf (0.1.4)

unf_ext (0.0.7.6)

uri (default: 0.10.0)

vagrant (2.2.9)

vagrant-libvirt (0.3.0)

vagrant_cloud (2.0.3)

web-console (4.0.1)

webpacker (4.2.2) webrick (default: 1.6.0)

websocket-driver (0.6.3)

websocket-extensions (0.1.5)

winrm (2.3.5)

winrm-fs (1.3.5)

wpscan (3.8.10)

xmlrpc (0.3.0)

xpath (3.2.0)

yajl-ruby (1.4.1)

yaml (default: 0.1.0)

zeitwerk (2.4.2, 2.3.0)

zlib (default: 1.1.0)

reline gem:

gem list reline

*** LOCAL GEMS ***

reline (0.2.0, default: 0.1.5)

i tried also: gem cleanup reline

gem install reline

reinstalling metasploit

search in the web and try the commands that I though that they resolve my problem.

I want that metasploit works and pls be not toxic I am a noob in metasploit