json_token_authentication 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 88189f5a2606edf8d7857c09dbd15bb930daa10c
4
+ data.tar.gz: c4649457e7ae8023c43d6febab8c9dc8ce667f78
5
+ SHA512:
6
+ metadata.gz: 1fd3e0180c18398ec2f0f955708f348eeda62e22ef66e0b66682edaceaecd46dc471d627d9f26454f36ca92f65d4b1d16fa3e27af5df1c778a9a2b10c5d821f1
7
+ data.tar.gz: 7298a95ec462ea82565b10ff4d5f3ee0a3ca2d0f86739ad482ca834620b8bf771fcf8a5c50883c9ddca89b500a11fa83d187a499db2917d36b949e715a45364f
@@ -0,0 +1,18 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ todo.txt
11
+ .rspec
12
+ *.gem
13
+ *.rbc
14
+ .bundle
15
+ .config
16
+ InstalledFiles
17
+ lib/bundler/man
18
+ rdoc
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.2
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http:contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in json_token_authentication.gemspec
4
+ gemspec
5
+
6
+ group :test do
7
+ gem 'activesupport', '4.2.4'
8
+ end
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Kuldeep Aggarwal
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.
@@ -0,0 +1,39 @@
1
+ # JsonTokenAuthentication
2
+
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/json_token_authentication`. 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
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'json_token_authentication'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install json_token_authentication
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
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` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ 1. Fork it ( https://github.com/[my-github-username]/json_token_authentication/fork )
36
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
37
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
38
+ 4. Push to the branch (`git push origin my-new-feature`)
39
+ 5. Create a new Pull Request
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "json_token_authentication"
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
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'json_token_authentication/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "json_token_authentication"
8
+ spec.version = JsonTokenAuthentication::VERSION
9
+ spec.authors = ["Kuldeep Aggarwal"]
10
+ spec.email = ["kd.engineer@yahoo.co.in"]
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/kuldeepaggarwal/json_token_authentication"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.9"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_development_dependency "rspec"
25
+
26
+ spec.add_dependency 'jwt'
27
+ spec.add_dependency 'activesupport', '>= 3', '<= 5'
28
+ end
@@ -0,0 +1,15 @@
1
+ require 'rails/generators/base'
2
+
3
+ module JsonTokenAuthentication
4
+ module Generators
5
+ class InstallGenerator < Rails::Generators::Base
6
+ source_root File.expand_path('../../templates', __FILE__)
7
+
8
+ desc 'Creates a JsonTokenAuthentication initializer'
9
+
10
+ def copy_initializer
11
+ template 'json_token_authentication.rb', 'config/initializers/json_token_authentication.rb'
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,28 @@
1
+ # Use this hook to configure json token authentication.
2
+ JsonTokenAuthentication.configure do |config|
3
+ # ==> Entity Class Name
4
+ # This options will let authentication mechanism to aware about the class
5
+ # of the entity. Defaults to +User+.
6
+ # config.entity_class_name = 'User'
7
+
8
+ # ==> Entity Identifier Method Name
9
+ # This key will be used for to decode and encode value of the entity.
10
+ # Defaults to +email+.
11
+ # config.entity_identifier_method_name = 'email'
12
+
13
+ # ==> Configuration for expiration
14
+ # This option can be used to set the expiry of token. Defaults to +24.hours+
15
+ # config.expiry_within = 24.hours
16
+
17
+ # ==> Configurations for Encoder
18
+ #
19
+ # ==> Secret
20
+ # This option is to use secret for encoding the token.
21
+ # Defaults to +secret+.
22
+ # config.encoder_secret = 'secret'
23
+ #
24
+ # ==> Encoder
25
+ # This option will let you to select you custom encoder.
26
+ # Defaults to +JWT+
27
+ # config.encoder = JWT
28
+ end
@@ -0,0 +1,31 @@
1
+ require 'json_token_authentication/version'
2
+
3
+ module JsonTokenAuthentication
4
+ class << self
5
+ attr_accessor :entity_class_name, :entity_identifier_method_name,
6
+ :expiry_within, :encoder, :encoder_secret
7
+
8
+ def max_expiry_time
9
+ expiry_within.from_now.to_i
10
+ end
11
+
12
+ def configure
13
+ yield(self)
14
+ end
15
+
16
+ def encoder
17
+ @encoder ||= begin
18
+ require 'jwt'
19
+ JWT
20
+ end
21
+ end
22
+ end
23
+ self.entity_class_name = 'User'
24
+ self.entity_identifier_method_name = 'email'
25
+ self.expiry_within = 24.hours
26
+ self.encoder_secret = 'secret'
27
+ end
28
+
29
+ require 'json_token_authentication/utility'
30
+ require 'json_token_authentication/responder'
31
+ require 'json_token_authentication/validator'
@@ -0,0 +1,37 @@
1
+ module JsonTokenAuthentication
2
+ class Responder
3
+ include JsonTokenAuthentication::Utility
4
+ attr_reader :entity
5
+
6
+ def initialize(entity)
7
+ @entity = entity
8
+ end
9
+
10
+ def invalid_token
11
+ { :message => 'Invalid Username/Password' }
12
+ end
13
+
14
+ def entity_token
15
+ entity.present? ? valid_token : invalid_token
16
+ end
17
+
18
+ def valid_token
19
+ {
20
+ 'token' => generate_valid_tokens,
21
+ JsonTokenAuthentication.entity_identifier_method_name => entity_identifier
22
+ }
23
+ end
24
+
25
+ private
26
+ def generate_valid_tokens
27
+ JsonTokenAuthentication.encoder.encode(entity_data, JsonTokenAuthentication.encoder_secret)
28
+ end
29
+
30
+ def entity_data
31
+ {
32
+ entity.id.to_s => entity_identifier,
33
+ 'exp' => JsonTokenAuthentication.max_expiry_time
34
+ }
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,7 @@
1
+ module JsonTokenAuthentication
2
+ module Utility
3
+ def entity_identifier
4
+ entity.public_send(JsonTokenAuthentication.entity_identifier_method_name).to_s
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,35 @@
1
+ module JsonTokenAuthentication
2
+ class Validator
3
+ include JsonTokenAuthentication::Utility
4
+
5
+ def initialize(token)
6
+ @decoded_data = (JsonTokenAuthentication.encoder.decode(token, nil, false).first rescue {})
7
+ end
8
+
9
+ def valid_token?
10
+ decoded_data.present? &&
11
+ entity_identifier == token_identifier
12
+ end
13
+
14
+ def entity
15
+ if decoded_data.present?
16
+ @entity ||= JsonTokenAuthentication.entity_class_name.constantize.find_by(id: entity_id)
17
+ end
18
+ end
19
+
20
+ private
21
+ attr_reader :decoded_data
22
+
23
+ def entity_id
24
+ @entity_id ||= decoded_data.each_key.first
25
+ end
26
+
27
+ def token_identifier
28
+ decoded_data[entity_id.to_s]
29
+ end
30
+
31
+ def entity_identifier
32
+ entity && entity.public_send(JsonTokenAuthentication.entity_identifier_method_name)
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,3 @@
1
+ module JsonTokenAuthentication
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,137 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: json_token_authentication
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Kuldeep Aggarwal
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-11-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.9'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.9'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: jwt
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
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: activesupport
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '3'
76
+ - - "<="
77
+ - !ruby/object:Gem::Version
78
+ version: '5'
79
+ type: :runtime
80
+ prerelease: false
81
+ version_requirements: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ version: '3'
86
+ - - "<="
87
+ - !ruby/object:Gem::Version
88
+ version: '5'
89
+ description: Write a longer description or delete this line
90
+ email:
91
+ - kd.engineer@yahoo.co.in
92
+ executables: []
93
+ extensions: []
94
+ extra_rdoc_files: []
95
+ files:
96
+ - ".gitignore"
97
+ - ".travis.yml"
98
+ - CODE_OF_CONDUCT.md
99
+ - Gemfile
100
+ - LICENSE.txt
101
+ - README.md
102
+ - Rakefile
103
+ - bin/console
104
+ - bin/setup
105
+ - json_token_authentication.gemspec
106
+ - lib/generators/json_token_authentication/install_generator.rb
107
+ - lib/generators/templates/json_token_authentication.rb
108
+ - lib/json_token_authentication.rb
109
+ - lib/json_token_authentication/responder.rb
110
+ - lib/json_token_authentication/utility.rb
111
+ - lib/json_token_authentication/validator.rb
112
+ - lib/json_token_authentication/version.rb
113
+ homepage: https://github.com/kuldeepaggarwal/json_token_authentication
114
+ licenses:
115
+ - MIT
116
+ metadata: {}
117
+ post_install_message:
118
+ rdoc_options: []
119
+ require_paths:
120
+ - lib
121
+ required_ruby_version: !ruby/object:Gem::Requirement
122
+ requirements:
123
+ - - ">="
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ required_rubygems_version: !ruby/object:Gem::Requirement
127
+ requirements:
128
+ - - ">="
129
+ - !ruby/object:Gem::Version
130
+ version: '0'
131
+ requirements: []
132
+ rubyforge_project:
133
+ rubygems_version: 2.4.6
134
+ signing_key:
135
+ specification_version: 4
136
+ summary: Write a short summary, because Rubygems requires one.
137
+ test_files: []