Skip to content

Commit

Permalink
Push common functionality into Doorkeeper::Request::Strategy
Browse files Browse the repository at this point in the history
  • Loading branch information
reidab committed Oct 27, 2015
1 parent e676684 commit 13229e3
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 42 deletions.
13 changes: 6 additions & 7 deletions lib/doorkeeper/request/authorization_code.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
require 'doorkeeper/request/strategy'

module Doorkeeper
module Request
class AuthorizationCode
attr_accessor :grant, :client, :server
class AuthorizationCode < Strategy
attr_accessor :grant, :client

def initialize(server)
@grant, @client, @server = server.grant, server.client, server
super
@grant, @client = server.grant, server.client
end

def request
@request ||= OAuth::AuthorizationCodeRequest.new(Doorkeeper.configuration, grant, client, server.parameters)
end

def authorize
request.authorize
end
end
end
end
13 changes: 6 additions & 7 deletions lib/doorkeeper/request/client_credentials.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
require 'doorkeeper/request/strategy'

module Doorkeeper
module Request
class ClientCredentials
attr_accessor :client, :server
class ClientCredentials < Strategy
attr_accessor :client

def initialize(server)
@client, @server = server.client, server
super
@client = server.client
end

def request
@request ||= OAuth::ClientCredentialsRequest.new(Doorkeeper.configuration, client, server.parameters)
end

def authorize
request.authorize
end
end
end
end
12 changes: 5 additions & 7 deletions lib/doorkeeper/request/code.rb
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
require 'doorkeeper/request/strategy'

module Doorkeeper
module Request
class Code
attr_accessor :pre_auth, :server
class Code < Strategy
attr_accessor :pre_auth

def initialize(server)
super
@pre_auth = server.context.send(:pre_auth)
@server = server
end

def request
@request ||= OAuth::CodeRequest.new(pre_auth, server.current_resource_owner)
end

def authorize
request.authorize
end
end
end
end
12 changes: 5 additions & 7 deletions lib/doorkeeper/request/password.rb
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
require 'doorkeeper/request/strategy'

module Doorkeeper
module Request
class Password
attr_accessor :credentials, :resource_owner, :server
class Password < Strategy
attr_accessor :credentials, :resource_owner

def initialize(server)
super
@credentials = server.credentials
@resource_owner = server.resource_owner
@server = server
end

def request
@request ||= OAuth::PasswordAccessTokenRequest.new(Doorkeeper.configuration, credentials, resource_owner, server.parameters)
end

def authorize
request.authorize
end
end
end
end
12 changes: 5 additions & 7 deletions lib/doorkeeper/request/refresh_token.rb
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
require 'doorkeeper/request/strategy'

module Doorkeeper
module Request
class RefreshToken
attr_accessor :refresh_token, :credentials, :server
class RefreshToken < Strategy
attr_accessor :refresh_token, :credentials

def initialize(server)
super
@refresh_token = server.current_refresh_token
@credentials = server.credentials
@server = server
end

def request
@request ||= OAuth::RefreshTokenRequest.new(Doorkeeper.configuration, refresh_token, credentials, server.parameters)
end

def authorize
request.authorize
end
end
end
end
19 changes: 19 additions & 0 deletions lib/doorkeeper/request/strategy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module Doorkeeper
module Request
class Strategy
attr_accessor :server

def initialize(server)
self.server = server
end

def request
raise NotImplementedError, "request strategies must define #request"
end

def authorize
request.authorize
end
end
end
end
12 changes: 5 additions & 7 deletions lib/doorkeeper/request/token.rb
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
require 'doorkeeper/request/strategy'

module Doorkeeper
module Request
class Token
attr_accessor :pre_auth, :server
class Token < Strategy
attr_accessor :pre_auth

def initialize(server)
super
@pre_auth = server.context.send(:pre_auth)
@server = server
end

def request
@request ||= OAuth::TokenRequest.new(pre_auth, server.current_resource_owner)
end

def authorize
request.authorize
end
end
end
end

0 comments on commit 13229e3

Please sign in to comment.