7aef87db |
require 'ipaddr'
class Token
include Mongoid::Document
include Mongoid::Timestamps::Created
index({ token: 1 }) |
e6634eb6 |
belongs_to :user |
7aef87db |
field :token, type: String
field :date_expiring, type: DateTime
field :ip, type: Integer |
1b9ff864 |
field :user_id, type: Moped::BSON::ObjectId |
7aef87db |
|
1b9ff864 |
##
# @return [Token] |
7aef87db |
def self.authenticate(session) |
1b9ff864 |
if session[:created_at].nil? |
7aef87db |
return false
end
matched_token = self.where(:token => session[:token],
:ip => IPAddr.new(session[:ip]).to_i,
:date_expiring.lte => session[:created_at] + Rails.application.config.max_lifetime,
:created_at => session[:created_at]
).limit(1).first
matched_token
end
|
1b9ff864 |
def save_session(session, user)
self.token = session[:token]
self.date_expiring = session[:created_at] + Rails.application.config.max_lifetime
self.created_at = session[:created_at]
self.ip = IPAddr.new(session[:ip]).to_i
self.user_id = user |
7aef87db |
|
1b9ff864 |
save
end |
7aef87db |
|
1b9ff864 |
#def self.remove_old_session(token)
# self.delete_all(token: token)
#end |
7aef87db |
|
e6634eb6 |
def self.fing_user_accounts_having_to_token(token)
where(token: token).first().user.jids
end
|
81ab4d54 |
def self.remove_token(token)
where(token: token).delete
end
|
e6634eb6 |
private |
7aef87db |
def self.generate_token
begin
token = SecureRandom.urlsafe_base64
end while Token.where(token: token).exists?
token
end
end |