mail-xoauth 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d2029f29f71b33164985ee6ca57e92a092e16bc4
4
+ data.tar.gz: 373dde7ebc4feb01c9e62f27f7556462a98a3b8c
5
+ SHA512:
6
+ metadata.gz: b50b2074cd2b1a7fec11514c33530c3f8ab3adace21a64975ebc537e1eb725a18176aab1a89a101bfce2e9cfd060c88f7c9c85927992a155e101e490b8668c80
7
+ data.tar.gz: 8620c019ba076e378041301c1f055c2476058d81689e2e9431770ba2fc7ad2fd0bf1294d019476d12f647a024ea3e4cce5655e4920e4f3de4c401899aa51b48b
@@ -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 mail-xoauth.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 ahawkins
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,60 @@
1
+ # Mail::Xoauth
2
+
3
+ Make the [Mail](https://github.com/mikel/mail) work with Gmail using
4
+ [OAuth](https://developers.google.com/gmail/oauth_overview). This
5
+ project is build on top of [gmail_xaouth](https://github.com/nfo/gmail_xoauth).
6
+ `Mail::Xoauth` simply makes provides a nice connect to mail's rich
7
+ delivery and retrevial interface.
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ gem 'mail-xoauth'
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install mail-xoauth
22
+
23
+ ## Usage
24
+
25
+ This gem **does not retreive oauth access or refresh tokesn for you!**
26
+ You must retrieve them yourself. `Mail::Xoauth` requires an access
27
+ token. It's up to you to ensure a valid access token using a refresh
28
+ token. If you do not know how to do that, then look at the tests.
29
+ There is an example class to handle this. Given a valid oauth access
30
+ token and email address you're ready to connect.
31
+
32
+ ### Getting Email
33
+
34
+ ```ruby
35
+ gmail = Mail::XOauthIMAP.new address: 'example@gmail.com', access_token: 'foo'
36
+
37
+ # Query using Mail's standard interface
38
+ gmail.all
39
+ ```
40
+
41
+ ### Sending Mail
42
+
43
+ ```ruby
44
+ email = Mail.new do
45
+ from 'example@gmail.com'
46
+ to 'example@example.com'
47
+ subject 'XOauth Test'
48
+ end
49
+
50
+ gmail = Mail::XOauthSMTP.new address: 'example@example.com', access_token: 'foo'
51
+ gmail.deliver! email
52
+ ```
53
+
54
+ ## Contributing
55
+
56
+ 1. Fork it
57
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
58
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
59
+ 4. Push to the branch (`git push origin my-new-feature`)
60
+ 5. Create new Pull Request
@@ -0,0 +1,18 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ desc "Run tests"
4
+ task :test do
5
+ # Do this is way to avoid forking another ruby process.
6
+ # See: http://ngauthier.com/2012/02/quick-tests-with-bash.html
7
+ root = File.dirname __FILE__
8
+ Dir["#{root}/test/**/*_test.rb"].each do |test_file|
9
+ begin
10
+ require test_file
11
+ rescue => ex
12
+ puts ex
13
+ puts ex.backtrace.join("\n")
14
+ exit!
15
+ end
16
+ end
17
+ end
18
+ task :default => :test
@@ -0,0 +1,5 @@
1
+ email: testing@radiumcrm.com
2
+ oauth_key: 148490173223.apps.googleusercontent.com
3
+ oauth_secret: IftxRkgtDISFDBbKBFm2Aw9K
4
+ access_token: ya29.AHES6ZSb_Pqh9RW-F6ZruqOobQoT5iWEoDUsXYsSW1l0tPs
5
+ refresh_token: 1/KU5bpLcckmUgBBgz6g66mgfCSyVrgvx6kdciswhBPEg
@@ -0,0 +1 @@
1
+ require 'mail/xoauth'
@@ -0,0 +1,27 @@
1
+ module Mail
2
+ class XOauthSMTP < SMTP
3
+ class MissingAuthInfo < KeyError ; end
4
+
5
+ def initialize(values)
6
+ fail MissingAuthInfo unless values[:address]
7
+ fail MissingAuthInfo unless values[:access_token]
8
+ super values
9
+ end
10
+
11
+ def deliver!(mail)
12
+ smtp_from, smtp_to, message = check_delivery_params(mail)
13
+
14
+ smtp = Net::SMTP.new('smtp.gmail.com', 587)
15
+ smtp.enable_starttls_auto
16
+ smtp.start('gmail.com', settings.fetch(:address), settings.fetch(:access_token), :xoauth2) do |connection|
17
+ response = connection.sendmail(message, smtp_from, smtp_to)
18
+ end
19
+
20
+ if settings[:return_response]
21
+ response
22
+ else
23
+ self
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,24 @@
1
+ module Mail
2
+ class XOauthIMAP < IMAP
3
+ class MissingAuthInfo < KeyError ; end
4
+
5
+ def initialize(values)
6
+ fail MissingAuthInfo unless values[:address]
7
+ fail MissingAuthInfo unless values[:access_token]
8
+ super values
9
+ end
10
+
11
+ def start(config=Mail::Configuration.instance, &block)
12
+ raise ArgumentError.new("Mail::Retrievable#imap_start takes a block") unless block_given?
13
+
14
+ imap = Net::IMAP.new('imap.gmail.com', 993, usessl = true, certs = nil, verify = false)
15
+ imap.authenticate 'XOAUTH2', settings.fetch(:address), settings.fetch(:access_token)
16
+
17
+ yield imap
18
+ ensure
19
+ if defined?(imap) && imap && !imap.disconnected?
20
+ imap.disconnect
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,12 @@
1
+ require "mail/xoauth/version"
2
+ require "mail"
3
+ require "gmail_xoauth"
4
+ require "mail/network/retreiver_methods/xoauth_imap"
5
+ require "mail/network/delivery_methods/xoauth_smtp"
6
+
7
+ module Mail
8
+ module Xoauth
9
+ # Your code goes here...
10
+ end
11
+ end
12
+
@@ -0,0 +1,5 @@
1
+ module Mail
2
+ module Xoauth
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'mail/xoauth/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "mail-xoauth"
8
+ spec.version = Mail::Xoauth::VERSION
9
+ spec.authors = ["ahawkins"]
10
+ spec.email = ["me@broadcastingadam.com"]
11
+ spec.description = %q{Gmail OAuth IMAP & SMTP connections for the Mail gem}
12
+ spec.summary = %q{}
13
+ spec.homepage = "https://github.com/ahawkins/mail-xoauth"
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 'mail'
22
+ spec.add_dependency 'gmail_xoauth'
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.3"
25
+ spec.add_development_dependency "rake"
26
+ spec.add_development_dependency "faraday"
27
+ spec.add_development_dependency "faraday_middleware"
28
+ end
@@ -0,0 +1,60 @@
1
+ require_relative "./test_helper"
2
+
3
+ class GettingMailTest < MiniTest::Unit::TestCase
4
+ def test_connects_and_returns_messages
5
+ gmail = Mail::XOauthIMAP.new address: Config.email, access_token: access_token!
6
+ gmail.all
7
+ end
8
+
9
+ def test_round_trips_email
10
+ email = Mail.new do
11
+ from Config.email
12
+ to Config.email
13
+ subject 'XOauth Test'
14
+ end
15
+
16
+ gmail = Mail::XOauthSMTP.new address: Config.email, access_token: access_token!
17
+
18
+ gmail.deliver! email
19
+ end
20
+
21
+ def test_raises_an_error_when_missing_the_address_for_imap
22
+ assert_raises Mail::XOauthIMAP::MissingAuthInfo do
23
+ Mail::XOauthIMAP.new address: nil, access_token: 'foo'
24
+ end
25
+
26
+ assert_raises Mail::XOauthIMAP::MissingAuthInfo do
27
+ Mail::XOauthIMAP.new access_token: 'foo'
28
+ end
29
+ end
30
+
31
+ def test_raises_an_error_when_missing_the_access_token_for_imap
32
+ assert_raises Mail::XOauthIMAP::MissingAuthInfo do
33
+ Mail::XOauthIMAP.new address: 'foo', access_token: nil
34
+ end
35
+
36
+ assert_raises Mail::XOauthIMAP::MissingAuthInfo do
37
+ Mail::XOauthIMAP.new address: 'foo'
38
+ end
39
+ end
40
+
41
+ def test_raises_an_error_when_missing_the_address_for_smtp
42
+ assert_raises Mail::XOauthSMTP::MissingAuthInfo do
43
+ Mail::XOauthSMTP.new address: nil, access_token: 'foo'
44
+ end
45
+
46
+ assert_raises Mail::XOauthSMTP::MissingAuthInfo do
47
+ Mail::XOauthSMTP.new access_token: 'foo'
48
+ end
49
+ end
50
+
51
+ def test_raises_an_error_when_missing_the_access_token_for_smtp
52
+ assert_raises Mail::XOauthSMTP::MissingAuthInfo do
53
+ Mail::XOauthSMTP.new address: 'foo', access_token: nil
54
+ end
55
+
56
+ assert_raises Mail::XOauthSMTP::MissingAuthInfo do
57
+ Mail::XOauthSMTP.new address: 'foo'
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,109 @@
1
+ require 'bundler/setup'
2
+ require 'mail-xoauth'
3
+ require 'minitest/autorun'
4
+ require 'faraday'
5
+ require 'faraday_middleware'
6
+
7
+ config_klass = Class.new do
8
+ attr_accessor :email
9
+ attr_accessor :access_token, :refresh_token
10
+ attr_accessor :oauth_key, :oauth_secret
11
+
12
+ def initialize(hash = {})
13
+ hash.each_pair do |name, value|
14
+ send "#{name}=", value
15
+ end
16
+ end
17
+ end
18
+
19
+ root = File.expand_path "../../", __FILE__
20
+ Config = config_klass.new YAML.load_file "#{root}/config.yml"
21
+
22
+ class MiniTest::Unit::TestCase
23
+ def access_token!
24
+ auth.refresh_token! Config.refresh_token
25
+ auth.access_token
26
+ end
27
+
28
+ private
29
+ def auth
30
+ @auth ||= Oauth.new Config.oauth_key, Config.oauth_secret
31
+ end
32
+
33
+ # Handle Authentication for the Google Data API. For more information
34
+ # @see http://code.google.com/apis/gdata/docs/auth/overview.html
35
+ class Oauth
36
+ END_POINT = 'https://accounts.google.com/o/oauth2'.freeze
37
+
38
+ attr_reader :access_token, :refresh_token
39
+
40
+ def initialize(client_id, client_secret, redirect_uri = nil)
41
+ @auth_ok = false
42
+ @client_id = client_id
43
+ @client_secret = client_secret
44
+ @redirect_uri = redirect_uri
45
+ end
46
+
47
+ def authorize_code!(code)
48
+ response = request :code => code,
49
+ :client_id => client_id, :client_secret => client_secret,
50
+ :redirect_uri => redirect_uri,
51
+ :grant_type => 'authorization_code'
52
+
53
+ @access_token = response['access_token']
54
+ @refresh_token = response['refresh_token']
55
+ @auth_ok = true
56
+ end
57
+
58
+ # This method is for subsequent initialization if a user has already granted access to
59
+ # the application. It will fetch a new access_token from a refresh_token.
60
+ def refresh_token!(refresh_token)
61
+ @refresh_token = refresh_token
62
+
63
+ response = request :refresh_token => refresh_token,
64
+ :client_id => client_id, :client_secret => client_secret,
65
+ :grant_type => 'refresh_token'
66
+
67
+ @access_token = response['access_token']
68
+ @auth_ok = true
69
+ end
70
+
71
+ # Return the HTTP headers appropriate for this authentication type. This method
72
+ # is used in Google:HTTP.
73
+ # @return [Hash]
74
+ def http_headers
75
+ {'Authorization' => "Bearer #{@access_token}"}
76
+ end
77
+
78
+ private
79
+ def client_id
80
+ @client_id
81
+ end
82
+
83
+ def client_secret
84
+ @client_secret
85
+ end
86
+
87
+ def redirect_uri
88
+ @redirect_uri
89
+ end
90
+
91
+ # Send a request to the Token uri
92
+ # @param [String] body The body to POST
93
+ # @return [Net::HTTPResponse]
94
+ def request(params)
95
+ conn = Faraday.new do |builder|
96
+ builder.request :url_encoded
97
+
98
+ builder.use FaradayMiddleware::ParseJson
99
+ builder.response :raise_error
100
+
101
+ builder.adapter :net_http
102
+ end
103
+
104
+ response = conn.post "#{END_POINT}/token", params
105
+
106
+ response.body
107
+ end
108
+ end
109
+ end
metadata ADDED
@@ -0,0 +1,144 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mail-xoauth
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - ahawkins
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-07-25 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: mail
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: gmail_xoauth
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '1.3'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '1.3'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: faraday
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: faraday_middleware
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description: Gmail OAuth IMAP & SMTP connections for the Mail gem
98
+ email:
99
+ - me@broadcastingadam.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - .gitignore
105
+ - Gemfile
106
+ - LICENSE.txt
107
+ - README.md
108
+ - Rakefile
109
+ - config.yml
110
+ - lib/mail-xoauth.rb
111
+ - lib/mail/network/delivery_methods/xoauth_smtp.rb
112
+ - lib/mail/network/retreiver_methods/xoauth_imap.rb
113
+ - lib/mail/xoauth.rb
114
+ - lib/mail/xoauth/version.rb
115
+ - mail-xoauth.gemspec
116
+ - test/email_test.rb
117
+ - test/test_helper.rb
118
+ homepage: https://github.com/ahawkins/mail-xoauth
119
+ licenses:
120
+ - MIT
121
+ metadata: {}
122
+ post_install_message:
123
+ rdoc_options: []
124
+ require_paths:
125
+ - lib
126
+ required_ruby_version: !ruby/object:Gem::Requirement
127
+ requirements:
128
+ - - '>='
129
+ - !ruby/object:Gem::Version
130
+ version: '0'
131
+ required_rubygems_version: !ruby/object:Gem::Requirement
132
+ requirements:
133
+ - - '>='
134
+ - !ruby/object:Gem::Version
135
+ version: '0'
136
+ requirements: []
137
+ rubyforge_project:
138
+ rubygems_version: 2.0.3
139
+ signing_key:
140
+ specification_version: 4
141
+ summary: ''
142
+ test_files:
143
+ - test/email_test.rb
144
+ - test/test_helper.rb