Module: Crowd::SingleSignOn

Defined in:
lib/single_sign_on.rb

Overview

The single sign on (SSO) functionality for Atlassian Crowd as a mixin module.

To use this module, include it in your Rails ApplicationController class. The module uses controller methods such as cookies, session, params.

Author

Stefan Wille

Copyright

Copyright © 2010 Stefan Wille

License

BSD

Instance Method Summary collapse

Instance Method Details

#crowd_authenticate(user_name, password) ⇒ Object

Authenticates the user with the given user name and password and marks the user as authenticated on success.

Returns the crowd token on success, false on failure.



46
47
48
49
50
# File 'lib/single_sign_on.rb', line 46

def crowd_authenticate(user_name, password)      
  crowd_authenticate!(user_name, password)
rescue Crowd::AuthenticationException => e
  false
end

#crowd_authenticate!(user_name, password) ⇒ Object

Same as #crowd_authenticate, but raises an AuthenticationException on failure.



54
55
56
57
58
59
60
# File 'lib/single_sign_on.rb', line 54

def crowd_authenticate!(user_name, password)
  logger.info "Crowd: Authenticating user #{user_name}"
  token = Crowd.authenticate_principal(user_name, password, crowd_validation_factors)
  crowd_mark_session_as_authenticated(token)      
  logger.info "Crowd: Authentication successful, token #{token}"
  token
end

#crowd_authenticated?Boolean

Returns whether the user is already authenticated.

Returns:

  • (Boolean)


15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/single_sign_on.rb', line 15

def crowd_authenticated?
  logger.info "Crowd: All cookies: #{cookies.inspect}"
  
  token = crowd_token
  if token.blank?
    logger.info "Crowd: No token"
    return false
  end
  
  if crowd_authentication_cached?
    logger.info "Crowd: Authentication is cached"
    return true
  else 
    logger.info "Crowd: Authentication is not cached"
  end      
  
  if Crowd.is_valid_principal_token?(token, crowd_validation_factors)
    logger.info "Crowd: Token is valid"      
    crowd_mark_session_as_authenticated(token)
    return true
  else 
    logger.info "Crowd: Token is invalid"      
    return false
  end
end

#crowd_current_userObject

Returns the current user, as seen by crowd.



71
72
73
# File 'lib/single_sign_on.rb', line 71

def crowd_current_user
  crowd_token && Crowd.find_principal_by_token(crowd_token)
end

#crowd_current_user_display_nameObject

Returns the current users display name.



64
65
66
67
# File 'lib/single_sign_on.rb', line 64

def crowd_current_user_display_name
  user = crowd_current_user
  user && user[:attributes][:displayName]
end

#crowd_log_outObject

Marks the user as unauthenticated



88
89
90
91
92
# File 'lib/single_sign_on.rb', line 88

def crowd_log_out
  logger.info "Crowd: log out"
  crowd_update_token(nil)
  crowd_clear_cache
end

#crowd_tokenObject

Returns the crowd token or nil.



77
78
79
80
81
82
83
84
# File 'lib/single_sign_on.rb', line 77

def crowd_token
  logger.info "params token: #{params[Crowd.crowd_cookie_tokenkey]}"
  logger.info "cookies token: #{cookies[Crowd.crowd_cookie_tokenkey]}"
  logger.info "session token: #{session[Crowd.crowd_session_tokenkey]}"
  token = params[Crowd.crowd_cookie_tokenkey] || cookies[Crowd.crowd_cookie_tokenkey] || session[Crowd.crowd_session_tokenkey]
  logger.info "token = #{token}"
  token
end