kuvera-api 0.1.0 → 0.2.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
  SHA256:
3
- metadata.gz: 5fddfc99b0d0876c4fd02ffd90aca5e6fb178de283ab3ec631332528caa01c9b
4
- data.tar.gz: a821110c62d38067eb2d370aa37087dc7789077985cebb555355e93aa7df30ee
3
+ metadata.gz: 7b690819abf1e87b510cdd9f894cea824d4f7df76c4975c4a899439c76125cc4
4
+ data.tar.gz: 37d0fa5af70896776aaaa0deff5b24b195a19c6623223c767dcf83402b448078
5
5
  SHA512:
6
- metadata.gz: fae1327eb06d199560c2cfc362089b427be97163bea2af30dd78f7df2e16c1239b152b78d7d6832626efc148403d5b4c50aeb828fc0b4a3b2843ef82ac8e1ba5
7
- data.tar.gz: fb9af7509a3717daf11f1b9fd2444bad682a7658ed182f3bfc38741b2cf30e37e0bc3c85f70510ca8b6e0621d5ad6fef866002da0bb5435f47ecffecf8cfcb9c
6
+ metadata.gz: c7927b5d0192c756fdc1c1514062f775846f7368aeb64fa51a020428223195a96f8ba96dce5dd1467584d385fea9736cbbc2895af8fface0e64201dff0a5d4e8
7
+ data.tar.gz: 798e9e7d83b1ca34c2a255d10627c5d6d3b3c2fa0b62f2636cadfac2959977baaf1afc015d78c9fba1b9b9e8bc0b1442db9474a8eaff5f6004fcc7528b0efdc0
data/README.md CHANGED
@@ -20,13 +20,32 @@ Or install it yourself as:
20
20
 
21
21
  ## Usage
22
22
 
23
- Currently this gem only supports one-time secrets receving through the `at`
24
- command.
23
+ - One-time secrets receving through the `at` command.
25
24
 
26
25
  ```ruby
27
- Kuvera::Api.at('0x78c4e15f8f1f3e43f6950975f97ff7c2858bcc5a')
26
+ Kuvera::Api.at('SHARE-78c4e15f8f1f3e43f6950975f97ff7c2858bcc5a')
28
27
  ```
29
28
 
29
+ - Authentification with OAuth credentials
30
+ ```ruby
31
+ Kuvera::Api.me
32
+ # => {"id"=>1, "admin_id"=>2, "name"=>"Kuvera Carrier"}
33
+ ```
34
+
35
+ - Secret files uploading
36
+ ```ruby
37
+ Kuvera::Api.upload('My Passport', File.open('passport.pdf'), 'application/pdf')
38
+ # => {"address"=>"KEY-11ff53da91ba292ef628b457895bf7ea", "status"=>"success", "title"=>"My Passport"}
39
+ ```
40
+
41
+ - Secrets sharing
42
+ ```ruby
43
+ Kuvera::Api.share('KEY-11ff53da91ba292ef628b457895bf7ea')
44
+ # => {"path"=>"https://kuvera.io/at/SHARE-d00a96d19889639a5a5d3991c6fab49d", "status"=>"success"}
45
+ ```
46
+
47
+ In order to use carrier-related methods you need to provide `OAUTH_UID` and `OAUTH_SECRET`.
48
+
30
49
  ## Contributing
31
50
 
32
51
  Bug reports and pull requests are welcome on GitHub at https://github.com/CleverLabs/kuvera-api.
data/kuvera-api.gemspec CHANGED
@@ -19,6 +19,8 @@ Gem::Specification.new do |spec|
19
19
  end
20
20
  spec.require_paths = ['lib']
21
21
 
22
+ spec.add_dependency 'oauth2', '~> 1.4'
23
+
22
24
  spec.add_development_dependency 'bundler', '~> 1.17'
23
25
  spec.add_development_dependency 'rake', '~> 10.0'
24
26
  spec.add_development_dependency 'rspec', '~> 3.0'
data/lib/kuvera/api.rb CHANGED
@@ -11,7 +11,7 @@ module Kuvera
11
11
 
12
12
  module_function
13
13
 
14
- def_delegators :client, :at
14
+ def_delegators :client, :at, :me, :upload, :share
15
15
 
16
16
  def client
17
17
  @client ||= Client.new
@@ -4,28 +4,62 @@ require 'json'
4
4
  require 'net/http'
5
5
 
6
6
  require 'kuvera/api/errors'
7
+ require 'kuvera/api/oauth'
7
8
 
8
9
  module Kuvera
9
10
  module Api
10
11
  class Client
11
12
  HOST = 'https://kuvera.io/'
12
- PATH = 'at/'
13
+ ENV_UID = ENV['OAUTH_UID']
14
+ ENV_SECRET = ENV['OAUTH_SECRET']
15
+ ENV_HOST = ENV.fetch('KUVERA_API', HOST)
16
+
17
+ AT_ENDPOINT = 'at/'
18
+ ME_ENDPOINT = '/api/v1/me'
19
+ UPLOAD_ENDPOINT = '/api/v1/secrets'
20
+ SHARE_ENDPOINT = '/api/v1/links'
21
+
13
22
  ROOT = 'secret'
23
+ ATTACHMENT = 'SecretAttachment'
14
24
 
15
- def initialize(host: nil)
16
- @host = host || ENV.fetch("KUVERA_API", HOST)
25
+ def initialize(host: ENV_HOST, uid: ENV_UID, secret: ENV_SECRET)
26
+ @host = host
27
+ @uid = uid
28
+ @secret = secret
17
29
  end
18
30
 
19
31
  RESPONSES = {
20
- 200 => ->(response) { JSON.parse(response.body).fetch(ROOT) },
32
+ 200 => ->(response) { response.body },
21
33
  403 => ->(_response) { raise SecretAlreadyRead },
22
34
  404 => ->(_response) { raise SecretNotFound }
23
35
  }.freeze
24
36
 
25
37
  def at(address)
26
- response = Net::HTTP.get_response(URI.join(@host, PATH, address))
38
+ response = Net::HTTP.get_response(URI.join(@host, AT_ENDPOINT, address))
27
39
  RESPONSES.fetch(response.code.to_i).call(response)
28
40
  end
41
+
42
+ def me
43
+ oauth.get(ME_ENDPOINT)
44
+ end
45
+
46
+ def upload(title, io, mime)
47
+ upload = Faraday::UploadIO.new(io, mime, io.path.split('/').last)
48
+ body = { new_secret: { type: ATTACHMENT, title: title, body: upload } }
49
+ oauth.post(UPLOAD_ENDPOINT, body, headers: {})
50
+ end
51
+
52
+ def share(address)
53
+ oauth.post(SHARE_ENDPOINT, { secret_id: address }.to_json)
54
+ end
55
+
56
+ private
57
+
58
+ def oauth
59
+ raise MissingCredentials if [@host, @uid, @secret].any?(&:nil?)
60
+
61
+ @_oauth = Oauth.new(uid: @uid, secret: @secret, host: @host)
62
+ end
29
63
  end
30
64
  end
31
65
  end
@@ -4,5 +4,7 @@ module Kuvera
4
4
  module Api
5
5
  SecretNotFound = Class.new(StandardError)
6
6
  SecretAlreadyRead = Class.new(StandardError)
7
+
8
+ MissingCredentials = Class.new(StandardError)
7
9
  end
8
10
  end
@@ -0,0 +1,47 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'oauth2'
4
+ require 'net/http/post/multipart'
5
+
6
+ module Kuvera
7
+ module Api
8
+ class Oauth
9
+ DEFAULT_CONTENT_TYPE = { 'Content-Type' => 'application/json' }.freeze
10
+ FATAL_ERROR_MESSAGE = { 'error' => 'Internal error' }.freeze
11
+ CLIENT_SCOPE = 'ext_app'
12
+
13
+ def initialize(uid:, secret:, host:)
14
+ @oauth_client = OAuth2::Client.new(
15
+ uid, secret, site: host, token_method: :post
16
+ ) do |stack|
17
+ stack.request :multipart
18
+ stack.request :url_encoded
19
+ stack.adapter Faraday.default_adapter
20
+ end
21
+ end
22
+
23
+ def get(url, params = {})
24
+ token.get(url, params: params).parsed
25
+ rescue OAuth2::Error => error
26
+ process_exception(error)
27
+ end
28
+
29
+ def post(url, body, headers: DEFAULT_CONTENT_TYPE.dup)
30
+ token.post(url, headers: headers, body: body).parsed
31
+ rescue OAuth2::Error => error
32
+ process_exception(error)
33
+ end
34
+
35
+ private
36
+
37
+ def token
38
+ @token ||=
39
+ @oauth_client.client_credentials.get_token(scope: CLIENT_SCOPE)
40
+ end
41
+
42
+ def process_exception(exception)
43
+ exception.response.parsed || FATAL_ERROR_MESSAGE
44
+ end
45
+ end
46
+ end
47
+ end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Kuvera
4
4
  module Api
5
- VERSION = '0.1.0'
5
+ VERSION = '0.2.0'
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,15 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kuvera-api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ignat Zakrevsky
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-03-11 00:00:00.000000000 Z
11
+ date: 2019-11-12 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: oauth2
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.4'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.4'
13
27
  - !ruby/object:Gem::Dependency
14
28
  name: bundler
15
29
  requirement: !ruby/object:Gem::Requirement
@@ -88,6 +102,7 @@ files:
88
102
  - lib/kuvera/api.rb
89
103
  - lib/kuvera/api/client.rb
90
104
  - lib/kuvera/api/errors.rb
105
+ - lib/kuvera/api/oauth.rb
91
106
  - lib/kuvera/api/version.rb
92
107
  homepage: https://kuvera.io
93
108
  licenses: