token_auth_box 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 4d9f0de524949ec1354ce319aea0583273b42c40d35c2a00c87a51392f17bb3c
4
+ data.tar.gz: 44b300689f5afc04dd83539a398c67de03db72e758996e7af924162e259a1921
5
+ SHA512:
6
+ metadata.gz: 4800f2e8f105a521727d5194e3ff10c8de270acdc422619170ba71ed00382d4023cf839279f0d013f99c1a5dadc59ec547eb430333d33e84672989ae2e01e6f5
7
+ data.tar.gz: f6e25846674ff565dcb7d51aa8f59152872bfd5ebfcb44f77d33466e7d61c26eb750b003392c57d723e6dc3ba2ea09ddda512093d63a038955f1367cf16f7f08
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2019 drake
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,28 @@
1
+ # TokenAuthBox
2
+ Short description and motivation.
3
+
4
+ ## Usage
5
+ How to use my plugin.
6
+
7
+ ## Installation
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'token_auth_box'
12
+ ```
13
+
14
+ And then execute:
15
+ ```bash
16
+ $ bundle
17
+ ```
18
+
19
+ Or install it yourself as:
20
+ ```bash
21
+ $ gem install token_auth_box
22
+ ```
23
+
24
+ ## Contributing
25
+ Contribution directions go here.
26
+
27
+ ## License
28
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,19 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'TokenAuthBox'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.md')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+ load 'rails/tasks/statistics.rake'
18
+
19
+ require 'bundler/gem_tasks'
@@ -0,0 +1,6 @@
1
+ module TokenAuthBox
2
+ class ApplicationController < ActionController::API
3
+ # protect_from_forgery with: :exception
4
+ skip_before_action :verify_authenticity_token
5
+ end
6
+ end
@@ -0,0 +1,63 @@
1
+ require_dependency "token_auth_box/application_controller"
2
+
3
+ module TokenAuthBox
4
+ class AuthController < ApplicationController
5
+ include TokenAuthBox::Middleware
6
+
7
+ before_action :ensure_signed_in, only: [:middle]
8
+ def middle
9
+ render json: :ok
10
+ end
11
+
12
+ def register
13
+ user = User.new(user_params)
14
+ if user.save
15
+ data = user_response_data(user)
16
+ token = TokenService.gen_token data
17
+
18
+ render json: { user: data, token: token }
19
+ else
20
+ render json: user.errors.messages
21
+ end
22
+ end
23
+
24
+ def login
25
+ data = user_params
26
+ user = User.find_by(email: data["email"])
27
+ .try(:authenticate, data["password"])
28
+
29
+ if !user
30
+ render json: 'no', status: :unauthorized
31
+ else
32
+ token = TokenService.gen_token data
33
+ render json: {user: user_response_data(user), token: token }
34
+ end
35
+ end
36
+
37
+ def verify
38
+ token = get_header
39
+ begin
40
+ user_data = JWT.decode(token, nil, false).first
41
+ render json: user_data.to_json
42
+ rescue
43
+ render json: 'oopsie', status: :unauthorized
44
+ end
45
+ end
46
+
47
+ private
48
+
49
+ def get_header
50
+ request.headers["authorization"].split(" ")[1]
51
+ end
52
+
53
+ def user_params
54
+ params.require(:user).permit(:email, :password)
55
+ end
56
+
57
+ def user_response_data(user)
58
+ attrs = user.attributes
59
+ attrs.delete "password_digest"
60
+ attrs
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,4 @@
1
+ module TokenAuthBox
2
+ class ApplicationJob < ActiveJob::Base
3
+ end
4
+ end
@@ -0,0 +1,6 @@
1
+ module TokenAuthBox
2
+ class ApplicationMailer < ActionMailer::Base
3
+ default from: 'from@example.com'
4
+ layout 'mailer'
5
+ end
6
+ end
@@ -0,0 +1,5 @@
1
+ module TokenAuthBox
2
+ class ApplicationRecord < ActiveRecord::Base
3
+ self.abstract_class = true
4
+ end
5
+ end
data/config/routes.rb ADDED
@@ -0,0 +1,6 @@
1
+ TokenAuthBox::Engine.routes.draw do
2
+ post '/register', to: 'auth#register'
3
+ post '/login', to: 'auth#login'
4
+ get '/verify', to: 'auth#verify'
5
+ get '/middle', to: 'auth#middle'
6
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :token_auth_box do
3
+ # # Task goes here
4
+ # end
@@ -0,0 +1,5 @@
1
+ require "token_auth_box/engine"
2
+
3
+ module TokenAuthBox
4
+ # Your code goes here...
5
+ end
@@ -0,0 +1,6 @@
1
+ module TokenAuthBox
2
+ class Engine < ::Rails::Engine
3
+ isolate_namespace TokenAuthBox
4
+ config.generators.api_only = true
5
+ end
6
+ end
@@ -0,0 +1,18 @@
1
+ module TokenAuthBox
2
+ module Middleware
3
+ def ensure_signed_in
4
+ begin
5
+ token = get_header
6
+ @user_data = JWT.decode(token, nil, false).first
7
+ rescue
8
+ render json: 'oopsie', status: :unauthorized
9
+ end
10
+ end
11
+
12
+ private
13
+
14
+ def get_header
15
+ request.headers["authorization"].split(" ")[1]
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,9 @@
1
+ require "jwt"
2
+
3
+ module TokenAuthBox
4
+ class TokenService
5
+ def self.gen_token(data)
6
+ JWT.encode data, nil, 'none'
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,3 @@
1
+ module TokenAuthBox
2
+ VERSION = '0.0.1'
3
+ end
metadata ADDED
@@ -0,0 +1,86 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: token_auth_box
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - drake
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-09-03 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 5.2.3
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 5.2.3
27
+ - !ruby/object:Gem::Dependency
28
+ name: pg
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: Description of TokenAuthBox.
42
+ email:
43
+ - '='
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - MIT-LICENSE
49
+ - README.md
50
+ - Rakefile
51
+ - app/controllers/token_auth_box/application_controller.rb
52
+ - app/controllers/token_auth_box/auth_controller.rb
53
+ - app/jobs/token_auth_box/application_job.rb
54
+ - app/mailers/token_auth_box/application_mailer.rb
55
+ - app/models/token_auth_box/application_record.rb
56
+ - config/routes.rb
57
+ - lib/tasks/token_auth_box_tasks.rake
58
+ - lib/token_auth_box.rb
59
+ - lib/token_auth_box/engine.rb
60
+ - lib/token_auth_box/middleware.rb
61
+ - lib/token_auth_box/token.rb
62
+ - lib/token_auth_box/version.rb
63
+ homepage: http://draketalley.com
64
+ licenses:
65
+ - MIT
66
+ metadata: {}
67
+ post_install_message:
68
+ rdoc_options: []
69
+ require_paths:
70
+ - lib
71
+ required_ruby_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ required_rubygems_version: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ version: '0'
81
+ requirements: []
82
+ rubygems_version: 3.0.3
83
+ signing_key:
84
+ specification_version: 4
85
+ summary: Summary of TokenAuthBox.
86
+ test_files: []