class WarpEngine::DeviceGrant
One device authorization grant (table device_grants): a client without a browser asks for a sign-in, gets a device_code to poll with and a short user_code the person enters on the verification page.
Both codes are generated on create and expires_at is set from the host’s device_code_ttl. When the person approves, subject is the account and application_token the token issued to the device; issued_token holds the plain token until the device collects it or the grant expires.
Constants
- USER_CODE_ALPHABET
-
Characters a user code is drawn from: capital letters and digits, without the easily confused I, O, 0 and 1.
- USER_CODE_LENGTH
-
Number of characters in a user code.
Public Class Methods
# File app/models/warp_engine/device_grant.rb, line 34 def self.find_pending_by_user_code(code) pending.find_by(user_code: normalize_user_code(code)) end
Finds the pending (unanswered, unexpired) grant for the user code as a person typed it; case, spaces and dashes do not matter. Returns the WarpEngine::DeviceGrant or nil.
Source
# File app/models/warp_engine/device_grant.rb, line 39 def self.normalize_user_code(code) code.to_s.upcase.gsub(/[^A-Z0-9]/, "") end
Returns code upper-cased with everything but letters and digits removed.
# File app/models/warp_engine/device_grant.rb, line 73 def self.ransackable_associations(auth_object = nil) [] end
Allows no association in Ransack searches.
Source
# File app/models/warp_engine/device_grant.rb, line 68 def self.sweep_expired! where(expires_at: ...Time.current).where.not(issued_token: nil).update_all(issued_token: nil) end
Clears the uncollected plain issued_token of every expired grant. Returns the number of grants updated.
Public Instance Methods
Source
# File app/models/warp_engine/device_grant.rb, line 46 def approved? = approved_at.present?
Returns whether the person approved the sign-in.
Source
# File app/models/warp_engine/device_grant.rb, line 48 def denied? = denied_at.present?
Returns whether the person denied the sign-in.
Source
# File app/models/warp_engine/device_grant.rb, line 44 def expired? = expires_at <= Time.current
Returns whether expires_at has passed.
Source
# File app/models/warp_engine/device_grant.rb, line 62 def formatted_user_code user_code.to_s.scan(/.{1,4}/).join("-") end
Returns the user code in groups of four joined by a dash, e.g. ABCD-EFGH.
Source
# File app/models/warp_engine/device_grant.rb, line 52 def state return :denied if denied? return :approved if approved? return :expired if expired? :pending end
Returns the grant’s state: :denied, :approved, :expired or :pending, checked in that order.