Module: DeviseTokenAuth::Concerns::User
- Extended by:
- ActiveSupport::Concern
- Defined in:
- app/models/devise_token_auth/concerns/user.rb
Instance Method Summary collapse
- #build_auth_header(token, client_id = 'default') ⇒ Object
- #build_auth_url(base_url, args) ⇒ Object
-
#create_new_auth_token(client_id = nil) ⇒ Object
update user's auth token (should happen on each request).
- #extend_batch_buffer(token, client_id) ⇒ Object
-
#token_can_be_reused?(token, client_id) ⇒ Boolean
allow batch requests to use the previous token.
- #token_is_current?(token, client_id) ⇒ Boolean
- #valid_token?(token, client_id = 'default') ⇒ Boolean
Instance Method Details
#build_auth_header(token, client_id = 'default') ⇒ Object
103 104 105 106 107 108 109 110 |
# File 'app/models/devise_token_auth/concerns/user.rb', line 103 def build_auth_header(token, client_id='default') client_id ||= 'default' # client may use expiry to prevent validation request if expired expiry = self.tokens[client_id]['expiry'] return "token=#{token} client=#{client_id} expiry=#{expiry} uid=#{self.uid}" end |
#build_auth_url(base_url, args) ⇒ Object
113 114 115 116 117 118 |
# File 'app/models/devise_token_auth/concerns/user.rb', line 113 def build_auth_url(base_url, args) args[:uid] = self.uid args[:expiry] = self.tokens[args[:client_id]]['expiry'] generate_url(base_url, args) end |
#create_new_auth_token(client_id = nil) ⇒ Object
update user's auth token (should happen on each request)
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'app/models/devise_token_auth/concerns/user.rb', line 79 def create_new_auth_token(client_id=nil) client_id ||= SecureRandom.urlsafe_base64(nil, false) last_token ||= nil token = SecureRandom.urlsafe_base64(nil, false) token_hash = BCrypt::Password.create(token) expiry = (Time.now + DeviseTokenAuth.token_lifespan).to_i if self.tokens[client_id] and self.tokens[client_id]['token'] last_token = self.tokens[client_id]['token'] end self.tokens[client_id] = { token: token_hash, expiry: expiry, last_token: last_token, updated_at: Time.now } self.save! return build_auth_header(token, client_id) end |
#extend_batch_buffer(token, client_id) ⇒ Object
121 122 123 124 125 126 |
# File 'app/models/devise_token_auth/concerns/user.rb', line 121 def extend_batch_buffer(token, client_id) self.tokens[client_id]['updated_at'] = Time.now self.save! return build_auth_header(token, client_id) end |
#token_can_be_reused?(token, client_id) ⇒ Boolean
allow batch requests to use the previous token
63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'app/models/devise_token_auth/concerns/user.rb', line 63 def token_can_be_reused?(token, client_id) return true if ( # ensure that the last token and its creation time exist self.tokens[client_id]['updated_at'] and self.tokens[client_id]['last_token'] and # ensure that previous token falls within the batch buffer throttle time of the last request Time.parse(self.tokens[client_id]['updated_at']) > Time.now - DeviseTokenAuth.batch_request_buffer_throttle and # ensure that the token is valid BCrypt::Password.new(self.tokens[client_id]['last_token']) == token ) end |
#token_is_current?(token, client_id) ⇒ Boolean
47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'app/models/devise_token_auth/concerns/user.rb', line 47 def token_is_current?(token, client_id) return true if ( # ensure that expiry and token are set self.tokens[client_id]['expiry'] and self.tokens[client_id]['token'] and # ensure that the token was created within the last two weeks DateTime.strptime(self.tokens[client_id]['expiry'].to_s, '%s') > DeviseTokenAuth.token_lifespan.ago and # ensure that the token is valid BCrypt::Password.new(self.tokens[client_id]['token']) == token ) end |
#valid_token?(token, client_id = 'default') ⇒ Boolean
34 35 36 37 38 39 40 41 42 43 44 |
# File 'app/models/devise_token_auth/concerns/user.rb', line 34 def valid_token?(token, client_id='default') client_id ||= 'default' return false unless self.tokens[client_id] return true if token_is_current?(token, client_id) return true if token_can_be_reused?(token, client_id) # return false if none of the above conditions are met return false end |