Permalink
Browse files

認証まわりいじった

  • Loading branch information...
1 parent 42e6870 commit 30f4de073e18bee89ee4c4e6bc5ccf9459251d3b @mashiro committed May 8, 2012
View
@@ -53,6 +53,14 @@ def attached?
!!@session
end
+ def send_data(data)
+ if attached?
+ @session.send_data data
+ else
+ super data
+ end
+ end
+
include Rlyeh::Filters
define_filters :attached, :detached
end
@@ -1,8 +1,10 @@
module Rlyeh
module DeepOnes
module Auth
- autoload :Base, 'rlyeh/deep_ones/auth/base'
+ autoload :Base, 'rlyeh/deep_ones/auth/base'
+ autoload :Null, 'rlyeh/deep_ones/auth/null'
autoload :Basic, 'rlyeh/deep_ones/auth/basic'
+ autoload :OAuth, 'rlyeh/deep_ones/auth/oauth'
end
end
end
@@ -12,8 +12,8 @@ class Base
def initialize(app, &block)
@app = app
- @authenticator = block
@authenticated = false
+ instance_eval &block if block
end
def call(env)
@@ -29,19 +29,23 @@ def authenticated?
@authenticated
end
- def authenticate!(env)
- if @authenticator && @authenticator.call(self)
- succeeded env
- else
- failed env
- end
+ def try(env)
+ raise NotImplementedError
end
def succeeded(env)
@authorized = true
session = load_session env, to_session_id
session.attach env.connection
+ debug(env) { "Succeeded #{env.connection.host}:#{env.connection.port}" }
+ end
+
+ def failed(env)
+ env.connection.send_numeric_reply :passwdmismatch, @host, ':Password incorrect'
+ debug(env) { "Failed #{env.connection.host}:#{env.connection.port}" }
+ end
+ def welcome(env)
name = env.settings.server_name
version = env.settings.server_version
user_modes = env.settings.available_user_modes
@@ -55,15 +59,8 @@ def succeeded(env)
}
messages.each do |type, message|
- env.connection.send_numeric_reply type, @host, message
+ env.connection.send_numeric_reply type, @nick, ":#{message}", :prefix => {:servername => name}
end
-
- debug(env) { "Succeeded #{env.connection.host}:#{env.connection.port}" }
- end
-
- def failed(env)
- env.connection.send_numeric_reply :passwdmismatch, @host, ':Password incorrect'
- debug(env) { "Failed #{env.connection.host}:#{env.connection.port}" }
end
def to_session_id
@@ -86,7 +83,8 @@ def load_session(env, session_id)
@user = env.message.params[0]
@real = env.message.params[3]
@host = env.connection.host
- authenticate! env
+
+ try env
end
end
end
@@ -3,9 +3,16 @@ module DeepOnes
module Auth
class Basic < Rlyeh::DeepOnes::Auth::Base
def initialize(app, nick, pass)
- basic = [nick, pass].freeze
- super app do |auth|
- basic == [auth.nick, auth.pass]
+ @basic = [nick, pass].freeze
+ super app
+ end
+
+ def try(env)
+ if @basic == [nick, pass]
+ succeeded env
+ welcome env
+ else
+ failed env
end
end
end
@@ -0,0 +1,12 @@
+module Rlyeh
+ module DeepOnes
+ module Auth
+ class Null < Rlyeh::DeepOnes::Auth::Base
+ def try(env)
+ succeeded env
+ welcome env
+ end
+ end
+ end
+ end
+end
@@ -0,0 +1,51 @@
+require 'oauth'
+
+module Rlyeh
+ module DeepOnes
+ module Auth
+ class OAuth < Rlyeh::DeepOnes::Auth::Base
+ attr_accessor :consumer_key, :consumer_secret, :client_options
+ attr_accessor :open_timeout, :read_timeout
+ attr_accessor :authorize_params, :request_params
+ attr_reader :access_token
+
+ def initialize(app, consumer_key, consumer_secret, client_options = {})
+ @consumer_key = consumer_key
+ @consumer_secret = consumer_secret
+ @client_options = client_options
+ @open_timeout = nil
+ @read_timeout = nil
+ @authorize_params = {}
+ @request_params = {}
+ super app
+ end
+
+ def consumer
+ consumer = ::OAuth::Consumer.new @consumer_key, @consumer_secret, @client_options
+ consumer.http.open_timeout = @open_timeout if @open_timeout
+ consumer.http.read_timeout = @read_timeout if @read_timeout
+ consumer
+ end
+
+ def try(env)
+ welcome env
+ request_phase env
+ end
+
+ def request_phase(env)
+ request_token = consumer.get_request_token @request_params
+
+ talk env, 'Access this URL get the PIN and paste it here.'
+ talk env, request_token.authorize_url(@authorize_params)
+ end
+
+ private
+
+ def talk(env, text)
+ prefix = {:nick => '$oauth', :user => 'rlyeh', :host => env.settings.server_name}
+ env.connection.send_message 'PRIVMSG', @nick, ":#{text}", :prefix => prefix
+ end
+ end
+ end
+ end
+end
View
@@ -1,11 +1,18 @@
require 'ircp'
require 'rlyeh/numeric_reply'
+require 'rlyeh/utils'
module Rlyeh
module Sendable
- def send_numeric_reply(numeric, target, *args)
- numeric = Rlyeh::NumericReply.to_value numeric
- send_data Ircp::Message.new(target, *args, :command => numeric)
+ def send_message(command, *args)
+ options = Rlyeh::Utils.extract_options! args
+ send_data Ircp::Message.new(*args, options.merge(:command => command))
+ end
+
+ def send_numeric_reply(type, target, *args)
+ options = Rlyeh::Utils.extract_options! args
+ numeric = Rlyeh::NumericReply.to_value type
+ send_data Ircp::Message.new(target, *args, options.merge(:command => numeric))
end
end
end
View
@@ -19,4 +19,6 @@ Gem::Specification.new do |gem|
gem.add_dependency 'eventmachine'
gem.add_development_dependency 'rake'
gem.add_development_dependency 'rspec'
+
+ gem.add_development_dependency 'oauth'
end

0 comments on commit 30f4de0

Please sign in to comment.