gmail_xoauth 0.3.0 → 0.3.2

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE CHANGED
@@ -1,5 +1,5 @@
1
1
  Author: Nicolas Fouché <nicolas@silentale.com>
2
- Copyright: (C) 2010 Silentale SAS
2
+ Copyright: (C) 2011 Silentale SAS
3
3
 
4
4
  Licensed under the Apache License, Version 2.0 (the "License");
5
5
  you may not use this file except in compliance with the License.
data/README.markdown CHANGED
@@ -1,10 +1,10 @@
1
- # gmail_xoauth
1
+ # gmail_xoauth [![Dependency Status](https://gemnasium.com/nfo/gmail_xoauth.png)](https://gemnasium.com/nfo/gmail_xoauth)
2
2
 
3
3
  Get access to [Gmail IMAP and STMP via OAuth](http://code.google.com/apis/gmail/oauth), using the standard Ruby Net libraries.
4
4
 
5
- The gem supports 3-legged and 2-legged OAuth.
5
+ The gem supports 3-legged OAuth, and 2-legged OAuth for Google Apps Business or Education account owners.
6
6
 
7
- Note: 2-legged OAuth support was coded by [Wojciech Kruszewski](https://github.com/wojciech). I could not test it as I don't have a Google Apps Business or Education account. Please [send me a message](https://github.com/inbox/new/nfo) if it works for you.
7
+ Note: 2-legged OAuth support was added by [Wojciech Kruszewski](https://github.com/wojciech).
8
8
 
9
9
  ## Install
10
10
 
@@ -37,6 +37,16 @@ For your tests, Gmail allows to set 'anonymous' as the consumer key and secret.
37
37
 
38
38
  Note that the [Net::IMAP#login](http://www.ruby-doc.org/core/classes/Net/IMAP.html#M004191) method does not use support custom authenticators, so you have to use the [Net::IMAP#authenticate](http://www.ruby-doc.org/core/classes/Net/IMAP.html#M004190) method.
39
39
 
40
+ If you use 2-legged OAuth:
41
+
42
+ require 'gmail_xoauth'
43
+ imap = Net::IMAP.new('imap.gmail.com', 993, usessl = true, certs = nil, verify = false)
44
+ imap.authenticate('XOAUTH', 'myemail@mydomain.com',
45
+ :two_legged => true,
46
+ :consumer_key => 'a',
47
+ :consumer_secret => 'b'
48
+ )
49
+
40
50
  ### SMTP
41
51
 
42
52
  For your tests, Gmail allows to set 'anonymous' as the consumer key and secret.
@@ -55,6 +65,20 @@ For your tests, Gmail allows to set 'anonymous' as the consumer key and secret.
55
65
 
56
66
  Note that +Net::SMTP#enable_starttls_auto+ is not defined in Ruby 1.8.6.
57
67
 
68
+ If you use 2-legged OAuth:
69
+
70
+ require 'gmail_xoauth'
71
+ smtp = Net::SMTP.new('smtp.gmail.com', 587)
72
+ smtp.enable_starttls_auto
73
+ secret = {
74
+ :two_legged => true,
75
+ :consumer_key => 'a',
76
+ :consumer_secret => 'b'
77
+ }
78
+ smtp.start('gmail.com', 'myemail@mydomain.com', secret, :xoauth)
79
+ smtp.finish
80
+
81
+
58
82
  ## Compatibility
59
83
 
60
84
  Tested on Ruby MRI 1.8.6, 1.8.7, 1.9.1 and 1.9.2. Feel free to send me a message if you tested this code with other implementations of Ruby.
@@ -63,7 +87,9 @@ The only external dependency is the [oauth gem](http://rubygems.org/gems/oauth).
63
87
 
64
88
  ## History
65
89
 
66
- * 0.3.0 Experimental 2-legged OAuth support. [Give some feedback](https://github.com/inbox/new/nfo) !
90
+ * 0.3.2 New email for the maintainer
91
+ * 0.3.1 2-legged OAuth support confirmed by [BobDohnal](https://github.com/BobDohnal)
92
+ * 0.3.0 Experimental 2-legged OAuth support
67
93
  * 0.2.0 SMTP support
68
94
  * 0.1.0 Initial release with IMAP support and 3-legged OAuth
69
95
 
@@ -83,4 +109,4 @@ http://about.me/nfo
83
109
 
84
110
  ## Copyright
85
111
 
86
- Copyright (c) 2010 Silentale SAS. See LICENSE for details.
112
+ Copyright (c) 2011 Silentale SAS. See LICENSE for details.
@@ -1,3 +1,3 @@
1
1
  module GmailXoauth
2
- VERSION = "0.3.0"
2
+ VERSION = "0.3.2"
3
3
  end
data/test/helper.rb ADDED
@@ -0,0 +1,44 @@
1
+ require 'yaml'
2
+
3
+ require 'rubygems'
4
+ require 'test/unit'
5
+ require 'mocha'
6
+
7
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
8
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
9
+ require 'gmail_xoauth'
10
+
11
+ # Wanna debug ? Activate the IMAP debug mode, it will show the client/server conversation
12
+ # Net::IMAP.debug = true
13
+
14
+ # SMTP debugging can only be enabled on Net::SMTP instances
15
+ # Net::SMTP.class_eval do
16
+ # def initialize_with_debug(*args)
17
+ # initialize_without_debug(*args)
18
+ # @debug_output = STDERR
19
+ # end
20
+ # alias_method :initialize_without_debug, :initialize
21
+ # alias_method :initialize, :initialize_with_debug
22
+ # end
23
+
24
+ VALID_CREDENTIALS = begin
25
+ YAML.load_file(File.join(File.dirname(__FILE__), 'valid_credentials.yml'))
26
+ rescue Errno::ENOENT
27
+ STDERR.puts %(
28
+ Warning: some tests are disabled because they require valid credentials. To enable them, create a file \"test/valid_credentials.yml\".
29
+ It should contain valid OAuth tokens. Valid tokens can be generated thanks to \"xoauth.py\":http://code.google.com/p/google-mail-xoauth-tools/.
30
+ Of course, this file is .gitignored. Template:
31
+
32
+ ---
33
+ :email: someuser@gmail.com
34
+ :consumer_key: anonymous # "anonymous" is a valid value for testing
35
+ :consumer_secret: anonymous # "anonymous" is a valid value for testing
36
+ :token: 1/nE2xBCDOU0429bTeJySE11kRE95qzKQNlfTaaBcDeFg
37
+ :token_secret: 123Z/bMsi9fFhN6qHFWOabcd
38
+
39
+ )
40
+ false
41
+ end
42
+
43
+ class Test::Unit::TestCase
44
+ end
@@ -0,0 +1,68 @@
1
+ require 'helper'
2
+
3
+ class TestImapXoauthAuthenticator < Test::Unit::TestCase
4
+
5
+ def setup
6
+ end
7
+
8
+ def test_xoauth_authenticator_is_enabled
9
+ authenticators = Net::IMAP.__send__('class_variable_get', '@@authenticators')
10
+ assert_not_nil authenticators['XOAUTH']
11
+ assert_equal authenticators['XOAUTH'], GmailXoauth::ImapXoauthAuthenticator
12
+ end
13
+
14
+ def test_authenticate_with_invalid_credentials
15
+ imap = Net::IMAP.new('imap.gmail.com', 993, usessl = true, certs = nil, verify = false)
16
+ assert_raise(Net::IMAP::NoResponseError) do
17
+ imap.authenticate('XOAUTH', 'roger@moore.com',
18
+ :token => 'a',
19
+ :token_secret => 'b'
20
+ )
21
+ end
22
+ end
23
+
24
+ def test_authenticate_with_valid_credentials
25
+ return unless VALID_CREDENTIALS
26
+
27
+ imap = Net::IMAP.new('imap.gmail.com', 993, usessl = true, certs = nil, verify = false)
28
+ imap.authenticate('XOAUTH', VALID_CREDENTIALS[:email],
29
+ :consumer_key => VALID_CREDENTIALS[:consumer_key],
30
+ :consumer_secret => VALID_CREDENTIALS[:consumer_secret],
31
+ :token => VALID_CREDENTIALS[:token],
32
+ :token_secret => VALID_CREDENTIALS[:token_secret]
33
+ )
34
+ mailboxes = imap.list('', '*')
35
+ assert_instance_of Array, mailboxes
36
+ assert_instance_of Net::IMAP::MailboxList, mailboxes.first
37
+ ensure
38
+ imap.disconnect if imap
39
+ end
40
+
41
+ def test_2_legged_authenticate_with_invalid_credentials
42
+ imap = Net::IMAP.new('imap.gmail.com', 993, usessl = true, certs = nil, verify = false)
43
+ assert_raise(Net::IMAP::NoResponseError) do
44
+ imap.authenticate('XOAUTH', 'roger@moore.com',
45
+ :two_legged => true,
46
+ :consumer_key => 'a',
47
+ :consumer_secret => 'b'
48
+ )
49
+ end
50
+ end
51
+
52
+ def test_2_legged_authenticate_with_valid_credentials
53
+ return unless VALID_CREDENTIALS
54
+
55
+ imap = Net::IMAP.new('imap.gmail.com', 993, usessl = true, certs = nil, verify = false)
56
+ imap.authenticate('XOAUTH', VALID_CREDENTIALS[:email],
57
+ :two_legged => true,
58
+ :consumer_key => VALID_CREDENTIALS[:consumer_key],
59
+ :consumer_secret => VALID_CREDENTIALS[:consumer_secret]
60
+ )
61
+ mailboxes = imap.list('', '*')
62
+ assert_instance_of Array, mailboxes
63
+ assert_instance_of Net::IMAP::MailboxList, mailboxes.first
64
+ ensure
65
+ imap.disconnect if imap
66
+ end
67
+
68
+ end
@@ -0,0 +1,74 @@
1
+ require 'helper'
2
+
3
+ class TestOauthString < Test::Unit::TestCase
4
+
5
+ def setup
6
+ end
7
+
8
+ def test_build_oauth_string_should_accept_custom_consumer
9
+ OAuth::Helper.stubs(:generate_key).returns('abc')
10
+ OAuth::Helper.stubs(:generate_timestamp).returns(1274215474)
11
+
12
+ request_url = "https://mail.google.com/mail/b/user_name@gmail.com/imap/"
13
+ oauth_params = {
14
+ :consumer_key => 'c',
15
+ :consumer_secret => 'd',
16
+ :token => 'a',
17
+ :token_secret => 'b',
18
+ }
19
+
20
+ oauth_string = C.new.__send__('build_oauth_string', request_url, oauth_params)
21
+
22
+ assert_equal(
23
+ 'oauth_consumer_key="c",oauth_nonce="abc",oauth_signature="eseW9YybDf3fPToiwyLdUwSlfUw%3D",oauth_signature_method="HMAC-SHA1",oauth_timestamp="1274215474",oauth_token="a",oauth_version="1.0"',
24
+ oauth_string
25
+ )
26
+ end
27
+
28
+ def test_build_oauth_string_should_set_consumer_anonymous_by_default
29
+ OAuth::Helper.stubs(:generate_key).returns('abc')
30
+ OAuth::Helper.stubs(:generate_timestamp).returns(1274215474)
31
+
32
+ request_url = "https://mail.google.com/mail/b/user_name@gmail.com/imap/"
33
+ oauth_params = {
34
+ :token => 'a',
35
+ :token_secret => 'b',
36
+ }
37
+
38
+ oauth_string = C.new.__send__('build_oauth_string', request_url, oauth_params)
39
+
40
+ assert_equal(
41
+ 'oauth_consumer_key="anonymous",oauth_nonce="abc",oauth_signature="weu3Z%2Baqn6YUNnSLJmIvUwnCEmo%3D",oauth_signature_method="HMAC-SHA1",oauth_timestamp="1274215474",oauth_token="a",oauth_version="1.0"',
42
+ oauth_string
43
+ )
44
+ end
45
+
46
+ def test_build_2_legged_oauth_string_should_accept_custom_consumer
47
+ OAuth::Helper.stubs(:generate_key).returns('abc')
48
+ OAuth::Helper.stubs(:generate_timestamp).returns(1274215474)
49
+
50
+ request_url = "https://mail.google.com/mail/b/user_name@gmail.com/imap/?xoauth_requestor_id=user_name%40gmail.com";
51
+ oauth_params = {
52
+ :two_legged => true,
53
+ :consumer_key => 'c',
54
+ :consumer_secret => 'd',
55
+ :xoauth_requestor_id => 'user_name@gmail.com'
56
+ }
57
+
58
+ oauth_string = C.new.__send__('build_oauth_string', request_url, oauth_params)
59
+
60
+ assert_equal(
61
+ 'oauth_consumer_key="c",oauth_nonce="abc",oauth_signature="eG6PG7Q%2BPbI%2FNeLLCZ9PvlB%2BUjg%3D",oauth_signature_method="HMAC-SHA1",oauth_timestamp="1274215474",oauth_version="1.0"',
62
+ oauth_string
63
+ )
64
+ end
65
+
66
+ def test_build_sasl_client_request
67
+ assert_equal 'GET 1 2', C.new.__send__('build_sasl_client_request', '1', '2')
68
+ end
69
+
70
+ end
71
+
72
+ class C
73
+ include GmailXoauth::OauthString
74
+ end
@@ -0,0 +1,66 @@
1
+ require 'helper'
2
+
3
+ class TestSmtpXoauthAuthenticator < Test::Unit::TestCase
4
+
5
+ def setup
6
+ end
7
+
8
+ def test_smtp_authenticator_is_enabled
9
+ assert Net::SMTP.new(nil).respond_to?(:auth_xoauth), 'The Net::SMTP class should define the method :auth_xoauth'
10
+ end
11
+
12
+ def test_authenticate_with_invalid_credentials
13
+ smtp = Net::SMTP.new('smtp.gmail.com', 587)
14
+ smtp.enable_starttls_auto
15
+ assert_raise(Net::SMTPAuthenticationError) do
16
+ smtp.start('gmail.com', 'roger@moore.com', {:token => 'a', :token_secret => 'b'}, :xoauth)
17
+ end
18
+ end
19
+
20
+ def test_authenticate_with_valid_credentials
21
+ return unless VALID_CREDENTIALS
22
+
23
+ smtp = Net::SMTP.new('smtp.gmail.com', 587)
24
+ smtp.enable_starttls_auto
25
+
26
+ secret = {
27
+ :consumer_key => VALID_CREDENTIALS[:consumer_key],
28
+ :consumer_secret => VALID_CREDENTIALS[:consumer_secret],
29
+ :token => VALID_CREDENTIALS[:token],
30
+ :token_secret => VALID_CREDENTIALS[:token_secret],
31
+ }
32
+
33
+ assert_nothing_raised do
34
+ smtp.start('gmail.com', VALID_CREDENTIALS[:email], secret, :xoauth)
35
+ end
36
+ ensure
37
+ smtp.finish if smtp.started?
38
+ end
39
+
40
+ def test_2_legged_authenticate_with_invalid_credentials
41
+ smtp = Net::SMTP.new('smtp.gmail.com', 587)
42
+ smtp.enable_starttls_auto
43
+ assert_raise(Net::SMTPAuthenticationError) do
44
+ smtp.start('gmail.com', 'roger@moore.com', {:two_legged => true, :consumer_key => 'a', :consumer_secret => 'b'}, :xoauth)
45
+ end
46
+ end
47
+
48
+ def test_2_legged_authenticate_with_valid_credentials
49
+ return unless VALID_CREDENTIALS
50
+
51
+ smtp = Net::SMTP.new('smtp.gmail.com', 587)
52
+ smtp.enable_starttls_auto
53
+
54
+ secret = {
55
+ :two_legged => true,
56
+ :consumer_key => VALID_CREDENTIALS[:consumer_key],
57
+ :consumer_secret => VALID_CREDENTIALS[:consumer_secret],
58
+ }
59
+
60
+ assert_nothing_raised do
61
+ smtp.start('gmail.com', VALID_CREDENTIALS[:email], secret, :xoauth)
62
+ end
63
+ ensure
64
+ smtp.finish if smtp.started?
65
+ end
66
+ end
metadata CHANGED
@@ -1,100 +1,91 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: gmail_xoauth
3
- version: !ruby/object:Gem::Version
4
- prerelease: false
5
- segments:
6
- - 0
7
- - 3
8
- - 0
9
- version: 0.3.0
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.3.2
5
+ prerelease:
10
6
  platform: ruby
11
- authors:
12
- - "Nicolas Fouch\xC3\xA9"
7
+ authors:
8
+ - Nicolas Fouché
13
9
  autorequire:
14
10
  bindir: bin
15
11
  cert_chain: []
16
-
17
- date: 2010-12-13 00:00:00 +01:00
18
- default_executable:
19
- dependencies:
20
- - !ruby/object:Gem::Dependency
12
+ date: 2012-04-16 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
21
15
  name: oauth
22
- prerelease: false
23
- requirement: &id001 !ruby/object:Gem::Requirement
16
+ requirement: !ruby/object:Gem::Requirement
24
17
  none: false
25
- requirements:
26
- - - ">="
27
- - !ruby/object:Gem::Version
28
- segments:
29
- - 0
30
- - 3
31
- - 6
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
32
21
  version: 0.3.6
33
22
  type: :runtime
34
- version_requirements: *id001
35
- - !ruby/object:Gem::Dependency
36
- name: shoulda
37
23
  prerelease: false
38
- requirement: &id002 !ruby/object:Gem::Requirement
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: 0.3.6
30
+ - !ruby/object:Gem::Dependency
31
+ name: shoulda
32
+ requirement: !ruby/object:Gem::Requirement
39
33
  none: false
40
- requirements:
41
- - - ">="
42
- - !ruby/object:Gem::Version
43
- segments:
44
- - 0
45
- version: "0"
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
46
38
  type: :development
47
- version_requirements: *id002
48
- description: Get access to Gmail IMAP and STMP via OAuth, using the standard Ruby Net libraries
49
- email:
50
- - nicolas@silentale.com
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ description: Get access to Gmail IMAP and STMP via OAuth, using the standard Ruby
47
+ Net libraries
48
+ email:
49
+ - nicolas.fouche@gmail.com
51
50
  executables: []
52
-
53
51
  extensions: []
54
-
55
52
  extra_rdoc_files: []
56
-
57
- files:
53
+ files:
58
54
  - lib/gmail_xoauth/imap_xoauth_authenticator.rb
59
55
  - lib/gmail_xoauth/oauth_string.rb
60
56
  - lib/gmail_xoauth/smtp_xoauth_authenticator.rb
61
57
  - lib/gmail_xoauth/version.rb
62
58
  - lib/gmail_xoauth.rb
59
+ - test/helper.rb
60
+ - test/test_imap_xoauth_authenticator.rb
61
+ - test/test_oauth_string.rb
62
+ - test/test_smtp_xoauth_authenticator.rb
63
63
  - LICENSE
64
64
  - README.markdown
65
- has_rdoc: true
66
- homepage: http://geeks.silentale.com/2010/05/26/gmail-and-oauth-ruby-gem
65
+ homepage: https://github.com/nfo/gmail_xoauth
67
66
  licenses: []
68
-
69
67
  post_install_message:
70
- rdoc_options:
68
+ rdoc_options:
71
69
  - --charset=UTF-8
72
- require_paths:
70
+ require_paths:
73
71
  - lib
74
- required_ruby_version: !ruby/object:Gem::Requirement
72
+ required_ruby_version: !ruby/object:Gem::Requirement
75
73
  none: false
76
- requirements:
77
- - - ">="
78
- - !ruby/object:Gem::Version
79
- segments:
80
- - 0
81
- version: "0"
82
- required_rubygems_version: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ required_rubygems_version: !ruby/object:Gem::Requirement
83
79
  none: false
84
- requirements:
85
- - - ">="
86
- - !ruby/object:Gem::Version
87
- segments:
88
- - 1
89
- - 3
90
- - 6
80
+ requirements:
81
+ - - ! '>='
82
+ - !ruby/object:Gem::Version
91
83
  version: 1.3.6
92
84
  requirements: []
93
-
94
85
  rubyforge_project:
95
- rubygems_version: 1.3.7
86
+ rubygems_version: 1.8.21
96
87
  signing_key:
97
88
  specification_version: 3
98
- summary: Get access to Gmail IMAP and STMP via OAuth, using the standard Ruby Net libraries
89
+ summary: Get access to Gmail IMAP and STMP via OAuth, using the standard Ruby Net
90
+ libraries
99
91
  test_files: []
100
-