Add OAuth2 authentication (#147)

- Added Site settings > Authentication section
- Create/edit/delete your custom oauth2 configurations
- Login or signup with oauth2
This commit is contained in:
Riccardo Graziosi
2022-08-05 18:15:17 +02:00
committed by GitHub
parent 3bda6dee08
commit 4c73b398e8
65 changed files with 2096 additions and 129 deletions

15
spec/factories/o_auths.rb Normal file
View File

@@ -0,0 +1,15 @@
FactoryBot.define do
factory :o_auth do
sequence(:name) { |n| "OAuth#{n}" }
logo { "url_to_logo" }
is_enabled { false }
client_id { "123456" }
client_secret { "123456" }
authorize_url { "authorize_url" }
token_url { "token_url" }
profile_url { "profile_url" }
scope { "read" }
json_user_name_path { "user.name" }
json_user_email_path { "user.email" }
end
end

View File

@@ -0,0 +1,53 @@
require 'rails_helper'
RSpec.describe OAuthsHelper, type: :helper do
context 'query_path_from_hash method' do
it 'queries a path from hash' do
email = "admin@example.com"
name = "Admin"
surname = "Example"
hash = {
"email" => email,
"info" => {
"name" => name,
"additional_info" => {
"surnames" => [
["surname" => "Surname1"],
["surname" => "Surname2"],
["surname" => surname]
]
}
}
}
email_path = "email"
name_path = "info.name"
surname_path = "info.additional_info.surnames[2][0].surname"
expect(helper.query_path_from_hash(hash, name_path)).to eq(name)
expect(helper.query_path_from_hash(hash, email_path)).to eq(email)
expect(helper.query_path_from_hash(hash, surname_path)).to eq(surname)
end
it 'returns nil if inputs are not of type Hash and String respectively' do
expect(helper.query_path_from_hash({"valid" => true}, ["invalid"])).to eq(nil)
expect(helper.query_path_from_hash("invalid", "valid")).to eq(nil)
end
it 'returns nil if path not found' do
email = "admin@example.com"
name = "Admin"
hash = {
"email" => email,
"info" => {
"name" => name,
}
}
name_path = "name"
expect(helper.query_path_from_hash(hash, name_path)).to eq(nil)
name_path = "info.names[0]"
expect(helper.query_path_from_hash(hash, name_path)).to eq(nil)
end
end
end

View File

@@ -0,0 +1,64 @@
require 'rails_helper'
RSpec.describe OAuth, type: :model do
let(:o_auth) { FactoryBot.create(:o_auth) }
it 'should be valid' do
expect(o_auth).to be_valid
end
it 'has a non-nil unique name' do
o_auth2 = FactoryBot.build_stubbed(:o_auth, name: o_auth.name)
expect(o_auth2).to be_invalid
end
it 'is disabled by default' do
o_auth = OAuth.new
expect(o_auth.is_enabled).to eq(false)
end
it 'has a boolean enabled status' do
o_auth = FactoryBot.build_stubbed(:o_auth, is_enabled: nil)
expect(o_auth).to be_invalid
o_auth = FactoryBot.build_stubbed(:o_auth, is_enabled: true)
expect(o_auth).to be_valid
o_auth = FactoryBot.build_stubbed(:o_auth, is_enabled: false)
expect(o_auth).to be_valid
end
it 'has non-nil client credentials' do
o_auth = FactoryBot.build_stubbed(:o_auth, client_id: nil)
expect(o_auth).to be_invalid
o_auth = FactoryBot.build_stubbed(:o_auth, client_secret: nil)
expect(o_auth).to be_invalid
end
it 'has non-nil urls' do
o_auth = FactoryBot.build_stubbed(:o_auth, authorize_url: nil)
expect(o_auth).to be_invalid
o_auth = FactoryBot.build_stubbed(:o_auth, token_url: nil)
expect(o_auth).to be_invalid
o_auth = FactoryBot.build_stubbed(:o_auth, profile_url: nil)
expect(o_auth).to be_invalid
end
it 'has a non-nil scope' do
o_auth = FactoryBot.build_stubbed(:o_auth, scope: nil)
expect(o_auth).to be_invalid
end
it 'has a non-nil json user email path and a nullable name path' do
o_auth = FactoryBot.build_stubbed(:o_auth, json_user_email_path: nil)
expect(o_auth).to be_invalid
o_auth = FactoryBot.build_stubbed(:o_auth, json_user_name_path: nil)
expect(o_auth).to be_valid
end
end