omniauth-moloni 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 115c7065e361f52e80786ab9f0b2c83747d541003014a5bc6244dbbae4e4db50
4
+ data.tar.gz: 4d84fa28b39dcb037b64abaec860ebe35d4dab0fa7917ae667cd0acb19a624cd
5
+ SHA512:
6
+ metadata.gz: 2fdcdfb91a28444c9f243e29e8044a819c3b6dac9263c2dbd5db3a7839de26af4be14f3d9f5e95612d4e14dc2a73a3d65e6e69c534e7931507679600a3908d0b
7
+ data.tar.gz: afdc7f2501b9ebbb2cf20baade83b14b0a1e5b58f2d0f4c62464efc83a7d26e1b2a1ef0d2eb1c6b788008ddd298cadad16522a36a7e9f93db5fdd873c983336a
@@ -0,0 +1,26 @@
1
+ name: Ruby
2
+
3
+ on:
4
+ push:
5
+ branches: [ master ]
6
+ pull_request:
7
+ branches: [ master ]
8
+
9
+ jobs:
10
+ test:
11
+ runs-on: ubuntu-latest
12
+ strategy:
13
+ matrix:
14
+ ruby-version: ['2.4', '2.5', '2.6', '2.7', '3.0', '3.1', 'truffleruby-head']
15
+
16
+ steps:
17
+ - uses: actions/checkout@v2
18
+ - name: Set up Ruby ${{ matrix.ruby-version }}
19
+ uses: ruby/setup-ruby@v1
20
+ with:
21
+ ruby-version: ${{ matrix.ruby-version }}
22
+ - name: Build and test with Rake
23
+ run: |
24
+ gem install bundler
25
+ bundle install --jobs 4 --retry 3
26
+ bundle exec rake
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ /pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --colour
data/Dockerfile ADDED
@@ -0,0 +1,11 @@
1
+ FROM ruby:3.0
2
+
3
+ WORKDIR /usr/src/app
4
+
5
+ COPY Gemfile .
6
+ COPY omniauth-moloni.gemspec .
7
+ COPY lib/omniauth-moloni/version.rb lib/omniauth-moloni/version.rb
8
+
9
+ RUN bundle install
10
+
11
+ COPY . .
data/Gemfile ADDED
@@ -0,0 +1,13 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in omniauth-moloni.gemspec
4
+ gemspec
5
+
6
+ group :development, :test do
7
+ gem 'guard'
8
+ gem 'guard-rspec'
9
+ gem 'guard-bundler'
10
+ gem 'rb-fsevent'
11
+ gem 'growl'
12
+ gem 'rake'
13
+ end
data/Guardfile ADDED
@@ -0,0 +1,10 @@
1
+ guard 'rspec', :version => 2 do
2
+ watch(%r{^spec/.+_spec\.rb$})
3
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
4
+ watch('spec/spec_helper.rb') { "spec" }
5
+ end
6
+
7
+ guard 'bundler' do
8
+ watch('Gemfile')
9
+ watch('omniauth-moloni.gemspec')
10
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,7 @@
1
+ Copyright (c) 2022 Dinis Lage
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,61 @@
1
+ ![Ruby](https://github.com/dlage/omniauth-moloni/workflows/Ruby/badge.svg?branch=master)
2
+
3
+ # OmniAuth Moloni
4
+
5
+ This is an unofficial OmniAuth strategy for authenticating to Moloni. To
6
+ use it, you'll need to sign up for an OAuth2 Application ID and Secret
7
+ on the [Moloni Applications Page](https://www.moloni.pt/dev/autenticacao/).
8
+
9
+ ## Installation
10
+
11
+ ```ruby
12
+ gem 'omniauth-moloni', github: 'dlage/omniauth-moloni', branch: 'master'
13
+ ```
14
+
15
+ ## Basic Usage
16
+
17
+ ```ruby
18
+ use OmniAuth::Builder do
19
+ provider :moloni, ENV['MOLONI_KEY'], ENV['MOLONI_SECRET']
20
+ end
21
+ ```
22
+
23
+ ## Basic Usage Rails
24
+
25
+ In `config/initializers/moloni.rb`
26
+
27
+ ```ruby
28
+ Rails.application.config.middleware.use OmniAuth::Builder do
29
+ provider :moloni, ENV['MOLONI_KEY'], ENV['MOLONI_SECRET']
30
+ end
31
+ ```
32
+
33
+ ## Moloni Sandbox Usage
34
+
35
+ ```ruby
36
+ provider :moloni, ENV['MOLONI_KEY'], ENV['MOLONI_SECRET'],
37
+ {
38
+ :client_options => {
39
+ :site => 'https://api.moloni.pt/sandbox/',
40
+ :authorize_url => 'https://api.moloni.pt/sandbox/authorize/',
41
+ :token_url => 'https://api.moloni.pt/sandbox/grant/'
42
+ }
43
+ }
44
+ ```
45
+
46
+ ## License
47
+
48
+ Copyright (c) 2022 Dinis Lage
49
+
50
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
51
+ documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
52
+ rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit
53
+ persons to whom the Software is furnished to do so, subject to the following conditions:
54
+
55
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
56
+ Software.
57
+
58
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
59
+ WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
60
+ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
61
+ OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
3
+ require 'rspec/core/rake_task'
4
+
5
+ RSpec::Core::RakeTask.new
6
+
7
+ desc 'Run specs'
8
+ task :default => :spec
@@ -0,0 +1,7 @@
1
+ version: "3.9"
2
+ services:
3
+ app:
4
+ build: .
5
+ command: bash -c "rake"
6
+ volumes:
7
+ - .:/usr/src/app
@@ -0,0 +1,62 @@
1
+ require 'omniauth-oauth2'
2
+
3
+ module OmniAuth
4
+ module Strategies
5
+ class Moloni < OmniAuth::Strategies::OAuth2
6
+ option :client_options, {
7
+ :site => 'https://api.moloni.pt/v1/',
8
+ :authorize_url => 'https://api.moloni.pt/v1/authorize/',
9
+ :token_url => 'https://api.moloni.pt/v1/grant/'
10
+ }
11
+
12
+ def request_phase
13
+ super
14
+ end
15
+
16
+ def authorize_params
17
+ super.tap do |params|
18
+ %w[client_options].each do |v|
19
+ if request.params[v]
20
+ params[v.to_sym] = request.params[v]
21
+ end
22
+ end
23
+ end
24
+ end
25
+
26
+ uid { raw_info['user_id'].to_s }
27
+
28
+ info do
29
+ {
30
+ 'name' => raw_info['name'],
31
+ 'email' => raw_info['email'],
32
+ 'cellphone' => raw_info['cellphone'],
33
+ 'language_id' => raw_info['language_id'],
34
+ 'language' => raw_info['language'],
35
+ 'registered_since' => raw_info['registered_since'],
36
+ 'last_login' => raw_info['last_login'],
37
+ }
38
+ end
39
+
40
+ extra do
41
+ { :raw_info => raw_info }
42
+ end
43
+
44
+ def raw_info
45
+ access_token.options[:mode] = :header
46
+ @raw_info ||= access_token.get('users/getMe/').parsed
47
+ end
48
+
49
+ def email
50
+ raw_info['email']
51
+ end
52
+
53
+ def scope
54
+ access_token['scope']
55
+ end
56
+
57
+ def callback_url
58
+ full_host + callback_path
59
+ end
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,5 @@
1
+ module OmniAuth
2
+ module Moloni
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
@@ -0,0 +1,2 @@
1
+ require "omniauth-moloni/version"
2
+ require 'omniauth/strategies/moloni'
@@ -0,0 +1,25 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/omniauth-moloni/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Dinis Lage"]
6
+ gem.email = ["dinis@lage.pw"]
7
+ gem.description = %q{Unofficial OmniAuth strategy for Moloni.}
8
+ gem.summary = %q{Unofficial OmniAuth strategy for Moloni.}
9
+ gem.homepage = "https://github.com/dlage/omniauth-moloni"
10
+ gem.license = "MIT"
11
+
12
+ gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
13
+ gem.files = `git ls-files`.split("\n")
14
+ gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
15
+ gem.name = "omniauth-moloni"
16
+ gem.require_paths = ["lib"]
17
+ gem.version = OmniAuth::Moloni::VERSION
18
+
19
+ gem.add_dependency 'omniauth', '~> 2.0'
20
+ gem.add_dependency 'omniauth-oauth2', '~> 1.7.1'
21
+ gem.add_development_dependency 'rspec', '~> 3.5'
22
+ gem.add_development_dependency 'rack-test'
23
+ gem.add_development_dependency 'simplecov'
24
+ gem.add_development_dependency 'webmock'
25
+ end
@@ -0,0 +1,102 @@
1
+ require 'spec_helper'
2
+
3
+ describe OmniAuth::Strategies::Moloni do
4
+ let(:moloni_user_path) { 'users/getMe/' }
5
+ let(:access_token) { instance_double('AccessToken', :options => {}, :[] => moloni_user_path) }
6
+ let(:parsed_response) { instance_double('ParsedResponse') }
7
+ let(:response) { instance_double('Response', :parsed => parsed_response) }
8
+
9
+ let(:sandbox_site) { 'https://api.moloni.pt/sandbox/' }
10
+ let(:sandbox_authorize_url) { 'https://api.moloni.pt/sandbox/authorize/' }
11
+ let(:sandbox_token_url) { 'https://api.moloni.pt/sandbox/grant/' }
12
+ let(:sandbox) do
13
+ OmniAuth::Strategies::Moloni.new(
14
+ 'MOLONI_KEY', 'MOLONI_SECRET',
15
+ {
16
+ :client_options => {
17
+ :site => sandbox_site,
18
+ :authorize_url => sandbox_authorize_url,
19
+ :token_url => sandbox_token_url
20
+ }
21
+ }
22
+ )
23
+ end
24
+
25
+ subject do
26
+ OmniAuth::Strategies::Moloni.new({})
27
+ end
28
+
29
+ before(:each) do
30
+ allow(subject).to receive(:access_token).and_return(access_token)
31
+ end
32
+
33
+ context 'client options' do
34
+ it 'should have correct site' do
35
+ expect(subject.options.client_options.site).to eq('https://api.moloni.pt/v1/')
36
+ end
37
+
38
+ it 'should have correct authorize url' do
39
+ expect(subject.options.client_options.authorize_url).to eq('https://api.moloni.pt/v1/authorize/')
40
+ end
41
+
42
+ it 'should have correct token url' do
43
+ expect(subject.options.client_options.token_url).to eq('https://api.moloni.pt/v1/grant/')
44
+ end
45
+
46
+ describe 'should be overrideable' do
47
+ it 'for site' do
48
+ expect(sandbox.options.client_options.site).to eq(sandbox_site)
49
+ end
50
+
51
+ it 'for authorize url' do
52
+ expect(sandbox.options.client_options.authorize_url).to eq(sandbox_authorize_url)
53
+ end
54
+
55
+ it 'for token url' do
56
+ expect(sandbox.options.client_options.token_url).to eq(sandbox_token_url)
57
+ end
58
+ end
59
+ end
60
+
61
+ context '#email' do
62
+ it 'should return email from raw_info if available' do
63
+ allow(subject).to receive(:raw_info).and_return({ 'email' => 'you@example.com' })
64
+ expect(subject.email).to eq('you@example.com')
65
+ end
66
+
67
+ it 'should return nil if there is no raw_info and email access is not allowed' do
68
+ allow(subject).to receive(:raw_info).and_return({})
69
+ expect(subject.email).to be_nil
70
+ end
71
+ end
72
+
73
+ context '#raw_info' do
74
+ it 'should use relative paths' do
75
+ expect(access_token).to receive(:get).with(moloni_user_path).and_return(response)
76
+ expect(subject.raw_info).to eq(parsed_response)
77
+ end
78
+
79
+ it 'should use the header auth mode' do
80
+ expect(access_token).to receive(:get).with(moloni_user_path).and_return(response)
81
+ subject.raw_info
82
+ expect(access_token.options[:mode]).to eq(:header)
83
+ end
84
+ end
85
+
86
+ context '#info.email' do
87
+ it 'should use any available email' do
88
+ allow(subject).to receive(:raw_info).and_return({ 'email' => 'you@example.com' })
89
+ allow(subject).to receive(:email).and_return('you@example.com')
90
+ expect(subject.info['email']).to eq('you@example.com')
91
+ end
92
+ end
93
+
94
+ describe '#callback_url' do
95
+ it 'is a combination of host, script name, and callback path' do
96
+ allow(subject).to receive(:full_host).and_return('https://example.com')
97
+ allow(subject).to receive(:script_name).and_return('/sub_uri')
98
+
99
+ expect(subject.callback_url).to eq('https://example.com/sub_uri/auth/moloni/callback')
100
+ end
101
+ end
102
+ end
@@ -0,0 +1,15 @@
1
+ $:.unshift File.expand_path('..', __FILE__)
2
+ $:.unshift File.expand_path('../../lib', __FILE__)
3
+ require 'simplecov'
4
+ SimpleCov.start
5
+ require 'rspec'
6
+ require 'rack/test'
7
+ require 'webmock/rspec'
8
+ require 'omniauth'
9
+ require 'omniauth-moloni'
10
+
11
+ RSpec.configure do |config|
12
+ config.include WebMock::API
13
+ config.include Rack::Test::Methods
14
+ config.extend OmniAuth::Test::StrategyMacros, :type => :strategy
15
+ end
metadata ADDED
@@ -0,0 +1,145 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: omniauth-moloni
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Dinis Lage
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2022-08-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: omniauth
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: omniauth-oauth2
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 1.7.1
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 1.7.1
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.5'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.5'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rack-test
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: simplecov
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: webmock
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description: Unofficial OmniAuth strategy for Moloni.
98
+ email:
99
+ - dinis@lage.pw
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".github/workflows/ruby.yml"
105
+ - ".gitignore"
106
+ - ".rspec"
107
+ - Dockerfile
108
+ - Gemfile
109
+ - Guardfile
110
+ - LICENSE.txt
111
+ - README.md
112
+ - Rakefile
113
+ - docker-compose.yml
114
+ - lib/omniauth-moloni.rb
115
+ - lib/omniauth-moloni/version.rb
116
+ - lib/omniauth/strategies/moloni.rb
117
+ - omniauth-moloni.gemspec
118
+ - spec/omniauth/strategies/moloni_spec.rb
119
+ - spec/spec_helper.rb
120
+ homepage: https://github.com/dlage/omniauth-moloni
121
+ licenses:
122
+ - MIT
123
+ metadata: {}
124
+ post_install_message:
125
+ rdoc_options: []
126
+ require_paths:
127
+ - lib
128
+ required_ruby_version: !ruby/object:Gem::Requirement
129
+ requirements:
130
+ - - ">="
131
+ - !ruby/object:Gem::Version
132
+ version: '0'
133
+ required_rubygems_version: !ruby/object:Gem::Requirement
134
+ requirements:
135
+ - - ">="
136
+ - !ruby/object:Gem::Version
137
+ version: '0'
138
+ requirements: []
139
+ rubygems_version: 3.0.3.1
140
+ signing_key:
141
+ specification_version: 4
142
+ summary: Unofficial OmniAuth strategy for Moloni.
143
+ test_files:
144
+ - spec/omniauth/strategies/moloni_spec.rb
145
+ - spec/spec_helper.rb