| 
				
			 | 
			
			
				@@ -14,7 +14,8 @@ class Token < ApplicationRecord 
			 | 
		
	
		
			
			| 
				14
			 | 
			
				14
			 | 
			
			
				   # 
			 | 
		
	
		
			
			| 
				15
			 | 
			
				15
			 | 
			
			
				   # @!attribute expires 
			 | 
		
	
		
			
			| 
				16
			 | 
			
				16
			 | 
			
			
				   #   @return [DateTime] 
			 | 
		
	
		
			
			| 
				17
			 | 
			
				
			 | 
			
			
				-  #     when the token will expire (and will no longer be usable). 
			 | 
		
	
		
			
			| 
				
			 | 
			
				17
			 | 
			
			
				+  #     when the token will expire (and will no longer be usable). May be nil 
			 | 
		
	
		
			
			| 
				
			 | 
			
				18
			 | 
			
			
				+  #     for no expiry. 
			 | 
		
	
		
			
			| 
				18
			 | 
			
				19
			 | 
			
			
				   # 
			 | 
		
	
		
			
			| 
				19
			 | 
			
				20
			 | 
			
			
				   # @!attribute tokentype 
			 | 
		
	
		
			
			| 
				20
			 | 
			
				21
			 | 
			
			
				   #   @return [String] 
			 | 
		
	
	
		
			
			| 
				
			 | 
			
			
				@@ -35,4 +36,29 @@ class Token < ApplicationRecord 
			 | 
		
	
		
			
			| 
				35
			 | 
			
				36
			 | 
			
			
				   validates :user, presence: true 
			 | 
		
	
		
			
			| 
				36
			 | 
			
				37
			 | 
			
			
				  
			 | 
		
	
		
			
			| 
				37
			 | 
			
				38
			 | 
			
			
				   belongs_to :user 
			 | 
		
	
		
			
			| 
				
			 | 
			
				39
			 | 
			
			
				+ 
			 | 
		
	
		
			
			| 
				
			 | 
			
				40
			 | 
			
			
				+  before_validation :generate_token, if: "self.token.blank?" 
			 | 
		
	
		
			
			| 
				
			 | 
			
				41
			 | 
			
			
				+  before_validation :generate_expiry, on: :create 
			 | 
		
	
		
			
			| 
				
			 | 
			
				42
			 | 
			
			
				+ 
			 | 
		
	
		
			
			| 
				
			 | 
			
				43
			 | 
			
			
				+  private 
			 | 
		
	
		
			
			| 
				
			 | 
			
				44
			 | 
			
			
				+  def generate_token 
			 | 
		
	
		
			
			| 
				
			 | 
			
				45
			 | 
			
			
				+    candidate = nil 
			 | 
		
	
		
			
			| 
				
			 | 
			
				46
			 | 
			
			
				+    loop do 
			 | 
		
	
		
			
			| 
				
			 | 
			
				47
			 | 
			
			
				+      candidate = SecureRandom::urlsafe_base64 32 
			 | 
		
	
		
			
			| 
				
			 | 
			
				48
			 | 
			
			
				+      break candidate unless Token.exists?(token: candidate) 
			 | 
		
	
		
			
			| 
				
			 | 
			
				49
			 | 
			
			
				+    end 
			 | 
		
	
		
			
			| 
				
			 | 
			
				50
			 | 
			
			
				+    self.token = candidate 
			 | 
		
	
		
			
			| 
				
			 | 
			
				51
			 | 
			
			
				+  end 
			 | 
		
	
		
			
			| 
				
			 | 
			
				52
			 | 
			
			
				+ 
			 | 
		
	
		
			
			| 
				
			 | 
			
				53
			 | 
			
			
				+  # Defines the default expiry for the expiring tokens. 
			 | 
		
	
		
			
			| 
				
			 | 
			
				54
			 | 
			
			
				+  def generate_expiry 
			 | 
		
	
		
			
			| 
				
			 | 
			
				55
			 | 
			
			
				+    case self.tokentype 
			 | 
		
	
		
			
			| 
				
			 | 
			
				56
			 | 
			
			
				+    when TYPES[:password_reset] 
			 | 
		
	
		
			
			| 
				
			 | 
			
				57
			 | 
			
			
				+      1.days.since 
			 | 
		
	
		
			
			| 
				
			 | 
			
				58
			 | 
			
			
				+    when TYPES[:account_confirmation] 
			 | 
		
	
		
			
			| 
				
			 | 
			
				59
			 | 
			
			
				+      7.days.since 
			 | 
		
	
		
			
			| 
				
			 | 
			
				60
			 | 
			
			
				+    else 
			 | 
		
	
		
			
			| 
				
			 | 
			
				61
			 | 
			
			
				+      nil 
			 | 
		
	
		
			
			| 
				
			 | 
			
				62
			 | 
			
			
				+    end 
			 | 
		
	
		
			
			| 
				
			 | 
			
				63
			 | 
			
			
				+  end 
			 | 
		
	
		
			
			| 
				38
			 | 
			
				64
			 | 
			
			
				 end 
			 |