mirror of
				https://github.com/sinatra/sinatra
				synced 2023-03-27 23:18:01 -04:00 
			
		
		
		
	import authenticity token implementation
This commit is contained in:
		
							parent
							
								
									b75b5a596d
								
							
						
					
					
						commit
						ab177702bb
					
				
					 2 changed files with 17 additions and 0 deletions
				
			
		| 
						 | 
					@ -14,6 +14,13 @@ module Rack
 | 
				
			||||||
    #
 | 
					    #
 | 
				
			||||||
    # Not Yet Implemented!
 | 
					    # Not Yet Implemented!
 | 
				
			||||||
    class AuthenticityToken < Base
 | 
					    class AuthenticityToken < Base
 | 
				
			||||||
 | 
					      def accepts?(env)
 | 
				
			||||||
 | 
					        return true if safe? env
 | 
				
			||||||
 | 
					        request = Request.new env
 | 
				
			||||||
 | 
					        token   = session[:csrf] ||= session['_csrf_token'] || random_string
 | 
				
			||||||
 | 
					        env['HTTP_X_CSRF_TOKEN'] == token or
 | 
				
			||||||
 | 
					          request.params['authenticity_token'] == token
 | 
				
			||||||
 | 
					      end
 | 
				
			||||||
    end
 | 
					    end
 | 
				
			||||||
  end
 | 
					  end
 | 
				
			||||||
end
 | 
					end
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -54,6 +54,16 @@ module Rack
 | 
				
			||||||
        [options[:status], {'Content-Type' => 'text/plain'}, [options[:message]]]
 | 
					        [options[:status], {'Content-Type' => 'text/plain'}, [options[:message]]]
 | 
				
			||||||
      end
 | 
					      end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      def session(env)
 | 
				
			||||||
 | 
					        env['rack.session'] ||= {}
 | 
				
			||||||
 | 
					      end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      def random_string(secure = defined? SecureRandom)
 | 
				
			||||||
 | 
					        secure ? SecureRandom.hex(32) : "%032x" % rand(2**128-1)
 | 
				
			||||||
 | 
					      rescue NotImpelentedError
 | 
				
			||||||
 | 
					        random_string false
 | 
				
			||||||
 | 
					      end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      def drop_session(env)
 | 
					      def drop_session(env)
 | 
				
			||||||
        env['rack.session'] = {}
 | 
					        env['rack.session'] = {}
 | 
				
			||||||
      end
 | 
					      end
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue