login_with_google 0.1.0 → 1.0.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: '08423c8292dfec122ab23a3c88aa89706f8ea682232dff5e3e1b4d2cfa563947'
4
- data.tar.gz: 85435d394529a49c7c3e53a98211bbb797132d3955beb2e077032e1a7ca7fa03
3
+ metadata.gz: c3d1dbbb3a395fbee2c9650c7972f277932aad52296d20ce5ef228e8299ae0e5
4
+ data.tar.gz: fb363aea755694cade3a4f89eb26af4eeccf3bcd6bab1c87821e55983d8db9ee
5
5
  SHA512:
6
- metadata.gz: 23a90e8320b405bc1614611e14ce93964c2e9907f80249b26197f5bbdeef9c04d75378d8563f18b61e4708797d59f249aba7e6b8dd06b2b4e2372de6fecd7bd8
7
- data.tar.gz: 5f1ab5b35c300281bce51cbd28a1c1f0419e745749f8fe4bbe194cf074ffa7771eb49b8795ec0cbf41850ae21551bc49c39503edd43b56af5aa64a322adb3d1c
6
+ metadata.gz: b0e65868de9c01652b9ffa55a6c5b5e2fae81857c75f76792d1549535c180a50c2074fcd0a18bd732efb12b35983e1760aac756e7a2ffbf14eded01f0648b772
7
+ data.tar.gz: 320361a10618e187dc43773efe0421247961fb18b494b32a1062d12c2f25f781de0575edc4a2c4fc389dd7752bcce4aa9a03674456413ea654ad25e9f99eaf79
data/.gitignore CHANGED
@@ -6,3 +6,5 @@
6
6
  /pkg/
7
7
  /spec/reports/
8
8
  /tmp/
9
+ .DS_Store
10
+ Gemfile.lock
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --require spec_helper
data/README.md CHANGED
@@ -1,8 +1,5 @@
1
- # LoginWithGoogle
2
1
 
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/login_with_google`. 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
2
+ # login\_with\_google
6
3
 
7
4
  ## Installation
8
5
 
@@ -19,22 +16,57 @@ And then execute:
19
16
  Or install it yourself as:
20
17
 
21
18
  $ gem install login_with_google
19
+
20
+
21
+ Generate config file
22
+
23
+ $ bundle exec login_with_google:g
24
+
25
+ ## Creating your credentials
26
+
27
+ [Full steps to create your credentials](creating_credentials.md)
28
+
29
+ #### Add credentials and redirect_uri to env
30
+
31
+ ```
32
+ export G_CLIENT_ID='client_id'
33
+ export G_CLIENT_SECRET='client_secret'
34
+ export G_REDIRECT_URI='http://localhost:3000/callback'
35
+ ```
22
36
 
23
37
  ## Usage
24
38
 
25
- TODO: Write usage instructions here
39
+ #### Url to login
26
40
 
27
- ## Development
41
+ ```ruby
42
+ LoginWithGoogle::Api.url_in
43
+ ```
28
44
 
29
- After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
45
+ #### Url to login with helper
30
46
 
31
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
47
+ ```ruby
48
+ g_url_in
49
+ ```
50
+
51
+
52
+ #### Process callback
53
+
54
+ Get auth return keys `access_token expires_in scope token_type id_token`
55
+
56
+ ```ruby
57
+ @auth = LoginWithGoogle::Api.auth code: params[:code]
58
+ ```
32
59
 
33
- ## Contributing
60
+ Get user info return keys `id email verified_email name given_name family_name picture locale`
34
61
 
35
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/login_with_google. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/login_with_google/blob/master/CODE_OF_CONDUCT.md).
62
+ ```ruby
63
+ @info = LoginWithGoogle::Api.info @auth
64
+ ```
36
65
 
66
+ #### Refresh a token
37
67
 
38
- ## Code of Conduct
68
+ Pass a `access_token` from `@auth` and receive a keys `access_token expires_in scope token_type id_token`
39
69
 
40
- Everyone interacting in the LoginWithGoogle project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/login_with_google/blob/master/CODE_OF_CONDUCT.md).
70
+ ```ruby
71
+ @refresh = LoginWithGoogle::Api.refresh(token: @auth['access_token'])
72
+ ```
@@ -0,0 +1,49 @@
1
+ default: &default
2
+ app:
3
+ client_id: <%= ENV['G_CLIENT_ID'] %>
4
+ client_secret: <%= ENV['G_CLIENT_SECRET'] %>
5
+
6
+ auth:
7
+ grant_type: "authorization_code"
8
+ client_id: <%= ENV['G_CLIENT_ID'] %>
9
+ client_secret: <%= ENV['G_CLIENT_SECRET'] %>
10
+
11
+ refresh:
12
+ grant_type: "refresh_token"
13
+ client_id: <%= ENV['G_CLIENT_ID'] %>
14
+ client_secret: <%= ENV['G_CLIENT_SECRET'] %>
15
+
16
+ default_url: &default_url
17
+ url:
18
+ callback: <%= ENV['G_REDIRECT_URI'] %>
19
+ auth: https://oauth2.googleapis.com/token
20
+ refresh: https://www.googleapis.com/oauth2/v4/token
21
+ info: https://www.googleapis.com/oauth2/v1/userinfo
22
+ login: https://accounts.google.com/o/oauth2/auth
23
+
24
+ params_login:
25
+ client_id: <%= ENV['G_CLIENT_ID'] %>
26
+ response_type: code
27
+ scope: email profile openid
28
+
29
+ production:
30
+ <<: *default
31
+ <<: *default_url
32
+
33
+ development:
34
+ <<: *default
35
+ <<: *default_url
36
+
37
+ test_with_sinatra:
38
+ <<: *default
39
+ url:
40
+ callback: /callback
41
+ auth: http://127.0.0.1:4567/google/token
42
+ refresh: http://127.0.0.1:4567/refresh
43
+ info: http://127.0.0.1:4567/oauth2/v1/userinfo
44
+ login: http://127.0.0.1:4567/o/oauth2/auth
45
+
46
+ params_login:
47
+ client_id: abc123
48
+ response_type: code
49
+ scope: email profile openid
@@ -0,0 +1,39 @@
1
+ default: &default
2
+ app:
3
+ client_id: <%= ENV['G_CLIENT_ID'] %>
4
+ client_secret: <%= ENV['G_CLIENT_SECRET'] %>
5
+
6
+ auth:
7
+ grant_type: "authorization_code"
8
+ client_id: <%= ENV['G_CLIENT_ID'] %>
9
+ client_secret: <%= ENV['G_CLIENT_SECRET'] %>
10
+
11
+ refresh:
12
+ grant_type: "refresh_token"
13
+ client_id: <%= ENV['G_CLIENT_ID'] %>
14
+ client_secret: <%= ENV['G_CLIENT_SECRET'] %>
15
+
16
+ default_url: &default_url
17
+ url:
18
+ callback: <%= ENV['G_REDIRECT_URI'] %>
19
+ auth: https://oauth2.googleapis.com/token
20
+ refresh: https://www.googleapis.com/oauth2/v4/token
21
+ info: https://www.googleapis.com/oauth2/v1/userinfo
22
+ login: https://accounts.google.com/o/oauth2/auth
23
+
24
+ params_login:
25
+ client_id: <%= ENV['G_CLIENT_ID'] %>
26
+ response_type: code
27
+ scope: email profile openid
28
+
29
+ development:
30
+ <<: *default
31
+ <<: *default_url
32
+
33
+ production:
34
+ <<: *default
35
+ <<: *default_url
36
+
37
+ test:
38
+ <<: *default
39
+ <<: *default_url
@@ -0,0 +1,42 @@
1
+
2
+ # login\_with\_google
3
+
4
+ ## Creating your credentials
5
+
6
+
7
+ #### #1 Go to [https://code.google.com/apis/console/](https://code.google.com/apis/console/) and click on "CREATE PROJECT"
8
+
9
+ ![Create your project](https://live.staticflickr.com/65535/50498079282_ecac8b2569_o_d.png)
10
+
11
+ #### #2 Choose a name and create
12
+
13
+ ![Choose a name](https://live.staticflickr.com/65535/50497219293_6ccf317f18_o_d.png)
14
+
15
+ #### #3 Navigate to "OAuth consent screen"
16
+
17
+ ![Navigate to "OAuth consent screen"](https://live.staticflickr.com/65535/50497929696_ffb2b6b332_o_d.png)
18
+
19
+ #### #4 Select external user type
20
+
21
+ ![Select external user type](https://live.staticflickr.com/65535/50497219148_5a89698a93_o_d.png)
22
+
23
+ #### #5 Define a app name and choose a app logo and save
24
+
25
+ ![Define a app name](https://live.staticflickr.com/65535/50498079157_c96529734b_o_d.png)
26
+
27
+ #### #6 Navigate to Credentials
28
+
29
+ ![Navigate to Credentials](https://live.staticflickr.com/65535/50497219078_d15e510890_o_d.png)
30
+
31
+
32
+ #### #7 Click on "+ CREATE CREDENTIALS" and choose "OAuth client ID"
33
+
34
+ ![Click on "+ CREATE CREDENTIALS" and choose "OAuth client ID"](https://live.staticflickr.com/65535/50497218463_7c0fcf0af5_o_d.png)
35
+
36
+ #### #8 Set a name and redirect URI
37
+
38
+ ![Set a name and redirect URI](https://live.staticflickr.com/65535/50497928466_15cf04f957_o_d.png)
39
+
40
+ #### #9 Get your client\_id and client\_secret
41
+
42
+ ![Get your client_id and client_secret](https://live.staticflickr.com/65535/50497217183_77d2dea71d_o_d.png)
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ template = File.expand_path('../config/templates/google_login.yml', __dir__)
5
+ config_file = 'config/google_login.yml'
6
+
7
+ FileUtils.mkdir_p(File.dirname(config_file))
8
+ FileUtils.cp(template, "#{Dir.pwd}/#{config_file}")
9
+ puts "File generated: \e[32m#{config_file}\e[0m"
10
+ exit
@@ -0,0 +1,5 @@
1
+ module LoginWithGoogleHelper
2
+ def g_url_in
3
+ LoginWithGoogle::Api.url_in
4
+ end
5
+ end
@@ -1,8 +1,13 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'yaml'
3
4
  require 'login_with_google/version'
4
5
 
5
- module LoginWithGoogle
6
- class Error < StandardError; end
7
- # Your code goes here...
8
- end
6
+ module LoginWithGoogle; end
7
+
8
+ require 'login_with_google/config'
9
+ require 'login_with_google/api_base'
10
+ require 'login_with_google/google_api'
11
+ require 'helper/login_with_google_helper'
12
+
13
+ ActionView::Base.send :include, LoginWithGoogleHelper if defined?(ActionView)
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'net/http'
4
+ module LoginWithGoogle
5
+ module ApiBase
6
+ def http_get(options)
7
+ request = Net::HTTP::Get.new(options[:uri], options[:head])
8
+ request.body = options[:body] if options[:body]
9
+ JSON.parse(request_http(options, request).body)
10
+ end
11
+
12
+ def http_post(options)
13
+ request = Net::HTTP::Post.new(options[:uri], options[:head])
14
+ request.body = options[:body] if options[:body]
15
+ request.basic_auth(*options[:auth]) if options[:auth]
16
+
17
+ return request_http(options, request).body if options[:body_response]
18
+
19
+ JSON.parse(request_http(options, request).body)
20
+ end
21
+
22
+ def http_delete(options)
23
+ request = Net::HTTP::Delete.new(options[:uri], options[:head])
24
+ request.body = options[:body] if options[:body]
25
+ request_http(options, request)
26
+ end
27
+
28
+ private
29
+
30
+ def request_http(options, request)
31
+ http = Net::HTTP.new(options[:uri].hostname, options[:uri].port)
32
+ http.use_ssl = options[:uri].scheme == 'https'
33
+ http.request(request)
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'erb'
4
+
5
+ module LoginWithGoogle
6
+ class << self
7
+ def config
8
+ project_config.any? ? project_config : yaml_load(default_config_path)
9
+ end
10
+
11
+ private
12
+
13
+ def yaml_load(path)
14
+ template = ERB.new(File.new(path).read)
15
+ YAML.safe_load(template.result, aliases: true)[ENV.fetch('RAILS_ENV', 'development')]
16
+ end
17
+
18
+ def project_config
19
+ @project_tools ||= yaml_load(project_config_path) || {}
20
+ rescue Errno::ENOENT
21
+ {}
22
+ end
23
+
24
+ def default_config_path
25
+ File.expand_path('../../config/google_login.yml', __dir__)
26
+ end
27
+
28
+ def project_config_path
29
+ "#{Dir.pwd}/config/google_login.yml"
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,65 @@
1
+ # frozen_string_literal: true
2
+
3
+ module LoginWithGoogle
4
+ class Api
5
+ class << self
6
+ include ApiBase
7
+
8
+ attr_accessor :show_url, :show_response
9
+
10
+ def url_in
11
+ [
12
+ LoginWithGoogle.config['url']['login'], '?',
13
+ CGI.unescape(login_params)
14
+ ].join
15
+ end
16
+
17
+ def auth(options = {})
18
+ data = LoginWithGoogle.config['auth'].clone
19
+ data[:code] = options[:code]
20
+ data[:redirect_uri] = redirect_uri
21
+ http_post({
22
+ uri: URI(LoginWithGoogle.config['url']['auth']),
23
+ head: { 'content-type' => 'application/json' },
24
+ body: data.to_json
25
+ })
26
+ end
27
+
28
+ def refresh(options = {})
29
+ data = LoginWithGoogle.config['refresh'].clone
30
+ data[:refresh_token] = options[:token]
31
+ http_post({
32
+ head: { 'content-type' => 'application/json' },
33
+ uri: URI(LoginWithGoogle.config['url']['refresh']),
34
+ body: data.to_json
35
+ })
36
+ end
37
+
38
+ def info(options = {})
39
+ response = http_get({
40
+ uri: URI([
41
+ LoginWithGoogle.config['url']['info'],
42
+ "?access_token=#{options['access_token'] || options['token']}"
43
+ ].join)
44
+ })
45
+ response
46
+ end
47
+
48
+ private
49
+
50
+ def redirect_uri
51
+ LoginWithGoogle.config['url']['callback']
52
+ end
53
+
54
+ def login_params
55
+ params = LoginWithGoogle.config['url']['params_login'].clone
56
+ params[:redirect_uri] = redirect_uri
57
+ if params.respond_to?(:to_query)
58
+ params.to_query
59
+ else
60
+ params.collect { |k, v| "#{k}=#{v}" }.join('&')
61
+ end
62
+ end
63
+ end
64
+ end
65
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module LoginWithGoogle
4
- VERSION = '0.1.0'
4
+ VERSION = '1.0.0'
5
5
  end
@@ -25,4 +25,10 @@ Gem::Specification.new do |spec|
25
25
  spec.bindir = 'exe'
26
26
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
27
27
  spec.require_paths = ['lib']
28
+
29
+ spec.add_dependency 'yaml', '~> 0.1.0'
30
+ spec.add_development_dependency 'activesupport', '~> 6'
31
+ spec.add_development_dependency 'pry', '~> 0.12'
32
+ spec.add_development_dependency 'rspec', '~> 3'
33
+ spec.add_development_dependency 'sinatra', '~> 2'
28
34
  end
metadata CHANGED
@@ -1,30 +1,110 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: login_with_google
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - thiaguerd
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-10-15 00:00:00.000000000 Z
12
- dependencies: []
11
+ date: 2020-10-17 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: yaml
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.1.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.1.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: activesupport
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '6'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '6'
41
+ - !ruby/object:Gem::Dependency
42
+ name: pry
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '0.12'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '0.12'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3'
69
+ - !ruby/object:Gem::Dependency
70
+ name: sinatra
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '2'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '2'
13
83
  description: Easily login with Google in your app
14
84
  email:
15
85
  - mail@thiago.pro
16
- executables: []
86
+ executables:
87
+ - login_with_google:g
17
88
  extensions: []
18
89
  extra_rdoc_files: []
19
90
  files:
20
91
  - ".gitignore"
92
+ - ".rspec"
21
93
  - CODE_OF_CONDUCT.md
22
94
  - Gemfile
23
95
  - README.md
24
96
  - Rakefile
25
97
  - bin/console
26
98
  - bin/setup
99
+ - config/google_login.yml
100
+ - config/templates/google_login.yml
101
+ - creating_credentials.md
102
+ - exe/login_with_google:g
103
+ - lib/helper/login_with_google_helper.rb
27
104
  - lib/login_with_google.rb
105
+ - lib/login_with_google/api_base.rb
106
+ - lib/login_with_google/config.rb
107
+ - lib/login_with_google/google_api.rb
28
108
  - lib/login_with_google/version.rb
29
109
  - login_with_google.gemspec
30
110
  homepage: https://github.com/caxinaua/login_with_google