Skip to content

Commit 7fdad73

Browse files
committed
Second pass, because wow my code was rough
1 parent 3fbe2f8 commit 7fdad73

File tree

19 files changed

+149
-156
lines changed

19 files changed

+149
-156
lines changed

lib/doorkeeper.rb

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#
88
module Doorkeeper
99
autoload :Errors, "doorkeeper/errors"
10+
autoload :ClientAuthentication, "doorkeeper/client_authentication"
1011
autoload :GrantFlow, "doorkeeper/grant_flow"
1112
autoload :OAuth, "doorkeeper/oauth"
1213
autoload :Rake, "doorkeeper/rake"
@@ -33,7 +34,6 @@ module Request
3334
autoload :RefreshToken, "doorkeeper/request/refresh_token"
3435
autoload :Token, "doorkeeper/request/token"
3536
end
36-
3737
module RevocableTokens
3838
autoload :RevocableAccessToken, "doorkeeper/revocable_tokens/revocable_access_token"
3939
autoload :RevocableRefreshToken, "doorkeeper/revocable_tokens/revocable_refresh_token"
@@ -62,16 +62,22 @@ module OAuth
6262
autoload :TokenRequest, "doorkeeper/oauth/token_request"
6363
autoload :TokenResponse, "doorkeeper/oauth/token_response"
6464

65+
module ClientAuthentication
66+
autoload :None, "doorkeeper/oauth/client_authentication/none"
67+
autoload :ClientSecretBasic, "doorkeeper/oauth/client_authentication/client_secret_basic"
68+
autoload :ClientSecretPost, "doorkeeper/oauth/client_authentication/client_secret_post"
69+
end
70+
6571
module Authorization
6672
autoload :Code, "doorkeeper/oauth/authorization/code"
6773
autoload :Context, "doorkeeper/oauth/authorization/context"
6874
autoload :Token, "doorkeeper/oauth/authorization/token"
6975
autoload :URIBuilder, "doorkeeper/oauth/authorization/uri_builder"
7076
end
7177

72-
class Client
73-
autoload :Credentials, "doorkeeper/oauth/client/credentials"
74-
end
78+
# class Client
79+
# autoload :Credentials, "doorkeeper/oauth/client/credentials"
80+
# end
7581

7682
module ClientCredentials
7783
autoload :Validator, "doorkeeper/oauth/client_credentials/validator"
Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# frozen_string_literal: true
22

3-
require "doorkeeper/client_authentication/flow"
3+
require "doorkeeper/client_authentication/credentials"
4+
require "doorkeeper/client_authentication/fallback_mechanism"
5+
require "doorkeeper/client_authentication/mechanism"
46
require "doorkeeper/client_authentication/registry"
57

68
module Doorkeeper
@@ -9,18 +11,18 @@ module ClientAuthentication
911

1012
register(
1113
:none,
12-
mechanism: Doorkeeper::ClientAuthentication::Mechanisms::None,
14+
mechanism: Doorkeeper::OAuth::ClientAuthentication::None,
1315
authenticates_client: false
1416
)
1517

1618
register(
1719
:client_secret_post,
18-
mechanism: Doorkeeper::ClientAuthentication::Mechanisms::ClientSecretPost,
20+
mechanism: Doorkeeper::OAuth::ClientAuthentication::ClientSecretPost,
1921
)
2022

2123
register(
2224
:client_secret_basic,
23-
mechanism: Doorkeeper::ClientAuthentication::Mechanisms::ClientSecretBasic,
25+
mechanism: Doorkeeper::OAuth::ClientAuthentication::ClientSecretBasic,
2426
)
2527
end
2628
end
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# frozen_string_literal: true
2+
3+
module Doorkeeper
4+
module ClientAuthentication
5+
class FallbackMechanism
6+
def authenticate(request)
7+
Doorkeeper::ClientAuthentication::Credentials.new(nil, nil)
8+
end
9+
end
10+
end
11+
end

lib/doorkeeper/client_authentication/mechanism.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ def initialize(name, **options)
1414
def authenticates_client?
1515
!!@authenticates_client
1616
end
17+
18+
def matches_request?(request)
19+
@mechanism.matches_request?(request)
20+
end
1721
end
1822
end
1923
end

lib/doorkeeper/client_authentication/registry.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# frozen_string_literal: true
22

33
module Doorkeeper
4-
module GrantFlow
4+
module ClientAuthentication
55
module Registry
66
mattr_accessor :mechanisms
77
self.mechanisms = {}

lib/doorkeeper/oauth/client/credentials.rb

Lines changed: 0 additions & 34 deletions
This file was deleted.

lib/doorkeeper/client_authentication/mechanisms/client_secret_basic.rb renamed to lib/doorkeeper/oauth/client_authentication/client_secret_basic.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# frozen_string_literal: true
22

33
module Doorkeeper
4-
module ClientAuthentication
5-
module Mechanisms
4+
module OAuth
5+
module ClientAuthentication
66
class ClientSecretBasic
77
def self.matches_request?(request)
88
request.authorization.present? && request.authorization.downcase.start_with?('basic')

lib/doorkeeper/client_authentication/mechanisms/client_secret_post.rb renamed to lib/doorkeeper/oauth/client_authentication/client_secret_post.rb

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
11
# frozen_string_literal: true
22

33
module Doorkeeper
4-
module ClientAuthentication
5-
module Mechanisms
4+
module OAuth
5+
module ClientAuthentication
66
class ClientSecretPost
77
def self.matches_request?(request)
8-
request.method.upcase === "POST"
8+
request.method.upcase === "POST" && request.request_parameters[:client_id].present? && request.request_parameters[:client_secret].present?
99
end
1010

1111
def authenticate(request)
1212
client_id = request.request_parameters[:client_id]
1313
client_secret = request.request_parameters[:client_secret]
1414

15-
return unless client_id.present? && client_secret.present?
16-
1715
Doorkeeper::ClientAuthentication::Credentials.new(
1816
client_id,
1917
client_secret

lib/doorkeeper/client_authentication/mechanisms/none.rb renamed to lib/doorkeeper/oauth/client_authentication/none.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# frozen_string_literal: true
22

33
module Doorkeeper
4-
module ClientAuthentication
5-
module Mechanisms
4+
module OAuth
5+
module ClientAuthentication
66
class None
77
def self.matches_request?(request)
88
!request.authorization && request.request_parameters[:client_id] && !request.request_parameters[:client_secret]

lib/doorkeeper/request.rb

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@ module Doorkeeper
44
module Request
55
class << self
66
def client_authentication_mechanism(request)
7-
client_authentication_mechanisms = client_authentication_mechanisms.detect do |mechanism|
8-
mechanism.matches_request?(request)
7+
strategy = client_authentication_strategies.detect do |strategy|
8+
strategy.matches_request?(request)
99
end
1010

11-
if client_authentication_mechanisms
12-
client_authentication_mechanisms.mechanism
11+
if strategy
12+
strategy.mechanism
13+
else
14+
Doorkeeper::ClientAuthentication::FallbackMechanism
1315
end
1416
end
1517

@@ -50,7 +52,7 @@ def token_strategy(grant_type)
5052

5153
private
5254

53-
def client_authentication_mechanisms
55+
def client_authentication_strategies
5456
Doorkeeper.configuration.client_authentication_mechanisms
5557
end
5658

0 commit comments

Comments
 (0)