faraday-digestauth 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in faraday-digestauth.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Brian Haberer
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,43 @@
1
+ # Faraday::Digestauth
2
+
3
+ This gem started as a direct copy of a gist belonging to @kapkaev
4
+
5
+ It is located at https://gist.github.com/kapkaev/5088751
6
+
7
+ I merely gemmed it up so that I could use it in multiple places.
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ gem 'faraday-digestauth'
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install faraday-digestauth
22
+
23
+ ## Usage
24
+
25
+ ```
26
+ require 'faraday'
27
+ require 'faraday/digestauth'
28
+
29
+ @conn = Faraday.new(url: HOSTNAME) do |f|
30
+ f.request :digest, USERNAME, PASSWORD
31
+ f.adapter Faraday.default_adapter
32
+ end
33
+
34
+ @conn.get 'resource'
35
+ ```
36
+
37
+ ## Contributing
38
+
39
+ 1. Fork it
40
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
41
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
42
+ 4. Push to the branch (`git push origin my-new-feature`)
43
+ 5. Create new Pull Request
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'faraday/digestauth/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "faraday-digestauth"
8
+ spec.version = Faraday::DigestAuth::VERSION
9
+ spec.authors = ["Brian Haberer", "Ildar Kapkaev"]
10
+ spec.email = ["bhaberer@gmail.com", "kirs.box@gmail.com"]
11
+ spec.description = %q{Faraday extension to enable digest auth}
12
+ spec.summary = %q{Digest Auth for Faraday}
13
+ spec.homepage = "https://github.com/bhaberer/faraday-digestauth"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency 'faraday'
22
+
23
+ spec.add_development_dependency 'bundler', '~> 1.3'
24
+ spec.add_development_dependency 'rake'
25
+ end
@@ -0,0 +1,103 @@
1
+ require 'faraday'
2
+ require 'faraday/digestauth/version'
3
+ require 'net/http/digest_auth'
4
+
5
+ module Faraday
6
+ # Public: A Faraday middleware to use digest authentication. Since order of
7
+ # middlewares do care, it should be the first one of the Request middlewares
8
+ # in order to work properly (due to how digest authentication works).
9
+ #
10
+ # If some requests using the connection don't need to use digest auth you
11
+ # don't have to worry, the middleware will do nothing.
12
+ #
13
+ # It uses Net::HTTP::DigestAuth to generate the authorization header but it
14
+ # should work with any adapter.
15
+ #
16
+ # Examples:
17
+ #
18
+ # connection = Faraday.new(...) do |connection|
19
+ # connection.request :digest, USER, PASSWORD
20
+ # end
21
+ #
22
+ # # You can also use it later with a connection:
23
+ # connection.digest_auth('USER', 'PASSWORD')
24
+ #
25
+ class Request::DigestAuth < Faraday::Middleware
26
+
27
+
28
+ # Public: Initializes a DigestAuth.
29
+ #
30
+ # app - The Faraday app.
31
+ # user - A String with the user to authentication the connection.
32
+ # password - A String with the password to authentication the connection.
33
+ def initialize(app, user, password)
34
+ super(app)
35
+ @user, @password = user, password
36
+ end
37
+
38
+
39
+ # Public: Sends a first request with an empty body to get the
40
+ # authentication headers and then send the same request with the body and
41
+ # authorization header.
42
+ #
43
+ # env - A Hash with the request environment.
44
+ #
45
+ # Returns a Faraday::Response.
46
+ def call(env)
47
+ response = handshake(env)
48
+ return response unless response.status == 401
49
+
50
+
51
+ env[:request_headers]['Authorization'] = header(response)
52
+ @app.call(env)
53
+ end
54
+
55
+
56
+ private
57
+ # Internal: Sends the the request with an empry body.
58
+ #
59
+ # env - A Hash with the request environment.
60
+ #
61
+ # Returns a Faraday::Response.
62
+ def handshake(env)
63
+ env_without_body = env.dup
64
+ env_without_body.delete(:body)
65
+ @app.call(env_without_body)
66
+ end
67
+
68
+
69
+ # Internal: Builds the authorization header with the authentication data.
70
+ #
71
+ # response - A Faraday::Response with the authenticate headers.
72
+ #
73
+ # Returns a String with the DigestAuth header.
74
+ def header(response)
75
+ uri = response.env[:url]
76
+ uri.user = @user
77
+ uri.password = @password
78
+
79
+
80
+ realm = response.headers['www-authenticate']
81
+ method = response.env[:method].to_s.upcase
82
+
83
+
84
+ Net::HTTP::DigestAuth.new.auth_header(uri, realm, method)
85
+ end
86
+ end
87
+
88
+ class Connection
89
+ # Public: Adds the digest auth middleware at the top and sets the user and
90
+ # password.
91
+ #
92
+ # user - A String with the user.
93
+ # password - A String with the password.
94
+ #
95
+ def digest_auth(user, password)
96
+ self.builder.insert(0, Faraday::Request::DigestAuth, user, password)
97
+ end
98
+ end
99
+ end
100
+
101
+
102
+ # Register the middleware as a Request middleware with the name :digest
103
+ Faraday.register_middleware :request, digest: Faraday::Request::DigestAuth
@@ -0,0 +1,5 @@
1
+ module Faraday
2
+ module DigestAuth
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,105 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: faraday-digestauth
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Brian Haberer
9
+ - Ildar Kapkaev
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2013-11-06 00:00:00.000000000 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: faraday
17
+ requirement: !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ! '>='
21
+ - !ruby/object:Gem::Version
22
+ version: '0'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ none: false
27
+ requirements:
28
+ - - ! '>='
29
+ - !ruby/object:Gem::Version
30
+ version: '0'
31
+ - !ruby/object:Gem::Dependency
32
+ name: bundler
33
+ requirement: !ruby/object:Gem::Requirement
34
+ none: false
35
+ requirements:
36
+ - - ~>
37
+ - !ruby/object:Gem::Version
38
+ version: '1.3'
39
+ type: :development
40
+ prerelease: false
41
+ version_requirements: !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ~>
45
+ - !ruby/object:Gem::Version
46
+ version: '1.3'
47
+ - !ruby/object:Gem::Dependency
48
+ name: rake
49
+ requirement: !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: !ruby/object:Gem::Requirement
58
+ none: false
59
+ requirements:
60
+ - - ! '>='
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ description: Faraday extension to enable digest auth
64
+ email:
65
+ - bhaberer@gmail.com
66
+ - kirs.box@gmail.com
67
+ executables: []
68
+ extensions: []
69
+ extra_rdoc_files: []
70
+ files:
71
+ - .gitignore
72
+ - Gemfile
73
+ - LICENSE.txt
74
+ - README.md
75
+ - Rakefile
76
+ - faraday-digestauth.gemspec
77
+ - lib/faraday/digestauth.rb
78
+ - lib/faraday/digestauth/version.rb
79
+ homepage: https://github.com/bhaberer/faraday-digestauth
80
+ licenses:
81
+ - MIT
82
+ post_install_message:
83
+ rdoc_options: []
84
+ require_paths:
85
+ - lib
86
+ required_ruby_version: !ruby/object:Gem::Requirement
87
+ none: false
88
+ requirements:
89
+ - - ! '>='
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ required_rubygems_version: !ruby/object:Gem::Requirement
93
+ none: false
94
+ requirements:
95
+ - - ! '>='
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ requirements: []
99
+ rubyforge_project:
100
+ rubygems_version: 1.8.25
101
+ signing_key:
102
+ specification_version: 3
103
+ summary: Digest Auth for Faraday
104
+ test_files: []
105
+ has_rdoc: