twitter_oauth2 0.0.1 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: '0419576dd3cc8963503f733895c8ad852ae70acb24128dff23bf97abbf3ca785'
4
- data.tar.gz: 94722540e2acd589fc6498b216fe6c818de1a34f76d650300050e3100fe0065d
3
+ metadata.gz: f122e87aaf15c361806ef0e5b433d4581e5917602ccb1ecdc3ffc143dccb8c9a
4
+ data.tar.gz: ab48249dd3d6ef1d43028bb9fd80896a3b6f9dd051dd3def027cc92d6f6da7a9
5
5
  SHA512:
6
- metadata.gz: 9b89d5d4e00dc7c79fa391954dd3d4068944ab554c4b854c4c9920132ba1c369a9f8665114d67364d8ed993458a0614e973e5f5f6a97d604299175197e6d0653
7
- data.tar.gz: db47363df8d70c8f0786d65eb07a714218dbe2448b000f3004bbf3788485064fbd014df9b4d94e01cc6f9aa186addd35a996e7339df0ad6d738ab2e4aa59a425
6
+ metadata.gz: 3e28f5eef3c5b78cc78ef8fe46afb019bed576d9ee45a7f6ae74a9618fda9d3e6e549bd189c702dcaafa85d4ee148745b034c2fb5cefac1bfe9c849683e7b035
7
+ data.tar.gz: bc2708b3af710699168313319fa51b47a63cd640a1998b65b5890143f450203e20f5e948040577420adf2f8d2871f718e219e16c1f7a697e8655bcf5097e3e97
data/.travis.yml CHANGED
@@ -1,6 +1,7 @@
1
- ---
2
- language: ruby
3
- cache: bundler
1
+ before_install:
2
+ - gem install bundler
3
+
4
4
  rvm:
5
- - 2.7.2
6
- before_install: gem install bundler -v 2.2.28
5
+ - 2.5.8
6
+ - 2.6.6
7
+ - 2.7.2
data/README.md CHANGED
@@ -1,8 +1,6 @@
1
- # TwitterOauth2
1
+ # TwitterOAuth2
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/twitter_oauth2`. To experiment with that code, run `bin/console` for an interactive prompt.
4
-
5
- TODO: Delete this and the text above, and describe your gem
3
+ Twitter OAuth2 Client Library in Ruby.
6
4
 
7
5
  ## Installation
8
6
 
@@ -22,7 +20,50 @@ Or install it yourself as:
22
20
 
23
21
  ## Usage
24
22
 
25
- TODO: Write usage instructions here
23
+ This gem is built on `rack/oauth2` gem.
24
+ Basically, the usage is same with [the underling gem](https://github.com/nov/rack-oauth2/wiki).
25
+
26
+ The only difference is that this gem is supporting PKCE as default, since [Twitter **requires** it](https://developer.twitter.com/en/docs/twitter-api/oauth2).
27
+
28
+ ```ruby
29
+ require 'twitter_oauth2'
30
+
31
+ client = TwitterOAuth2::Client.new(
32
+ identifier: '<YOUR-CLIENT-ID>',
33
+ redirect_uri: '<YOUR-CALLBACK-URL>'
34
+ )
35
+
36
+ authorization_uri = client.authorization_uri(
37
+ scope: [
38
+ :'users.read',
39
+ :'tweet.read',
40
+ :'offline.access'
41
+ )
42
+
43
+ # NOTE:
44
+ # When `TwitterOAuth2::Client#authorization_uri` is called,
45
+ # PKCE `code_verifier` and `state` are automatically generated.
46
+ # You can get it here.
47
+
48
+ code_verifier = client.code_verifier
49
+ state = client.state
50
+
51
+ puts authorization_uri
52
+ `open "#{authorization_uri}"`
53
+
54
+ print 'code: ' and STDOUT.flush
55
+ code = gets.chop
56
+
57
+ # NOTE: Obtaining Access Token & Refresh Token using Authorization Code
58
+ client.authorization_code = code
59
+ token_response = client.access_token! code_verifier
60
+
61
+ # NOTE: Refreshing Access Token using Refresh Token
62
+ client.refresh_token = token_response.refresh_token
63
+ client.access_token!
64
+ ```
65
+
66
+ For more usage, read [the underling gem's wiki](https://github.com/nov/rack-oauth2/wiki).
26
67
 
27
68
  ## Development
28
69
 
@@ -40,4 +81,4 @@ The gem is available as open source under the terms of the [MIT License](https:/
40
81
 
41
82
  ## Code of Conduct
42
83
 
43
- Everyone interacting in the TwitterOauth2 project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/twitter_oauth2/blob/master/CODE_OF_CONDUCT.md).
84
+ Everyone interacting in the TwitterOAuth2 project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/twitter_oauth2/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile CHANGED
@@ -1,8 +1,19 @@
1
- # frozen_string_literal: true
2
-
3
- require "bundler/gem_tasks"
4
- require "rspec/core/rake_task"
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
5
3
 
4
+ require 'rspec/core/rake_task'
6
5
  RSpec::Core::RakeTask.new(:spec)
7
6
 
8
- task default: :spec
7
+ namespace :coverage do
8
+ desc 'Open coverage report'
9
+ task :report do
10
+ require 'simplecov'
11
+ `open "#{File.join SimpleCov.coverage_path, 'index.html'}"`
12
+ end
13
+ end
14
+
15
+ task :spec do
16
+ Rake::Task[:'coverage:report'].invoke unless ENV['TRAVIS_RUBY_VERSION']
17
+ end
18
+
19
+ task :default => :spec
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.1
1
+ 0.4.0
@@ -1,5 +1,7 @@
1
1
  module TwitterOAuth2
2
2
  class Client < Rack::OAuth2::Client
3
+ attr_accessor :code_verifier, :code_challenge, :code_challenge_method, :state
4
+
3
5
  def initialize(attributes)
4
6
  attributes_with_default = {
5
7
  authorization_endpoint: 'https://twitter.com/i/oauth2/authorize',
@@ -9,29 +11,37 @@ module TwitterOAuth2
9
11
  end
10
12
 
11
13
  def authorization_uri(params = {})
12
- code_challenge, code_verifier = setup_pkce_session
14
+ authorization_session!
13
15
  authorization_uri = super({
14
16
  code_challenge: code_challenge,
15
- code_challenge_method: :s256
17
+ code_challenge_method: code_challenge_method,
18
+ state: state
16
19
  }.merge(params))
17
- [authorization_uri, code_verifier]
18
20
  end
19
21
 
20
- def access_token!(code_verifier, options = {})
22
+ def access_token!(*args)
23
+ options = args.extract_options!
21
24
  super :body, {
22
- code_verifier: code_verifier
25
+ code_verifier: args.first || self.code_verifier
23
26
  }.merge(options)
24
27
  end
25
28
 
26
29
  private
27
30
 
28
- def setup_pkce_session
29
- code_verifier = SecureRandom.hex(8)
30
- code_challenge = Base64.urlsafe_encode64(
31
+ def authorization_session!
32
+ self.state = Base64.urlsafe_encode64(
33
+ SecureRandom.random_bytes(16),
34
+ padding: false
35
+ )
36
+ self.code_verifier = Base64.urlsafe_encode64(
37
+ SecureRandom.random_bytes(32),
38
+ padding: false
39
+ )
40
+ self.code_challenge = Base64.urlsafe_encode64(
31
41
  OpenSSL::Digest::SHA256.digest(code_verifier),
32
42
  padding: false
33
43
  )
34
- [code_challenge, code_verifier]
44
+ self.code_challenge_method = :s256
35
45
  end
36
46
  end
37
47
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: twitter_oauth2
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - nov
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-10-01 00:00:00.000000000 Z
11
+ date: 2021-10-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rack-oauth2
@@ -148,7 +148,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
148
148
  - !ruby/object:Gem::Version
149
149
  version: '0'
150
150
  requirements: []
151
- rubygems_version: 3.1.4
151
+ rubygems_version: 3.0.3
152
152
  signing_key:
153
153
  specification_version: 4
154
154
  summary: Twitter OAuth 2.0 Client