twitter_oauth2 0.1.0 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +6 -4
- data/README.md +47 -6
- data/VERSION +1 -1
- data/lib/twitter_oauth2/client.rb +27 -11
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b4e38a68d54be58e74f4ffc2236f0df0c2aa9a9d872eb180af0f902dcb0f3a1e
|
4
|
+
data.tar.gz: e0f00e6f8bf166574fdf636f6228c3a6adbcd0e7cfc79ac3ff280654411d8a05
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a9b38da9a8e90292815b6a8fbe162886780c93225ff6006f9d4c9fb64213958340c1e833aa4678aa1883ce31f74478fb1a058bfc4619b09697e899d31765dd02
|
7
|
+
data.tar.gz: 719fbeadae3a8813317e50a4ea5fa1c0e746f528440405f5534f0f690aea8a6bc0d3eab52b8b8c2938180f3c2d28d380d68afb8937c9d429e7bfa4810c2a37cb
|
data/.travis.yml
CHANGED
data/README.md
CHANGED
@@ -1,8 +1,6 @@
|
|
1
|
-
#
|
1
|
+
# TwitterOAuth2
|
2
2
|
|
3
|
-
|
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
|
-
|
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
|
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/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.5.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,43 @@ module TwitterOAuth2
|
|
9
11
|
end
|
10
12
|
|
11
13
|
def authorization_uri(params = {})
|
12
|
-
|
14
|
+
authorization_session!
|
13
15
|
authorization_uri = super({
|
14
16
|
code_challenge: code_challenge,
|
15
|
-
code_challenge_method:
|
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!(
|
21
|
-
|
22
|
-
|
23
|
-
|
22
|
+
def access_token!(*args)
|
23
|
+
options = args.extract_options!
|
24
|
+
super({
|
25
|
+
# NOTE:
|
26
|
+
# For some reason, Twitter requires client_id duplication both in body & header for confidentail clients.
|
27
|
+
# Follow such behaviour for now.
|
28
|
+
# Hopefully, I can remove this line in near future.
|
29
|
+
client_id: identifier,
|
30
|
+
|
31
|
+
code_verifier: args.first || self.code_verifier
|
32
|
+
}.merge(options))
|
24
33
|
end
|
25
34
|
|
26
35
|
private
|
27
36
|
|
28
|
-
def
|
29
|
-
|
30
|
-
|
37
|
+
def authorization_session!
|
38
|
+
self.state = Base64.urlsafe_encode64(
|
39
|
+
SecureRandom.random_bytes(16),
|
40
|
+
padding: false
|
41
|
+
)
|
42
|
+
self.code_verifier = Base64.urlsafe_encode64(
|
43
|
+
SecureRandom.random_bytes(32),
|
44
|
+
padding: false
|
45
|
+
)
|
46
|
+
self.code_challenge = Base64.urlsafe_encode64(
|
31
47
|
OpenSSL::Digest::SHA256.digest(code_verifier),
|
32
48
|
padding: false
|
33
49
|
)
|
34
|
-
|
50
|
+
self.code_challenge_method = :s256
|
35
51
|
end
|
36
52
|
end
|
37
53
|
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.
|
4
|
+
version: 0.5.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-
|
11
|
+
date: 2021-12-15 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
|
151
|
+
rubygems_version: 3.0.3.1
|
152
152
|
signing_key:
|
153
153
|
specification_version: 4
|
154
154
|
summary: Twitter OAuth 2.0 Client
|