authz_jurnal_client 0.0.8 → 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: cce433a0abf6a809cf51a500be0f95a23c581af6bc2b05ce56ff7514e4af6d9e
4
- data.tar.gz: 3c67c5b9589d9c351df4e6d1c6ee6e4a9454957caa5fdcc50cae0b370760131f
3
+ metadata.gz: 93c657e409a5a2c68bfd2d7bae8bd74cbcfb9ed65e0e0b5f271c385b5bb2f122
4
+ data.tar.gz: ec9383d9b8f0c274c391e8b6cf7dfee0df93b37651e222a7fecaa609a7b07d5c
5
5
  SHA512:
6
- metadata.gz: 6edc9d4d7222acd96f9c46225edb44228b1c9e2dcc25666250d02a8e5c2ebbba6b7ab0989d05ab998f9e3611107d843e838ca750abf6c0f0a52602183c5b860b
7
- data.tar.gz: 44e59bb6cd13a5c813d220e023be9f97a50062b3259d662c5b78ea5cd0a7e7265948c3cc7b96bc23b1370357cfff051aa01d3b0b29e28189057c6d565e370aee
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.8)
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") or AuthzJurnalClient::UserRoles.config(username: nil, password: nil)
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.8"
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,65 +8,28 @@ module AuthzJurnalClient
10
8
 
11
9
  class UserRoles
12
10
  class << self
13
- attr_accessor :redis, :domain, :authorization, :username, :password, :expiry, :cache
14
- def config(opts = {})
15
- @username = opts[:username]
16
- @password = opts[:password]
17
- @authorization = init_auth(opts)
18
- @headers = { "Authorization": @authorization }
19
- @domain = opts[:domain] || 'http://localhost:3000'
20
- redis_path = opts[:redis] || 'redis://localhost:6379'
21
- @redis = Redis.new(url: redis_path)
22
- @expiry = opts[:expiry] || 3600
23
- @cache = opts[:cache].nil? ? true : opts[:cache]
24
- end
25
-
26
- def init_auth(opts)
27
- @authorization = opts[:authorization]
11
+ attr_accessor :headers
28
12
 
29
- if @username && @password
30
- @authorization = "Basic #{Base64.encode64("#{@username}:#{@password}")}"
31
- end
32
-
33
- @authorization
34
- end
13
+ def call(req)
14
+ @headers = r_headers(req.headers)
35
15
 
36
- def call(id, cid)
37
- return get_errors({ errors: { messages: "uid or cid cannot be blank!" } }) if id.nil? || cid.nil?
16
+ return get_errors({ errors: { messages: "You are not authorized to access!" } }) if @headers == {} || @headers['Authorization'].nil?
38
17
 
39
- if @redis && @cache
40
- cached = @redis.get(make_key(id, cid))
41
- return get_response(cached) if cached
18
+ if @headers['X-Consumer-Roles'].nil?
19
+ return get_errors({ errors: { messages: "Roles not found on headers!" } })
42
20
  end
43
21
 
44
- response = backend_request(id, cid)
45
- return get_errors(JSON.parse(response.body)) if !response.success?
46
-
47
- @redis&.setex(make_key(id, cid), @expiry, response.body)
48
- get_response(response.body)
49
- rescue Redis::CannotConnectError
50
- response = backend_request(id, cid)
51
- get_response(response.body)
22
+ roles
52
23
  end
53
24
 
54
- def backend_request(id, cid)
55
- conn = Faraday.new("#{@domain}/api/v1/users/#{id}") do |f|
56
- f.options.open_timeout = 10
57
- f.options.timeout = 60
58
- f.adapter :net_http
59
- end
60
-
61
- conn.get('roles', { company_id: cid }, @headers)
62
- end
25
+ private
63
26
 
64
- def make_key(id, cid)
65
- "manage_roles:#{id}-#{cid}"
27
+ def roles
28
+ @headers['X-Consumer-Roles'].split(',').collect { |e| e.strip }
66
29
  end
67
30
 
68
- def get_response(j)
69
- data = eval(j)
70
- response = JSON.parse(data.to_json, object_class: OpenStruct)
71
- response.data.roles
31
+ def r_headers(h)
32
+ h.is_a?(Hash) ? JSON.parse(h.to_json) : h
72
33
  end
73
34
 
74
35
  def get_errors(e)
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.8
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-13 00:00:00.000000000 Z
11
+ date: 2023-02-14 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description:
14
14
  email: