Module: PuavoAuthentication::Controllers::Helpers

Defined in:
lib/puavo_authentication/controllers/helpers.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#authenticationObject

Returns the value of attribute authentication.



5
6
7
# File 'lib/puavo_authentication/controllers/helpers.rb', line 5

def authentication
  @authentication
end

Instance Method Details

#acquire_credentialsObject

Returns user dn/uid and password for some available login mean



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/puavo_authentication/controllers/helpers.rb', line 28

def acquire_credentials

  # OAuth Access Token
  if auth_header = request.headers["HTTP_AUTHORIZATION"]
    type, data = auth_header.split
    if type.downcase == "bearer"
      return AccessToken.decrypt_token data
    end
  end

  # Basic Auth
  #  * OAuth Client Server ID & Secrect
  #  * External Service UID & password
  #  * User UID & password
  #  * Server dn & password
  authenticate_with_http_basic do |username, password|
    logger.debug "Using basic authentication with #{ username }"

    # FIXME: move to Puavo::Authentication class (configure_ldap_connection)
    if match = username.match(/^oauth_client_id\/(.*)\/(.*)$/)

      org_key = match[1]
      oauth_client_id = match[2]

      @authentication.configure_ldap_connection(
        :organisation_key => org_key
      )

      oauth_client_server = OauthClient.find(:first,
        :attribute => "puavoOAuthClientId",
        :value => oauth_client_id)

      return {
        :dn => oauth_client_server.dn,
        :organisation_key => org_key,
        :password => password,
        :scope => oauth_client_server.puavoOAuthScope
      }

    end

    # Authenticate with server's distinguished name and password
    if !username.to_s.empty? && (server_dn = ActiveLdap::DistinguishedName.parse(username) rescue nil)
      if server_dn.parent.rdns.first["ou"] == "Servers"
        return {
          :dn => server_dn,
          :organisation_key => organisation_key_from_host,
          :password => password,
        }
      end
    end

    return {
      :uid => username,
      :organisation_key => organisation_key_from_host,
      :password => password
    }
  end

  # Puavo Session (User UID & password)
  if uid = session[:uid]
    logger.debug "Using session authentication with #{ uid }"
    return {
      :uid => uid,
      :organisation_key => organisation_key_from_host,
      :password => session[:password_plaintext]
    }
  end

end

#current_organisationObject



17
18
19
20
21
22
23
24
# File 'lib/puavo_authentication/controllers/helpers.rb', line 17

def current_organisation
  if @authentication.nil?
    raise "Cannot call 'current_organisation' before 'setup_authentication'"
  end

  @authentication.current_organisation

end

#current_userObject



7
8
9
10
11
12
13
14
15
# File 'lib/puavo_authentication/controllers/helpers.rb', line 7

def current_user

  if @authentication.nil?
    raise "Cannot call 'current_user' before 'setup_authentication'"
  end

  @authentication.current_user

end

#organisation_key_from_host(host = nil) ⇒ Object



188
189
190
191
192
193
194
# File 'lib/puavo_authentication/controllers/helpers.rb', line 188

def organisation_key_from_host(host=nil)
  organisation_key = Puavo::Organisation.key_by_host(request.host)
  unless organisation_key
    organisation_key = Puavo::Organisation.key_by_host("*")
  end
  return organisation_key
end

#perform_login(credentials) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/puavo_authentication/controllers/helpers.rb', line 109

def (credentials)

  if credentials.nil?
    raise Puavo::AuthenticationFailed, "No credentials supplied"
  end

  # Configure ActiveLdap to use the credentials
  @authentication.configure_ldap_connection credentials

  # Authenticate above credentials
  @authentication.authenticate

  # Set locale from user's organisation
  I18n.locale = current_organisation.locale

  return true
end

#redirect_back_or_default(default) ⇒ Object



183
184
185
186
# File 'lib/puavo_authentication/controllers/helpers.rb', line 183

def redirect_back_or_default(default)
  redirect_to(session[:return_to] || default)
  session[:return_to] = nil
end

#remove_ldap_connectionObject



214
215
216
# File 'lib/puavo_authentication/controllers/helpers.rb', line 214

def remove_ldap_connection
  Puavo::Authentication.remove_connection
end

#require_loginObject

Before filter Require user login credentials



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/puavo_authentication/controllers/helpers.rb', line 129

def 

  begin
    (acquire_credentials)
  rescue Puavo::AuthenticationError => e
    logger.info "Login failed for: #{ e }"
    show_authentication_error e.code, t('flash.session.failed')
    return false
  end

  if session[:login_flash]
    flash[:notice] = session[:login_flash]
    session.delete :login_flash
  end

  return true
end

#require_puavo_authorizationObject

Before filter Require Puavo access rights



149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/puavo_authentication/controllers/helpers.rb', line 149

def require_puavo_authorization

  # Unauthorized always when not authenticated
  return false unless @authentication

  begin
    @authentication.authorize
  rescue Puavo::AuthorizationFailed => e
    logger.info "Authorization  failed: #{ e }"
    show_authentication_error "unauthorized", t('flash.session.failed')
    return false
  end
end

#set_initial_localeObject



201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/puavo_authentication/controllers/helpers.rb', line 201

def set_initial_locale
  # Default to English
  I18n.locale = "en"

  # TODO: set from user agent

  # Set from hostname if it is a known organisation
  if organisation = Puavo::Organisation.find_by_host(request.host)
    I18n.locale = organisation.locale
  end

end

#set_organisation_to_sessionObject



197
198
199
# File 'lib/puavo_authentication/controllers/helpers.rb', line 197

def set_organisation_to_session
  session[:organisation] = current_organisation if current_organisation
end

#setup_authenticationObject

Before filter Setup authentication object with default credentials from config/ldap.yml



102
103
104
105
106
# File 'lib/puavo_authentication/controllers/helpers.rb', line 102

def setup_authentication

  @authentication = Puavo::Authentication.new

end

#show_authentication_error(code, message) ⇒ Object



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/puavo_authentication/controllers/helpers.rb', line 163

def show_authentication_error(code, message)
  session.delete :password_plaintext
  session.delete :uid
  if request.format == Mime::JSON
    render(:json => {
      :error => code,
      :message => message,
    }.to_json,
    :status => 401)
  else
    store_location
    flash[:notice] = message
    redirect_to 
  end
end

#store_locationObject



179
180
181
# File 'lib/puavo_authentication/controllers/helpers.rb', line 179

def store_location
  session[:return_to] = request.request_uri
end

#themeObject



218
219
220
221
222
223
224
# File 'lib/puavo_authentication/controllers/helpers.rb', line 218

def theme
  if current_organisation
    theme = current_organisation.value_by_key('theme')
  end

  return theme || "breathe"
end