Commit 7a7f9648104716b34fdaefe3dcaa5ca85b68803e

Authored by Niklas
1 parent 425ee7ea3b
Exists in master

authentication (via imap)

Showing 3 changed files with 48 additions and 0 deletions Side-by-side Diff

... ... @@ -6,13 +6,22 @@
6 6  
7 7 require 'models'
8 8 require 'maildrop_serializer'
  9 +require 'net/imap'
9 10  
  11 +MAILSERVER = 'mail.sourceagency.net'
  12 +
10 13 class App < Sinatra::Base
11 14 enable :sessions
12 15 use Rack::Flash
13 16  
14 17 set :public, 'public'
15 18  
  19 + before do
  20 + unless env['PATH_INFO'] =~ /^\/login/ || session[:user]
  21 + redirect '/login'
  22 + end
  23 + end
  24 +
16 25 get '/' do
17 26 redirect '/filters'
18 27 end
... ... @@ -56,6 +65,29 @@
56 65 get '/expression/new' do
57 66 @expression = Expression.new
58 67 partial(:expression, :expression => @expression)
  68 + end
  69 +
  70 + get '/login' do
  71 + haml :login
  72 + end
  73 +
  74 + get '/logout' do
  75 + session[:user] = nil
  76 + session[:notice] = "You're logged out now."
  77 + redirect '/'
  78 + end
  79 +
  80 + post '/login' do
  81 + @imap = Net::IMAP.new(MAILSERVER)
  82 + begin
  83 + @imap.login(params[:email], params[:password])
  84 + session[:user] = params[:email]
  85 + flash[:success] = "Authentication successful"
  86 + redirect '/'
  87 + rescue => exc
  88 + flash.now[:error] = "Authentication failed"
  89 + haml :login
  90 + end
59 91 end
60 92  
61 93 helpers do
... ... @@ -10,6 +10,10 @@
10 10 %script{:src => "/javascripts/app.js"}
11 11 %body
12 12 %div.container
  13 + %div.prepend-19.span-5.last
  14 + - if session[:user]
  15 + %a{:href => "/logout"} Logout
  16 + = "(#{session[:user]})"
13 17 - %w(notice error success).each do |key|
14 18 - if flash[key.to_sym]
15 19 %p{:class => key}= flash[key.to_sym]
  1 +
  2 +%form{:action => "/login", :method => "post"}
  3 + %div
  4 + %label{:for => "email"}
  5 + E-Mail
  6 + %input{:name => "email", :type => "text"}/
  7 + %div
  8 + %label{:for => "password"}
  9 + Password
  10 + %input{:name => "password", :type => "password"}/
  11 + %div.buttons
  12 + %input{:type => "submit", :value => "Login"}/