authz_jurnal_client 0.0.7 → 0.0.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d069465c6785118e6e04894f5e8b876d782d240db6a0202086f9891e8cba5e10
4
- data.tar.gz: c2c16f5fe8092437ad332a843544fd284865ba291509f10fd8d586740eaf03e0
3
+ metadata.gz: 93c657e409a5a2c68bfd2d7bae8bd74cbcfb9ed65e0e0b5f271c385b5bb2f122
4
+ data.tar.gz: ec9383d9b8f0c274c391e8b6cf7dfee0df93b37651e222a7fecaa609a7b07d5c
5
5
  SHA512:
6
- metadata.gz: d041033d4ee886da9f2568f34b4737252b8c996dd29b47f143bd80682b8cb67a2f0b16691550860c0ea649e8cf39437a46918e4643751c58b17f809ef6f17fb2
7
- data.tar.gz: a7fc7343a40bba4805b8d0796f9db0b3d7fafe5e7654938eae98d92548aa4676c6b6136bb1fa48555cba40659a4b6c128333c7399e0ecb05fbb676d1516c78ba
6
+ metadata.gz: 3f2557b899d5237e218e0e7277a2196bb7a85220c6636e75a758e402eee4e933294bd0c8d739ac3f9121ad20e80bc1ff6eb4c9e68e51b8c5cd9114dafe6f35b8
7
+ data.tar.gz: 5036d37930dcd53905e39665945e0beb0e0f420e0157f5274d4bcf76ac57f4b5d5ebe729da188e3c5b1a6149e4af84cf5f6a416f0c1ed59ec60d63f46b136a32
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- authz_jurnal_client (0.0.7)
4
+ authz_jurnal_client (0.0.9)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -10,7 +10,7 @@ AuthzJurnalClient allows you to access the user roles API directly. It also prov
10
10
  Install the gem thusly in the gemfile
11
11
 
12
12
  ``` markdown
13
- gem 'authz_jurnal_client', git: 'git@bitbucket.org:jurnal/authz-jurnal-client.git', tag: 'v0.0.1'
13
+ gem 'authz_jurnal_client', git: 'git@bitbucket.org:jurnal/authz-jurnal-client.git', tag: 'v0.0.9'
14
14
  ```
15
15
 
16
16
 
@@ -20,14 +20,9 @@ TODO: Write usage instructions here
20
20
 
21
21
  ``` Ruby
22
22
  Default :
23
- AuthzJurnalClient::UserRoles.config(authorization: "Basic ApiToken")
24
- roles = AuthzJurnalClient::UserRoles.call(uid, cid)
25
- puts roles
26
-
27
- Customize :
28
- AuthzJurnalClient::UserRoles.config(redis: "redis://localhost:6379", domain: 'http://mydomain.com', expiry: 3600, authorization: "Basic ApiToken")
29
- roles = AuthzJurnalClient::UserRoles.call(uid, cid)
30
- puts roles
23
+ AuthzJurnalClient::UserRoles.call(request)
24
+ ex: ['ultimate','admin']
25
+
31
26
  ```
32
27
 
33
28
  The response is a json parsed Openstruct
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module AuthzJurnalClient
4
- VERSION = "0.0.7"
4
+ VERSION = "0.0.9"
5
5
  end
@@ -1,7 +1,5 @@
1
1
  require "ostruct"
2
2
  require "json"
3
- require "faraday"
4
- require "redis"
5
3
 
6
4
  require_relative "authz_jurnal_client/version"
7
5
 
@@ -10,60 +8,33 @@ module AuthzJurnalClient
10
8
 
11
9
  class UserRoles
12
10
  class << self
13
- attr_accessor :redis, :domain, :authorization, :expiry, :cache
14
- def config(opts = {})
15
- @authorization = opts[:authorization]
16
- @headers = {"Authorization" => @authorization}
17
- @domain = opts[:domain] || 'http://localhost:3000'
18
- redis_path = opts[:redis] || 'redis://localhost:6379'
19
- @redis = Redis.new(url: redis_path)
20
- @expiry = opts[:expiry] || 3600
21
- @cache = opts[:cache].nil? ? true : opts[:cache]
22
- end
11
+ attr_accessor :headers
23
12
 
24
- def call(id, cid)
25
- return get_errors({errors: { messages: "uid or cid cannot be blank!"}}) if id.nil? || cid.nil?
13
+ def call(req)
14
+ @headers = r_headers(req.headers)
26
15
 
27
- if @redis && @cache
28
- cached = @redis.get(make_key(id, cid))
29
- return get_response(cached) if cached
30
- end
16
+ return get_errors({ errors: { messages: "You are not authorized to access!" } }) if @headers == {} || @headers['Authorization'].nil?
31
17
 
32
- response = backend_request(id, cid)
33
- if !response.success?
34
- return get_errors({errors: { messages: "request failed to fetch data!"}})
18
+ if @headers['X-Consumer-Roles'].nil?
19
+ return get_errors({ errors: { messages: "Roles not found on headers!" } })
35
20
  end
36
21
 
37
- @redis&.setex(make_key(id, cid), @expiry, response.body)
38
- get_response(response.body)
39
- rescue Redis::CannotConnectError
40
- response = backend_request(id, cid)
41
- get_response(response.body)
22
+ roles
42
23
  end
43
24
 
44
- def backend_request(id, cid)
45
- conn = Faraday.new("#{@domain}/api/v1/users/#{id}") do |f|
46
- f.options.open_timeout = 10
47
- f.options.timeout = 60
48
- f.adapter :net_http
49
- end
50
-
51
- conn.get('roles', { company_id: cid }, @headers)
52
- end
25
+ private
53
26
 
54
- def make_key(id, cid)
55
- "manage_roles:#{id}-#{cid}"
27
+ def roles
28
+ @headers['X-Consumer-Roles'].split(',').collect { |e| e.strip }
56
29
  end
57
30
 
58
- def get_response(j)
59
- data = eval(j)
60
- response = JSON.parse( data.to_json, object_class: OpenStruct)
61
- response.data.roles
31
+ def r_headers(h)
32
+ h.is_a?(Hash) ? JSON.parse(h.to_json) : h
62
33
  end
63
34
 
64
35
  def get_errors(e)
65
36
  data = e.is_a?(Hash) ? e : eval(e)
66
- response = JSON.parse( data.to_json, object_class: OpenStruct)
37
+ JSON.parse(data.to_json, object_class: OpenStruct)
67
38
  end
68
39
  end
69
40
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: authz_jurnal_client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.7
4
+ version: 0.0.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alam Topani
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-02-09 00:00:00.000000000 Z
11
+ date: 2023-02-14 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description:
14
14
  email: