crowd_client 0.0.6
Sign up to get free protection for your applications and to get access to all the features.
- data/generators/crowd_client/crowd_client_generator.rb +21 -0
- data/generators/crowd_client/templates/initializers_crowd_client.rb +9 -0
- data/generators/crowd_client/templates/new.html.erb +12 -0
- data/generators/crowd_client/templates/sessions_controller.rb +27 -0
- data/lib/crowd_client.rb +16 -0
- data/lib/mixins/application_controller.rb +114 -0
- data/lib/mixins/crowd_helpers.rb +44 -0
- data/lib/models/crowd_user.rb +11 -0
- data/rails/init.rb +12 -0
- metadata +63 -0
@@ -0,0 +1,21 @@
|
|
1
|
+
class CrowdClientGenerator < Rails::Generator::Base
|
2
|
+
|
3
|
+
def banner
|
4
|
+
"Usage #{$0} #{spec.name}"
|
5
|
+
end
|
6
|
+
|
7
|
+
def manifest
|
8
|
+
record do |m|
|
9
|
+
# copy the session controller
|
10
|
+
m.directory('app/controllers')
|
11
|
+
m.file('sessions_controller.rb', 'app/controllers/sessions_controller.rb')
|
12
|
+
|
13
|
+
# copy the default login (session/new) view
|
14
|
+
m.directory('app/views/sessions')
|
15
|
+
m.file('new.html.erb', 'app/views/sessions/new.html.erb')
|
16
|
+
|
17
|
+
# copy the initializer
|
18
|
+
m.file 'initializers_crowd_client.rb', 'config/initializers/crowd_client.rb'
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,9 @@
|
|
1
|
+
# Configuration Parameters for crowd client gem
|
2
|
+
|
3
|
+
Crowd::Client::Config.service_base_url = 'https://crowd.gnoso.com/'
|
4
|
+
Crowd::Client::Config.api_key = 'replace-with-crowd-api-key'
|
5
|
+
Crowd::Client::Config.realm = 'replace-with-crowd-realm'
|
6
|
+
Crowd::Client::Config.application_name = 'replace-with-crowd-application-name'
|
7
|
+
|
8
|
+
# Apply the settings across the application
|
9
|
+
Crowd::Client::Config.apply_configuration
|
@@ -0,0 +1,12 @@
|
|
1
|
+
|
2
|
+
<% form_tag sessions_resource_url do -%>
|
3
|
+
<%= hidden_field_tag 'realm', crowd_realm %>
|
4
|
+
<%= hidden_field_tag 'redirect_url', sessions_create_url %>
|
5
|
+
|
6
|
+
<div><%= label_tag "username" %><%= text_field_tag "username" %></div>
|
7
|
+
<div><%= label_tag "password" %><%= password_field_tag "password" %></div>
|
8
|
+
|
9
|
+
<div><%= check_box_tag 'save_login'%><%= label_tag 'save_login', 'Remember me.'%></div>
|
10
|
+
|
11
|
+
<%= submit_tag "Login" %>
|
12
|
+
<% end -%>
|
@@ -0,0 +1,27 @@
|
|
1
|
+
#
|
2
|
+
# This file was generated by the crowd_client gem.
|
3
|
+
#
|
4
|
+
# To regenerate:
|
5
|
+
#
|
6
|
+
# script/generate crowd_client
|
7
|
+
#
|
8
|
+
class SessionsController < ApplicationController
|
9
|
+
|
10
|
+
skip_before_filter :must_authenticate
|
11
|
+
|
12
|
+
def create
|
13
|
+
if create_session(params) then
|
14
|
+
flash[:message] = "Logged in."
|
15
|
+
redirect_to after_login_url || root_url
|
16
|
+
else
|
17
|
+
flash[:error] = "Unsuccessful login."
|
18
|
+
redirect_to :action => "new"
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def destroy
|
23
|
+
destroy_session
|
24
|
+
redirect_to :action => "new"
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
data/lib/crowd_client.rb
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
module Crowd
|
2
|
+
module Client
|
3
|
+
|
4
|
+
class Config
|
5
|
+
|
6
|
+
cattr_accessor :service_base_url # base url of the crowd service: default => "https://crowd.gnoso.com"
|
7
|
+
cattr_accessor :api_key # api-key from crowd to allow api access
|
8
|
+
cattr_accessor :realm # name of the crowd realm that this site will use for authentication
|
9
|
+
cattr_accessor :application_name # name of the application (used to verify API keys)
|
10
|
+
|
11
|
+
def self.apply_configuration
|
12
|
+
CrowdUser.apply_configuration
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,114 @@
|
|
1
|
+
# Add authentication methods to the application controller
|
2
|
+
#
|
3
|
+
# Usually you would include this in your Application Controller Class like so
|
4
|
+
#
|
5
|
+
# class ApplicationController < ActionController::Base
|
6
|
+
# include Crowd::Client::ApplicationControllerMethods
|
7
|
+
#
|
8
|
+
# .......
|
9
|
+
#
|
10
|
+
# end
|
11
|
+
|
12
|
+
module Crowd
|
13
|
+
module Client
|
14
|
+
module ApplicationControllerMethods
|
15
|
+
|
16
|
+
def logged_in?
|
17
|
+
session[:logged_in]
|
18
|
+
end
|
19
|
+
|
20
|
+
def logged_in_name
|
21
|
+
session[:name]
|
22
|
+
end
|
23
|
+
|
24
|
+
def after_login_url=(url)
|
25
|
+
session[:after_login_url] = url
|
26
|
+
end
|
27
|
+
|
28
|
+
def after_login_url
|
29
|
+
session[:after_login_url]
|
30
|
+
end
|
31
|
+
|
32
|
+
def authenticated_with_api?
|
33
|
+
@authenticated_with_api
|
34
|
+
end
|
35
|
+
|
36
|
+
def must_authenticate
|
37
|
+
return if authenticated_with_api?
|
38
|
+
crowd_realm = Crowd::Client::Config.realm
|
39
|
+
|
40
|
+
if logged_in?
|
41
|
+
return true
|
42
|
+
elsif (token = params.delete(:c_token))
|
43
|
+
register_login(token)
|
44
|
+
redirect_to params
|
45
|
+
elsif !checked_with_crowd?
|
46
|
+
session[:checked_crowd] = true
|
47
|
+
redirect_to "https://crowd.gnoso.com/sessions/check?realm=#{crowd_realm}&redirect_url=#{url_for(params.merge(:only_path => false))}"
|
48
|
+
else
|
49
|
+
after_login_url = params
|
50
|
+
redirect_to :controller => :sessions, :action => :new
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
def verify_api_key
|
55
|
+
key = authenticate_with_http_basic { |api_key, ignore| api_key }
|
56
|
+
logger.info "Key: #{key}"
|
57
|
+
|
58
|
+
if key then
|
59
|
+
# check the key against the crowd server
|
60
|
+
client = HTTPClient.new
|
61
|
+
response = client.get crowd_auth_url(key)
|
62
|
+
logger.info "Crowd Response Status: #{response.status}"
|
63
|
+
logger.info "Crowd Response: #{response.body.content}"
|
64
|
+
|
65
|
+
@authenticated_with_api = (response.status == 200)
|
66
|
+
end
|
67
|
+
|
68
|
+
true # continue filter chain
|
69
|
+
end
|
70
|
+
|
71
|
+
def crowd_auth_url(key)
|
72
|
+
crowd_application_name = Crowd::Client::Config.application_name
|
73
|
+
crowd_realm = Crowd::Client::Config.realm
|
74
|
+
URI.join( Crowd::Client::Config.service_base_url, "/api_keys/authenticate?realm=#{crowd_realm}&key=#{key}&application_name=#{crowd_application_name}")
|
75
|
+
end
|
76
|
+
|
77
|
+
def register_login(token)
|
78
|
+
session[:logged_in] = true
|
79
|
+
crowd_user = CrowdUser.find(token)
|
80
|
+
session[:name] = "#{crowd_user.first_name} #{crowd_user.last_name}"
|
81
|
+
end
|
82
|
+
|
83
|
+
def checked_with_crowd?
|
84
|
+
session[:checked_crowd]
|
85
|
+
end
|
86
|
+
|
87
|
+
def create_session(params)
|
88
|
+
status = params[:c_status]
|
89
|
+
access_token = params[:c_token]
|
90
|
+
|
91
|
+
case status
|
92
|
+
when "SUCCESS":
|
93
|
+
register_login(access_token)
|
94
|
+
return true
|
95
|
+
when "INVALID_LOGIN":
|
96
|
+
return false
|
97
|
+
else
|
98
|
+
raise "Something is configured wrong. Invalid status for session.", :status => 500
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
# handler intended to be used within the create_session block
|
103
|
+
def has_status(status, &block)
|
104
|
+
status == @session_status
|
105
|
+
end
|
106
|
+
|
107
|
+
def destroy_session
|
108
|
+
session[:logged_in] = false
|
109
|
+
session[:name] = nil
|
110
|
+
end
|
111
|
+
|
112
|
+
end
|
113
|
+
end
|
114
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
module Crowd
|
2
|
+
module Client
|
3
|
+
module ViewHelpers
|
4
|
+
|
5
|
+
def logged_in?
|
6
|
+
controller.logged_in?
|
7
|
+
end
|
8
|
+
|
9
|
+
def logged_in_name
|
10
|
+
controller.logged_in_name
|
11
|
+
end
|
12
|
+
|
13
|
+
def crowd_realm
|
14
|
+
Crowd::Client::Config.realm
|
15
|
+
end
|
16
|
+
|
17
|
+
def crowd_application_name
|
18
|
+
Crowd::Client::Config.application_name
|
19
|
+
end
|
20
|
+
|
21
|
+
def sessions_resource_url
|
22
|
+
URI.join( Crowd::Client::Config.service_base_url, 'sessions' ).to_s
|
23
|
+
end
|
24
|
+
|
25
|
+
def sessions_create_url
|
26
|
+
url_for(:controller => 'sessions', :action => 'create', :only_path => false)
|
27
|
+
end
|
28
|
+
|
29
|
+
def sessions_destroy_url
|
30
|
+
url_for(:controller => 'sessions', :action => 'destroy', :only_path => false)
|
31
|
+
end
|
32
|
+
|
33
|
+
def crowd_logout_url
|
34
|
+
URI.join( Crowd::Client::Config.service_base_url, "/sessions/0?realm=#{crowd_realm}&redirect_url=#{sessions_destroy_url}").to_s
|
35
|
+
end
|
36
|
+
|
37
|
+
def link_to_logout(text = nil)
|
38
|
+
link_to text || "Log Out", crowd_logout_url, :method => "delete"
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
@@ -0,0 +1,11 @@
|
|
1
|
+
class CrowdUser < ActiveResource::Base
|
2
|
+
|
3
|
+
# This is called after the configuraion settings are set in the initializer
|
4
|
+
def self.apply_configuration
|
5
|
+
self.site = Crowd::Client::Config.service_base_url
|
6
|
+
self.element_name = "user"
|
7
|
+
self.user = Crowd::Client::Config.api_key
|
8
|
+
self.password = ""
|
9
|
+
end
|
10
|
+
|
11
|
+
end
|
data/rails/init.rb
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), '..', 'lib', 'crowd_client')
|
2
|
+
|
3
|
+
require File.join(File.dirname(__FILE__), '..', 'lib', 'mixins', 'application_controller')
|
4
|
+
require File.join(File.dirname(__FILE__), '..', 'lib', 'mixins', 'crowd_helpers')
|
5
|
+
|
6
|
+
require File.join(File.dirname(__FILE__), '..', 'lib', 'models', 'crowd_user')
|
7
|
+
|
8
|
+
#require File.join(File.dirname(__FILE__), '..', 'generators', 'crowd_client', 'crowd_client_generator')
|
9
|
+
|
10
|
+
# Hook our helpers into rails
|
11
|
+
ActionView::Base.send :include, Crowd::Client::ViewHelpers
|
12
|
+
ActionController::Base.send :include, Crowd::Client::ApplicationControllerMethods
|
metadata
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: crowd_client
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.6
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Gnoso, Inc.
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-10-18 00:00:00 -04:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: Crowd is a service for authenticating and tracking user accounts. This is the client library for crowd.
|
17
|
+
email: conversation@gnoso.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files: []
|
23
|
+
|
24
|
+
files:
|
25
|
+
- rails/init.rb
|
26
|
+
- generators/crowd_client/crowd_client_generator.rb
|
27
|
+
- generators/crowd_client/templates/initializers_crowd_client.rb
|
28
|
+
- generators/crowd_client/templates/new.html.erb
|
29
|
+
- generators/crowd_client/templates/sessions_controller.rb
|
30
|
+
- lib/crowd_client.rb
|
31
|
+
- lib/mixins/application_controller.rb
|
32
|
+
- lib/mixins/crowd_helpers.rb
|
33
|
+
- lib/models/crowd_user.rb
|
34
|
+
has_rdoc: true
|
35
|
+
homepage: http://www.gnoso.com
|
36
|
+
licenses: []
|
37
|
+
|
38
|
+
post_install_message:
|
39
|
+
rdoc_options: []
|
40
|
+
|
41
|
+
require_paths:
|
42
|
+
- lib
|
43
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: "0"
|
48
|
+
version:
|
49
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: "0"
|
54
|
+
version:
|
55
|
+
requirements: []
|
56
|
+
|
57
|
+
rubyforge_project:
|
58
|
+
rubygems_version: 1.3.5
|
59
|
+
signing_key:
|
60
|
+
specification_version: 3
|
61
|
+
summary: Client library for the crowd user account system.
|
62
|
+
test_files: []
|
63
|
+
|