authrocket 1.2.0 → 1.3.0

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: 86bb498763a05b226efad50e30a2c6feb5e65fdc
4
- data.tar.gz: 36e8b257613ab077c46fbcb1e49f0cb3892e6162
3
+ metadata.gz: f59945f00ec3c5935d070f1011f55b384227f21a
4
+ data.tar.gz: b14d874dd31885358b4127c6c6d39bc2d533faac
5
5
  SHA512:
6
- metadata.gz: 88c49fdef9a6178b0960b509627ebec2cf8ec1c4715db56ffdcbae64efa3915b89d42ea668a01a528039c4f8f6a14555b152fc5e5ffdf594d135c11744241a08
7
- data.tar.gz: 1a054adbc2869b9f01ff4ff6fc450813a4a46f9006ff3915dc8586627c1126ed3a0b3fffba8f6a421ed67270816cf17ea4d45806dbde913418f0f76c236775b8
6
+ metadata.gz: 6e694cddf853fc34fcef928cf6b7d41c22a158674779e1f2a1a24024bc3a222a3581e915c4c8c10ae0105826f5842a70f1472292c3647342965cc83dee4b1f2e
7
+ data.tar.gz: cee76d864e476184378695b702c9d77cac710f81463a2e1cb7ada82ad9348c6b6579184a0149f41a9155a4e1621042b0a394c226a8a77818cc966935082c4e69
data/CHANGELOG.md CHANGED
@@ -1,3 +1,8 @@
1
+ #### 1.3.0
2
+ - Add Session resource
3
+ - Deprecate Event.validate_token - Replaced by Session.from_token and Session.find
4
+ - Add missing auth_provider.* events
5
+
1
6
  #### 1.2.0
2
7
 
3
8
  - Add Credential resource
data/README.md CHANGED
@@ -14,22 +14,26 @@ For installation, add `gem 'authrocket'` to your Gemfile. More details are below
14
14
 
15
15
  By default, AuthRocket automatically loads your credentials from environment variables. For such hosting environments, including Heroku, just configure these:
16
16
 
17
- AUTHROCKET_ACCOUNT = org_SAMPLE
18
- AUTHROCKET_API_KEY = key_SAMPLE
19
- AUTHROCKET_URL = https://api-e1.authrocket.com/v1
20
- AUTHROCKET_REALM = rl_SAMPLE # optional
17
+ AUTHROCKET_ACCOUNT = org_SAMPLE
18
+ AUTHROCKET_API_KEY = key_SAMPLE
19
+ AUTHROCKET_URL = https://api-e1.authrocket.com/v1
20
+ AUTHROCKET_REALM = rl_SAMPLE # optional
21
+ AUTHROCKET_JWT_SECRET = jsk_SAMPLE # optional
21
22
 
22
23
  `AUTHROCKET_URL` must be updated based on what cluster your account is provisioned on.
23
24
 
24
25
  `AUTHROCKET_REALM` is optional. If you're using a single Realm, it's easiest to add it here as an application-wide default. If you're using multiple Realms with your app, we recommend leaving it out here and setting it as you go.
25
26
 
27
+ `AUTHROCKET_JWT_SECRET` is optional. It only should be included if you've also specified a single realm via AUTHROCKET_REALM *and* you're using hosted logins or authrocket.js. The tokens returned by both are JWT-compatible and can be verified in-app using a matching secret.
28
+
26
29
  It's possible to configure AuthRocket using a Rails initializer (or other initializaiton code) too.
27
30
 
28
31
  AuthRocket::Api.credentials = {
29
32
  account: 'org_SAMPLE',
30
33
  api_key: 'key_SAMPLE',
31
34
  url: 'https://api-e1.authrocket.com/v1',
32
- realm: 'rl_SAMPLE'
35
+ realm: 'rl_SAMPLE',
36
+ jwt_secret: 'jsk_SAMPLE'
33
37
  }
34
38
 
35
39
 
@@ -58,18 +62,15 @@ Let's add a couple methods to your Application Controller, substituting the corr
58
62
  # shown in the Login Policy details.
59
63
 
60
64
  def require_user
61
- unless session[:ar_user_id]
65
+ unless current_user
62
66
  flash.keep
63
67
  redirect_to LOGIN_URL
64
68
  end
65
69
  end
66
70
 
71
+ helper_method :current_user
67
72
  def current_user
68
- @_current_user ||= session[:ar_user_id] && AuthRocket::User.find(session[:ar_user_id])
69
- end
70
-
71
- def current_user_name
72
- session[:name]
73
+ @_current_user ||= AuthRocket::Session.from_token(session[:ar_token]).try(:user)
73
74
  end
74
75
  end
75
76
 
@@ -85,10 +86,8 @@ Then add login and logout methods:
85
86
  def login
86
87
  flash.keep
87
88
  if params[:token]
88
- if login_rec = AuthRocket::Event.validate_token(params[:token])
89
- user = login_rec.user
90
- session[:ar_user_id] = user.id
91
- session[:name] = user.name
89
+ if AuthRocket::Session.from_token(params[:token], within: 60.seconds)
90
+ session[:ar_token] = params[:token]
92
91
  redirect_to root_path
93
92
  return
94
93
  end
@@ -97,7 +96,7 @@ Then add login and logout methods:
97
96
  end
98
97
 
99
98
  def logout
100
- session[:ar_user_id] = nil
99
+ session[:ar_token] = nil
101
100
  redirect_to root_path, notice: 'You have been logged out.'
102
101
  end
103
102
  end
data/authrocket.gemspec CHANGED
@@ -18,7 +18,8 @@ Gem::Specification.new do |gem|
18
18
  gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
19
19
  gem.require_paths = ["lib"]
20
20
 
21
- gem.add_dependency 'ncore', '~> 1.0'
21
+ gem.add_dependency 'ncore', '~> 1.2'
22
+ gem.add_dependency 'jwt', '~> 1.2.0'
22
23
 
23
24
  gem.add_development_dependency "bundler", "~> 1.3"
24
25
  gem.add_development_dependency "rake"
data/lib/authrocket.rb CHANGED
@@ -1,10 +1,11 @@
1
1
  require 'ncore'
2
+ require 'jwt'
2
3
 
3
- %w(version api_config).each do |f|
4
+ %w(version client api_config).each do |f|
4
5
  require "authrocket/api/#{f}"
5
6
  end
6
7
 
7
- %w(app_hook auth_provider credential event login_policy membership org realm user user_token).each do |f|
8
+ %w(app_hook auth_provider credential event login_policy membership org realm session user user_token).each do |f|
8
9
  require "authrocket/#{f}"
9
10
  end
10
11
 
@@ -1,5 +1,7 @@
1
1
  module AuthRocket
2
2
  include NCore::Builder
3
+ Resource.include AuthRocket::Client
4
+ SingletonResource.include AuthRocket::Client
3
5
 
4
6
  configure do
5
7
  self.default_url = ENV['AUTHROCKET_URL']
@@ -16,7 +18,8 @@ module AuthRocket
16
18
  self.credentials = {
17
19
  api_key: ENV['AUTHROCKET_API_KEY'],
18
20
  account: ENV['AUTHROCKET_ACCOUNT'],
19
- realm: ENV['AUTHROCKET_REALM']
21
+ realm: ENV['AUTHROCKET_REALM'],
22
+ jwt_secret: ENV['AUTHROCKET_JWT_SECRET']
20
23
  }
21
24
  end
22
25
 
@@ -52,6 +55,8 @@ module AuthRocket
52
55
  o = {}
53
56
  [url.password, url.user].each do |part|
54
57
  case part
58
+ when /^jsk_/
59
+ o[:jwt_secret] = part
55
60
  when /^key_/
56
61
  o[:api_key] = part
57
62
  when /^org_/
@@ -0,0 +1,14 @@
1
+ module AuthRocket
2
+ module Client
3
+ extend ActiveSupport::Concern
4
+
5
+ module ClassMethods
6
+
7
+ def parse_credentials(creds)
8
+ creds.with_indifferent_access.except :jwt_secret
9
+ end
10
+
11
+ end
12
+
13
+ end
14
+ end
@@ -1,3 +1,3 @@
1
1
  module AuthRocket
2
- VERSION = '1.2.0'
2
+ VERSION = '1.3.0'
3
3
  end
@@ -16,6 +16,7 @@ module AuthRocket
16
16
  org.* org.created org.updated org.deleted
17
17
  membership.* membership.created membership.updated membership.deleted
18
18
  app_hook.* app_hook.created app_hook.updated app_hook.deleted
19
+ auth_provider.* auth_provider.created auth_provider.updated auth_provider.deleted
19
20
  login_policy.* login_policy.created login_policy.updated login_policy.deleted
20
21
  ).sort
21
22
  end
@@ -13,6 +13,7 @@ module AuthRocket
13
13
  attr_datetime :event_at
14
14
 
15
15
 
16
+ # deprecated - use Session.from_token() or Session.find()
16
17
  def self.validate_token(token, params={}, api_creds=nil)
17
18
  parsed, creds = request(:get, "#{url}/login/#{CGI.escape token}", api_creds, params)
18
19
  new(parsed, creds)
@@ -9,8 +9,10 @@ module AuthRocket
9
9
  has_many :orgs
10
10
  has_many :users
11
11
 
12
- attr :api_key_policy, :api_key_prefix, :custom, :name
13
- attr :require_unique_emails, :state, :username_validation_human
12
+ attr :api_key_policy, :api_key_prefix, :custom, :name # :api_key_minutes
13
+ attr :jwt_data, :require_unique_emails, :session_minutes, :session_type
14
+ attr :state, :username_validation_human
15
+ attr :jwt_secret # readonly
14
16
 
15
17
 
16
18
  def reset!(params={})
@@ -0,0 +1,56 @@
1
+ module AuthRocket
2
+ class Session < Resource
3
+ crud :all, :find, :create, :delete
4
+
5
+ belongs_to :user
6
+
7
+ attr :ip
8
+ attr :token # readonly
9
+ attr_datetime :created_at, :expires_at # readonly
10
+
11
+ # options - :within - (in seconds) Maximum time since the token was originally issued
12
+ def self.from_token(token, options={}, api_creds=nil)
13
+ secret = (api_creds||credentials)[:jwt_secret]
14
+ raise Error, "missing :jwt_secret (or AUTHROCKET_JWT_SECRET)" unless secret
15
+ return unless token
16
+
17
+ jwt, _ = JWT.decode token, secret
18
+
19
+ if within = options.delete(:within)
20
+ return if jwt['iat'] < Time.now.to_i - within
21
+ end
22
+
23
+ user = User.new({
24
+ id: jwt['uid'],
25
+ username: jwt['un'],
26
+ first_name: jwt['fn'],
27
+ last_name: jwt['ln'],
28
+ name: jwt['n'],
29
+ memberships: jwt['m'] && jwt['m'].map do |m|
30
+ Membership.new({
31
+ permissions: m['p'],
32
+ user_id: jwt['uid'],
33
+ org_id: m['oid'],
34
+ org: m['oid'] && Org.new({
35
+ id: m['oid'],
36
+ name: m['o'],
37
+ }),
38
+ })
39
+ end,
40
+ }, api_creds)
41
+ session = new({
42
+ id: jwt['tk'],
43
+ created_at: jwt['iat'],
44
+ expires_at: jwt['exp'],
45
+ token: token,
46
+ user_id: jwt['uid'],
47
+ user: user
48
+ }, api_creds)
49
+
50
+ session
51
+ rescue JWT::DecodeError, JWT::ExpiredSignature
52
+ nil
53
+ end
54
+
55
+ end
56
+ end
@@ -6,6 +6,7 @@ module AuthRocket
6
6
  has_many :credentials
7
7
  has_many :events
8
8
  has_many :memberships
9
+ has_many :sessions
9
10
 
10
11
  attr :api_key # deprecated
11
12
  attr :custom, :email, :first_name
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: authrocket
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.0
4
+ version: 1.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - thomas morgan
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-01-26 00:00:00.000000000 Z
11
+ date: 2015-02-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: ncore
@@ -16,14 +16,28 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '1.0'
19
+ version: '1.2'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '1.0'
26
+ version: '1.2'
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.2.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 1.2.0
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: bundler
29
43
  requirement: !ruby/object:Gem::Requirement
@@ -68,6 +82,7 @@ files:
68
82
  - authrocket.gemspec
69
83
  - lib/authrocket.rb
70
84
  - lib/authrocket/api/api_config.rb
85
+ - lib/authrocket/api/client.rb
71
86
  - lib/authrocket/api/log_subscriber.rb
72
87
  - lib/authrocket/api/railtie.rb
73
88
  - lib/authrocket/api/version.rb
@@ -79,6 +94,7 @@ files:
79
94
  - lib/authrocket/membership.rb
80
95
  - lib/authrocket/org.rb
81
96
  - lib/authrocket/realm.rb
97
+ - lib/authrocket/session.rb
82
98
  - lib/authrocket/user.rb
83
99
  - lib/authrocket/user_token.rb
84
100
  homepage: https://authrocket.com/