Skip to content

Commit

Permalink
Added authorization capabilities.
Browse files Browse the repository at this point in the history
  • Loading branch information
totallymike committed Jan 10, 2013
1 parent 4ea144b commit 94f6488
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/nntp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ module NNTP
def self.open(options)
connection = Connection.new(options)
session = Session.new(:connection => connection)

session.auth(options[:auth]) if options[:auth]

if block_given?
yield session
session.quit
Expand Down
15 changes: 15 additions & 0 deletions lib/nntp/session.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ def initialize(options)
raise ArgumentError ":connection missing"
end
end

def auth(args)
auth_method = args.fetch(:type, :standard)
standard_auth(args) if auth_method == :standard
end

def groups
group_list = []
connection.query :list do |status, list|
Expand Down Expand Up @@ -55,6 +61,15 @@ def quit
end

private
def standard_auth(args)
status = connection.command(:authinfo, "USER #{args[:user]}")
if status[:code] == 381
connection.command(:authinfo, "PASS #{args[:pass]}")
elsif [281, 482, 502].include? status[:code]
status
end
end

def group_from_list(group_string)
params = group_string.split
name = params[0]
Expand Down
12 changes: 12 additions & 0 deletions spec/session_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,18 @@
let(:sock) { double() }
let(:nntp) { NNTP.open(:socket => sock) }

describe "authorization" do
it "will use authinfo style authentication if a user and pass are provided" do
NNTP::Session.any_instance.should_receive(:auth).
with({:user => 'foo', :pass => 'bar'}).and_call_original
NNTP::Session.any_instance.should_receive(:standard_auth)
NNTP.open(
:socket => sock,
:auth => {:user => 'foo', :pass => 'bar'}
)
end
end

describe "#groups" do
before(:each) do
@connection = double()
Expand Down

0 comments on commit 94f6488

Please sign in to comment.