Skip to content

Commit a7b3b73

Browse files
author
Jesse Doyle
committed
OAuth2 - PKCE | CI
* Resolve all current rubocop violations for rubocop v0.86.0.
1 parent e53f2cb commit a7b3b73

File tree

9 files changed

+68
-28
lines changed

9 files changed

+68
-28
lines changed

.rubocop.yml

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,28 @@
1+
AllCops:
2+
NewCops: enable
3+
14
Layout/AccessModifierIndentation:
25
EnforcedStyle: outdent
36

7+
Layout/LineLength:
8+
AllowURI: true
9+
Enabled: false
10+
411
Layout/SpaceInsideHashLiteralBraces:
512
EnforcedStyle: no_space
613

14+
Metrics/AbcSize:
15+
Max: 17
16+
17+
Metrics/BlockLength:
18+
Exclude:
19+
- spec/omniauth/strategies/oauth2_spec.rb
20+
721
Metrics/BlockNesting:
822
Max: 2
923

10-
Metrics/LineLength:
11-
AllowURI: true
12-
Enabled: false
24+
Metrics/ClassLength:
25+
Max: 110
1326

1427
Metrics/MethodLength:
1528
CountComments: false
@@ -19,6 +32,10 @@ Metrics/ParameterLists:
1932
Max: 4
2033
CountKeywordArgs: true
2134

35+
Naming/FileName:
36+
Exclude:
37+
- lib/omniauth-oauth2.rb
38+
2239
Style/CollectionMethods:
2340
PreferredMethods:
2441
map: 'collect'
@@ -52,4 +69,3 @@ Style/TrailingCommaInHashLiteral:
5269

5370
Style/TrailingCommaInArrayLiteral:
5471
EnforcedStyleForMultiline: comma
55-

Gemfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
source "https://rubygems.org"
24

35
gem "rake", "~> 12.0"

Rakefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
#!/usr/bin/env rake
2+
# frozen_string_literal: true
3+
24
require "bundler/gem_tasks"
35
require "rspec/core/rake_task"
46

lib/omniauth-oauth2.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
1-
require "omniauth-oauth2/version" # rubocop:disable FileName
1+
# frozen_string_literal: true
2+
3+
require "omniauth-oauth2/version"
24
require "omniauth/strategies/oauth2"

lib/omniauth-oauth2/version.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
# frozen_string_literal: true
2+
13
module OmniAuth
24
module OAuth2
3-
VERSION = "1.6.0".freeze
5+
VERSION = "1.6.0"
46
end
57
end

lib/omniauth/strategies/oauth2.rb

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
require "oauth2"
24
require "omniauth"
35
require "securerandom"
@@ -24,7 +26,7 @@ def self.inherited(subclass)
2426
option :client_secret, nil
2527
option :client_options, {}
2628
option :authorize_params, {}
27-
option :authorize_options, [:scope, :state]
29+
option :authorize_options, %i[scope state]
2830
option :token_params, {}
2931
option :token_options, []
3032
option :auth_token_params, {}
@@ -52,15 +54,7 @@ def request_phase
5254
def authorize_params
5355
verifier = SecureRandom.hex(64)
5456

55-
if options.pkce
56-
# NOTE: see https://tools.ietf.org/html/rfc7636#appendix-A
57-
challenge = Base64
58-
.urlsafe_encode64(Digest::SHA2.digest(verifier))
59-
.split("=")
60-
.first
61-
options.authorize_params[:code_challenge] = challenge
62-
options.authorize_params[:code_challenge_method] = "S256"
63-
end
57+
pkce_authorize_params!(verifier)
6458

6559
options.authorize_params[:state] = SecureRandom.hex(24)
6660
params = options.authorize_params.merge(options_for("authorize"))
@@ -70,16 +64,15 @@ def authorize_params
7064
@env["rack.session"] ||= {}
7165
end
7266

73-
session["omniauth.pkce.verifier"] = verifier if options.pkce
74-
session["omniauth.state"] = params[:state]
67+
build_authorize_session!(params, verifier)
7568
params
7669
end
7770

7871
def token_params
7972
options.token_params.merge(options_for("token")).merge(pkce_token_params)
8073
end
8174

82-
def callback_phase # rubocop:disable AbcSize, CyclomaticComplexity, MethodLength, PerceivedComplexity
75+
def callback_phase # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity
8376
error = request.params["error_reason"] || request.params["error"]
8477
if error
8578
fail!(error, CallbackError.new(request.params["error"], request.params["error_description"] || request.params["error_reason"], request.params["error_uri"]))
@@ -100,10 +93,27 @@ def callback_phase # rubocop:disable AbcSize, CyclomaticComplexity, MethodLength
10093

10194
protected
10295

96+
def build_authorize_session!(params, verifier)
97+
session["omniauth.pkce.verifier"] = verifier if options.pkce
98+
session["omniauth.state"] = params[:state]
99+
end
100+
101+
def pkce_authorize_params!(verifier)
102+
return unless options.pkce
103+
104+
# NOTE: see https://tools.ietf.org/html/rfc7636#appendix-A
105+
challenge = Base64
106+
.urlsafe_encode64(Digest::SHA2.digest(verifier))
107+
.split("=")
108+
.first
109+
options.authorize_params[:code_challenge] = challenge
110+
options.authorize_params[:code_challenge_method] = "S256"
111+
end
112+
103113
def pkce_token_params
104114
return {} unless options.pkce
105115

106-
{ code_verifier: session.delete("omniauth.pkce.verifier") }
116+
{:code_verifier => session.delete("omniauth.pkce.verifier")}
107117
end
108118

109119
def build_access_token
@@ -121,10 +131,10 @@ def options_for(option)
121131
hash = {}
122132
options.send(:"#{option}_options").select { |key| options[key] }.each do |key|
123133
hash[key.to_sym] = if options[key].respond_to?(:call)
124-
options[key].call(env)
125-
else
126-
options[key]
127-
end
134+
options[key].call(env)
135+
else
136+
options[key]
137+
end
128138
end
129139
hash
130140
end

omniauth-oauth2.gemspec

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
lib = File.expand_path("../lib", __FILE__)
24
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
35
require "omniauth-oauth2/version"

spec/helper.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
$LOAD_PATH.unshift File.expand_path("..", __FILE__)
24
$LOAD_PATH.unshift File.expand_path("../../lib", __FILE__)
35

spec/omniauth/strategies/oauth2_spec.rb

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
# frozen_string_literal: true
2+
13
require "helper"
24

3-
describe OmniAuth::Strategies::OAuth2 do # rubocop:disable Metrics/BlockLength
5+
describe OmniAuth::Strategies::OAuth2 do
46
def app
57
lambda do |_env|
68
[200, {}, ["Hello."]]
@@ -62,13 +64,13 @@ def app
6264
end
6365

6466
it "includes custom state in the authorize params" do
65-
instance = subject.new("abc", "def", state: Proc.new { "qux" } )
67+
instance = subject.new("abc", "def", :state => proc { "qux" })
6668
expect(instance.authorize_params.keys).to eq(["state"])
6769
expect(instance.session["omniauth.state"]).to eq("qux")
6870
end
6971

7072
it "includes PKCE parameters if enabled" do
71-
instance = subject.new("abc", "def", pkce: true)
73+
instance = subject.new("abc", "def", :pkce => true)
7274
expect(instance.authorize_params[:code_challenge]).to be_a(String)
7375
expect(instance.authorize_params[:code_challenge_method]).to eq("S256")
7476
expect(instance.session["omniauth.pkce.verifier"]).to be_a(String)
@@ -89,7 +91,7 @@ def app
8991
end
9092

9193
it "includes the PKCE code_verifier if enabled" do
92-
instance = subject.new("abc", "def", pkce: true)
94+
instance = subject.new("abc", "def", :pkce => true)
9395
# setup session
9496
instance.authorize_params
9597
expect(instance.token_params[:code_verifier]).to be_a(String)

0 commit comments

Comments
 (0)