omniauth-fortnox-oauth2 0.0.1

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: a4022f4d3f910ee1f59a350edebc056f1dd60b93c91895b93ce703757b163f57
4
+ data.tar.gz: 186a3c2d187f38b97509e8b4594640104d8c70435b5e48aff812b433ae69b49f
5
+ SHA512:
6
+ metadata.gz: 329f0f93ccb9daedc67ff40083b336dd3b9ca973256391805dec85693aee894b9c44338f52e98f4e5264a265597b8332939f9b135cea77630b592eb800bd9130
7
+ data.tar.gz: 8e77814e839af30c89b41823255372ea40d0a5a97dc5bafca6b955b270d50dac5fed587f687458b4869424fa1946fb0846695628d35574e5ca30dd48619cf79b
@@ -0,0 +1,41 @@
1
+ name: Gem push
2
+
3
+ on:
4
+ release:
5
+ types: [published]
6
+
7
+ jobs:
8
+ github:
9
+ name: Build + Publish to GitHub
10
+ runs-on: ubuntu-latest
11
+ steps:
12
+ - uses: actions/checkout@v2
13
+ - name: Set up Ruby 3.0.0
14
+ uses: actions/setup-ruby@v1
15
+ with:
16
+ version: 3.0.0
17
+ - name: Publish to GitHub
18
+ uses: jstastny/publish-gem-to-github@master
19
+ with:
20
+ token: ${{ secrets.GITHUB_TOKEN }}
21
+ owner: standout
22
+
23
+ rubygems:
24
+ name: Build + Publish to Rubygems
25
+ runs-on: ubuntu-latest
26
+ steps:
27
+ - uses: actions/checkout@v2
28
+ - name: Set up Ruby 3.0.0
29
+ uses: actions/setup-ruby@v1
30
+ with:
31
+ version: 3.0.0
32
+ - name: Publish to RubyGems
33
+ run: |
34
+ mkdir -p $HOME/.gem
35
+ touch $HOME/.gem/credentials
36
+ chmod 0600 $HOME/.gem/credentials
37
+ printf -- "---\n:rubygems_api_key: ${GEM_HOST_API_KEY}\n" > $HOME/.gem/credentials
38
+ gem build *.gemspec
39
+ gem push *.gem
40
+ env:
41
+ GEM_HOST_API_KEY: ${{secrets.RUBYGEMS_AUTH_TOKEN}}
@@ -0,0 +1,18 @@
1
+ name: Ruby
2
+
3
+ on: [pull_request]
4
+
5
+ jobs:
6
+ build:
7
+ runs-on: ubuntu-latest
8
+ steps:
9
+ - uses: actions/checkout@v2
10
+ - name: Set up Ruby
11
+ uses: ruby/setup-ruby@v1
12
+ with:
13
+ ruby-version: 3.0.0
14
+ - name: Run the default task
15
+ run: |
16
+ gem install bundler -v 2.2.3
17
+ bundle install
18
+ bundle exec rake
data/.gitignore ADDED
@@ -0,0 +1,2 @@
1
+ .DS_Store
2
+ app/
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 3.0.0
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ # Specify your gem's dependencies in omniauth-flattr.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,98 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ omniauth-fortnox-oauth2 (0.0.1)
5
+ omniauth-oauth2
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ addressable (2.8.0)
11
+ public_suffix (>= 2.0.2, < 5.0)
12
+ byebug (11.1.3)
13
+ coderay (1.1.3)
14
+ crack (0.4.5)
15
+ rexml
16
+ diff-lcs (1.4.4)
17
+ faraday (1.8.0)
18
+ faraday-em_http (~> 1.0)
19
+ faraday-em_synchrony (~> 1.0)
20
+ faraday-excon (~> 1.1)
21
+ faraday-httpclient (~> 1.0.1)
22
+ faraday-net_http (~> 1.0)
23
+ faraday-net_http_persistent (~> 1.1)
24
+ faraday-patron (~> 1.0)
25
+ faraday-rack (~> 1.0)
26
+ multipart-post (>= 1.2, < 3)
27
+ ruby2_keywords (>= 0.0.4)
28
+ faraday-em_http (1.0.0)
29
+ faraday-em_synchrony (1.0.0)
30
+ faraday-excon (1.1.0)
31
+ faraday-httpclient (1.0.1)
32
+ faraday-net_http (1.0.1)
33
+ faraday-net_http_persistent (1.2.0)
34
+ faraday-patron (1.0.0)
35
+ faraday-rack (1.0.0)
36
+ hashdiff (1.0.1)
37
+ hashie (4.1.0)
38
+ jwt (2.2.3)
39
+ method_source (1.0.0)
40
+ multi_json (1.15.0)
41
+ multi_xml (0.6.0)
42
+ multipart-post (2.1.1)
43
+ oauth2 (1.4.7)
44
+ faraday (>= 0.8, < 2.0)
45
+ jwt (>= 1.0, < 3.0)
46
+ multi_json (~> 1.3)
47
+ multi_xml (~> 0.5)
48
+ rack (>= 1.2, < 3)
49
+ omniauth (2.0.4)
50
+ hashie (>= 3.4.6)
51
+ rack (>= 1.6.2, < 3)
52
+ rack-protection
53
+ omniauth-oauth2 (1.7.1)
54
+ oauth2 (~> 1.4)
55
+ omniauth (>= 1.9, < 3)
56
+ pry (0.13.1)
57
+ coderay (~> 1.1)
58
+ method_source (~> 1.0)
59
+ pry-byebug (3.9.0)
60
+ byebug (~> 11.0)
61
+ pry (~> 0.13.0)
62
+ public_suffix (4.0.6)
63
+ rack (2.2.3)
64
+ rack-protection (2.1.0)
65
+ rack
66
+ rake (13.0.6)
67
+ rexml (3.2.5)
68
+ rspec (3.10.0)
69
+ rspec-core (~> 3.10.0)
70
+ rspec-expectations (~> 3.10.0)
71
+ rspec-mocks (~> 3.10.0)
72
+ rspec-core (3.10.1)
73
+ rspec-support (~> 3.10.0)
74
+ rspec-expectations (3.10.1)
75
+ diff-lcs (>= 1.2.0, < 2.0)
76
+ rspec-support (~> 3.10.0)
77
+ rspec-mocks (3.10.2)
78
+ diff-lcs (>= 1.2.0, < 2.0)
79
+ rspec-support (~> 3.10.0)
80
+ rspec-support (3.10.2)
81
+ ruby2_keywords (0.0.5)
82
+ webmock (3.14.0)
83
+ addressable (>= 2.8.0)
84
+ crack (>= 0.3.2)
85
+ hashdiff (>= 0.4.0, < 2.0.0)
86
+
87
+ PLATFORMS
88
+ x86_64-darwin-19
89
+
90
+ DEPENDENCIES
91
+ omniauth-fortnox-oauth2!
92
+ pry-byebug
93
+ rake
94
+ rspec (> 3)
95
+ webmock
96
+
97
+ BUNDLED WITH
98
+ 2.2.3
data/README.md ADDED
@@ -0,0 +1,130 @@
1
+ # OmniAuth Fortnox OAuth2 Strategy
2
+
3
+ Strategy to authenticate with Fortnox via OAuth2 in OmniAuth.
4
+
5
+ You will need to create your app in order to get `Client-ID` and `Client-Secret`, read more here: [Fortnox](https://developer.fortnox.se/get-started-details/#create-your-app)
6
+
7
+ For more details, read the Fortnox docs: [Fortnox Developer](https://developer.fortnox.se/general/authentication/)
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'omniauth-fortnox-oauth2'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle install
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install omniauth-fortnox-oauth2
24
+
25
+ ## Usage
26
+
27
+ Here's an example for adding the middleware to a Rails app in config/initializers/omniauth.rb:
28
+
29
+ ```ruby
30
+ provider :fortnox_oauth2,
31
+ 'client-id',
32
+ 'client-secret',
33
+ scope: 'companyinformation',
34
+ ```
35
+
36
+ Can also be set up with dynamic configuration:
37
+
38
+ ```ruby
39
+ provider :fortnox_oauth2,
40
+ setup: (lambda do |env|
41
+ # The following can be set dynamically from params, session, or ENV
42
+
43
+ env['omniauth.strategy'].options[:client_id] = 'client-id'
44
+ env['omniauth.strategy'].options[:client_secret] = 'client-secret'
45
+ env['omniauth.strategy'].options[:scope] = 'companyinformation'
46
+ end)
47
+ ```
48
+
49
+
50
+ You can now access the OmniAuth Fortnox OAuth2 URL: /auth/fortnox_oauth2
51
+ Later a controller can be set up to handle the response after authentication, for example:
52
+
53
+ ```ruby
54
+ get '/auth/fortnox_oauth2/callback', to: 'auth/fortnox_oauth2#callback'
55
+ ```
56
+
57
+ ## Configuration
58
+
59
+ You can configure several options, which you pass in to the provider method via a hash:
60
+
61
+ * `scope`: A comma-separated list of permissions you want to request from the user. See the [Fortnox](https://developer.fortnox.se/general/scopes/) for a full list of available permissions. Caveats:
62
+ * Note that you app will need the same scopes! The scope `companyinformation` is used by default. By defining your own `scope`, you override these defaults.
63
+
64
+ * `callback_url`: Override the callback_url used by the gem.
65
+
66
+ You can also configure the `client_options` by passing in any of the following settings in a `client_options` hash, inside options.
67
+
68
+ * `site`: Override the site used by the gem, default: `https://apps.fortnox.se`.
69
+
70
+ * `token_url`: Override the token_url used by the gem, default: `/oauth-v1/token`.
71
+
72
+ * `authorize_url`: Override the authorize_url used by the gem, default `/oauth-v1/auth`.
73
+
74
+ * `auth_scheme`: Override the auth_scheme used by the gem, default `:basic_auth`.
75
+
76
+ * `token_method`: Override the token_method used by the gem, default `:post`.
77
+
78
+ ## Auth Hash
79
+
80
+ Here's an example of an authentication hash available in the callback by accessing `request.env['omniauth.auth']`:
81
+
82
+ ```ruby
83
+ {
84
+ "provider" => "fortnox_oauth2",
85
+ "uid" => "556469-6291",
86
+ "info" => {
87
+ "address" => "Bollvägen",
88
+ "city" => "Växjö",
89
+ "country_code" => "SE",
90
+ "database_number" => "654896",
91
+ "company_name" => "Fortnox",
92
+ "organization_number" => "556469-6291",
93
+ "zip_code" => "35246"
94
+ },
95
+ "credentials" => {
96
+ "token" => "TOKEN",
97
+ "refresh_token" => "REFRESH_TOKEN",
98
+ "expires_at" => 1496120719,
99
+ "expires" => true
100
+ },
101
+ "extra" => {
102
+ "raw_info" => {
103
+ "Address" => "Bollvägen",
104
+ "City" => "Växjö",
105
+ "CountryCode" => "SE",
106
+ "DatabaseNumber" => "654896",
107
+ "CompanyName" => "Fortnox",
108
+ "OrganizationNumber" => "556469-6291",
109
+ "VisitAddress" => "",
110
+ "VisitCity" => "",
111
+ "VisitZipCode" => "",
112
+ "ZipCode" => "35246",
113
+ }
114
+ }
115
+ }
116
+ ```
117
+
118
+ ## Development
119
+
120
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
121
+
122
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
123
+
124
+ ## Contributing
125
+
126
+ Bug reports and pull requests are welcome on GitHub at https://github.com/standout/omniauth-fortnox-oauth2.
127
+
128
+ ## License
129
+
130
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ # !/usr/bin/env rake
4
+
5
+ require 'bundler/gem_tasks'
6
+ require 'rspec/core/rake_task'
7
+
8
+ desc 'Default: run specs.'
9
+ task default: :spec
10
+
11
+ desc 'Run specs'
12
+ RSpec::Core::RakeTask.new
13
+
14
+ desc 'Run specs'
15
+ task default: :spec
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/setup'
5
+ require 'omniauth-fortnox'
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require 'irb'
15
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'net/http'
4
+ require 'net/https'
5
+ require 'uri'
6
+
7
+ module OmniAuth
8
+ module FortnoxOAuth2
9
+ # API
10
+ class API
11
+ class Error < StandardError; end
12
+
13
+ def initialize(token)
14
+ @access_token = token
15
+ @base_uri = 'https://api.fortnox.se/3'
16
+ end
17
+
18
+ def get(url)
19
+ uri = URI([@base_uri, url].join(''))
20
+ http = Net::HTTP.new(uri.host, uri.port)
21
+ http.use_ssl = true
22
+
23
+ request = build_request(uri)
24
+ response = http.request(request)
25
+ raise Error, "#{response.code}: #{response.body}" unless response.code == '200'
26
+
27
+ JSON.parse(response.body)
28
+ end
29
+
30
+ private
31
+
32
+ def build_request(uri)
33
+ request = Net::HTTP::Get.new(uri)
34
+ request['Authorization'] = "Bearer #{@access_token}"
35
+ request['Content-Type'] = 'application/json'
36
+ request['Accept'] = 'application/json'
37
+
38
+ request
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Omniauth
4
+ module FortnoxOAuth2
5
+ VERSION = '0.0.1'
6
+ end
7
+ end
@@ -0,0 +1,88 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'omniauth-oauth2'
4
+
5
+ module OmniAuth
6
+ module Strategies
7
+ # Fortnox
8
+ class FortnoxOauth2 < OmniAuth::Strategies::OAuth2
9
+ DEFAULT_SCOPE = 'companyinformation'
10
+
11
+ option :name, 'fortnox_oauth2'
12
+
13
+ option :client_options, {
14
+ site: 'https://apps.fortnox.se',
15
+ token_url: '/oauth-v1/token',
16
+ authorize_url: '/oauth-v1/auth',
17
+ auth_scheme: :basic_auth,
18
+ token_method: :post
19
+ }
20
+
21
+ option :authorize_options, %i[scope state]
22
+ option :provider_ignores_state, false
23
+
24
+ uid { raw_info['CompanyInformation']['OrganizationNumber'] }
25
+
26
+ info do
27
+ prune!(
28
+ 'address' => raw_info['CompanyInformation']['Address'],
29
+ 'city' => raw_info['CompanyInformation']['City'],
30
+ 'country_code' => raw_info['CompanyInformation']['CountryCode'],
31
+ 'database_number' => raw_info['CompanyInformation']['DatabaseNumber'],
32
+ 'company_name' => raw_info['CompanyInformation']['CompanyName'],
33
+ 'organization_number' => raw_info['CompanyInformation']['OrganizationNumber'],
34
+ 'visit_address' => raw_info['CompanyInformation']['VisitAddress'],
35
+ 'visit_city' => raw_info['CompanyInformation']['VisitCity'],
36
+ 'visit_country_code' => raw_info['CompanyInformation']['VisitCountryCode'],
37
+ 'visit_zip_code' => raw_info['CompanyInformation']['VisitZipCode'],
38
+ 'zip_code' => raw_info['CompanyInformation']['ZipCode']
39
+ )
40
+ end
41
+
42
+ extra do
43
+ hash = {}
44
+ hash['raw_info'] = raw_info unless skip_info?
45
+ prune! hash
46
+ end
47
+
48
+ def raw_info
49
+ return if access_token.blank?
50
+
51
+ api_client = OmniAuth::FortnoxOAuth2::API.new(access_token&.token)
52
+ @raw_info ||= api_client.get('/companyinformation')
53
+ end
54
+
55
+ def request_phase
56
+ options[:authorize_params] = {
57
+ client_id: options['client_id'],
58
+ response_type: 'code',
59
+ scope: (options['scope'] || DEFAULT_SCOPE)
60
+ }
61
+
62
+ super
63
+ end
64
+
65
+ def callback_url
66
+ options[:callback_url] || (full_host + script_name + callback_path)
67
+ end
68
+
69
+ def build_access_token
70
+ verifier = request.params['code']
71
+ client.auth_code.get_token(
72
+ verifier,
73
+ { redirect_uri: callback_url }.merge(token_params.to_hash(symbolize_keys: true)),
74
+ deep_symbolize(options.auth_token_params)
75
+ )
76
+ end
77
+
78
+ private
79
+
80
+ def prune!(hash)
81
+ hash.delete_if do |_, value|
82
+ prune!(value) if value.is_a?(Hash)
83
+ value.nil? || (value.respond_to?(:empty?) && value.empty?)
84
+ end
85
+ end
86
+ end
87
+ end
88
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'omniauth/fortnox_oauth2/version'
4
+ require 'omniauth/fortnox_oauth2/api'
5
+ require 'omniauth/strategies/fortnox_oauth2'
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ $LOAD_PATH.push File.expand_path('lib', __dir__)
4
+ require 'omniauth/fortnox_oauth2/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = 'omniauth-fortnox-oauth2'
8
+ gem.version = Omniauth::FortnoxOAuth2::VERSION
9
+ gem.authors = ['svenne87']
10
+ gem.email = ['devops@standout.se']
11
+ gem.homepage = 'https://github.com/standout/omniauth-fortnox-oauth2'
12
+ gem.description = 'OmniAuth OAuth2 strategy for Fortnox'
13
+ gem.summary = gem.description
14
+ gem.licenses = ['MIT']
15
+
16
+ gem.metadata['homepage_uri'] = gem.homepage
17
+ gem.metadata['source_code_uri'] = gem.homepage
18
+ gem.metadata['changelog_uri'] = gem.homepage
19
+
20
+ gem.files = `git ls-files`.split($\)
21
+ gem.executables = gem.files.grep(%r{^bin/}).map { |f| File.basename(f) }
22
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
23
+ gem.require_paths = ['lib']
24
+
25
+ gem.required_ruby_version = '~> 3.0'
26
+
27
+ gem.add_dependency 'omniauth-oauth2'
28
+
29
+ gem.add_development_dependency 'pry-byebug'
30
+ gem.add_development_dependency 'rake'
31
+ gem.add_development_dependency 'rspec', '> 3'
32
+ gem.add_development_dependency 'webmock'
33
+ end
data/spec/helper.rb ADDED
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ $LOAD_PATH.unshift File.expand_path(__dir__)
4
+ $LOAD_PATH.unshift File.expand_path('../lib', __dir__)
5
+
6
+ require 'rspec'
7
+ require 'webmock/rspec'
8
+ require 'omniauth'
9
+ require 'omniauth-fortnox-oauth2'
10
+
11
+ RSpec.configure do |config|
12
+ config.expect_with :rspec do |c|
13
+ c.syntax = :expect
14
+ end
15
+ config.extend OmniAuth::Test::StrategyMacros, type: :strategy
16
+ config.include WebMock::API
17
+ end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'helper'
4
+
5
+ describe OmniAuth::FortnoxOAuth2::API do
6
+ subject(:api) { OmniAuth::FortnoxOAuth2::API.new(token) }
7
+
8
+ let(:token) { 'abc' }
9
+
10
+ describe '#get' do
11
+ let(:headers) do
12
+ {
13
+ 'Accept' => 'application/json',
14
+ 'Authorization' => 'Bearer abc',
15
+ 'Content-Type' => 'application/json'
16
+ }
17
+ end
18
+
19
+ before do
20
+ stub_request(:get, 'https://api.fortnox.se/3/companyinformation')
21
+ .with(
22
+ headers: headers
23
+ ).to_return(status: 200, body: { 'x' => 'o' }.to_json, headers: {})
24
+ end
25
+
26
+ it 'performs the OmniAuth::Strategy included hook' do
27
+ api.get('/companyinformation')
28
+ expect(a_request(:get, 'https://api.fortnox.se/3/companyinformation'))
29
+ .to have_been_made
30
+ .once
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,292 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'helper'
4
+
5
+ describe OmniAuth::Strategies::FortnoxOauth2 do
6
+ def app
7
+ lambda do |_env|
8
+ [200, {}, ['Hello.']]
9
+ end
10
+ end
11
+
12
+ subject(:fortnox_oauth2) do
13
+ OmniAuth::Strategies::FortnoxOauth2.new(*args)
14
+ end
15
+
16
+ let(:args) { ['client_id', 'client_secret', options] }
17
+ let(:options) { { callback_url: 'https://example.com/callback' } }
18
+
19
+ before do
20
+ OmniAuth.config.test_mode = true
21
+ end
22
+
23
+ after do
24
+ OmniAuth.config.test_mode = false
25
+ end
26
+
27
+ describe 'Subclassing Behavior' do
28
+ it 'performs the OmniAuth::Strategy included hook' do
29
+ expect(OmniAuth.strategies)
30
+ .to include(OmniAuth::Strategies::FortnoxOauth2)
31
+ end
32
+ end
33
+
34
+ describe '#client' do
35
+ context 'client options' do
36
+ let(:options) { { client_options: client_options } }
37
+ let(:client_options) { {} }
38
+
39
+ it 'has the correct name' do
40
+ expect(fortnox_oauth2.options.name).to eq('fortnox_oauth2')
41
+ end
42
+
43
+ context 'when using default settings' do
44
+ it 'has the default site set' do
45
+ expect(fortnox_oauth2.options.client_options.site)
46
+ .to eq('https://apps.fortnox.se')
47
+ end
48
+
49
+ it 'has the default token_url set' do
50
+ expect(fortnox_oauth2.options.client_options.token_url)
51
+ .to eq('/oauth-v1/token')
52
+ end
53
+
54
+ it 'has the default authorize_url set' do
55
+ expect(fortnox_oauth2.options.client_options.authorize_url)
56
+ .to eq('/oauth-v1/auth')
57
+ end
58
+
59
+ it 'has the default token_method set' do
60
+ expect(fortnox_oauth2.options.client_options.token_method)
61
+ .to eq(:post)
62
+ end
63
+
64
+ it 'has the default auth_scheme set' do
65
+ expect(fortnox_oauth2.options.client_options.auth_scheme)
66
+ .to eq(:basic_auth)
67
+ end
68
+ end
69
+
70
+ context 'when changing client options' do
71
+ let(:client_options) do
72
+ {
73
+ site: 'https://www.example.com',
74
+ token_url: '/oauth-v2/token',
75
+ authorize_url: '/oauth-v2/auth',
76
+ auth_scheme: :request_body,
77
+ token_method: :get
78
+ }
79
+ end
80
+
81
+ it 'has the passed site set' do
82
+ expect(fortnox_oauth2.options.client_options.site)
83
+ .to eq('https://www.example.com')
84
+ end
85
+
86
+ it 'has the passed token_url set' do
87
+ expect(fortnox_oauth2.options.client_options.token_url)
88
+ .to eq('/oauth-v2/token')
89
+ end
90
+
91
+ it 'has the passed authorize_url set' do
92
+ expect(fortnox_oauth2.options.client_options.authorize_url)
93
+ .to eq('/oauth-v2/auth')
94
+ end
95
+
96
+ it 'has the passed auth_scheme set' do
97
+ expect(fortnox_oauth2.options.client_options.auth_scheme)
98
+ .to eq(:request_body)
99
+ end
100
+
101
+ it 'has the passed token_method set' do
102
+ expect(fortnox_oauth2.options.client_options.token_method)
103
+ .to eq(:get)
104
+ end
105
+ end
106
+ end
107
+ end
108
+
109
+ describe '#callback_phase' do
110
+ context 'when an error occurs' do
111
+ let(:error_request) do
112
+ double(
113
+ 'Request',
114
+ params: { 'error_reason' => 'user_denied', 'error' => 'access_denied' }
115
+ )
116
+ end
117
+
118
+ before do
119
+ allow(fortnox_oauth2).to receive(:request) { error_request }
120
+ end
121
+
122
+ it 'calls fail with the client error received' do
123
+ expect(fortnox_oauth2)
124
+ .to receive(:fail!)
125
+ .with('user_denied', anything)
126
+
127
+ fortnox_oauth2.callback_phase
128
+ end
129
+ end
130
+ end
131
+
132
+ describe '#uid' do
133
+ before do
134
+ allow(fortnox_oauth2)
135
+ .to receive(:raw_info)
136
+ .and_return({ 'CompanyInformation' => { 'OrganizationNumber' => '555555-5555' } })
137
+ end
138
+
139
+ it 'sets the correct uid from companyinformation organization number' do
140
+ expect(fortnox_oauth2.uid).to eq('555555-5555')
141
+ end
142
+ end
143
+
144
+ describe '#credentials' do
145
+ let(:access_token) do
146
+ double(
147
+ 'OAuth2::AccessToken',
148
+ token: 'abc',
149
+ refresh_token: 'cde',
150
+ expires_at: 1_632_830_458,
151
+ expires?: true
152
+ )
153
+ end
154
+
155
+ before do
156
+ allow(fortnox_oauth2).to receive(:access_token).and_return(access_token)
157
+ end
158
+
159
+ it 'returns the correct access token' do
160
+ expect(fortnox_oauth2.credentials['token']).to eq('abc')
161
+ end
162
+
163
+ it 'returns the correct refresh token' do
164
+ expect(fortnox_oauth2.credentials['refresh_token']).to eq('cde')
165
+ end
166
+
167
+ it 'returns the correct expires at' do
168
+ expect(fortnox_oauth2.credentials['expires_at']).to eq(1_632_830_458)
169
+ end
170
+ end
171
+
172
+ describe '#info' do
173
+ let(:raw_info) do
174
+ {
175
+ 'CompanyInformation' => {
176
+ 'Address' => 'Bollvägen',
177
+ 'City' => 'Växjö',
178
+ 'CountryCode' => 'SE',
179
+ 'DatabaseNumber' => '654896',
180
+ 'CompanyName' => 'Fortnox',
181
+ 'OrganizationNumber' => '555555-5555',
182
+ 'VisitAddress' => '',
183
+ 'VisitCity' => '',
184
+ 'VisitCountryCode' => '',
185
+ 'VisitZipCode' => '',
186
+ 'ZipCode' => '35246'
187
+ }
188
+ }
189
+ end
190
+
191
+ context 'with formatted info' do
192
+ before do
193
+ allow(fortnox_oauth2).to receive(:raw_info).and_return(raw_info)
194
+ end
195
+
196
+ it 'return info without blank values' do
197
+ expect(fortnox_oauth2.info).to eq(
198
+ {
199
+ 'address' => 'Bollvägen',
200
+ 'city' => 'Växjö',
201
+ 'country_code' => 'SE',
202
+ 'database_number' => '654896',
203
+ 'company_name' => 'Fortnox',
204
+ 'organization_number' => '555555-5555',
205
+ 'zip_code' => '35246'
206
+ }
207
+ )
208
+ end
209
+ end
210
+
211
+ context 'when fetchin API data' do
212
+ let(:api_client) do
213
+ instance_double(OmniAuth::FortnoxOAuth2::API, get: nil)
214
+ end
215
+
216
+ before do
217
+ allow(fortnox_oauth2)
218
+ .to receive(:access_token)
219
+ .and_return(double('Bogus', token: 'abc', blank?: false))
220
+
221
+ allow(OmniAuth::FortnoxOAuth2::API)
222
+ .to receive(:new)
223
+ .and_return(api_client)
224
+ end
225
+
226
+ it 'calls to fetch data from /companyinformation' do
227
+ expect(api_client)
228
+ .to receive(:get)
229
+ .with('/companyinformation')
230
+ .and_return(raw_info)
231
+
232
+ fortnox_oauth2.info
233
+ end
234
+ end
235
+ end
236
+
237
+ describe '#callback_url' do
238
+ context 'when not set in options' do
239
+ let(:options) { { 'callback_path' => '/callback' } }
240
+ let(:request) do
241
+ double(
242
+ 'Request',
243
+ scheme: 'https',
244
+ url: '1234',
245
+ params: { 'url' => '1234' }
246
+ )
247
+ end
248
+
249
+ before do
250
+ allow(fortnox_oauth2).to receive(:request) { request }
251
+ allow(fortnox_oauth2).to receive(:script_name).and_return('')
252
+ end
253
+
254
+ it 'returns correct url from current app' do
255
+ expect(fortnox_oauth2.callback_url).to eq('/callback')
256
+ end
257
+ end
258
+
259
+ context 'when set in options' do
260
+ it 'returns correct url from callback_url option' do
261
+ expect(fortnox_oauth2.callback_url)
262
+ .to eq('https://example.com/callback')
263
+ end
264
+ end
265
+ end
266
+
267
+ describe '#request_phase' do
268
+ before do
269
+ allow(fortnox_oauth2).to receive(:callback_url).and_return('')
270
+
271
+ fortnox_oauth2.request_phase
272
+ end
273
+
274
+ it 'includes the default scope' do
275
+ expect(fortnox_oauth2.authorize_params[:scope])
276
+ .to eq('companyinformation')
277
+ end
278
+
279
+ it 'includes the response type' do
280
+ expect(fortnox_oauth2.authorize_params[:response_type]).to eq('code')
281
+ end
282
+
283
+ context 'when setting scope in options' do
284
+ let(:options) { { 'scope' => 'companyinformation,invoice' } }
285
+
286
+ it 'uses the new scope' do
287
+ expect(fortnox_oauth2.authorize_params[:scope])
288
+ .to eq('companyinformation,invoice')
289
+ end
290
+ end
291
+ end
292
+ end
metadata ADDED
@@ -0,0 +1,139 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: omniauth-fortnox-oauth2
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - svenne87
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-09-28 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: omniauth-oauth2
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: pry-byebug
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">"
60
+ - !ruby/object:Gem::Version
61
+ version: '3'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">"
67
+ - !ruby/object:Gem::Version
68
+ version: '3'
69
+ - !ruby/object:Gem::Dependency
70
+ name: webmock
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
+ description: OmniAuth OAuth2 strategy for Fortnox
84
+ email:
85
+ - devops@standout.se
86
+ executables:
87
+ - console
88
+ - setup
89
+ extensions: []
90
+ extra_rdoc_files: []
91
+ files:
92
+ - ".github/workflows/gempush.yml"
93
+ - ".github/workflows/main.yml"
94
+ - ".gitignore"
95
+ - ".ruby-version"
96
+ - Gemfile
97
+ - Gemfile.lock
98
+ - README.md
99
+ - Rakefile
100
+ - bin/console
101
+ - bin/setup
102
+ - lib/omniauth-fortnox-oauth2.rb
103
+ - lib/omniauth/fortnox_oauth2/api.rb
104
+ - lib/omniauth/fortnox_oauth2/version.rb
105
+ - lib/omniauth/strategies/fortnox_oauth2.rb
106
+ - omniauth-fortnox-oauth2.gemspec
107
+ - spec/helper.rb
108
+ - spec/omniauth/fortnox_oauth/api_spec.rb
109
+ - spec/strategies/fortnox_spec.rb
110
+ homepage: https://github.com/standout/omniauth-fortnox-oauth2
111
+ licenses:
112
+ - MIT
113
+ metadata:
114
+ homepage_uri: https://github.com/standout/omniauth-fortnox-oauth2
115
+ source_code_uri: https://github.com/standout/omniauth-fortnox-oauth2
116
+ changelog_uri: https://github.com/standout/omniauth-fortnox-oauth2
117
+ post_install_message:
118
+ rdoc_options: []
119
+ require_paths:
120
+ - lib
121
+ required_ruby_version: !ruby/object:Gem::Requirement
122
+ requirements:
123
+ - - "~>"
124
+ - !ruby/object:Gem::Version
125
+ version: '3.0'
126
+ required_rubygems_version: !ruby/object:Gem::Requirement
127
+ requirements:
128
+ - - ">="
129
+ - !ruby/object:Gem::Version
130
+ version: '0'
131
+ requirements: []
132
+ rubygems_version: 3.2.22
133
+ signing_key:
134
+ specification_version: 4
135
+ summary: OmniAuth OAuth2 strategy for Fortnox
136
+ test_files:
137
+ - spec/helper.rb
138
+ - spec/omniauth/fortnox_oauth/api_spec.rb
139
+ - spec/strategies/fortnox_spec.rb