challah-jwt 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/Gemfile +4 -0
- data/README.md +66 -0
- data/Rakefile +2 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/challah-jwt.gemspec +28 -0
- data/lib/challah-jwt.rb +1 -0
- data/lib/challah/jwt.rb +14 -0
- data/lib/challah/jwt/technique.rb +31 -0
- data/lib/challah/jwt/tokenizer.rb +41 -0
- data/lib/challah/jwt/version.rb +5 -0
- metadata +111 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 1d0e70a00bf7f163dcc79d200a28b5848f1bbb78
|
4
|
+
data.tar.gz: ef354f85f0201bd2b5159f772fe6beb3d3022549
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 822500c6516ae7b2f909a423daaab1471afeb27adbf7e5c8d5ccc609c0987478a5e47987394e5696009f6bf50db8e3fd1d43f99f2fca14f1de7e839facd02472
|
7
|
+
data.tar.gz: 9911d5cb1aa541dae8ee9bd4daaa381efabcc5274b08987efcc37386498b7be465d9b60d31446cb5033ba9cc2253e4bf8834eae3c53b451036453467aee83145
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
# Challah JWT
|
2
|
+
|
3
|
+
Authenticate your Challah users with JSON Web Tokens (JWT).
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
1. Add this line to your application's Gemfile and then `bundle install`:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'challah-jwt'
|
11
|
+
```
|
12
|
+
|
13
|
+
2. In your Challah initializer, add the following line:
|
14
|
+
|
15
|
+
```ruby
|
16
|
+
Challah.register_technique :jwt, Challah::Jwt::Technique
|
17
|
+
```
|
18
|
+
|
19
|
+
3. Include the tokenizer concern in your user model:
|
20
|
+
|
21
|
+
```ruby
|
22
|
+
class User < ApplicationRecord
|
23
|
+
include Challah::Userable
|
24
|
+
include Challah::Jwt::Tokenizer
|
25
|
+
end
|
26
|
+
```
|
27
|
+
|
28
|
+
## Usage
|
29
|
+
|
30
|
+
You'll need to include the JWT in your sign in response, e.g.:
|
31
|
+
|
32
|
+
```JSON
|
33
|
+
{
|
34
|
+
"user": {
|
35
|
+
"jwt": "adi8e98uie.saxbbbgudinocgeigc84y9834.8ui9odeion",
|
36
|
+
"id": "1",
|
37
|
+
"first_name": "Slick",
|
38
|
+
"last_name": "McSpeedy",
|
39
|
+
}
|
40
|
+
}
|
41
|
+
```
|
42
|
+
|
43
|
+
Send the JWT in the `Authorization` header like this:
|
44
|
+
|
45
|
+
```http
|
46
|
+
GET /
|
47
|
+
Authorization: Bearer adi8e98uie.saxbbbgudinocgeigc84y9834.8ui9odeion
|
48
|
+
```
|
49
|
+
|
50
|
+
## Development
|
51
|
+
|
52
|
+
After checking out the repo, run `bin/setup` to install dependencies. You can
|
53
|
+
also run `bin/console` for an interactive prompt that will allow you to
|
54
|
+
experiment.
|
55
|
+
|
56
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To
|
57
|
+
release a new version, update the version number in `version.rb`, and then run
|
58
|
+
`bundle exec rake release`, which will create a git tag for the version, push
|
59
|
+
git commits and tags, and push the `.gem` file to
|
60
|
+
[rubygems.org](https://rubygems.org).
|
61
|
+
|
62
|
+
## Contributing
|
63
|
+
|
64
|
+
Bug reports and pull requests are welcome on GitHub at
|
65
|
+
<https://github.com/philtr/challah-jwt>.
|
66
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "challah/jwt"
|
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
|
data/bin/setup
ADDED
data/challah-jwt.gemspec
ADDED
@@ -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 'challah/jwt/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "challah-jwt"
|
8
|
+
spec.version = Challah::Jwt::VERSION
|
9
|
+
spec.authors = ["Phillip Ridlen"]
|
10
|
+
spec.email = ["p@rdln.net"]
|
11
|
+
|
12
|
+
spec.summary = %q{Use JSON Web Tokens (JWT) to authenticate with Challah}
|
13
|
+
spec.homepage = "https://github.com/philtr/challah-jwt.git"
|
14
|
+
|
15
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
16
|
+
f.match(%r{^(test|spec|features)/})
|
17
|
+
end
|
18
|
+
|
19
|
+
spec.bindir = "exe"
|
20
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
21
|
+
spec.require_paths = ["lib"]
|
22
|
+
|
23
|
+
spec.add_dependency "challah", "~> 1.4"
|
24
|
+
spec.add_dependency "jwt", "~> 1.5"
|
25
|
+
|
26
|
+
spec.add_development_dependency "bundler", "~> 1.13"
|
27
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
28
|
+
end
|
data/lib/challah-jwt.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "challah/jwt"
|
data/lib/challah/jwt.rb
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
require "challah"
|
2
|
+
require "jwt"
|
3
|
+
|
4
|
+
require "challah/jwt/technique"
|
5
|
+
require "challah/jwt/tokenizer"
|
6
|
+
require "challah/jwt/version"
|
7
|
+
|
8
|
+
module Challah
|
9
|
+
module Jwt
|
10
|
+
def self.secret_key
|
11
|
+
Rails.application.secrets.secret_key_base
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
module Challah
|
2
|
+
module Jwt
|
3
|
+
class Technique
|
4
|
+
TOKEN_REGEX = /^Bearer\s+(?<token>.*?)$/i
|
5
|
+
|
6
|
+
def initialize(session)
|
7
|
+
if auth_header = session.request.headers["Authorization"]
|
8
|
+
@token = auth_header.match(TOKEN_REGEX) { |match| match["token"] }
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
def authenticate
|
13
|
+
user_model.find_by_jwt(@token)
|
14
|
+
end
|
15
|
+
|
16
|
+
private
|
17
|
+
|
18
|
+
def persist?
|
19
|
+
false
|
20
|
+
end
|
21
|
+
|
22
|
+
def user_model
|
23
|
+
@user_model ||= Challah.user
|
24
|
+
end
|
25
|
+
|
26
|
+
def token_provided?(session)
|
27
|
+
session.request.headers["Authorization"] =~ TOKEN_REGEX
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
require "jwt"
|
2
|
+
|
3
|
+
module Challah
|
4
|
+
module Jwt
|
5
|
+
module Tokenizer
|
6
|
+
extend ActiveSupport::Concern
|
7
|
+
|
8
|
+
class_methods do
|
9
|
+
def find_by_jwt(token)
|
10
|
+
payload = ::JWT.decode(token, Challah::Jwt.secret_key)
|
11
|
+
model_id = payload.dig(0, jwt_root, "id")
|
12
|
+
find(model_id)
|
13
|
+
rescue JWT::DecodeError
|
14
|
+
nil
|
15
|
+
end
|
16
|
+
|
17
|
+
def jwt_root
|
18
|
+
model_name.singular
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def to_jwt
|
23
|
+
::JWT.encode(jwt_payload, Challah::Jwt.secret_key)
|
24
|
+
end
|
25
|
+
|
26
|
+
def jwt_attrs
|
27
|
+
serializable_hash.slice("id")
|
28
|
+
end
|
29
|
+
|
30
|
+
def jwt_payload
|
31
|
+
{
|
32
|
+
jwt_root => jwt_attrs
|
33
|
+
}
|
34
|
+
end
|
35
|
+
|
36
|
+
def jwt_root
|
37
|
+
self.class.jwt_root
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
metadata
ADDED
@@ -0,0 +1,111 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: challah-jwt
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Phillip Ridlen
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-12-19 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: challah
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.4'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.4'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: jwt
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.5'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.5'
|
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.13'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.13'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '10.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '10.0'
|
69
|
+
description:
|
70
|
+
email:
|
71
|
+
- p@rdln.net
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".gitignore"
|
77
|
+
- Gemfile
|
78
|
+
- README.md
|
79
|
+
- Rakefile
|
80
|
+
- bin/console
|
81
|
+
- bin/setup
|
82
|
+
- challah-jwt.gemspec
|
83
|
+
- lib/challah-jwt.rb
|
84
|
+
- lib/challah/jwt.rb
|
85
|
+
- lib/challah/jwt/technique.rb
|
86
|
+
- lib/challah/jwt/tokenizer.rb
|
87
|
+
- lib/challah/jwt/version.rb
|
88
|
+
homepage: https://github.com/philtr/challah-jwt.git
|
89
|
+
licenses: []
|
90
|
+
metadata: {}
|
91
|
+
post_install_message:
|
92
|
+
rdoc_options: []
|
93
|
+
require_paths:
|
94
|
+
- lib
|
95
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
96
|
+
requirements:
|
97
|
+
- - ">="
|
98
|
+
- !ruby/object:Gem::Version
|
99
|
+
version: '0'
|
100
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
101
|
+
requirements:
|
102
|
+
- - ">="
|
103
|
+
- !ruby/object:Gem::Version
|
104
|
+
version: '0'
|
105
|
+
requirements: []
|
106
|
+
rubyforge_project:
|
107
|
+
rubygems_version: 2.5.2
|
108
|
+
signing_key:
|
109
|
+
specification_version: 4
|
110
|
+
summary: Use JSON Web Tokens (JWT) to authenticate with Challah
|
111
|
+
test_files: []
|