blunt 0.0.1 → 0.0.2

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: adbc10560d0347656a0327782f69d0c01fb51fef
4
- data.tar.gz: c4cd0c2dcf6473465dacd23e32b1cf19d3ffacaa
3
+ metadata.gz: 2c71c52c585bf1704dac5c8b46ded0b13ed651fd
4
+ data.tar.gz: 9ed0c21e46a12cfe925171a88867dfa1a4993c80
5
5
  SHA512:
6
- metadata.gz: d0079bf491ed749b945b63079de4e0c593c16f33cbb10b3891cf76693368f3506e906b1ce3f51fd00562fa6fb2e2ab5e2eae03c6e46899992ab3bff25f82c32b
7
- data.tar.gz: 375a3ab671cd4dbefdfe23780854056666af5ff57c5ce93102dfeba70f79daa8e0075698bfa0c08fb2a303944696baa1d7538b79d0be62ec57e22276fe7e819b
6
+ metadata.gz: 28683f5d8f9152a5ebf60ae3314266999661ec4ddc55427e93fd5b900eb0e73a4d285092c3090fd4631a7315616f8ea7343ae42864b35c5f6fab3e5b0ea57607
7
+ data.tar.gz: 62b66de1365600d72e32b029260c206237e12b75dc7cd04753a94b3d66f8151d1aff05ae10bc74e98f09fb985188a8739dd0bfba1376cd68cc8522e73d51ed1d
data/README.md CHANGED
@@ -1 +1,62 @@
1
1
  # Blunt
2
+
3
+ **Blunt** provides framework-agnostic authentication using [JSON Web Tokens](https://jwt.io). It wraps [ruby-jwt](https://github.com/jwt/ruby-jwt) with an easy-to-use interface and some common conventions. Great for APIs.
4
+
5
+ ## Installation
6
+
7
+ Come on now:
8
+
9
+ ```ruby
10
+ gem 'blunt'
11
+ ```
12
+ ```bash
13
+ bundle install
14
+ ```
15
+
16
+ Or:
17
+
18
+ ```bash
19
+ gem install blunt
20
+ ```
21
+
22
+ ## Usage
23
+
24
+ Add a secret key at `ENV['BLUNT_SECRET']`. You can generate one with `Blunt.new_secret`.
25
+
26
+ ### Signup
27
+
28
+ ```ruby
29
+ # inside your signup interactor
30
+ if digest = Blunt.signup(password, password_confirmation)
31
+ # create user
32
+ else
33
+ # trigger an error
34
+ end
35
+ ```
36
+
37
+ Pretty straightforward: returns an encrypted password if the unencrypted inputs match, otherwise nil. You may want to validate the password first, e.g. minimum length.
38
+
39
+ ### Login
40
+
41
+ ```ruby
42
+ # inside your login controller
43
+ token = Blunt.login(expected, attempted, claims)
44
+ ```
45
+
46
+ - `expected` is the user's encrypted password as stored in the database.
47
+ - `attempted` is the unencrypted password attempt as sent by the client.
48
+ - `claims` is a hash of JWT claims. It _must_ contain a `:sub` key whose value is any unique way to identify the user. You can also send optional JWT claims with the payload, such as `:exp`. Refer to the [ruby-jwt docs](https://github.com/jwt/ruby-jwt) for more information.
49
+
50
+ If the passwords match and a `:sub` claim is present, a token will be generated for the claims. If the login attempt fails, the token will be nil. Have your controller return the token to the client and store it somewhere (cookies, local storage, etc).
51
+
52
+ ### Request Authentication
53
+
54
+ Pass the token from the client in a request header: `'HTTP_AUTHORIZATION' => 'Bearer <TOKEN>`.
55
+
56
+ `include Blunt::Controller` in your controller class. `current_user` will memoize whatever is in `:sub` in the token's payload, or nil if there are any errors.
57
+
58
+ If the hash of request headers is not at `request.env`, you will need to override `#_blunt_request_env` to return it. (This works out of the box for Rails and Hanami.)
59
+
60
+ ### Logout
61
+
62
+ To logout, simply have the controller respond to the client with instructions to unset the token, wherever it is stored.
data/TODO.md ADDED
@@ -0,0 +1,7 @@
1
+ # TODO
2
+
3
+ - Error handling
4
+ - Defaults for important reserved claims (exp, iss, iat)
5
+ - Devise-esque modules (rememberable, confirmable, reset password, etc)
6
+ - Multiple credentials (Omniauth)
7
+ - Adapters for common implementation patterns
data/bin/console CHANGED
@@ -1,4 +1,5 @@
1
1
  #!/usr/bin/env ruby
2
+ ENV['BLUNT_SECRET'] = "oKEtb3oB6TVGvHvH37vvCnH5whY0dtf9eYL2HVQDFoO8nrr-veAQ6s9FRRyZmI00ILG7NYuwkNJWnIM-RvgoUQ"
2
3
 
3
4
  require "bundler/setup"
4
5
  require "blunt"
data/blunt.gemspec CHANGED
@@ -8,9 +8,10 @@ Gem::Specification.new do |spec|
8
8
  spec.version = Blunt::VERSION
9
9
  spec.authors = ["Josh Greenberg"]
10
10
  spec.email = ["joshgreenberg91@gmail.com"]
11
+ spec.homepage = "https://github.com/joshgreenberg/blunt"
11
12
 
12
- spec.summary = "Tokin' authentication"
13
- spec.description = "Tokin' authentication"
13
+ spec.summary = "Token authentication"
14
+ spec.description = "Token authentication"
14
15
  spec.license = "MIT"
15
16
 
16
17
  spec.files = `git ls-files -z`.split("\x0").reject do |f|
@@ -28,4 +29,7 @@ Gem::Specification.new do |spec|
28
29
  spec.add_development_dependency "guard"
29
30
  spec.add_development_dependency "guard-minitest"
30
31
  spec.add_development_dependency "rake-notes"
32
+
33
+ spec.add_runtime_dependency "jwt"
34
+ spec.add_runtime_dependency "bcrypt", ">= 3.1.11"
31
35
  end
@@ -0,0 +1,24 @@
1
+ require 'blunt/token'
2
+
3
+ module Blunt
4
+ module Controller
5
+ private
6
+
7
+ def current_user
8
+ @current_user ||= Blunt::Token.decode(token)[:sub]
9
+ end
10
+
11
+ def logged_in?
12
+ !!current_user
13
+ end
14
+
15
+ def token
16
+ _blunt_request_env.fetch("HTTP_AUTHORIZATION", "").scan(/Bearer (.*)$/).flatten.last
17
+ end
18
+
19
+ def _blunt_request_env
20
+ request.env
21
+ end
22
+
23
+ end
24
+ end
@@ -0,0 +1,25 @@
1
+ require "json"
2
+ require "jwt"
3
+
4
+ module Blunt
5
+ module Token
6
+ extend self
7
+
8
+ ALGORITHM = 'HS256'
9
+
10
+ def issue(claims)
11
+ JWT.encode(claims, secret, ALGORITHM)
12
+ end
13
+
14
+ def decode(token)
15
+ JWT.decode(token, secret, true, {algorithm: ALGORITHM}).first.map{|k,v|[k.to_sym,v]}.to_h
16
+ end
17
+
18
+ private
19
+
20
+ def secret
21
+ ENV["BLUNT_SECRET"]
22
+ end
23
+
24
+ end
25
+ end
data/lib/blunt/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Blunt
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
data/lib/blunt.rb CHANGED
@@ -1,4 +1,23 @@
1
1
  require "blunt/version"
2
+ require "blunt/token"
3
+ require "blunt/controller"
2
4
 
3
5
  module Blunt
6
+ extend self
7
+
8
+ def new_secret(n = 64)
9
+ require 'securerandom'
10
+ SecureRandom.urlsafe_base64(n)
11
+ end
12
+
13
+ def signup(a, b)
14
+ require 'bcrypt'
15
+ BCrypt::Password.create(a) if a == b
16
+ end
17
+
18
+ def login(expected, attempted, claims = {})
19
+ return unless claims[:sub]
20
+ require 'bcrypt'
21
+ Token.issue(claims) if BCrypt::Password.new(expected) == attempted
22
+ end
4
23
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: blunt
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Josh Greenberg
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-07-02 00:00:00.000000000 Z
11
+ date: 2017-07-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -122,7 +122,35 @@ dependencies:
122
122
  - - ">="
123
123
  - !ruby/object:Gem::Version
124
124
  version: '0'
125
- description: Tokin' authentication
125
+ - !ruby/object:Gem::Dependency
126
+ name: jwt
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :runtime
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ - !ruby/object:Gem::Dependency
140
+ name: bcrypt
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: 3.1.11
146
+ type: :runtime
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: 3.1.11
153
+ description: Token authentication
126
154
  email:
127
155
  - joshgreenberg91@gmail.com
128
156
  executables: []
@@ -136,12 +164,15 @@ files:
136
164
  - LICENSE.txt
137
165
  - README.md
138
166
  - Rakefile
167
+ - TODO.md
139
168
  - bin/console
140
169
  - bin/setup
141
170
  - blunt.gemspec
142
171
  - lib/blunt.rb
172
+ - lib/blunt/controller.rb
173
+ - lib/blunt/token.rb
143
174
  - lib/blunt/version.rb
144
- homepage:
175
+ homepage: https://github.com/joshgreenberg/blunt
145
176
  licenses:
146
177
  - MIT
147
178
  metadata: {}
@@ -164,5 +195,5 @@ rubyforge_project:
164
195
  rubygems_version: 2.6.8
165
196
  signing_key:
166
197
  specification_version: 4
167
- summary: Tokin' authentication
198
+ summary: Token authentication
168
199
  test_files: []