yt-auth 0.2.3 → 0.3.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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +15 -0
- data/README.md +52 -17
- data/lib/yt/auth.rb +59 -31
- data/lib/yt/auth/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 90acfd2ff7bdc68980c0c12c9e72761981cecb55
|
4
|
+
data.tar.gz: 863100fe5cfcf1429428980fd18f0df8c8b1c2c5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 21301ea2bd24299adc34767cdbc33d21fd96c622bd562ded4c420a4ccfd237a32b3c5b708a9e64ae2dffdd1e1296fcb3ff041eff71db8e5ef0da564f0299f2b8
|
7
|
+
data.tar.gz: 1da658c63db79a7908f6ba1ba11052e1835eb6bc1b82b5753be318b4d5c2a002beb547406f73280094e4ee405af622dd74d3ca87948f2f9d1efcb66d050d3cb0
|
data/CHANGELOG.md
CHANGED
@@ -6,6 +6,21 @@ For more information about changelogs, check
|
|
6
6
|
[Keep a Changelog](http://keepachangelog.com) and
|
7
7
|
[Vandamme](http://tech-angels.github.io/vandamme).
|
8
8
|
|
9
|
+
## 0.3.0 - 2017-08-25
|
10
|
+
|
11
|
+
**How to upgrade**
|
12
|
+
|
13
|
+
If your code uses `Yt::Auth#url` then you must use `Yt::Auth.url_for` instead.
|
14
|
+
If your code uses `Yt::Auth.new(code:)` then you must use `Yt::Auth.create(code:)` instead.
|
15
|
+
If your code uses `Yt::Auth.new(refresh_token:)` then you must use `Yt::Auth.find_by(refresh_token:)` instead.
|
16
|
+
|
17
|
+
* [ENHANCEMENT] Extract `Auth#url` into `Auth.url_for`
|
18
|
+
* [ENHANCEMENT] Rename `Auth.new(code:)` into `Auth.create(code:)`
|
19
|
+
* [ENHANCEMENT] Rename `Auth.new(refresh_token:)` into `Auth.create(refresh_token:)`
|
20
|
+
* [FEATURE] Yt::Auth.url_for now accepts the scope to authenticate
|
21
|
+
* [FEATURE] Yt::Auth.url_for now accepts an option to force re-authentication
|
22
|
+
* [FEATURE] Add `Auth#revoke` to revoke a refresh token
|
23
|
+
|
9
24
|
## 0.2.3 - 2017-08-24
|
10
25
|
|
11
26
|
* [FEATURE] Add the ability to generate access token from a refresh token
|
data/README.md
CHANGED
@@ -16,48 +16,83 @@ The **source code** is available on [GitHub](https://github.com/fullscreen/yt-au
|
|
16
16
|
[](http://www.rubydoc.info/gems/yt-auth/frames)
|
17
17
|
[](http://rubygems.org/gems/yt-auth)
|
18
18
|
|
19
|
-
|
19
|
+
Yt::Auth.url_for
|
20
|
+
----------------
|
20
21
|
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
With the `url` method, you can obtain a URL where to redirect users who need to
|
25
|
-
authenticate with their Google account in order to use your application:
|
22
|
+
With the `url_for` class method, you can obtain a URL where to redirect users
|
23
|
+
who need to authenticate with their Google account in order to use your
|
24
|
+
application:
|
26
25
|
|
27
26
|
```ruby
|
28
27
|
redirect_uri = 'https://example.com/auth' # REPLACE WITH REAL ONE
|
29
|
-
|
28
|
+
scope = %i(yt-analytics.readonly youtube)
|
29
|
+
Yt::Auth.url_for(redirect_uri: redirect_uri, scope: scope, force: true)
|
30
30
|
# => https://accounts.google.com/o/oauth2/auth?client_id=...&scope=email&redirect_uri=https%3A%2F%2Fexample.com%2Fauth&response_type=code
|
31
31
|
```
|
32
32
|
|
33
|
-
Yt::Auth
|
34
|
-
|
33
|
+
Yt::Auth.create
|
34
|
+
----------------
|
35
35
|
|
36
36
|
After users have authenticated with their Google account, they will be
|
37
37
|
redirected to the `redirect_uri` you indicated, with an extra `code` query
|
38
38
|
parameter, e.g. `https://example.com/auth?code=1234`
|
39
39
|
|
40
|
-
With the `
|
40
|
+
With the `create` class method, you can create an instance for that
|
41
|
+
authentication:
|
41
42
|
|
42
43
|
```ruby
|
43
44
|
redirect_uri = 'https://example.com/auth' # REPLACE WITH REAL ONE
|
44
|
-
code = '
|
45
|
-
Yt::Auth.
|
45
|
+
code = 'dfwe7r9djd234ffdjf3009dfknfd98re' # REPLACE WITH REAL ONE
|
46
|
+
auth = Yt::Auth.create(redirect_uri: redirect_uri, code: code)
|
47
|
+
# => #<Yt::Auth:0x007fe61d…>
|
48
|
+
```
|
49
|
+
|
50
|
+
Yt::Auth#email
|
51
|
+
--------------
|
52
|
+
|
53
|
+
Once you have an instance of `Yt::Auth`, you can obtain the verified email
|
54
|
+
of the authenticated user:
|
55
|
+
|
56
|
+
```ruby
|
57
|
+
auth.email
|
46
58
|
# => "user@example.com"
|
47
59
|
```
|
48
60
|
|
49
61
|
Yt::Auth#access_token
|
50
62
|
---------------------
|
51
63
|
|
52
|
-
|
64
|
+
Once you have an instance of `Yt::Auth`, you can also obtain the access token
|
65
|
+
of the authenticated user:
|
53
66
|
|
54
67
|
```ruby
|
55
|
-
|
56
|
-
|
57
|
-
Yt::Auth.new(redirect_uri: redirect_uri, code: code).access_token
|
58
|
-
# => "ya29.GltbBLXt74GrwX8S_xr70aX"
|
68
|
+
auth.access_token
|
69
|
+
# => "ya29.df8er8e9r89er"
|
59
70
|
```
|
60
71
|
|
72
|
+
Yt::Auth#refresh_token
|
73
|
+
----------------------
|
74
|
+
|
75
|
+
Once you have an instance of `Yt::Auth`, you can also obtain the refresh token
|
76
|
+
of the authenticated user:
|
77
|
+
|
78
|
+
```ruby
|
79
|
+
auth.refresh_token
|
80
|
+
# => "sdf7f7erre98df"
|
81
|
+
```
|
82
|
+
|
83
|
+
Yt::Auth.find_by
|
84
|
+
----------------
|
85
|
+
|
86
|
+
If you already know the refresh token of a Google account, you can obtain its
|
87
|
+
complete authentication object:
|
88
|
+
|
89
|
+
```ruby
|
90
|
+
auth = Auth.find_by(refresh_token: "sdf7f7erre98df")
|
91
|
+
auth.email
|
92
|
+
# => "user@example.com"
|
93
|
+
```
|
94
|
+
|
95
|
+
|
61
96
|
Yt::HTTPError
|
62
97
|
-------------
|
63
98
|
|
data/lib/yt/auth.rb
CHANGED
@@ -13,20 +13,48 @@ module Yt
|
|
13
13
|
# after they have completed the Google OAuth flow.
|
14
14
|
# @option options [String] :code A single-use authorization code provided
|
15
15
|
# by Google OAuth to obtain an access token to access Google API.
|
16
|
-
def
|
17
|
-
|
18
|
-
|
19
|
-
|
16
|
+
def self.create(options = {})
|
17
|
+
new options.merge(grant_type: :authorization_code)
|
18
|
+
end
|
19
|
+
|
20
|
+
# @param [Hash] options the options to initialize an instance of Yt::Auth.
|
21
|
+
# @option options [String] :refresh_token A multi-use refresh token to
|
22
|
+
# obtain an access token to access Google API.
|
23
|
+
def self.find_by(options = {})
|
24
|
+
new options.merge(grant_type: :refresh_token)
|
20
25
|
end
|
21
26
|
|
22
27
|
# @return [String] the URL where to authenticate with a Google account.
|
23
|
-
|
28
|
+
# @param [Hash] options the options to initialize an instance of Yt::Auth.
|
29
|
+
# @option options [String] :redirect_uri The URI to redirect users to
|
30
|
+
# after they have completed the Google OAuth flow.
|
31
|
+
# @option options [Boolean] :force whether to force users to re-authenticate
|
32
|
+
# an account that was previously authenticated.
|
33
|
+
# @option options [Array<String>] :scopes The list of scopes that users
|
34
|
+
# are requested to authorize.
|
35
|
+
def self.url_for(options = {})
|
24
36
|
host = 'accounts.google.com'
|
25
37
|
path = '/o/oauth2/auth'
|
26
|
-
query = URI.encode_www_form url_params
|
38
|
+
query = URI.encode_www_form url_params(options)
|
27
39
|
URI::HTTPS.build(host: host, path: path, query: query).to_s
|
28
40
|
end
|
29
41
|
|
42
|
+
# @param [Hash] options the options to initialize an instance of Yt::Auth.
|
43
|
+
# @option options [String] :grant_type
|
44
|
+
# @option options [String] :redirect_uri
|
45
|
+
# @option options [String] :code
|
46
|
+
# @option options [String] :refresh_token
|
47
|
+
def initialize(options = {})
|
48
|
+
@tokens_body = options
|
49
|
+
@tokens_body[:client_id] = Yt.configuration.client_id
|
50
|
+
@tokens_body[:client_secret] = Yt.configuration.client_secret
|
51
|
+
end
|
52
|
+
|
53
|
+
# @return [Boolean] whether the authentication was revoked.
|
54
|
+
def revoke
|
55
|
+
!!HTTPRequest.new(revoke_params).run
|
56
|
+
end
|
57
|
+
|
30
58
|
# @return [String] the email of an authenticated Google account.
|
31
59
|
def email
|
32
60
|
profile['email']
|
@@ -37,21 +65,33 @@ module Yt
|
|
37
65
|
tokens['access_token']
|
38
66
|
end
|
39
67
|
|
68
|
+
# @return [String] the refresh token of an authenticated Google account.
|
69
|
+
def refresh_token
|
70
|
+
tokens['refresh_token']
|
71
|
+
end
|
72
|
+
|
40
73
|
private
|
41
74
|
|
42
|
-
def url_params
|
75
|
+
def self.url_params(options)
|
43
76
|
{}.tap do |params|
|
44
77
|
params[:client_id] = Yt.configuration.client_id
|
45
|
-
params[:scope] = :
|
78
|
+
params[:scope] = scope_for(options.fetch :scopes, [])
|
46
79
|
params[:access_type] = :offline
|
47
|
-
params[:
|
80
|
+
params[:approval_prompt] = options[:force] ? :force : :auto
|
81
|
+
params[:redirect_uri] = options[:redirect_uri]
|
48
82
|
params[:response_type] = :code
|
49
83
|
end
|
50
84
|
end
|
51
85
|
|
86
|
+
def self.scope_for(scopes)
|
87
|
+
['userinfo.email'].concat(scopes).map do |scope|
|
88
|
+
"https://www.googleapis.com/auth/#{scope}"
|
89
|
+
end.join(' ')
|
90
|
+
end
|
91
|
+
|
52
92
|
# @return [Hash] the tokens of an authenticated Google account.
|
53
93
|
def tokens
|
54
|
-
HTTPRequest.new(tokens_params).run.body
|
94
|
+
@tokens ||= HTTPRequest.new(tokens_params).run.body
|
55
95
|
end
|
56
96
|
|
57
97
|
# @return [Hash] the profile of an authenticated Google account.
|
@@ -66,34 +106,22 @@ module Yt
|
|
66
106
|
params[:path] = '/o/oauth2/token'
|
67
107
|
params[:method] = :post
|
68
108
|
params[:request_format] = :form
|
69
|
-
params[:body] = tokens_body
|
70
|
-
params[:error_message] = ->(body) {
|
71
|
-
JSON(body)['error_description'] || token_error_message
|
72
|
-
}
|
109
|
+
params[:body] = @tokens_body
|
110
|
+
params[:error_message] = ->(body) { error_message_for body }
|
73
111
|
end
|
74
112
|
end
|
75
113
|
|
76
|
-
def
|
114
|
+
def revoke_params
|
77
115
|
{}.tap do |params|
|
78
|
-
params[:
|
79
|
-
params[:
|
80
|
-
|
81
|
-
params[:refresh_token] = @refresh_token
|
82
|
-
params[:grant_type] = :refresh_token
|
83
|
-
else
|
84
|
-
params[:code] = @code
|
85
|
-
params[:redirect_uri] = @redirect_uri
|
86
|
-
params[:grant_type] = :authorization_code
|
87
|
-
end
|
116
|
+
params[:host] = 'accounts.google.com'
|
117
|
+
params[:path] = '/o/oauth2/revoke'
|
118
|
+
params[:params] = {token: refresh_token || access_token}
|
88
119
|
end
|
89
120
|
end
|
90
121
|
|
91
|
-
def
|
92
|
-
|
93
|
-
|
94
|
-
else
|
95
|
-
'Invalid authorization code.'
|
96
|
-
end
|
122
|
+
def error_message_for(body)
|
123
|
+
key = @tokens_body[:grant_type].to_s.tr '_', ' '
|
124
|
+
JSON(body)['error_description'] || "Invalid #{key}."
|
97
125
|
end
|
98
126
|
end
|
99
127
|
end
|
data/lib/yt/auth/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: yt-auth
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Claudio Baccigalupo
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: exe
|
11
11
|
cert_chain: []
|
12
|
-
date: 2017-08-
|
12
|
+
date: 2017-08-26 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: yt-support
|