yt-auth 0.0.0 → 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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: aa59f6dde7868b7585152893893aa77a79404528
4
- data.tar.gz: 64625fa7604cb3f253c577102ade540aa317c14d
3
+ metadata.gz: eae916ff8a0b31773c93ae830d2d151c894724af
4
+ data.tar.gz: 8143530fa4e8b21f3457fc411602e55d4d13ddd5
5
5
  SHA512:
6
- metadata.gz: 135e580bd9fb49f1d4011fc63a2f3dcb5899b7879e632558b1b5be9517d7033c87244c31792172b0be41c0d24ff9e32282dc1a8e06c754ab9940c2a81ed72fa2
7
- data.tar.gz: 17888e161fe3e40501f2257e0a94428ddcb2d56ba9427f2e2a15922be4508aa0b2bafaca0ff81ab33ad534a80bda31b469cc94cb08fb66c59e33ce509669b11b
6
+ metadata.gz: a0e6e06cc458c1ec49e1e17fdf86bbe76f37982e4b875ac0e6dc2c7325a94116186512f09a2a2ddf05f2a74f1d00f10444a3fbaf5f69a4e9783298df10edd28a
7
+ data.tar.gz: 41678fd22b58200a537f4a7c9461b61dd3d6f2fc1180644afc59b7f38610fe7015d37f0c580d3300a940737d937cf0bb923f769e87ab2ec8427769f0dc7e258f
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml CHANGED
@@ -1,3 +1,9 @@
1
+ sudo: false
1
2
  language: ruby
3
+ notifications:
4
+ email: false
2
5
  rvm:
3
- - 2.2.2
6
+ - 2.3.1
7
+ script:
8
+ - bundle exec rspec
9
+ - bundle exec yard stats | grep "100.00% documented"
data/CHANGELOG.md ADDED
@@ -0,0 +1,13 @@
1
+ # Changelog
2
+
3
+ All notable changes to this project will be documented in this file.
4
+
5
+ For more information about changelogs, check
6
+ [Keep a Changelog](http://keepachangelog.com) and
7
+ [Vandamme](http://tech-angels.github.io/vandamme).
8
+
9
+ ## 0.1.0 - 2017-03-27
10
+
11
+ * [FEATURE] Add `AuthError`
12
+ * [FEATURE] Add `email`
13
+ * [FEATURE] Add `url`
data/LICENSE.txt CHANGED
@@ -1,6 +1,6 @@
1
1
  The MIT License (MIT)
2
2
 
3
- Copyright (c) 2015 claudiob
3
+ Copyright (c) 2017 Fullscreen, Inc.
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
data/README.md CHANGED
@@ -1,3 +1,70 @@
1
- # Yt::Auth
1
+ Yt::Auth - authenticate users with their Google account
2
+ =======================================================
2
3
 
3
- :star: :star: :star:
4
+ Yt::Auth helps you write apps that need to authenticate users by means of their Google account.
5
+
6
+ The **source code** is available on [GitHub](https://github.com/fullscreen/yt-auth) and the **documentation** on [RubyDoc](http://www.rubydoc.info/gems/yt-auth/frames).
7
+
8
+ [![Build Status](http://img.shields.io/travis/Fullscreen/yt-auth/master.svg)](https://travis-ci.org/Fullscreen/yt-auth)
9
+ [![Coverage Status](http://img.shields.io/coveralls/Fullscreen/yt-auth/master.svg)](https://coveralls.io/r/Fullscreen/yt-auth)
10
+ [![Dependency Status](http://img.shields.io/gemnasium/Fullscreen/yt-auth.svg)](https://gemnasium.com/Fullscreen/yt-auth)
11
+ [![Code Climate](http://img.shields.io/codeclimate/github/Fullscreen/yt-auth.svg)](https://codeclimate.com/github/Fullscreen/yt-auth)
12
+ [![Online docs](http://img.shields.io/badge/docs-✓-green.svg)](http://www.rubydoc.info/gems/yt-auth/frames)
13
+ [![Gem Version](http://img.shields.io/gem/v/yt-auth.svg)](http://rubygems.org/gems/yt-auth)
14
+
15
+ The Yt::Auth library provides two methods: `url` and `email`.
16
+
17
+ Yt::Auth#url
18
+ ------------
19
+
20
+ With the `url` method, you can obtain a URL where to redirect users who need to
21
+ authenticate with their Google account in order to use your application:
22
+
23
+ ```ruby
24
+ redirect_uri = 'https://example.com/auth' # REPLACE WITH REAL ONE
25
+ Yt::Auth.new(redirect_uri: redirect_uri).url
26
+ # => https://accounts.google.com/o/oauth2/auth?client_id=...&scope=email&redirect_uri=https%3A%2F%2Fexample.com%2Fauth&response_type=code
27
+ ```
28
+
29
+ Yt::Auth#email
30
+ --------------
31
+
32
+ After users have authenticated with their Google account, they will be
33
+ redirected to the `redirect_uri` you indicated, with an extra `code` query
34
+ parameter, e.g. `https://example.com/auth?code=1234`
35
+
36
+ With the `email` method, you can obtain the verified email of the users:
37
+
38
+ ```ruby
39
+ redirect_uri = 'https://example.com/auth' # REPLACE WITH REAL ONE
40
+ code = '1234' # REPLACE WITH REAL ONE
41
+ Yt::Auth.new(redirect_uri: redirect_uri, code: code).email
42
+ # => "user@example.com"
43
+ ```
44
+
45
+ Yt::AuthError
46
+ -------------
47
+
48
+ `Yt::AuthError` will be raised whenever something goes wrong during the
49
+ authentication process. The message of the error will include the details:
50
+
51
+ ```ruby
52
+ redirect_uri = 'https://example.com/auth' # REPLACE WITH REAL ONE
53
+ code = 'this-is-not-a-valid-code'
54
+ Yt::Auth.new(redirect_uri: redirect_uri, code: code).email
55
+ # => Yt::AuthError: Invalid authorization code.
56
+ ```
57
+
58
+
59
+ How to contribute
60
+ =================
61
+
62
+ Contribute to the code by forking the project, adding the missing code,
63
+ writing the appropriate tests and submitting a pull request.
64
+
65
+ In order for a PR to be approved, all the tests need to pass and all the public
66
+ methods need to be documented and listed in the guides. Remember:
67
+
68
+ - to run all tests locally: `bundle exec rspec`
69
+ - to generate the docs locally: `bundle exec yard`
70
+ - to list undocumented methods: `bundle exec yard stats --list-undoc`
data/Rakefile CHANGED
@@ -1 +1,9 @@
1
- require "bundler/gem_tasks"
1
+ require 'bundler/gem_tasks'
2
+
3
+ require 'rspec/core/rake_task'
4
+ require 'rspec/core/version'
5
+
6
+ desc 'Run all examples'
7
+ RSpec::Core::RakeTask.new :spec
8
+
9
+ task default: [:spec]
data/bin/console CHANGED
@@ -1,14 +1,14 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- require "bundler/setup"
4
- require "yt-auth"
3
+ require 'bundler/setup'
4
+ require 'yt/auth'
5
5
 
6
6
  # You can add fixtures and/or initialization code here to make experimenting
7
7
  # with your gem easier. You can also use a different console, if you like.
8
8
 
9
9
  # (If you use this, don't forget to add pry to your Gemfile!)
10
- # require "pry"
10
+ # require 'pry'
11
11
  # Pry.start
12
12
 
13
- require "irb"
14
- IRB.start
13
+ require 'irb'
14
+ IRB.start(__FILE__)
data/bin/setup CHANGED
@@ -1,6 +1,7 @@
1
- #!/bin/bash
1
+ #!/usr/bin/env bash
2
2
  set -euo pipefail
3
3
  IFS=$'\n\t'
4
+ set -vx
4
5
 
5
6
  bundle install
6
7
 
@@ -1,5 +1,6 @@
1
1
  module Yt
2
- module Auth
3
- VERSION = "0.0.0"
2
+ class Auth
3
+ # current version of gem
4
+ VERSION = '0.1.0'
4
5
  end
5
6
  end
data/lib/yt/auth.rb ADDED
@@ -0,0 +1,80 @@
1
+ require 'yt/config'
2
+ require 'yt/auth_request'
3
+
4
+ # An object-oriented Ruby client for YouTube.
5
+ # @see http://www.rubydoc.info/gems/yt/
6
+ module Yt
7
+ # Provides methods to authenticate a user with the Google OAuth flow.
8
+ # @see https://developers.google.com/accounts/docs/OAuth2
9
+ class Auth
10
+ # @param [Hash] options the options to initialize an instance of Yt::Auth.
11
+ # @option options [String] :redirect_uri The URI to redirect users to
12
+ # after they have completed the Google OAuth flow.
13
+ # @option options [String] :code A single-use authorization code provided
14
+ # by Google OAuth to obtain an access token to access Google API.
15
+ def initialize(options = {})
16
+ @redirect_uri = options[:redirect_uri]
17
+ @code = options[:code]
18
+ end
19
+
20
+ # @return [String] the URL where to authenticate with a Google account.
21
+ def url
22
+ host = 'accounts.google.com'
23
+ path = '/o/oauth2/auth'
24
+ query = URI.encode_www_form url_params
25
+ URI::HTTPS.build(host: host, path: path, query: query).to_s
26
+ end
27
+
28
+ # @return [String] the email of an authenticated Google account.
29
+ def email
30
+ response = AuthRequest.new(email_params).run
31
+ response.body['email']
32
+ end
33
+
34
+ private
35
+
36
+ def url_params
37
+ {}.tap do |params|
38
+ params[:client_id] = Yt.configuration.client_id
39
+ params[:scope] = :email
40
+ params[:redirect_uri] = @redirect_uri
41
+ params[:response_type] = :code
42
+ end
43
+ end
44
+
45
+ def email_params
46
+ {}.tap do |params|
47
+ params[:path] = '/oauth2/v2/userinfo'
48
+ params[:headers] = {Authorization: "Bearer #{tokens['access_token']}"}
49
+ end
50
+ end
51
+
52
+ # @return [Hash] the tokens of an authenticated Google account.
53
+ def tokens
54
+ AuthRequest.new(tokens_params).run.body
55
+ end
56
+
57
+ def tokens_params
58
+ {}.tap do |params|
59
+ params[:host] = 'accounts.google.com'
60
+ params[:path] = '/o/oauth2/token'
61
+ params[:method] = :post
62
+ params[:request_format] = :form
63
+ params[:body] = tokens_body
64
+ params[:error_message] = ->(body) {
65
+ JSON(body)['error_description'] || 'Invalid authorization code.'
66
+ }
67
+ end
68
+ end
69
+
70
+ def tokens_body
71
+ {}.tap do |params|
72
+ params[:client_id] = Yt.configuration.client_id
73
+ params[:client_secret] = Yt.configuration.client_secret
74
+ params[:code] = @code
75
+ params[:redirect_uri] = @redirect_uri
76
+ params[:grant_type] = :authorization_code
77
+ end
78
+ end
79
+ end
80
+ end
@@ -0,0 +1,5 @@
1
+ module Yt
2
+ # A wrapper around StandardError.
3
+ class AuthError < StandardError
4
+ end
5
+ end
@@ -0,0 +1,91 @@
1
+ require 'net/http'
2
+ require 'json'
3
+ require 'yt/auth_error'
4
+
5
+ module Yt
6
+ # @private
7
+ # A wrapper around Net::HTTP to send HTTP requests to any web API and
8
+ # return their result or raise an error if the result is unexpected.
9
+ class AuthRequest
10
+ # Initializes an AuthRequest object.
11
+ def initialize(options = {})
12
+ @host = options.fetch :host, 'www.googleapis.com'
13
+ @path = options[:path]
14
+ @method = options.fetch :method, :get
15
+ @headers = options.fetch :headers, {}
16
+ @body = options[:body]
17
+ @request_format = options.fetch :request_format, :json
18
+ @error_message = options.fetch :error_message, ->(body) {"Error: #{body}"}
19
+ end
20
+
21
+ # Sends the request and returns the response.
22
+ def run
23
+ if response.is_a? Net::HTTPSuccess
24
+ response.tap do
25
+ parse_response!
26
+ end
27
+ else
28
+ raise Yt::AuthError, error_message
29
+ end
30
+ end
31
+
32
+ private
33
+
34
+ # @return [URI::HTTPS] the (memoized) URI of the request.
35
+ def uri
36
+ attributes = {host: @host, path: @path}
37
+ @uri ||= URI::HTTPS.build attributes
38
+ end
39
+
40
+ # @return [Net::HTTPRequest] the full HTTP request object,
41
+ # inclusive of headers of request body.
42
+ def http_request
43
+ net_http_class = Object.const_get "Net::HTTP::#{@method.capitalize}"
44
+ @http_request ||= net_http_class.new(uri.request_uri).tap do |request|
45
+ set_request_body! request
46
+ set_request_headers! request
47
+ end
48
+ end
49
+
50
+ # Adds the request body to the request in the appropriate format.
51
+ # if the request body is a JSON Object, transform its keys into camel-case,
52
+ # since this is the common format for JSON APIs.
53
+ def set_request_body!(request)
54
+ if @body
55
+ request.set_form_data @body
56
+ end
57
+ end
58
+
59
+ # Adds the request headers to the request in the appropriate format.
60
+ # The User-Agent header is also set to recognize the request, and to
61
+ # tell the server that gzip compression can be used, since Net::HTTP
62
+ # supports it and automatically sets the Accept-Encoding header.
63
+ def set_request_headers!(request)
64
+ if @request_format == :json
65
+ request.initialize_http_header 'Content-Type' => 'application/json'
66
+ end
67
+ @headers.each do |name, value|
68
+ request.add_field name, value
69
+ end
70
+ end
71
+
72
+ # Run the request and memoize the response or the server error received.
73
+ def response
74
+ @response ||= Net::HTTP.start(uri.host, uri.port, use_ssl: true) do |http|
75
+ http.request http_request
76
+ end
77
+ end
78
+
79
+ # Replaces the body of the response with the parsed version of the body,
80
+ # according to the format specified in the AuthRequest.
81
+ def parse_response!
82
+ if response.body
83
+ response.body = JSON response.body
84
+ end
85
+ end
86
+
87
+ def error_message
88
+ @error_message.call response.body
89
+ end
90
+ end
91
+ end
data/yt-auth.gemspec CHANGED
@@ -4,22 +4,30 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
  require 'yt/auth/version'
5
5
 
6
6
  Gem::Specification.new do |spec|
7
- spec.name = "yt-auth"
7
+ spec.name = 'yt-auth'
8
8
  spec.version = Yt::Auth::VERSION
9
- spec.authors = ["claudiob"]
10
- spec.email = ["claudiob@gmail.com"]
9
+ spec.authors = ['Claudio Baccigalupo', 'Kang-Kyu Lee']
10
+ spec.email = ['claudio@fullscreen.net', 'kang-kyu.lee@fullscreen.net']
11
11
 
12
- spec.summary = %q{Write a short summary, because Rubygems requires one.}
13
- spec.description = %q{Write a longer description or delete this line.}
14
- spec.homepage = "https://github.com/fullscreen/yt-auth"
15
- spec.license = "MIT"
12
+ spec.summary = %q{Google Authentication Ruby client}
13
+ spec.description = %q{Yt::Auth makes it easy to authenticate users to any
14
+ web application by means of their Google account.}
15
+ spec.homepage = 'https://github.com/fullscreen/yt-auth'
16
+ spec.license = 'MIT'
17
+
18
+ spec.required_ruby_version = '>= 2.2.2'
16
19
 
17
- # Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
18
20
  spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
19
- spec.bindir = "exe"
21
+ spec.bindir = 'exe'
20
22
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
- spec.require_paths = ["lib"]
23
+ spec.require_paths = ['lib']
24
+
25
+ spec.add_dependency 'yt-support', '>= 0.1'
22
26
 
23
- spec.add_development_dependency "bundler", "~> 1.9"
24
- spec.add_development_dependency "rake", "~> 10.0"
27
+ spec.add_development_dependency 'bundler', '~> 1.14'
28
+ spec.add_development_dependency 'rspec', '~> 3.5'
29
+ spec.add_development_dependency 'rake', '~> 10.0'
30
+ spec.add_development_dependency 'coveralls', '~> 0.8.15'
31
+ spec.add_development_dependency 'pry-nav', '~> 0.2.4'
32
+ spec.add_development_dependency 'yard', '~> 0.9.5'
25
33
  end
metadata CHANGED
@@ -1,29 +1,58 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: yt-auth
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.0
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
- - claudiob
7
+ - Claudio Baccigalupo
8
+ - Kang-Kyu Lee
8
9
  autorequire:
9
10
  bindir: exe
10
11
  cert_chain: []
11
- date: 2017-03-22 00:00:00.000000000 Z
12
+ date: 2017-03-27 00:00:00.000000000 Z
12
13
  dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: yt-support
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - ">="
19
+ - !ruby/object:Gem::Version
20
+ version: '0.1'
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ version: '0.1'
13
28
  - !ruby/object:Gem::Dependency
14
29
  name: bundler
15
30
  requirement: !ruby/object:Gem::Requirement
16
31
  requirements:
17
32
  - - "~>"
18
33
  - !ruby/object:Gem::Version
19
- version: '1.9'
34
+ version: '1.14'
20
35
  type: :development
21
36
  prerelease: false
22
37
  version_requirements: !ruby/object:Gem::Requirement
23
38
  requirements:
24
39
  - - "~>"
25
40
  - !ruby/object:Gem::Version
26
- version: '1.9'
41
+ version: '1.14'
42
+ - !ruby/object:Gem::Dependency
43
+ name: rspec
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - "~>"
47
+ - !ruby/object:Gem::Version
48
+ version: '3.5'
49
+ type: :development
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - "~>"
54
+ - !ruby/object:Gem::Version
55
+ version: '3.5'
27
56
  - !ruby/object:Gem::Dependency
28
57
  name: rake
29
58
  requirement: !ruby/object:Gem::Requirement
@@ -38,23 +67,72 @@ dependencies:
38
67
  - - "~>"
39
68
  - !ruby/object:Gem::Version
40
69
  version: '10.0'
41
- description: Write a longer description or delete this line.
70
+ - !ruby/object:Gem::Dependency
71
+ name: coveralls
72
+ requirement: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - "~>"
75
+ - !ruby/object:Gem::Version
76
+ version: 0.8.15
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - "~>"
82
+ - !ruby/object:Gem::Version
83
+ version: 0.8.15
84
+ - !ruby/object:Gem::Dependency
85
+ name: pry-nav
86
+ requirement: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - "~>"
89
+ - !ruby/object:Gem::Version
90
+ version: 0.2.4
91
+ type: :development
92
+ prerelease: false
93
+ version_requirements: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - "~>"
96
+ - !ruby/object:Gem::Version
97
+ version: 0.2.4
98
+ - !ruby/object:Gem::Dependency
99
+ name: yard
100
+ requirement: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - "~>"
103
+ - !ruby/object:Gem::Version
104
+ version: 0.9.5
105
+ type: :development
106
+ prerelease: false
107
+ version_requirements: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - "~>"
110
+ - !ruby/object:Gem::Version
111
+ version: 0.9.5
112
+ description: |-
113
+ Yt::Auth makes it easy to authenticate users to any
114
+ web application by means of their Google account.
42
115
  email:
43
- - claudiob@gmail.com
116
+ - claudio@fullscreen.net
117
+ - kang-kyu.lee@fullscreen.net
44
118
  executables: []
45
119
  extensions: []
46
120
  extra_rdoc_files: []
47
121
  files:
48
122
  - ".gitignore"
123
+ - ".rspec"
49
124
  - ".travis.yml"
125
+ - CHANGELOG.md
50
126
  - Gemfile
51
127
  - LICENSE.txt
52
128
  - README.md
53
129
  - Rakefile
54
130
  - bin/console
55
131
  - bin/setup
56
- - lib/yt-auth.rb
132
+ - lib/yt/auth.rb
57
133
  - lib/yt/auth/version.rb
134
+ - lib/yt/auth_error.rb
135
+ - lib/yt/auth_request.rb
58
136
  - yt-auth.gemspec
59
137
  homepage: https://github.com/fullscreen/yt-auth
60
138
  licenses:
@@ -68,7 +146,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
68
146
  requirements:
69
147
  - - ">="
70
148
  - !ruby/object:Gem::Version
71
- version: '0'
149
+ version: 2.2.2
72
150
  required_rubygems_version: !ruby/object:Gem::Requirement
73
151
  requirements:
74
152
  - - ">="
@@ -79,5 +157,5 @@ rubyforge_project:
79
157
  rubygems_version: 2.6.11
80
158
  signing_key:
81
159
  specification_version: 4
82
- summary: Write a short summary, because Rubygems requires one.
160
+ summary: Google Authentication Ruby client
83
161
  test_files: []
data/lib/yt-auth.rb DELETED
@@ -1,3 +0,0 @@
1
- module Yt::Auth
2
- # Your code goes here...
3
- end