horse_power 0.9.0 → 1.0.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: ff025ef2c76ca3e27f59c6fa8473ab267fc2c8e6
4
- data.tar.gz: ab3cb1da09c35e2245f400a4112aee8cc5c758e4
3
+ metadata.gz: 4d38ab66e62fa078e82fc8290d67faa8adcf2842
4
+ data.tar.gz: f58d61a10861b6a9fc9e95ae705dc517c925b107
5
5
  SHA512:
6
- metadata.gz: 236dd5f857239cc3e0d1408acfdd57f67e2cde30b82d8682950c69cf2d1362c864ba4e2efaaad9bc50e326c677432874dd8c6832710e4b8d0b47948b476eb262
7
- data.tar.gz: 5e1f3548f28b0c8531d5de81fb82ad52b704c4097efabbc0397159887d0c81f282c102686add215ca396453d048a8b88a06f2e01d9d38e3b5ec9df62376051e5
6
+ metadata.gz: 30f683ea6e670a7007f1969cbb2b0a13676f9ce41a5d66152236e06ce8788c35b54b3f2def55b8391566b2559c0fe0fdc5bea9e7b6b066971f81966087a419a0
7
+ data.tar.gz: 748158b74f3dd373bd3506d67f1b8ab048919be09317dfd94a08b2602f8a1dcec30e84ca4a88520dedd6264d11a1729f16b0e57c50f46bdaa6eb0a195d4ac895
@@ -22,7 +22,7 @@ module HorsePower
22
22
  def createSettings
23
23
  run "rails g rails_config:install"
24
24
  prepend_to_file 'config/settings.yml' do
25
- "token_header: \"Authorization\"\nmain_api_header: \"Main-Api-Header\"\nexpire_time: 4000\n"
25
+ "token_header: \"Authorization\"\nmain_api_header: \"Main-Api-Header\"\nexpire_time: 4000\nrevalidate_tokens: \"true\""
26
26
  end
27
27
  createEnvSettings
28
28
  end
@@ -25,11 +25,14 @@ class Api::V1::ApplicationController < ::ActionController::API
25
25
  end
26
26
 
27
27
  def set_hash
28
- @instance_hash = ::TokenHash.decode(params,request)
28
+ @instance_hash = ::TokenHash.decode(params,request,response)
29
29
  end
30
30
 
31
31
  def current_user
32
32
  if !@instance_hash.nil?
33
+ if !@instance_hash["user_id"].nil?
34
+ @instance_hash["current_owner"] = ::User.find_by(id: @instance_hash["user_id"])
35
+ end
33
36
  return @instance_hash["current_owner"]
34
37
  else
35
38
  return nil
@@ -1,59 +1,66 @@
1
1
  class TokenHash
2
2
 
3
- =begin
4
- Ideally, the expiration for the jwt token would be less than the db token,
5
- and we would return a new jwt token to the user if the current jwt token expired
6
- but the db token was not yet expired.
7
- =end
3
+ def self.encode(auth_token_hash,user_id)
4
+ obj = {}
5
+ obj["auth_token"] = auth_token_hash
6
+ obj["user_id"] = user_id
7
+ #Lasts a 4th of the time as the db tokens
8
+ obj["exp"] = ::Time.now.to_i() + ::Settings.expire_time*15
9
+ return ::JWT.encode(obj,::Rails.application.secrets.secret_key_base)
10
+ end
8
11
 
9
- def self.encode(auth_token_hash,user_id)
10
- obj = {}
11
- obj["auth_token"] = auth_token_hash
12
- obj["user_id"] = user_id
13
- obj["exp"] = ::Time.now.to_i() + ::Settings.expire_time*60
14
- return ::JWT.encode(obj,::Rails.application.secrets.secret_key_base)
15
- end
12
+ def self.decode(params,request,response)
13
+ instance_hash = nil
14
+ auth_token_obj = ::Arcadex::Header.grab_param_header(params,request,::Settings.token_header,false)
15
+ begin
16
+ # Try JWT token
17
+ jwt = ::JWT.decode(auth_token_obj,::Rails.application.secrets.secret_key_base)
18
+ token = jwt[0]
19
+ return make_hash(token["user_id"],token["auth_token"])
20
+ rescue ::JWT::ExpiredSignature
21
+ return handle_expired(auth_token_obj,params,request,response)
22
+ rescue ::JWT::DecodeError
23
+ return handle_abnormal(auth_token_obj,params,request,response)
24
+ end
25
+ end
16
26
 
17
- def self.decode(params,request)
18
- instance_hash = nil
19
- auth_token_obj = ::Arcadex::Header.grab_param_header(params,request,::Settings.token_header,false)
20
- begin
21
- # Try JWT token
22
- jwt = ::JWT.decode(auth_token_obj,::Rails.application.secrets.secret_key_base)
23
- token = jwt[0]
24
- user = ::User.find_by(id: token["user_id"])
25
- instance_hash = {}
26
- instance_hash["current_owner"] = user
27
- instance_hash["current_token"] = nil
28
- instance_hash["auth_token"] = token["auth_token"]
29
- return instance_hash
30
- rescue ::JWT::ExpiredSignature
31
- return handle_expired(auth_token_obj)
32
- rescue ::JWT::DecodeError
33
- return handle_abnormal(params,request)
34
- end
35
- end
27
+ def self.handle_expired(auth_token_obj,params,request,response)
28
+ # Token expired, destroy arcadex token
29
+ jwt = ::JWT.decode(auth_token_obj,::Rails.application.secrets.secret_key_base,true,{verify_expiration: false})
30
+ token = jwt[0]
31
+ # This is nil if the db_token is expired
32
+ db_token = ::Arcadex::Find.find_token_by_auth_token(token["auth_token"])
33
+ if db_token.nil?
34
+ return nil
35
+ else
36
+ if ::Settings.revalidate_tokens == "true"
37
+ # Send a new JWT back to the user since the db_token is still valid
38
+ new_token = encode(db_token.auth_token,token["user_id"])
39
+ response.headers[::Settings.token_header] = new_token
40
+ return make_hash(token["user_id"],token["auth_token"])
41
+ else
42
+ db_token.destroy
43
+ return nil
44
+ end
45
+ end
46
+ end
36
47
 
37
- private
48
+ def self.handle_abnormal(auth_token_obj,params,request,response)
49
+ # Try Arcadex token
50
+ instance_hash = ::Arcadex::Authentication.get_instance(params,request,::Settings.token_header)
51
+ if !instance_hash.nil?
52
+ instance_hash["auth_token"] = nil
53
+ end
54
+ return instance_hash
55
+ end
38
56
 
39
- def self.handle_expired(auth_token_obj)
40
- # Token expired, destroy arcadex token
41
- jwt = ::JWT.decode(auth_token_obj,::Rails.application.secrets.secret_key_base,true,{verify_expiration: false})
42
- token = jwt[0]
43
- db_token = ::Arcadex::Find.find_token_by_auth_token(token["auth_token"])
44
- if !db_token.nil?
45
- db_token.destroy
46
- end
47
- return nil
48
- end
49
-
50
- def self.handle_abnormal(params,request)
51
- # Try Arcadex token
52
- instance_hash = ::Arcadex::Authentication.get_instance(params,request,::Settings.token_header)
53
- if !instance_hash.nil?
54
- instance_hash["auth_token"] = nil
55
- end
56
- return instance_hash
57
- end
57
+ def self.make_hash(user_id,auth_token)
58
+ instance_hash = {}
59
+ instance_hash["current_owner"] = nil
60
+ instance_hash["current_token"] = nil
61
+ instance_hash["user_id"] = user_id
62
+ instance_hash["auth_token"] = auth_token
63
+ return instance_hash
64
+ end
58
65
 
59
66
  end
@@ -1,3 +1,3 @@
1
1
  module HorsePower
2
- VERSION = "0.9.0"
2
+ VERSION = "1.0.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: horse_power
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.0
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Cleophus Robinson IV
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-04-28 00:00:00.000000000 Z
11
+ date: 2015-04-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails