omniauth-bungie-oauth2 0.1.2 → 1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a1c3aa5d2f1b05dc0187ed37d39f075c66fb3b29
4
- data.tar.gz: c9aad36bed6c0079d5e37db9eb3a622cce82d215
3
+ metadata.gz: b651f2022b030e894273b6b25b4611671df43ff0
4
+ data.tar.gz: 581fcf757b94b5959c57f2c40a465277ef380e7e
5
5
  SHA512:
6
- metadata.gz: 310acd967bf14533cd1d490992be687ead6bb733d102a5193f43ff65ce967319c350ae790868e8fb1a12eb1890fe25c099d11c7abea7eeff7f99ca1307aa8165
7
- data.tar.gz: 67edafe5cce55d5f2c5e2eb39ab343e5eb9a679c86a687fe072b37e4beec746e50863368d665281c1c87fd40f9356d71468be35ef0afabc1c8856340e2d97b31
6
+ metadata.gz: 4382fe1d09c6d158cf099a5feb99ba0dc12d67e1722a6aaa707a433fc60e7c1d2ac4df5cbd80ea9d23deabad425521cc974f7f968184a2d8c7d7d7ef2711fd31
7
+ data.tar.gz: 0d3142627717410b590c4377fe2405a7dc043d1eb551ef40c9be6bbecf8df1d482e8b618fdd9a21ab292b91477cc3b9b5302698fa1d63440acf9c9f205f6467e
data/README.md CHANGED
@@ -9,9 +9,9 @@ A Bungie OAuth2 strategy for Omniauth.
9
9
 
10
10
  Add this line to your application's Gemfile:
11
11
 
12
- ```ruby
12
+ ~~~~ruby
13
13
  gem 'omniauth-bungie-oauth2'
14
- ```
14
+ ~~~~
15
15
 
16
16
  And then execute:
17
17
 
@@ -21,20 +21,17 @@ And then execute:
21
21
 
22
22
  ### Settings
23
23
 
24
- For full usage this gem You must create an application with authentication access on [Bungie.net](https://www.bungie.net/en/application).
24
+ For usage this gem You must create an application with authentication access on [Bungie.net](https://www.bungie.net/en/application).
25
+ You should set **Confidential** value in the `OAuth Client Type` field.
25
26
 
26
27
  After this, you can integrate this strategy with your application. (More about A Bungie OAuth2 you can read on [Help page](https://www.bungie.net/en/Help/Article/45481))
27
28
 
28
- For example, you can add the middleware to a Rails application in `/config/application.rb`:
29
+ **This provider uses four arguments:**
29
30
 
30
- ~~~ruby
31
- config.middleware.use OmniAuth::Builder do
32
- provider :bungie,
33
- 'x_api_key_from_bungie_app_settings',
34
- 'authorization_url_from_bungie_app_settings',
35
- :origin => 'origin_url_if_you_need'
36
- end
37
- ~~~~
31
+ * `client_id` - OAuth client_id,
32
+ * `client_secret` - OAuth client_secret,
33
+ * `x_api_key` - API Key,
34
+ * `redirect_uri` - Redirect URL.
38
35
 
39
36
  ### Rails integration
40
37
 
@@ -43,12 +40,15 @@ For integration with Rails You have to setup your strategy configuration in `con
43
40
  ~~~~ruby
44
41
  Devise.setup do |config|
45
42
  config.omniauth :bungie,
46
- 'x_api_key_from_bungie_app_settings',
47
- 'authorization_url_from_bungie_app_settings',
48
- :origin => 'origin_url_if_you_need'
43
+ 'client_id',
44
+ 'client_secret',
45
+ 'x_api_key',
46
+ 'redirect_url'
49
47
  end
50
48
  ~~~~
51
49
 
50
+ > You can also define it in initializers with `config.middleware.use OmniAuth::Builder`.
51
+
52
52
  After this You should define omniauth callback in routes and controller.
53
53
 
54
54
  **routes.rb:**
@@ -78,28 +78,30 @@ class Devise::OmniauthCallbacksController < Devise::OmniauthCallbacksController
78
78
  redirect_to '/'
79
79
  end
80
80
  end
81
+ end
82
+ ~~~~
81
83
 
82
- def failure
83
- redirect_to '/'
84
+ Now You should define `from_omniauth` method in your `User` model:
85
+
86
+ ~~~~ruby
87
+ def self.from_omniauth(auth)
88
+ where(:uid => auth.uid).first_or_create do |user|
89
+ user.membership_id = auth.info.membership_id
90
+ user.display_name = auth.info.display_name
91
+ user.unique_name = auth.info.unique_name
84
92
  end
85
93
  end
86
94
  ~~~~
87
95
 
96
+ > Do not forget to specify model fields in your migration.
97
+
88
98
  ### Result
89
99
 
90
100
  After all manipulation the `request.env["omniauth.auth"]` have the next fields:
91
101
 
92
102
  * `uid` with BungieNetUser membershipId
93
- * `info` with Destiny membershipId, membershipType and displayName
94
- * `extra` with [GetBungieAccount](https://destinydevs.github.io/BungieNetPlatform/docs/UserService/GetBungieAccount) result
95
-
96
- ## Configuration
97
-
98
- This provider require two arguments and have one special option:
99
-
100
- * `api_key` - X-Api-Key for Bungie API;
101
- * `auth_url` - Autherization url;
102
- * `origin` - Origin url;
103
+ * `info` with membershipId, uniqueName, displayName
104
+ * `extra` with [GetCurrentBungieNetUser](https://destinydevs.github.io/BungieNetPlatform/docs/UserService/GetCurrentBungieNetUser) result
103
105
 
104
106
  ## Contributing
105
107
 
@@ -1,118 +1,50 @@
1
- require 'omniauth'
2
- require 'omniauth-oauth2'
3
- require 'socket'
4
- require 'timeout'
5
-
6
1
  module OmniAuth
7
2
  module Strategies
8
3
  class Bungie < OmniAuth::Strategies::OAuth2
9
- # Arguments
10
- args [:api_key, :auth_url]
4
+ args [:client_id, :client_secret, :api_key, :redirect_uri]
11
5
 
12
- # Options
13
6
  option :name, 'bungie'
14
- option :origin, nil
15
-
16
- # Update client with Faraday middleware & special authorize url.
17
- def client
18
- client_options = {
19
- :authorize_url => options.auth_url
20
- }.merge(options.client_options)
21
-
22
- ::OAuth2::BungieClient.new(nil, nil, deep_symbolize(client_options)) do |b|
23
- b.request :json
24
-
25
- b.adapter Faraday.default_adapter
26
- end
27
- end
28
-
29
- def merge_stack(stack)
30
- stack.inject({}) do |a, e|
31
- a.merge!(e) unless e.nil?
32
- a
33
- end
34
- end
35
-
36
- # Updated callback phase with new refreshing
37
- def callback_phase
38
- error = request.params["error_reason"] || request.params["error"]
39
-
40
- if error
41
- fail!(error, CallbackError.new(request.params["error"], request.params["error_description"] || request.params["error_reason"], request.params["error_uri"]))
42
- elsif !options.provider_ignores_state && (request.params["state"].to_s.empty? || request.params["state"] != session.delete("omniauth.state"))
43
- fail!(:csrf_detected, CallbackError.new(:csrf_detected, "CSRF detected"))
44
- else
45
- self.access_token = build_access_token
46
- self.access_token = access_token.refresh!(token_params) if access_token.expired?
47
7
 
48
- env['omniauth.auth'] = auth_hash
8
+ option :client_options, {
9
+ :site => 'https://www.bungie.net',
10
+ :authorize_url => '/en/oauth/authorize',
11
+ :token_url => '/platform/app/oauth/token'
12
+ }
49
13
 
50
- call_app!
51
- end
52
- rescue ::OAuth2::Error, CallbackError => e
53
- fail!(:invalid_credentials, e)
54
- rescue ::Timeout::Error, ::Errno::ETIMEDOUT => e
55
- fail!(:timeout, e)
56
- rescue ::SocketError => e
57
- fail!(:failed_to_connect, e)
58
- end
14
+ def client
15
+ client_options = options.client_options.merge(
16
+ :connection_opts => {
17
+ :headers => { 'X-API-Key' => options.api_key }
18
+ },
19
+ :redirect_uri => options.redirect_uri
20
+ )
59
21
 
60
- # Defining of Origin string
61
- def origin
62
- if options.origin === true
63
- request.base_url
64
- elsif options.origin.is_a? String
65
- options.origin
66
- else
67
- ''
68
- end
22
+ ::OAuth2::Client.new(
23
+ options.client_id,
24
+ options.client_secret,
25
+ deep_symbolize(client_options)
26
+ )
69
27
  end
70
28
 
71
- # Token params with X-Api-Key & Origin
72
- def token_params
73
- token_params = options.token_params.merge(options_for("token"))
74
-
75
- token_params[:headers] ||= {}
76
- token_params[:headers]['X-Api-Key'] = options.api_key
77
- token_params[:headers]['Origin'] = origin unless options.origin.nil?
78
-
79
- token_params
29
+ uid do
30
+ raw_info['membershipId']
80
31
  end
81
32
 
82
- # Get important data
83
- uid { raw_info.dig('bungieNetUser', 'membershipId') }
84
33
  info do
85
- if raw_info['destinyAccounts'].any?
86
- destiny = raw_info['destinyAccounts'].first
87
- {
88
- :membership_id => destiny.dig('userInfo', 'membershipId'),
89
- :membership_type => destiny.dig('userInfo', 'membershipType'),
90
- :display_name => destiny.dig('userInfo', 'displayName')
91
- }
92
- else
93
- destiny = raw_info['bungieNetUser']
94
-
95
- {
96
- :membership_id => destiny['membershipId'],
97
- :membership_type => 254,
98
- :display_name => destiny['displayName']
99
- }
100
- end
101
- end
102
- extra do
103
34
  {
104
- 'raw_info' => raw_info
35
+ :membership_id => raw_info['membershipId'],
36
+ :unique_name => raw_info['uniqueName'],
37
+ :display_name => raw_info['displayName']
105
38
  }
106
39
  end
40
+
41
+ extra { raw_info }
42
+
107
43
  def raw_info
108
44
  return @raw_info unless @raw_info.nil?
109
45
 
110
- @raw_info = access_token.get(
111
- '/Platform/User/GetCurrentBungieAccount/',
112
- token_params
113
- ).parsed
114
-
115
- @raw_info = @raw_info.dig('Response')
46
+ @raw_info = access_token.get('/Platform/User/GetCurrentBungieNetUser/').parsed
47
+ @raw_info = (@raw_info['ErrorCode'] == 1) ? @raw_info['Response'] : {}
116
48
  end
117
49
  end
118
50
  end
@@ -1,5 +1,5 @@
1
1
  module OmniAuth
2
2
  module BungieOauth2
3
- VERSION = '0.1.2'
3
+ VERSION = '1.0'
4
4
  end
5
5
  end
@@ -1,9 +1,4 @@
1
- require 'json'
2
- require 'faraday'
3
- require 'faraday_middleware'
4
- require 'oauth2'
1
+ require 'omniauth-oauth2'
5
2
 
6
- require 'oauth2/bungie_access_token'
7
- require 'oauth2/bungie_client'
8
3
  require 'omniauth-bungie-oauth2/version'
9
4
  require 'omniauth/strategies/bungie'
@@ -23,9 +23,5 @@ Gem::Specification.new do |spec|
23
23
  spec.add_development_dependency 'rake', '~> 10.0'
24
24
  spec.add_development_dependency 'rspec', '~> 3.0'
25
25
 
26
- spec.add_runtime_dependency 'faraday', '~> 0.9'
27
- spec.add_runtime_dependency 'faraday_middleware', '~> 0'
28
- spec.add_runtime_dependency 'oauth2', '~> 1.2', '>= 1.2.0'
29
- spec.add_runtime_dependency 'omniauth', '~> 1.3', '>= 1.3.1'
30
26
  spec.add_runtime_dependency 'omniauth-oauth2', '~> 1.4', '>= 1.4.0'
31
27
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: omniauth-bungie-oauth2
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: '1.0'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dmitry Ruban
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-06-21 00:00:00.000000000 Z
11
+ date: 2017-06-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -52,74 +52,6 @@ dependencies:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
54
  version: '3.0'
55
- - !ruby/object:Gem::Dependency
56
- name: faraday
57
- requirement: !ruby/object:Gem::Requirement
58
- requirements:
59
- - - "~>"
60
- - !ruby/object:Gem::Version
61
- version: '0.9'
62
- type: :runtime
63
- prerelease: false
64
- version_requirements: !ruby/object:Gem::Requirement
65
- requirements:
66
- - - "~>"
67
- - !ruby/object:Gem::Version
68
- version: '0.9'
69
- - !ruby/object:Gem::Dependency
70
- name: faraday_middleware
71
- requirement: !ruby/object:Gem::Requirement
72
- requirements:
73
- - - "~>"
74
- - !ruby/object:Gem::Version
75
- version: '0'
76
- type: :runtime
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: oauth2
85
- requirement: !ruby/object:Gem::Requirement
86
- requirements:
87
- - - "~>"
88
- - !ruby/object:Gem::Version
89
- version: '1.2'
90
- - - ">="
91
- - !ruby/object:Gem::Version
92
- version: 1.2.0
93
- type: :runtime
94
- prerelease: false
95
- version_requirements: !ruby/object:Gem::Requirement
96
- requirements:
97
- - - "~>"
98
- - !ruby/object:Gem::Version
99
- version: '1.2'
100
- - - ">="
101
- - !ruby/object:Gem::Version
102
- version: 1.2.0
103
- - !ruby/object:Gem::Dependency
104
- name: omniauth
105
- requirement: !ruby/object:Gem::Requirement
106
- requirements:
107
- - - "~>"
108
- - !ruby/object:Gem::Version
109
- version: '1.3'
110
- - - ">="
111
- - !ruby/object:Gem::Version
112
- version: 1.3.1
113
- type: :runtime
114
- prerelease: false
115
- version_requirements: !ruby/object:Gem::Requirement
116
- requirements:
117
- - - "~>"
118
- - !ruby/object:Gem::Version
119
- version: '1.3'
120
- - - ">="
121
- - !ruby/object:Gem::Version
122
- version: 1.3.1
123
55
  - !ruby/object:Gem::Dependency
124
56
  name: omniauth-oauth2
125
57
  requirement: !ruby/object:Gem::Requirement
@@ -157,8 +89,6 @@ files:
157
89
  - Rakefile
158
90
  - bin/console
159
91
  - bin/setup
160
- - lib/oauth2/bungie_access_token.rb
161
- - lib/oauth2/bungie_client.rb
162
92
  - lib/omniauth-bungie-oauth2.rb
163
93
  - lib/omniauth-bungie-oauth2/version.rb
164
94
  - lib/omniauth/strategies/bungie.rb
@@ -1,19 +0,0 @@
1
- module OAuth2
2
- class BungieAccessToken < AccessToken
3
- # Updated refreshing method for a special bungie page
4
- def refresh!(params = {})
5
- raise('A refresh_token is not available') unless refresh_token
6
-
7
- params[:client_id] = @client.id
8
- params[:client_secret] = @client.secret
9
- params[:grant_type] = 'refresh_token'
10
- params[:refresh_token] = refresh_token
11
- params[:refreshToken] = params[:refresh_token]
12
-
13
- new_token = @client.get_token_with_refresh(params)
14
- new_token.options = options
15
- new_token.refresh_token = refresh_token unless new_token.refresh_token
16
- new_token
17
- end
18
- end
19
- end
@@ -1,90 +0,0 @@
1
- module OAuth2
2
- class BungieClient < Client
3
- def initialize(client_id, client_secret, options = {}, &block)
4
- opts = options.dup
5
- @id = client_id
6
- @secret = client_secret
7
- @site = 'https://www.bungie.net'
8
- ssl = opts.delete(:ssl)
9
- @options = {
10
- :authorize_url => 'https://www.bungie.net',
11
- :token_url => 'https://www.bungie.net/Platform/App/GetAccessTokensFromCode',
12
- :refresh_token_url => 'https://www.bungie.net/Platform/App/GetAccessTokensFromRefreshToken',
13
- :token_method => :post,
14
- :connection_opts => {},
15
- :connection_build => block,
16
- :max_redirects => 5,
17
- :raise_errors => true}.merge(opts)
18
- @options[:connection_opts][:ssl] = ssl if ssl
19
- end
20
-
21
- def get_token(params, access_token_opts = {}, access_token_class = BungieAccessToken)
22
- opts = {:raise_errors => options[:raise_errors], :parse => params.delete(:parse)}
23
-
24
- if options[:token_method] == :post
25
- headers = params.delete(:headers)
26
- opts[:body] = params
27
- opts[:headers] = {'Content-Type' => 'application/json'}
28
- opts[:headers].merge!(headers) if headers
29
- else
30
- opts[:params] = params
31
- end
32
-
33
- response = request(options[:token_method], token_url, opts)
34
-
35
- error = Error.new(response)
36
-
37
- response = get_normalized_response(response)
38
-
39
- raise(error) if options[:raise_errors] && (!response.is_a?(Hash) || response['access_token'].nil?)
40
-
41
- access_token_class.from_hash(self, response.merge(access_token_opts))
42
- end
43
-
44
- def get_token_with_refresh(params, access_token_opts = {}, access_token_class = BungieAccessToken)
45
- opts = {:raise_errors => options[:raise_errors], :parse => params.delete(:parse)}
46
-
47
- if options[:token_method] == :post
48
- headers = params.delete(:headers)
49
- opts[:body] = params
50
- opts[:headers] = {'Content-Type' => 'application/json'}
51
- opts[:headers].merge!(headers) if headers
52
- else
53
- opts[:params] = params
54
- end
55
-
56
- response = request(
57
- options[:token_method],
58
- connection.build_url(options[:refresh_token_url]).to_s,
59
- opts
60
- )
61
-
62
- error = Error.new(response)
63
-
64
- response = get_normalized_response(response)
65
-
66
- raise(error) if options[:raise_errors] && !(response.is_a?(Hash) && response['access_token'])
67
-
68
- access_token_class.from_hash(self, response.merge(access_token_opts))
69
- end
70
-
71
- # Transform response body to RFC specification
72
- def get_normalized_response(response)
73
- response = response.parsed
74
-
75
- return nil unless response.is_a?(Hash)
76
-
77
- if response['ErrorCode'] == 1 && !response.dig('Response', 'accessToken').nil?
78
- {
79
- 'access_token' => response.dig('Response', 'accessToken', 'value'),
80
- 'token_type' => 'Bearer',
81
- 'expires_in' => response.dig('Response', 'accessToken', 'expires'),
82
- 'refresh_token' => response.dig('Response', 'refreshToken', 'value'),
83
- 'refresh_expries_in' => response.dig('Response', 'refreshToken', 'expires')
84
- }
85
- else
86
- response
87
- end
88
- end
89
- end
90
- end