google-idtoken-verifier 0.0.1 → 0.1.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
  SHA1:
3
- metadata.gz: acdf11410e06bf2bd19f729bc35f2398916ee109
4
- data.tar.gz: f0cbbe93cb70ef45109f04220ed1cc415cddb0c3
3
+ metadata.gz: 334f749f8649883a8a3471d3d811cd8e8682de4e
4
+ data.tar.gz: 0e7fe4c598f1880753ee3d3a176f0ae2cf361fe3
5
5
  SHA512:
6
- metadata.gz: 9ec9ac7dd777c8e7fc120f2d021d6a55b6b296d1a03aa236abe43bff44a3ddbcd958491902f4cd60cd4e5ca558f62655d435a6bbc41b8aeda6c6e75dd0cf6707
7
- data.tar.gz: 91c5bf5eb2373e20de9f2cd056a350676b2a75db1bc510bcfb9d0821d0c72c6b225ccebe3671a7088d0829b72777f99d9e33466c97fffa126b6be238f32e97e2
6
+ metadata.gz: 8dab75fa9e097327dc8d802e8637eebeb8153ec7b2f08337d8fc657b50292f2a770ba0e1cabe466545ff28cc7cb8249d423224de6c6c9cf54fc782beaa2a0d7f
7
+ data.tar.gz: 936e0206546b2ae20324d1f2fabfed234e5fba5f20ed9345fac229b5ddfd3deeadf2d93cfe4450d6a207b7f090728a49ba6a5560d6432a3ca74dcf8918d275bb
data/.rubocop.yml ADDED
@@ -0,0 +1,10 @@
1
+ Style/StringLiterals:
2
+ EnforcedStyle: double_quotes
3
+
4
+ Style/Documentation:
5
+ Enabled: false
6
+
7
+ Metrics/MethodLength:
8
+ Exclude:
9
+ - "test/**/*"
10
+ - "lib/google/idtoken/verifier/cli/**/*"
data/Gemfile CHANGED
@@ -1,4 +1,4 @@
1
- source 'https://rubygems.org'
1
+ source "https://rubygems.org"
2
2
 
3
3
  # Specify your gem's dependencies in google-idtoken-verifier.gemspec
4
4
  gemspec
data/README.md CHANGED
@@ -1,5 +1,12 @@
1
1
  # Google::Idtoken::Verifier
2
2
 
3
+ [![Gem Version](https://badge.fury.io/rb/google-idtoken-verifier.svg)](http://badge.fury.io/rb/google-idtoken-verifier)
4
+ [![Build Status](https://travis-ci.org/jnbt/google-idtoken-verifier.svg?branch=master)](https://travis-ci.org/jnbt/google-idtoken-verifier)
5
+ [![Coverage Status](https://coveralls.io/repos/jnbt/google-idtoken-verifier/badge.svg?branch=master&service=github)](https://coveralls.io/r/jnbt/google-idtoken-verifier?branch=master)
6
+ [![Code Climate](https://codeclimate.com/github/jnbt/google-idtoken-verifier/badges/gpa.svg)](https://codeclimate.com/github/jnbt/google-idtoken-verifier)
7
+ [![Dependency Status](https://gemnasium.com/jnbt/google-idtoken-verifier.svg)](https://gemnasium.com/jnbt/google-idtoken-verifier)
8
+ [![Inline docs](https://inch-ci.org/github/jnbt/google-idtoken-verifier.svg?branch=master)](https://inch-ci.org/github/jnbt/google-idtoken-verifier)
9
+
3
10
  Ruby interface to Google's API to verify ID tokens
4
11
 
5
12
  ## Installation
@@ -20,7 +27,16 @@ Or install it yourself as:
20
27
 
21
28
  ## Usage
22
29
 
23
- TODO: Write usage instructions here
30
+ ```ruby
31
+ result = Verifier.verify("an_id_token")
32
+ if result.valid?
33
+ puts "ID: #{result.data["sub"]}"
34
+ puts "Audience: #{result.data["aud"]}"
35
+ # here you should check the 'aud' value against your apps
36
+ else
37
+ puts "Invalid ID token"
38
+ end
39
+ ```
24
40
 
25
41
  ## Development
26
42
 
data/Rakefile CHANGED
@@ -1,10 +1,13 @@
1
1
  require "bundler/gem_tasks"
2
2
  require "rake/testtask"
3
+ require "rubocop/rake_task"
3
4
 
4
5
  Rake::TestTask.new(:test) do |t|
5
6
  t.libs << "test"
6
7
  t.libs << "lib"
7
- t.test_files = FileList['test/**/*_test.rb']
8
+ t.test_files = FileList["test/**/*_test.rb"]
8
9
  end
9
10
 
10
- task :default => :test
11
+ RuboCop::RakeTask.new
12
+
13
+ task default: [:test, :rubocop]
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "google/idtoken/verifier"
5
+ require "google/idtoken/verifier/cli"
6
+
7
+ Google::Idtoken::Verifier::CLI::App.start
@@ -1,7 +1,7 @@
1
1
  # coding: utf-8
2
- lib = File.expand_path('../lib', __FILE__)
2
+ lib = File.expand_path("../lib", __FILE__)
3
3
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
- require 'google/idtoken/verifier/version'
4
+ require "google/idtoken/verifier/version"
5
5
 
6
6
  Gem::Specification.new do |spec|
7
7
  spec.name = "google-idtoken-verifier"
@@ -9,15 +9,26 @@ Gem::Specification.new do |spec|
9
9
  spec.authors = ["Jonas Thiel"]
10
10
  spec.email = ["jt@neopoly.de"]
11
11
 
12
- spec.summary = %q{Ruby interface to Google's API to verify ID tokens}
13
- spec.description = %q{Ruby interface to Google's API to verify ID tokens}
12
+ spec.summary = "Ruby interface to Google's API to verify ID tokens"
13
+ spec.description = "Ruby interface to Google's API to verify ID tokens"
14
14
  spec.homepage = "https://github.com/jnbt/google-idtoken-verifier"
15
15
  spec.license = "MIT"
16
16
 
17
- spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+
21
+ spec.executables = spec.files.grep(%r{^bin\/}) { |f| File.basename(f) }
22
+
18
23
  spec.require_paths = ["lib"]
19
24
 
25
+ spec.add_dependency "json", "~> 1.8"
26
+
20
27
  spec.add_development_dependency "bundler", "~> 1.10"
21
28
  spec.add_development_dependency "rake", "~> 10.0"
29
+ spec.add_development_dependency "coveralls", "~> 0.8"
22
30
  spec.add_development_dependency "minitest"
31
+ spec.add_development_dependency "webmock", "~> 1.21"
32
+ spec.add_development_dependency "inch", "~> 0.6"
33
+ spec.add_development_dependency "rubocop", "~> 0.33"
23
34
  end
@@ -1,9 +1,30 @@
1
+ require "google/idtoken/verifier/client"
2
+ require "google/idtoken/verifier/error"
3
+ require "google/idtoken/verifier/result"
1
4
  require "google/idtoken/verifier/version"
2
5
 
3
6
  module Google
4
7
  module Idtoken
8
+ # Ruby interface to Google's API to verify ID tokens
9
+ # @usage
10
+ #
11
+ # result = Verifier.verify("an_id_token")
12
+ # if result.valid?
13
+ # puts "ID: #{result.data["sub"]}"
14
+ # puts "Audience: #{result.data["aud"]}"
15
+ # # here you should check the 'aud' value against your apps
16
+ # else
17
+ # puts "Invalid ID token"
18
+ # end
19
+ #
5
20
  module Verifier
6
- # Your code goes here...
21
+ # Checks an ID token
22
+ # @param id_token [String] ID token to verify
23
+ # @return [Result] API result
24
+ def self.verify(id_token)
25
+ response = Client.new(id_token).call
26
+ Result.new(response)
27
+ end
7
28
  end
8
29
  end
9
30
  end
@@ -0,0 +1,13 @@
1
+ require "google/idtoken/verifier/cli/app"
2
+ require "google/idtoken/verifier/cli/out"
3
+ require "google/idtoken/verifier/cli/commands"
4
+
5
+ module Google
6
+ module Idtoken
7
+ module Verifier
8
+ # Namespace holding the implementation of the CLI
9
+ module CLI
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,68 @@
1
+ require "optparse"
2
+
3
+ module Google
4
+ module Idtoken
5
+ module Verifier
6
+ module CLI
7
+ # Main class for the executable 'google_idtoken_verifier'
8
+ # @example
9
+ # $ google_idtoken_verifier -h
10
+ class App
11
+ def self.start
12
+ App.new(ARGV).run
13
+ end
14
+
15
+ # Arguments the application was called with
16
+ attr_reader :args
17
+ # Output buffer
18
+ attr_reader :out
19
+
20
+ def initialize(args, out = Out.new)
21
+ @args = args
22
+ @out = out
23
+ end
24
+
25
+ def run
26
+ if args.empty?
27
+ out.print opt_parser
28
+ else
29
+ opt_parser.parse!(args)
30
+ end
31
+ end
32
+
33
+ def check(id_token)
34
+ Commands::Check.run(id_token)
35
+ end
36
+
37
+ def version
38
+ Commands::Version.run
39
+ end
40
+
41
+ private
42
+
43
+ def opt_parser
44
+ OptionParser.new do |opts|
45
+ opts.banner = "Usage: google_idtoken_verifier [options]"
46
+
47
+ opts.separator ""
48
+ opts.separator "Options:"
49
+
50
+ opts.on("-c", "--verify ID_TOKEN",
51
+ "Verify an Google Signin ID token") do |token|
52
+ check(token)
53
+ end
54
+
55
+ opts.on_tail("-h", "--help", "Show this message") do
56
+ out.print opts
57
+ end
58
+
59
+ opts.on_tail("-v", "--version", "Show version") do
60
+ version
61
+ end
62
+ end
63
+ end
64
+ end
65
+ end
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,30 @@
1
+ module Google
2
+ module Idtoken
3
+ module Verifier
4
+ module CLI
5
+ # Module for actual commands which can be invoked from the terminal
6
+ module Commands
7
+ # Base for all commands providing simple support for running a single
8
+ # command and printing to an {Out} instance
9
+ class Base
10
+ # Run a single instance of a command
11
+ # @param args [Array] arguments for the command
12
+ # @return [Base] the command after the run
13
+ def self.run(*args)
14
+ new(*args).tap(&:run)
15
+ end
16
+
17
+ protected
18
+
19
+ def out
20
+ @out ||= Out.new
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
28
+
29
+ require "google/idtoken/verifier/cli/commands/check"
30
+ require "google/idtoken/verifier/cli/commands/version"
@@ -0,0 +1,42 @@
1
+ module Google
2
+ module Idtoken
3
+ module Verifier
4
+ module CLI
5
+ module Commands
6
+ # Command to check an ID token
7
+ class Check < Base
8
+ # Prepare a verification run from the terminal
9
+ # @param id_token [String]
10
+ def initialize(id_token)
11
+ @id_token = id_token
12
+ end
13
+
14
+ # Prints the verification result of an ID token
15
+ def run
16
+ result = Verifier.verify(@id_token)
17
+ if result.valid?
18
+ print_valid
19
+ else
20
+ print_invalid
21
+ end
22
+ out.print "Data:"
23
+ out.pretty result.data
24
+ end
25
+
26
+ private
27
+
28
+ def print_valid
29
+ out.print "Valid ID token!"
30
+ out.print
31
+ end
32
+
33
+ def print_invalid
34
+ out.print "INVALID ID token!"
35
+ out.print
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,17 @@
1
+ module Google
2
+ module Idtoken
3
+ module Verifier
4
+ module CLI
5
+ module Commands
6
+ # Command to show the gem's version
7
+ class Version < Base
8
+ # Prints the current gem's version to the command line
9
+ def run
10
+ out.print Verifier::VERSION
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,36 @@
1
+ require "pp"
2
+
3
+ module Google
4
+ module Idtoken
5
+ module Verifier
6
+ module CLI
7
+ # A wrapper to output text information to any kind of buffer
8
+ # @example
9
+ # out = Out.new(std_buffer)
10
+ # out.print('something') # => appends 'something' to std_buffer
11
+ class Out
12
+ # @return [Object] buffer used as default outlet
13
+ attr_reader :out
14
+
15
+ # Bind a new out instance to two buffers
16
+ # @param out [Object] STDOUT is default
17
+ def initialize(out = $stdout)
18
+ @out = out
19
+ end
20
+
21
+ # Prints to +out+
22
+ # @param text [String]
23
+ def print(text = "")
24
+ out.puts text
25
+ end
26
+
27
+ # Pretty print an object to +out+
28
+ # @param object [Object]
29
+ def pretty(object)
30
+ PP.pp(object, out)
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,41 @@
1
+ require "json"
2
+
3
+ module Google
4
+ module Idtoken
5
+ module Verifier
6
+ # API client for requesting the state of an ID token
7
+ class Client
8
+ # Global REST endpoint to use
9
+ ENDPOINT = "https://www.googleapis.com/oauth2/v3/tokeninfo?id_token=%s"
10
+
11
+ # ID token to verify
12
+ attr_reader :id_token
13
+
14
+ # Initializes a new client bound to an ID token
15
+ # @param id_token [String] to verify
16
+ def initialize(id_token)
17
+ @id_token = id_token
18
+ end
19
+
20
+ # Interacts with the Google API
21
+ # @return [Hash] the parsed JSON response
22
+ # @raise [Error] if any error occures
23
+ def call
24
+ JSON.parse(verify_call)
25
+ rescue StandardError => e
26
+ raise Error, e.message
27
+ end
28
+
29
+ private
30
+
31
+ def verify_call
32
+ Net::HTTP.get(api_endpoint_uri)
33
+ end
34
+
35
+ def api_endpoint_uri
36
+ URI(ENDPOINT % id_token)
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,10 @@
1
+ require "json"
2
+
3
+ module Google
4
+ module Idtoken
5
+ module Verifier
6
+ # Custom error class for rescuing from all Verifier errors
7
+ class Error < StandardError; end
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,23 @@
1
+ module Google
2
+ module Idtoken
3
+ module Verifier
4
+ # Wraps the API response
5
+ class Result
6
+ # Parsed API response
7
+ attr_reader :data
8
+
9
+ # Wraps a parsed API response
10
+ # @param data [Hash] from Google's API
11
+ def initialize(data)
12
+ @data = data
13
+ end
14
+
15
+ # Checks if the API recognized the ID token as valid
16
+ # @return [Boolean]
17
+ def valid?
18
+ data && !data["sub"].nil?
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -1,7 +1,8 @@
1
1
  module Google
2
2
  module Idtoken
3
3
  module Verifier
4
- VERSION = "0.0.1"
4
+ # The current gem's version
5
+ VERSION = "0.1.0"
5
6
  end
6
7
  end
7
8
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: google-idtoken-verifier
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jonas Thiel
@@ -10,6 +10,20 @@ bindir: bin
10
10
  cert_chain: []
11
11
  date: 2015-09-01 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: json
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.8'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.8'
13
27
  - !ruby/object:Gem::Dependency
14
28
  name: bundler
15
29
  requirement: !ruby/object:Gem::Requirement
@@ -38,6 +52,20 @@ dependencies:
38
52
  - - "~>"
39
53
  - !ruby/object:Gem::Version
40
54
  version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: coveralls
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0.8'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0.8'
41
69
  - !ruby/object:Gem::Dependency
42
70
  name: minitest
43
71
  requirement: !ruby/object:Gem::Requirement
@@ -52,14 +80,58 @@ dependencies:
52
80
  - - ">="
53
81
  - !ruby/object:Gem::Version
54
82
  version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: webmock
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '1.21'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '1.21'
97
+ - !ruby/object:Gem::Dependency
98
+ name: inch
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '0.6'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '0.6'
111
+ - !ruby/object:Gem::Dependency
112
+ name: rubocop
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '0.33'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: '0.33'
55
125
  description: Ruby interface to Google's API to verify ID tokens
56
126
  email:
57
127
  - jt@neopoly.de
58
- executables: []
128
+ executables:
129
+ - google_idtoken_verifier
59
130
  extensions: []
60
131
  extra_rdoc_files: []
61
132
  files:
62
133
  - ".gitignore"
134
+ - ".rubocop.yml"
63
135
  - ".ruby-gemset"
64
136
  - ".ruby-version"
65
137
  - ".travis.yml"
@@ -67,8 +139,18 @@ files:
67
139
  - LICENSE.txt
68
140
  - README.md
69
141
  - Rakefile
142
+ - bin/google_idtoken_verifier
70
143
  - google-idtoken-verifier.gemspec
71
144
  - lib/google/idtoken/verifier.rb
145
+ - lib/google/idtoken/verifier/cli.rb
146
+ - lib/google/idtoken/verifier/cli/app.rb
147
+ - lib/google/idtoken/verifier/cli/commands.rb
148
+ - lib/google/idtoken/verifier/cli/commands/check.rb
149
+ - lib/google/idtoken/verifier/cli/commands/version.rb
150
+ - lib/google/idtoken/verifier/cli/out.rb
151
+ - lib/google/idtoken/verifier/client.rb
152
+ - lib/google/idtoken/verifier/error.rb
153
+ - lib/google/idtoken/verifier/result.rb
72
154
  - lib/google/idtoken/verifier/version.rb
73
155
  homepage: https://github.com/jnbt/google-idtoken-verifier
74
156
  licenses:
@@ -95,3 +177,4 @@ signing_key:
95
177
  specification_version: 4
96
178
  summary: Ruby interface to Google's API to verify ID tokens
97
179
  test_files: []
180
+ has_rdoc: