toktok 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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: e16666f292ece7bb076c8ed7672f4141102b2afc
4
+ data.tar.gz: a99deed7551d520bec1d258d70d9e4a356a68130
5
+ SHA512:
6
+ metadata.gz: 38b8a5680cf95497c2ef012a9f32573d2f8d6627462da97b39eca53a49c651d75f7cd66499e68f5abb9d31cbe5fa20288734eb3afea5558c60d32810fdb4fc87
7
+ data.tar.gz: 9f3b3878cc38ff13e1c8253102e9fbc33155f1e0881016a67e4da89f0f95a351c5324a36be27b6499a4c0411087e76ec212c9c1e703225468f970083e4969dd3
data/.config.reek ADDED
@@ -0,0 +1,4 @@
1
+ # https://github.com/troessner/reek/blob/master/defaults.reek
2
+
3
+ IrresponsibleModule:
4
+ enabled: false
data/.editorconfig ADDED
@@ -0,0 +1,12 @@
1
+ root = true
2
+
3
+ [*]
4
+ charset = utf-8
5
+ indent_size = 2
6
+ indent_style = space
7
+ insert_final_newline = true
8
+ trim_trailing_whitespace = true
9
+
10
+ [Makefile]
11
+ indent_size = 4
12
+ indent_style = tab
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,10 @@
1
+ # https://github.com/bbatsov/rubocop/blob/master/config/default.yml
2
+
3
+ AllCops:
4
+ TargetRubyVersion: 2.3.3
5
+
6
+ Metrics/LineLength:
7
+ Max: 110
8
+
9
+ Style/Documentation:
10
+ Enabled: false
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.3
5
+ before_install: gem install bundler -v 1.14.5
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in toktok.gemspec
4
+ gemspec
data/Guardfile ADDED
@@ -0,0 +1,13 @@
1
+ guard :rspec, cmd: 'bundle exec rspec' do
2
+ require 'guard/rspec/dsl'
3
+ dsl = Guard::RSpec::Dsl.new(self)
4
+
5
+ # RSpec files
6
+ rspec = dsl.rspec
7
+ watch(rspec.spec_helper) { rspec.spec_dir }
8
+ watch(rspec.spec_support) { rspec.spec_dir }
9
+ watch(rspec.spec_files)
10
+
11
+ # Lib files
12
+ watch(%r{^lib/(.+)\.rb$}) { |m| "#{rspec.spec_dir}/lib/#{m[1]}_spec.rb" }
13
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Arturo Guzman
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,73 @@
1
+ # Toktok
2
+
3
+ [![Build Status](https://travis-ci.org/guzart/toktok.svg?branch=master)](https://travis-ci.org/guzart/toktok)
4
+ [![codecov](https://codecov.io/gh/guzart/toktok/branch/master/graph/badge.svg)](https://codecov.io/gh/guzart/toktok)
5
+ [![Code Climate](https://codeclimate.com/github/guzart/toktok/badges/gpa.svg)](https://codeclimate.com/github/guzart/toktok)
6
+
7
+
8
+ JWT Authentication for Ruby
9
+
10
+ ## Installation
11
+
12
+ Add this line to your application's Gemfile:
13
+
14
+ ```ruby
15
+ gem 'toktok'
16
+ ```
17
+
18
+ And then execute:
19
+
20
+ $ bundle
21
+
22
+ Or install it yourself as:
23
+
24
+ $ gem install toktok
25
+
26
+ ## Usage
27
+
28
+ TODO: Improve
29
+
30
+ **Configuration**
31
+
32
+ ```ruby
33
+ Toktok.algorithm = 'HS256'
34
+ Toktok.secret_key = ENV['MY_SECRET_KEY']
35
+ ```
36
+
37
+ **Encode/Decode**
38
+
39
+ ```ruby
40
+ token = Toktok::Token.new(identity: 'guzart')
41
+ puts token.jwt # 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1...'
42
+
43
+ token = Toktok::Token.new(jwt: 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1...')
44
+ puts token.identity # 'guzart'
45
+ puts token.payload # { sub: 'guzart' }
46
+ ```
47
+
48
+ ## API
49
+
50
+ TODO: list public api
51
+
52
+ ### ::Toktok
53
+
54
+ ### ::Toktok::Token
55
+
56
+ ## Claims
57
+
58
+ TODO: describe relation in configuration and in token payload
59
+
60
+ ## Development
61
+
62
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
63
+
64
+ 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).
65
+
66
+ ## Contributing
67
+
68
+ Bug reports and pull requests are welcome on GitHub at https://github.com/guzart/toktok.
69
+
70
+ ## License
71
+
72
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
73
+
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "toktok"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/guard ADDED
@@ -0,0 +1,17 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+ #
4
+ # This file was generated by Bundler.
5
+ #
6
+ # The application 'guard' is installed as part of a gem, and
7
+ # this file is here to facilitate running it.
8
+ #
9
+
10
+ require "pathname"
11
+ ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
12
+ Pathname.new(__FILE__).realpath)
13
+
14
+ require "rubygems"
15
+ require "bundler/setup"
16
+
17
+ load Gem.bin_path("guard", "guard")
data/bin/rspec ADDED
@@ -0,0 +1,17 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+ #
4
+ # This file was generated by Bundler.
5
+ #
6
+ # The application 'rspec' is installed as part of a gem, and
7
+ # this file is here to facilitate running it.
8
+ #
9
+
10
+ require "pathname"
11
+ ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
12
+ Pathname.new(__FILE__).realpath)
13
+
14
+ require "rubygems"
15
+ require "bundler/setup"
16
+
17
+ load Gem.bin_path("rspec-core", "rspec")
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,16 @@
1
+ module Toktok
2
+ class Configuration
3
+ attr_reader :algorithm, :secret_key
4
+
5
+ SecretKeyMissingError = Class.new(StandardError)
6
+
7
+ def initialize(algorithm: nil, secret_key: nil)
8
+ @algorithm = algorithm || 'HS256'
9
+ @secret_key = secret_key
10
+
11
+ if algorithm != 'none' && (secret_key || '') == ''
12
+ raise SecretKeyMissingError, "Toktok: The algorithm #{algorithm} requires you to setup a 'secret_key'"
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,48 @@
1
+ module Toktok
2
+ InvalidIdentity = Class.new(StandardError)
3
+
4
+ class Token
5
+ attr_reader :config, :jwt, :payload
6
+
7
+ def initialize(identity: nil, jwt: nil)
8
+ @config = Toktok.config
9
+ @payload = {}
10
+ if jwt
11
+ initialize_decode(jwt, identity)
12
+ else
13
+ initialize_encode(identity)
14
+ end
15
+ end
16
+
17
+ def identity
18
+ payload['sub']
19
+ end
20
+
21
+ private
22
+
23
+ def initialize_encode(identity)
24
+ @payload['sub'] = identity
25
+ @jwt = JWT.encode(payload, config.secret_key, config.algorithm)
26
+ end
27
+
28
+ def initialize_decode(jwt, identity)
29
+ @jwt = jwt
30
+ options = decode_options(identity)
31
+ decoded_token = JWT.decode(jwt, config.secret_key, algorithm?, options)
32
+ @payload = decoded_token[0]
33
+ @header = decoded_token[1]
34
+ rescue JWT::InvalidSubError
35
+ raise InvalidIdentity, "Invalid identity. Expected #{identity}"
36
+ end
37
+
38
+ def decode_options(identity)
39
+ options = { algorithm: config.algorithm }
40
+ options = options.merge(sub: identity, verify_sub: true) if identity
41
+ options
42
+ end
43
+
44
+ def algorithm?
45
+ config.algorithm != 'none'
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,3 @@
1
+ module Toktok
2
+ VERSION = '0.1.0'
3
+ end
data/lib/toktok.rb ADDED
@@ -0,0 +1,22 @@
1
+ require 'toktok/version'
2
+ require 'jwt'
3
+
4
+ module Toktok
5
+ require 'toktok/configuration'
6
+ require 'toktok/token'
7
+
8
+ def self.algorithm=(value)
9
+ @algorithm = value
10
+ end
11
+
12
+ def self.secret_key=(value)
13
+ @secret_key = value
14
+ end
15
+
16
+ def self.config
17
+ ::Toktok::Configuration.new(
18
+ algorithm: @algorithm,
19
+ secret_key: @secret_key
20
+ )
21
+ end
22
+ end
data/toktok.gemspec ADDED
@@ -0,0 +1,37 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'toktok/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'toktok'
8
+ spec.version = Toktok::VERSION
9
+ spec.authors = ['Arturo Guzman']
10
+ spec.email = ['arturo@guzart.com']
11
+
12
+ spec.summary = 'Simplify JWT token encoding and decoding for Ruby'
13
+ spec.description = <<~DOC
14
+ Simplify JWT token encoding and decoding for Ruby. Use a configuration initializer to standardize the use
15
+ of JWT encoding/decoding accross your library. Simplifies the use of JWT Claims.
16
+ DOC
17
+ spec.homepage = 'https://github.com/guzart/toktok'
18
+ spec.license = 'MIT'
19
+
20
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
21
+ f.match(%r{^(test|spec|features)/})
22
+ end
23
+ spec.bindir = 'exe'
24
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
25
+ spec.require_paths = ['lib']
26
+
27
+ spec.add_dependency 'jwt', '~> 1.5'
28
+
29
+ spec.add_development_dependency 'awesome_print', '~> 1.7'
30
+ spec.add_development_dependency 'bundler', '~> 1.14'
31
+ spec.add_development_dependency 'codecov', '~> 0'
32
+ spec.add_development_dependency 'guard', '~> 2.14'
33
+ spec.add_development_dependency 'guard-rspec', '~> 4.7'
34
+ spec.add_development_dependency 'rake', '~> 10.0'
35
+ spec.add_development_dependency 'reek', '~> 4.5'
36
+ spec.add_development_dependency 'rspec', '~> 3.5'
37
+ end
metadata ADDED
@@ -0,0 +1,192 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: toktok
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Arturo Guzman
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-03-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: jwt
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.5'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.5'
27
+ - !ruby/object:Gem::Dependency
28
+ name: awesome_print
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.7'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.7'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.14'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.14'
55
+ - !ruby/object:Gem::Dependency
56
+ name: codecov
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: guard
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '2.14'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '2.14'
83
+ - !ruby/object:Gem::Dependency
84
+ name: guard-rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '4.7'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '4.7'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rake
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '10.0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '10.0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: reek
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '4.5'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: '4.5'
125
+ - !ruby/object:Gem::Dependency
126
+ name: rspec
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - "~>"
130
+ - !ruby/object:Gem::Version
131
+ version: '3.5'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - "~>"
137
+ - !ruby/object:Gem::Version
138
+ version: '3.5'
139
+ description: |
140
+ Simplify JWT token encoding and decoding for Ruby. Use a configuration initializer to standardize the use
141
+ of JWT encoding/decoding accross your library. Simplifies the use of JWT Claims.
142
+ email:
143
+ - arturo@guzart.com
144
+ executables: []
145
+ extensions: []
146
+ extra_rdoc_files: []
147
+ files:
148
+ - ".config.reek"
149
+ - ".editorconfig"
150
+ - ".gitignore"
151
+ - ".rspec"
152
+ - ".rubocop.yml"
153
+ - ".travis.yml"
154
+ - Gemfile
155
+ - Guardfile
156
+ - LICENSE.txt
157
+ - README.md
158
+ - Rakefile
159
+ - bin/console
160
+ - bin/guard
161
+ - bin/rspec
162
+ - bin/setup
163
+ - lib/toktok.rb
164
+ - lib/toktok/configuration.rb
165
+ - lib/toktok/token.rb
166
+ - lib/toktok/version.rb
167
+ - toktok.gemspec
168
+ homepage: https://github.com/guzart/toktok
169
+ licenses:
170
+ - MIT
171
+ metadata: {}
172
+ post_install_message:
173
+ rdoc_options: []
174
+ require_paths:
175
+ - lib
176
+ required_ruby_version: !ruby/object:Gem::Requirement
177
+ requirements:
178
+ - - ">="
179
+ - !ruby/object:Gem::Version
180
+ version: '0'
181
+ required_rubygems_version: !ruby/object:Gem::Requirement
182
+ requirements:
183
+ - - ">="
184
+ - !ruby/object:Gem::Version
185
+ version: '0'
186
+ requirements: []
187
+ rubyforge_project:
188
+ rubygems_version: 2.5.2
189
+ signing_key:
190
+ specification_version: 4
191
+ summary: Simplify JWT token encoding and decoding for Ruby
192
+ test_files: []