From f230a25e93095630e9f1855585c71c79be9f67c1 Mon Sep 17 00:00:00 2001 From: velusamy Date: Fri, 25 May 2018 13:37:36 +0530 Subject: [PATCH] gemscpec changes and add some features in token --- .byebug_history | 104 +++++++++++++++++++++++++++++++ Gemfile | 3 +- Gemfile.lock | 2 + README.md | 29 +++++++++ lib/token/web_token_gen.rb | 31 +++++++++ lib/validation/token_validate.rb | 41 ++++++++++++ lib/web_token.rb | 0 lib/webtoken.rb | 5 ++ web_token.gemspec | 16 +++++ 9 files changed, 230 insertions(+), 1 deletion(-) create mode 100644 .byebug_history create mode 100644 lib/token/web_token_gen.rb create mode 100644 lib/validation/token_validate.rb delete mode 100644 lib/web_token.rb create mode 100644 lib/webtoken.rb diff --git a/.byebug_history b/.byebug_history new file mode 100644 index 0000000..a563f7d --- /dev/null +++ b/.byebug_history @@ -0,0 +1,104 @@ +c +decoded_token.present? +decoded_token.present +decoded_token +c +cc +c +decoded_token +c +decoded_token +c +exit +decoded_token +decode_token +c +rescue JWT::ImmatureSignature +rescue JWT::ExpiredSignature + decoded_token = JWT.decode token, nil, false +c + Kernel.const_get(gfd) + Kernel.const_get() + Kernel.const_get("fdgfd") + Kernel.const_get(@model) + Kernel.const_get(@model).find(@data['userid'].to_i) +user = Kernel.const_get(@model).find(@data['userid'].to_i) +time = @data['time'].to_time+ 1.hour > Time.now +c +decoded_token = JWT.decode token, nil, false +@model = model +model +token +exit +Kernel.const_get(classname) +Kernel.const_get(classname).methods +classname = 'Integer' +eval classname + '.methods' +classname = 'Integer' +Model(@data['userid']) +@data['userid'] +@data['userid'].capitalize! +@data['userid'] +@data['userid'].capitalize! +@data['userid'] +@data['userid'].capitalize! +@data['userid'].capitalize!.new +@data['userid'].capitalize! +@data['userid'].Capitalize +@data['userid'].Capitalize! +@data['userid'] +@data +@data['time'].to_time+ 1.hour > Time.now +@data['time'].to_time > Time.now +@data['time'].to_time +@data['time'] +@data +c + @data = decoded_token[0]['data'] + decoded_token = JWT.decode token, nil, false +c +exit +decoded_token = JWT.decode @token, nil, false +token +tokken +c +@token +c + @token +c +@data['userid'] +@data.userid +@data +exit +@data['data'] +@data +@data' +exit +@data[0]['data']['userid'] +@data[0]['data'].userid +@data[0]['data'] +@data[0] +@data[0].data +@data[0].dats +@data[0].date +@data[0] +@data +exit +Time.now.to_s +Time.now.to_s.to_i +Time.now.to_s.to_time +Time.now.to_s +Time.now +Date.today + "g" +Date.today +Date.today.to_s.split("-").join("") +Date(Date.today.to_s.split("-").join("")) +Date.today.to_s.split("-").join("").to_date +Date.today.to_s.split("-").join("") +Date.today.to_s.split("-") +Date.today.to_s.split("") +Date.today.to_s +Date.today.to_a +Date.today.to_s +Date.today +@date diff --git a/Gemfile b/Gemfile index 95d01d6..e7930b7 100644 --- a/Gemfile +++ b/Gemfile @@ -1,3 +1,4 @@ source 'https://rubygems.org' git_source(:github) { |repo| "https://github.com/#{repo}.git" } -gem 'bcrypt', '~> 3.1', '>= 3.1.11' \ No newline at end of file +gem 'bcrypt', '~> 3.1', '>= 3.1.11' +gem 'jwt' \ No newline at end of file diff --git a/Gemfile.lock b/Gemfile.lock index 6cb4841..e90f392 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -2,12 +2,14 @@ GEM remote: https://rubygems.org/ specs: bcrypt (3.1.12) + jwt (2.1.0) PLATFORMS ruby DEPENDENCIES bcrypt (~> 3.1, >= 3.1.11) + jwt BUNDLED WITH 1.16.2 diff --git a/README.md b/README.md index 8b13789..5929de7 100644 --- a/README.md +++ b/README.md @@ -1 +1,30 @@ +# Web Token Gem for simplification of JWT +## Step 1 + gem install webtoken + +## Step 2 + + require 'webtoken' + +## Step 3 + + data = WebToken::MakeToken.new( user_id ) + + data.web_token + +### Response + + #sample = {"eyJhbGciOiJub25lIn0.eyJkYXRhIjp7InVzZXJpZCI6ImpodiIsImRhdGUiOiIyMDE4LTA1LTI1IiwidGltZSI6IjIwMTgtMDUtMjUgMTI6Mzk6MzIgKzA1MzAifX0."} + + +## Step 3 + + validate_token = WebToken::WebtokenValidate.new + + validate_token.validate( 'your token' ) + +### Response + {"userid" : "userid", date: "date", time: "time"} #access token valid + + false #access token invalid diff --git a/lib/token/web_token_gen.rb b/lib/token/web_token_gen.rb new file mode 100644 index 0000000..1d8ca9c --- /dev/null +++ b/lib/token/web_token_gen.rb @@ -0,0 +1,31 @@ +require 'active_support' +require 'active_support/all' +require 'fnv' +require 'byebug' +require 'bcrypt' +require 'jwt' +require "date" +module WebToken + class MakeToken + def initialize( user_id ) + data = { + userid: user_id, + date: Date.today.to_s, + time: Time.now + } + payload = {:data => data} + @token = JWT.encode payload, nil, 'none' + # decoded_token = JWT.decode @token, nil, false + # @data = decoded_token[0]['data'] + end + def web_token + @token + end + # def data + # @data + # end + + end +end + + diff --git a/lib/validation/token_validate.rb b/lib/validation/token_validate.rb new file mode 100644 index 0000000..3305efb --- /dev/null +++ b/lib/validation/token_validate.rb @@ -0,0 +1,41 @@ +require 'byebug' +require 'bcrypt' +require 'jwt' +require "date" + +module WebToken + + class WebtokenValidate + # def initialize( token, model ) + + # end + def validate( token ) + begin + decoded_token = JWT.decode token, nil, false + rescue JWT::ExpiredSignature + rescue JWT::ImmatureSignature + rescue JWT::DecodeError + + end + if decoded_token.present? + @data = decoded_token[0]['data'] + time = @data['time'].to_time+ 1.hour > Time.now + if time + @data + else + false + end + else + false + end + + # user = Kernel.const_get(@model).find(@data['userid'].to_i) + # if user.present? && time + # user + # else + # return false + # end + end + + end +end diff --git a/lib/web_token.rb b/lib/web_token.rb deleted file mode 100644 index e69de29..0000000 diff --git a/lib/webtoken.rb b/lib/webtoken.rb new file mode 100644 index 0000000..c8167be --- /dev/null +++ b/lib/webtoken.rb @@ -0,0 +1,5 @@ +require 'active_support' +require 'active_support/all' +require 'fnv' +require './lib/token/web_token_gen' +require './lib/validation/token_validate' \ No newline at end of file diff --git a/web_token.gemspec b/web_token.gemspec index e69de29..5da73cf 100644 --- a/web_token.gemspec +++ b/web_token.gemspec @@ -0,0 +1,16 @@ +lib = File.expand_path( '../lib/', __FILE__ ) +$LOAD_PATH.unshift( lib ) unless $LOAD_PATH.include?( lib ) + +Gem::Specification.new do |s| + s.name = 'webtoken' + s.version = '0.0.1' + s.date = '2010-05-25' + s.summary = "WebToken" + s.description = "Simplify web token using JWT" + s.authors = ["velusamy Venkatraman"] + s.email = 'velusamyvnp@gmail.com,' + s.files = ["lib/webtoken.rb"] + # s.require_paths = [ "lib" ] + s.homepage ='' + s.license = 'MIT' +end