Skip to content

Instantly share code, notes, and snippets.

@vtypal
Created June 2, 2009 13:19
Show Gist options
  • Save vtypal/122232 to your computer and use it in GitHub Desktop.
Save vtypal/122232 to your computer and use it in GitHub Desktop.
require 'rubygems'
require 'innate'
class Innatecookies
include Innate::Node
map '/'
provide :html, :engine => :Etanni, :type => 'text/html'
@@header =<<-HEADER
<?xml version="1.0" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Style-Type" content="text/css" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="description" content="Innate Login Example. />
<meta name="generator" content="Innate #{Innate::VERSION}" />
<meta name="author" content="vtypal_at_gmail_com" />
<meta name="date" content="#{Time.now.iso8601}" />
<title>#\{@title}</title>
</head>
<body>
HEADER
@@footer =<<-FOOTER
</body>
</html>
FOOTER
def index
@title = "Innate Login Example"
@@header + %|
<?r if auth_key_valid? ?>
#\{a('Logout', :logout)}
<?r end ?>
<br />
Hello, visitor from #\{Innate::Request.current.env["REMOTE_ADDR"]} <br />
| + @@footer
end
def login
@title = "Innate Login Example"
@username, @password = request[ :username, :password]
if request.post?
if @username == "admin" && @password =="innate"
response.set_cookie('site_auth', :expires => Time.now + 1209600, :value => auth_key(@username) )
session[:user] = @username
redirect(:secret)
else
flash[:error] = "Wrong username or password, pls. try again"
end
end
@@header + %|
<form method = 'POST', action = /login >
<fieldset>
<h1>Login</h1><br />
</p>
<?r if @username ?>
#\{flash[:error]}<br />
<?r end ?>
<br />
<label class="descr">username: </label>
<input type = 'text', name = 'username' />
<br /><br />
<label class="descr">password: </label>
<input type ='password', name = 'password' />
<br /><br />
<input type = 'submit', name = 'login' />
<br />
</form>
| + @@footer
end
def logout
response.delete_cookie('site_auth')
redirect(:index)
end
def secret
@title = "Secret Place"
@@header + %|
#\{a('Logout', :logout)}
<br />
Only authorized persons can access on this page<br />
That's your cookie! #\{request.cookies['site_auth']}
| + @@footer
end
before(:secret) { auth_key_valid? }
private
def auth_key(user)
Digest::SHA256.hexdigest(Innate::Request.current.env["REMOTE_ADDR"].to_s + user.to_s + "1qa4221fl4sdfds23@rf" )
end
def auth_key_valid?
if request.cookies['site_auth'] != auth_key(session[:user])
redirect(:login)
else
return true
end
end
end
######### S E T I N N A T E M O D E ###########
mode = :dev
if(mode == :dev)
ENV['ads'] = 'false'
Innate.options.mode = :dev
Innate.options.adapter.port = 7000
Innate.options.adapter.handler = :mongrel
Innate::Log.loggers = [Logger.new("#{__FILE__}.log")]
elsif(mode == :live)
ENV['ads'] = 'false'
Innate.options.mode = :live
Innate::Log.info "Starting in production on #\{Time.now} --------------- "
Innate.options.adapter.handler = :webrick
Innate.options.adapter.port = 7001
end
######## U S E R A C K M I D D L E W A R E S ############
Innate.start do |mw|
#mw.use Rack::MyMiddleware
mw.use Rack::Reloader #this is not necessary on :dev mode.
mw.innate
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment