Confluence4r Rails Authentication

Recent Changes

The authentication subsytem in the Confluence4r plugin has recently changed! Please see below for the revised controller methods that work with the new system.

We deal with the problem of authenticating against Confluence by adding a login filter to our application controller. This asks the user for their Confluence username and password and stores it in the Rails session:

class ApplicationController < ActionController::Base
  before_filter :authenticate, :except => :login

  def login
    @destination = params[:destination]
    if request.post?
      begin
        options = {}
        options[:username] = params[:username]
        options[:password] = params[:password]
        @connector = Confluence::Connector.new(options)
        @connector.connect
        
        session[:confluence_connector] = @connector
        
        redirect_to @destination and return
      rescue Confluence::RemoteAuthenticationException
        flash[:error] = $!
      end
    end
    
    render :template => 'shared/login', :layout => 'encore'
  end

  protected
    def authenticate
      if session[:confluence_connector]
        Confluence::RemoteDataObject.connector = session[:confluence_connector] and return true
      else
        redirect_to :action => :login, :destination => request.request_uri and return false
      end
    end
end

You'll also have to implement a shared/login.rhtml template. Ours looks like this:

<%= form_tag({}, {:style => "text-align: center;"}) -%>
    <div style="width: 500px; margin: auto;">
    <p>
        <strong>Please enter your username and password to continue.</strong><br />
        Why must you enter your authentication information again you ask?<br />
        It is a mystery. Leave it at that.
    </p>
    <%= auto_flash %>
    <%= hidden_field_tag(:destination, @destination) %>
    <table style="margin: auto;">
        <tr>
            <th style="text-align: right"><label for="username">Username:</label></th>
            <td><%= text_field_tag(:username, params[:username]) %></td>
        </tr>
        <tr>
            <th style="text-align: right"><label for="password">Password:</label></th>
            <td><%= password_field_tag(:password, params[:password]) %></td>
        </tr>
    </table>
    <p>
        <%= submit_tag("Log in") %>
    </p>
    </div>
<%= end_form_tag -%>
Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.