Skip to content

Commit

Permalink
Closes #51, SAML login implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
kalabiyau committed Mar 7, 2015
1 parent d6960c0 commit 8800a46
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 17 deletions.
9 changes: 4 additions & 5 deletions app/controllers/saml_controller.rb
Expand Up @@ -13,11 +13,11 @@ def consume
response.settings = saml_settings

if response.is_valid?
user = User.find_or_create_for_saml({ novell_username: response.name_id.downcase,
user = User.find_or_create_for_saml({ username: response.name_id.downcase,
email: saml_attribute(response, 'mail').downcase,
first_name: saml_attribute(response, 'givenName'),
last_name: saml_attribute(response, 'sn'),
webid: saml_attribute(response, 'webidSynchID') })
workforceid: saml_attribute(response, 'workforceID') })
sign_in_and_redirect(user) and return
else
response.validate!
Expand Down Expand Up @@ -46,9 +46,8 @@ def saml_settings
settings = OneLogin::RubySaml::Settings.new
settings.assertion_consumer_service_url = saml_consume_url
settings.issuer = 'act.suse.de'
settings.idp_sso_target_url = 'https://login.innerweb.novell.com/nidp/saml2/sso'
settings.idp_cert_fingerprint = '80:b8:c8:ef:e0:c3:67:6e:d5:5f:6a:fb:ef:ad:f7:a3:60:9d:65:af'
settings.authn_context_decl_ref = 'suse/name/password/uri'
settings.idp_sso_target_url = 'https://login.innerwebstage.novell.com/nidp/saml2/sso'
settings.idp_cert_fingerprint = 'd8:9d:8b:34:b5:76:c3:c8:06:b8:7c:1f:d8:73:e6:fb:07:fe:2a:38'
settings
end
end
Expand Down
2 changes: 1 addition & 1 deletion app/models/category.rb
@@ -1,6 +1,6 @@
class Category < ActiveRecord::Base

validates :name, presence: true, uniqueness: true
validates :color, presence: true, uniqueness: true
validates :color, presence: true

end
5 changes: 3 additions & 2 deletions app/models/user.rb
@@ -1,13 +1,14 @@
class User < ActiveRecord::Base

has_and_belongs_to_many :activities
validates :first_name, :last_name, :email, :novell_username, :webid, presence: true
validates :first_name, :email, :username, :workforceid, presence: true
#validates :first_name, :last_name, :email, :username, :workforceid, presence: true
scope :subscribers, -> { where(subscriber: true) }

class << self

def find_or_create_for_saml(data)
user = User.where('lower(novell_username) = ?', data[:novell_username].downcase).first || User.new
user = User.where('lower(username) = ?', data[:username].downcase).first || User.new
user.update_attributes!(data)
user
end
Expand Down
6 changes: 6 additions & 0 deletions db/migrate/20150307114954_change_user_attributes.rb
@@ -0,0 +1,6 @@
class ChangeUserAttributes < ActiveRecord::Migration
def change
rename_column :users, :novell_username, :username
rename_column :users, :webid, :workforceid
end
end
8 changes: 4 additions & 4 deletions db/schema.rb
Expand Up @@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20150306154002) do
ActiveRecord::Schema.define(version: 20150307114954) do

create_table "activities", force: :cascade do |t|
t.string "name"
Expand Down Expand Up @@ -41,9 +41,9 @@
t.string "first_name"
t.string "last_name"
t.string "email"
t.string "novell_username"
t.string "webid"
t.boolean "subscriber", default: false
t.string "username"
t.string "workforceid"
t.boolean "subscriber", default: false
end

end
4 changes: 2 additions & 2 deletions spec/factories/user_factory.rb
Expand Up @@ -4,8 +4,8 @@
sequence(:first_name) { Faker::Name.first_name }
sequence(:last_name) { Faker::Name.last_name }
sequence(:email) { Faker::Internet.email }
sequence(:novell_username) {|n| "#{Faker::Internet.user_name}#{n}" }
sequence(:webid) { Faker.numerify('#######') }
sequence(:username) {|n| "#{Faker::Internet.user_name}#{n}" }
sequence(:workforceid) { Faker.numerify('#######') }
end

end
6 changes: 3 additions & 3 deletions spec/models/user_spec.rb
Expand Up @@ -5,18 +5,18 @@
subject { create(:user) }

it { should validate_presence_of(:first_name) }
it { should validate_presence_of(:last_name) }
# it { should validate_presence_of(:last_name) }

describe '.find_or_create_for_saml' do

it 'finds the right instance for SAML' do
auth_hash = { novell_username: subject.novell_username, webid: subject.webid }
auth_hash = { username: subject.username, workforceid: subject.workforceid }
expect(User.find_or_create_for_saml(auth_hash)).to eq subject
end

it 'creates a new instance for SAML if necessary' do
user_count = User.count
auth_hash = { novell_username: 'test', first_name: 'test', last_name: 'test', email: 'test', webid: '121242' }
auth_hash = { username: 'test', first_name: 'test', last_name: 'test', email: 'test', workforceid: '121242' }
User.find_or_create_for_saml(auth_hash)
expect(User.count).to eq(user_count + 1)
end
Expand Down

0 comments on commit 8800a46

Please sign in to comment.