nomis-api-client 0.1.0 → 0.1.1

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: 3b189250ccb75265b2ea337a3b285f9aa0ac15d1
4
- data.tar.gz: 46e3e49ac09d5fcc76198f16a8ddda01cab1ae61
3
+ metadata.gz: 61abe8cac6c008008682e6496d4ec8b6678d3d54
4
+ data.tar.gz: 7b3f94e1036fc8be3cddb483529109de5e145e90
5
5
  SHA512:
6
- metadata.gz: 3cae9a8f6236c82c52ae8cff6a32036b0ce4f7ba9ddfed8560b3d4463c365983682369cc4223a88b4b707b2030b8acdd720171887eb8da4fcddafc0400ff7376
7
- data.tar.gz: b1db9098bc9958fe862e7216ec6a12d49558d020402264bd8c8fd137105934151ffbe476561324c9873304e0936a1b9154bc018a618b98927cfbb85040690abc
6
+ metadata.gz: f2a5063b7ad15686499cd2483c4eafb86c518f27b13613b48d535d599023c6343687277fb756736af94dbaba76e82d2b621d29adf73ad595a610ff94552926b8
7
+ data.tar.gz: a04e789fb87a8f53e9ee8919d0a0d3281bf51698ff9e5c97145430e78492f76df6396a1bd6b83b1c4f4a76e89f170f0dc6de0222b04a1301b6df95ae3a698d78
data/lib/nomis/api.rb CHANGED
@@ -2,3 +2,4 @@ require 'nomis/api/auth_token'
2
2
  require 'nomis/api/get'
3
3
  require 'nomis/api/parsed_response'
4
4
  require 'nomis/api/post'
5
+ require 'nomis/api/token_mismatch_error'
@@ -6,7 +6,7 @@ module NOMIS
6
6
  module API
7
7
  # Encapsulates the complexity of generating a JWT bearer token
8
8
  class AuthToken
9
- attr_accessor :client_token, :client_key, :iat_fudge_factor
9
+ attr_accessor :client_token, :client_key, :iat_fudge_factor, :now
10
10
 
11
11
  # iat_fudge_factor allows you to correct for time drift between your
12
12
  # client and the target server.
@@ -29,14 +29,12 @@ module NOMIS
29
29
  def bearer_token
30
30
  validate_keys!
31
31
 
32
- auth_token = JWT.encode(payload, client_key, 'ES256')
33
-
34
32
  "Bearer #{auth_token}"
35
33
  end
36
34
 
37
35
  def payload
38
36
  {
39
- iat: Time.now.to_i + iat_fudge_factor,
37
+ iat: current_timestamp + iat_fudge_factor,
40
38
  token: client_token
41
39
  }
42
40
  end
@@ -47,26 +45,44 @@ module NOMIS
47
45
  # error message can only say that the generated JWT token does not
48
46
  # validate.
49
47
  def validate_keys!
50
- client_pub = OpenSSL::PKey::EC.new client_key
51
- client_pub.private_key = nil
52
- client_pub_base64 = Base64.strict_encode64(client_pub.to_der)
53
-
54
- expected_client_pub = JWT.decode(client_token, nil, nil)[0]['key']
55
-
56
- unless client_pub_base64 == expected_client_pub
57
- raise 'Incorrect private key supplied ' \
58
- + '(does not match public key within token)'
48
+ unless client_public_key_base64 == expected_client_public_key
49
+ raise TokenMismatchError,
50
+ 'Incorrect private key supplied ' \
51
+ + '(does not match public key within token)',
52
+ caller
59
53
  end
60
54
  end
61
55
 
62
56
  protected
63
57
 
64
- def default_client_key(params={})
65
- read_client_key_file(params[:client_key_file] || ENV['NOMIS_API_CLIENT_KEY_FILE'])
58
+ def auth_token
59
+ JWT.encode(payload, client_key, 'ES256')
60
+ end
61
+
62
+ def client_public_key_base64
63
+ client_public_key = OpenSSL::PKey::EC.new client_key
64
+ client_public_key.private_key = nil
65
+ Base64.strict_encode64(client_public_key.to_der)
66
+ end
67
+
68
+ def expected_client_public_key
69
+ JWT.decode(client_token, nil, nil)[0]['key']
70
+ end
71
+
72
+ def current_timestamp
73
+ now || Time.now.to_i
74
+ end
75
+
76
+ def default_client_key(params = {})
77
+ read_client_key_file(
78
+ params[:client_key_file] || ENV['NOMIS_API_CLIENT_KEY_FILE']
79
+ )
66
80
  end
67
81
 
68
- def default_client_token(params={})
69
- read_client_key_file(params[:client_token_file] || ENV['NOMIS_API_CLIENT_TOKEN_FILE'])
82
+ def default_client_token(params = {})
83
+ read_client_key_file(
84
+ params[:client_token_file] || ENV['NOMIS_API_CLIENT_TOKEN_FILE']
85
+ )
70
86
  end
71
87
 
72
88
  def default_iat_fudge_factor(params={})
data/lib/nomis/api/get.rb CHANGED
@@ -1,7 +1,6 @@
1
1
  require 'uri'
2
2
  require 'net/http'
3
3
  require 'pp'
4
- require 'byebug'
5
4
 
6
5
  require 'nomis/api/auth_token'
7
6
  require 'nomis/api/parsed_response'
@@ -0,0 +1,6 @@
1
+ module NOMIS
2
+ module API
3
+ class TokenMismatchError < RuntimeError
4
+ end
5
+ end
6
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nomis-api-client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Al Davidson
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-06-01 00:00:00.000000000 Z
11
+ date: 2017-06-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: jwt
@@ -53,6 +53,7 @@ files:
53
53
  - lib/nomis/api/get.rb
54
54
  - lib/nomis/api/parsed_response.rb
55
55
  - lib/nomis/api/post.rb
56
+ - lib/nomis/api/token_mismatch_error.rb
56
57
  - lib/nomis_api_client_ruby.rb
57
58
  homepage: http://rubygems.org/gems/nomis_api_client_ruby
58
59
  licenses: