ruby-safenet 0.0.3 → 0.0.4
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 +4 -4
- data/README.md +7 -4
- data/lib/safenet.rb +132 -43
- data/lib/safenet/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA1:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 2fd845e03b3d1254c4a2d51007b7a7626a31efa0
|
|
4
|
+
data.tar.gz: cb52f9349cf546fe6ef203a06fd983b3d9305033
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 3b0c0aa75968b2c8db96d1cfd49d79f7ab8e780b290dd2a7495a1f29f7f176ef0f4802977305cd14adb785b8ebda24784474b49ec15f11b7f2ee893ddcdaa32c
|
|
7
|
+
data.tar.gz: 403be8627d2416aefefdcf47f8d803a09f0444db5946ce13ff97d7483d05bf7c6752ec938d83c56510adcf2952cb1265525d2c709cf9cc299e6a7d7cbb4c89df
|
data/README.md
CHANGED
|
@@ -3,8 +3,9 @@
|
|
|
3
3
|
A Ruby library for accessing the SAFE network
|
|
4
4
|
|
|
5
5
|
## Installation
|
|
6
|
-
|
|
6
|
+
```
|
|
7
7
|
$ gem install safenet
|
|
8
|
+
```
|
|
8
9
|
|
|
9
10
|
## Usage
|
|
10
11
|
|
|
@@ -18,14 +19,16 @@ SafeNet.set_app_info({
|
|
|
18
19
|
id: "org.thevendor.demo",
|
|
19
20
|
})
|
|
20
21
|
|
|
21
|
-
SafeNet.create_directory("/mydir")
|
|
22
|
+
SafeNet.create_directory("/mydir", is_private: false)
|
|
22
23
|
SafeNet.file("/mydir/index.html", is_private: false)
|
|
23
24
|
SafeNet.update_file_content("/mydir/index.html", "Hello world!<br>I'm a webpage :D")
|
|
24
|
-
SafeNet.register_service("
|
|
25
|
+
SafeNet.register_service("my-wonderful-app", "www", "/mydir")
|
|
25
26
|
SafeNet.get_file("/mydir/index.html")
|
|
27
|
+
|
|
28
|
+
# Then, open http://www.my-wonderful-app.safenet/
|
|
26
29
|
```
|
|
27
30
|
|
|
28
|
-
*Upload / Download:*
|
|
31
|
+
*File Upload / Download:*
|
|
29
32
|
```ruby
|
|
30
33
|
# upload
|
|
31
34
|
SafeNet.file("/mydir/dog.jpg")
|
data/lib/safenet.rb
CHANGED
|
@@ -12,7 +12,7 @@ module SafeNet
|
|
|
12
12
|
@@VERSION = "0.0.1"
|
|
13
13
|
@@VENDOR = "Vendor's Name"
|
|
14
14
|
@@ID = "org.thevendor.demo"
|
|
15
|
-
@@
|
|
15
|
+
@@LAUNCHER_SERVER = "http://localhost:8100/"
|
|
16
16
|
@@CONF_PATH = File.join(File.expand_path('..', __FILE__), "conf.json")
|
|
17
17
|
|
|
18
18
|
def self.set_app_info(options)
|
|
@@ -20,16 +20,42 @@ module SafeNet
|
|
|
20
20
|
@@VERSION = options[:version] if options.has_key?(:version)
|
|
21
21
|
@@VENDOR = options[:vendor] if options.has_key?(:vendor)
|
|
22
22
|
@@ID = options[:id] if options.has_key?(:id)
|
|
23
|
-
@@
|
|
23
|
+
@@LAUNCHER_SERVER = options[:server] if options.has_key?(:server)
|
|
24
24
|
@@CONF_PATH = options[:conf_file] if options.has_key?(:conf_file)
|
|
25
25
|
end
|
|
26
26
|
|
|
27
|
+
#
|
|
28
|
+
# An application exchanges data with the SAFE Launcher using symmetric key
|
|
29
|
+
# encryption. The symmetric key is session based and is securely transferred
|
|
30
|
+
# from the SAFE Launcher to the application using ECDH Key Exchange.
|
|
31
|
+
# Applications will generate an asymmetric key pair and a nonce for ECDH Key
|
|
32
|
+
# Exchange with the SAFE Launcher.
|
|
33
|
+
#
|
|
34
|
+
# The application will initiate the authorisation request with the generated
|
|
35
|
+
# nonce and public key, along with information about the application and the
|
|
36
|
+
# required permissions.
|
|
37
|
+
#
|
|
38
|
+
# The SAFE Launcher will prompt to the user with the application information
|
|
39
|
+
# along with the requested permissions. Once the user authorises the
|
|
40
|
+
# request, the symmetric keys for encryption are received. If the user
|
|
41
|
+
# denies the request then the SAFE Launcher sends an unauthorised error
|
|
42
|
+
# response.
|
|
43
|
+
#
|
|
44
|
+
# Usage: SafeNet.auth()
|
|
45
|
+
# Fail: nil
|
|
46
|
+
# Success: {token: "1222", encryptedKey: "232", "publicKey": "4323", "permissions": []}
|
|
47
|
+
#
|
|
48
|
+
# Reference: https://maidsafe.readme.io/docs/auth
|
|
49
|
+
#
|
|
27
50
|
def self.auth
|
|
28
|
-
|
|
51
|
+
# entry point
|
|
52
|
+
url = "#{@@LAUNCHER_SERVER}auth"
|
|
29
53
|
|
|
54
|
+
# new random key
|
|
30
55
|
private_key = RbNaCl::PrivateKey.generate
|
|
31
56
|
nonce = RbNaCl::Random.random_bytes(24)
|
|
32
57
|
|
|
58
|
+
# payload
|
|
33
59
|
payload = {
|
|
34
60
|
app: {
|
|
35
61
|
name: @@NAME,
|
|
@@ -42,12 +68,14 @@ module SafeNet
|
|
|
42
68
|
permissions: []
|
|
43
69
|
}
|
|
44
70
|
|
|
71
|
+
# api call
|
|
45
72
|
uri = URI(url)
|
|
46
73
|
http = Net::HTTP.new(uri.host, uri.port)
|
|
47
74
|
req = Net::HTTP::Post.new(uri.path, {'Content-Type' => 'application/json'})
|
|
48
75
|
req.body = payload.to_json
|
|
49
76
|
res = http.request(req)
|
|
50
77
|
|
|
78
|
+
# return's parser
|
|
51
79
|
if res.code == "200"
|
|
52
80
|
response = JSON.parse(res.body)
|
|
53
81
|
|
|
@@ -57,18 +85,33 @@ module SafeNet
|
|
|
57
85
|
conf["privateKey"] = Base64.strict_encode64(private_key)
|
|
58
86
|
File.open(@@CONF_PATH, "w") { |f| f << JSON.pretty_generate(conf) }
|
|
59
87
|
|
|
88
|
+
# invalidates @@keys
|
|
89
|
+
@@keys = {}
|
|
60
90
|
else
|
|
61
91
|
# puts "ERROR #{res.code}: #{res.message}"
|
|
62
92
|
response = nil
|
|
63
93
|
end
|
|
64
94
|
|
|
95
|
+
# return
|
|
65
96
|
response
|
|
66
97
|
end
|
|
67
98
|
|
|
68
99
|
|
|
100
|
+
#
|
|
101
|
+
# To check whether the authorisation token obtained is valid.
|
|
102
|
+
# The Authorization header must be present in the request.
|
|
103
|
+
#
|
|
104
|
+
# Usage: SafeNet.is_token_valid()
|
|
105
|
+
# Fail: false
|
|
106
|
+
# Success: true
|
|
107
|
+
#
|
|
108
|
+
# Reference: https://maidsafe.readme.io/docs/is-token-valid
|
|
109
|
+
#
|
|
69
110
|
def self.is_token_valid
|
|
70
|
-
|
|
111
|
+
# entry point
|
|
112
|
+
url = "#{@@LAUNCHER_SERVER}auth"
|
|
71
113
|
|
|
114
|
+
# api call
|
|
72
115
|
uri = URI(url)
|
|
73
116
|
http = Net::HTTP.new(uri.host, uri.port)
|
|
74
117
|
req = Net::HTTP::Get.new(uri.path, {
|
|
@@ -79,9 +122,20 @@ module SafeNet
|
|
|
79
122
|
end
|
|
80
123
|
|
|
81
124
|
|
|
125
|
+
#
|
|
126
|
+
# Removes the token from the SAFE Launcher.
|
|
127
|
+
#
|
|
128
|
+
# Usage: SafeNet.revoke_token()
|
|
129
|
+
# Fail: false
|
|
130
|
+
# Success: true
|
|
131
|
+
#
|
|
132
|
+
# Reference: https://maidsafe.readme.io/docs/revoke-token
|
|
133
|
+
#
|
|
82
134
|
def self.revoke_token
|
|
83
|
-
|
|
135
|
+
# entry point
|
|
136
|
+
url = "#{@@LAUNCHER_SERVER}auth"
|
|
84
137
|
|
|
138
|
+
# api call
|
|
85
139
|
uri = URI(url)
|
|
86
140
|
http = Net::HTTP.new(uri.host, uri.port)
|
|
87
141
|
req = Net::HTTP::Delete.new(uri.path, {
|
|
@@ -91,13 +145,25 @@ module SafeNet
|
|
|
91
145
|
res.code == "200"
|
|
92
146
|
end
|
|
93
147
|
|
|
148
|
+
|
|
149
|
+
#
|
|
150
|
+
# Create a directory using the NFS API.
|
|
151
|
+
# Only authorised requests can create a directory.
|
|
152
|
+
#
|
|
153
|
+
# Usage: SafeNet.get_directory("/photos", is_path_shared: false)
|
|
154
|
+
# Fail: {"errorCode"=>-1502, "description"=>"FfiError::PathNotFound"}
|
|
155
|
+
# Success: {"info"=> {"name"=> "Ruby Demo App-Root-Dir", ...}, ...}
|
|
156
|
+
#
|
|
157
|
+
# Reference: https://maidsafe.readme.io/docs/nfs-create-directory
|
|
158
|
+
#
|
|
94
159
|
def self.get_directory(dir_path, options = {})
|
|
95
160
|
# default values
|
|
96
|
-
options[:is_path_shared]
|
|
161
|
+
options[:is_path_shared] = false if ! options.has_key?(:is_path_shared)
|
|
97
162
|
|
|
98
163
|
# entry point
|
|
99
|
-
url = "#{@@
|
|
164
|
+
url = "#{@@LAUNCHER_SERVER}nfs/directory/#{CGI.escape(dir_path)}/#{options[:is_path_shared]}"
|
|
100
165
|
|
|
166
|
+
# api call
|
|
101
167
|
uri = URI(url)
|
|
102
168
|
http = Net::HTTP.new(uri.host, uri.port)
|
|
103
169
|
req = Net::HTTP::Get.new(uri.path, {
|
|
@@ -108,21 +174,30 @@ module SafeNet
|
|
|
108
174
|
end
|
|
109
175
|
|
|
110
176
|
|
|
111
|
-
#
|
|
177
|
+
#
|
|
178
|
+
# Create a File using the NFS API.
|
|
179
|
+
# Only authorised requests can invoke the API.
|
|
180
|
+
#
|
|
181
|
+
# Usage: SafeNet.file("/photos/cat.jpg", metadata: {item1: "ad sad"}, is_path_shared: false, is_private: true, is_versioned: false)
|
|
182
|
+
# Fail: {"errorCode"=>-505, "description"=>"NfsError::FileAlreadyExistsWithSameName"}
|
|
183
|
+
# Success: true
|
|
184
|
+
#
|
|
185
|
+
# Reference: https://maidsafe.readme.io/docs/nfsfile
|
|
186
|
+
#
|
|
112
187
|
def self.file(file_path, options = {})
|
|
113
|
-
url = "#{@@
|
|
188
|
+
url = "#{@@LAUNCHER_SERVER}nfs/file"
|
|
114
189
|
|
|
115
190
|
# default values
|
|
116
|
-
options[:is_private]
|
|
117
|
-
options[:is_versioned]
|
|
118
|
-
options[:is_path_shared]
|
|
191
|
+
options[:is_private] = true if ! options.has_key?(:is_private)
|
|
192
|
+
options[:is_versioned] = false if ! options.has_key?(:is_versioned)
|
|
193
|
+
options[:is_path_shared] = false if ! options.has_key?(:is_path_shared)
|
|
119
194
|
|
|
120
195
|
# payload
|
|
121
196
|
payload = {
|
|
122
197
|
filePath: file_path,
|
|
123
|
-
isPrivate: options[:is_private]
|
|
124
|
-
isVersioned: options[:is_versioned]
|
|
125
|
-
isPathShared: options[:is_path_shared]
|
|
198
|
+
isPrivate: options[:is_private],
|
|
199
|
+
isVersioned: options[:is_versioned],
|
|
200
|
+
isPathShared: options[:is_path_shared]
|
|
126
201
|
}
|
|
127
202
|
|
|
128
203
|
# optional
|
|
@@ -141,22 +216,31 @@ module SafeNet
|
|
|
141
216
|
end
|
|
142
217
|
|
|
143
218
|
|
|
144
|
-
#
|
|
145
|
-
#
|
|
219
|
+
#
|
|
220
|
+
# Create a directory using the NFS API.
|
|
221
|
+
# Only authorised requests can create a directory.
|
|
222
|
+
#
|
|
223
|
+
# Usage: SafeNet.create_directory("/photos", metadata: {entry1: "2323"}, is_private: true, is_path_shared: false, is_versioned: false)
|
|
224
|
+
# Fail: {"errorCode"=>-505, "description"=>"NfsError::FileAlreadyExistsWithSameName"}
|
|
225
|
+
# Success: true
|
|
226
|
+
#
|
|
227
|
+
# Reference: https://maidsafe.readme.io/docs/nfs-create-directory
|
|
228
|
+
#
|
|
146
229
|
def self.create_directory(dir_path, options = {})
|
|
147
|
-
|
|
230
|
+
# entry point
|
|
231
|
+
url = "#{@@LAUNCHER_SERVER}nfs/directory"
|
|
148
232
|
|
|
149
233
|
# default values
|
|
150
|
-
options[:is_private]
|
|
151
|
-
options[:is_versioned]
|
|
152
|
-
options[:is_path_shared]
|
|
234
|
+
options[:is_private] = true if ! options.has_key?(:is_private)
|
|
235
|
+
options[:is_versioned] = false if ! options.has_key?(:is_versioned)
|
|
236
|
+
options[:is_path_shared] = false if ! options.has_key?(:is_path_shared)
|
|
153
237
|
|
|
154
238
|
# payload
|
|
155
239
|
payload = {
|
|
156
240
|
dirPath: dir_path,
|
|
157
|
-
isPrivate: options[:is_private]
|
|
158
|
-
isVersioned: options[:is_versioned]
|
|
159
|
-
isPathShared: options[:is_path_shared]
|
|
241
|
+
isPrivate: options[:is_private],
|
|
242
|
+
isVersioned: options[:is_versioned],
|
|
243
|
+
isPathShared: options[:is_path_shared]
|
|
160
244
|
}
|
|
161
245
|
|
|
162
246
|
# optional
|
|
@@ -178,10 +262,10 @@ module SafeNet
|
|
|
178
262
|
# ex.: delete_directory("/photos")
|
|
179
263
|
def self.delete_directory(dir_path, options = {})
|
|
180
264
|
# default values
|
|
181
|
-
options[:is_path_shared]
|
|
265
|
+
options[:is_path_shared] = false if ! options.has_key?(:is_path_shared)
|
|
182
266
|
|
|
183
267
|
# entry point
|
|
184
|
-
url = "#{@@
|
|
268
|
+
url = "#{@@LAUNCHER_SERVER}nfs/directory/#{CGI.escape(dir_path)}/#{options[:is_path_shared]}"
|
|
185
269
|
|
|
186
270
|
# api call
|
|
187
271
|
uri = URI(url)
|
|
@@ -196,11 +280,11 @@ module SafeNet
|
|
|
196
280
|
# options: offset, length, is_path_shared
|
|
197
281
|
def self.get_file(file_path, options = {})
|
|
198
282
|
# default values
|
|
199
|
-
options[:offset]
|
|
200
|
-
options[:is_path_shared]
|
|
283
|
+
options[:offset] = 0 if ! options.has_key?(:offset)
|
|
284
|
+
options[:is_path_shared] = false if ! options.has_key?(:is_path_shared)
|
|
201
285
|
|
|
202
286
|
# entry point
|
|
203
|
-
url = "#{@@
|
|
287
|
+
url = "#{@@LAUNCHER_SERVER}nfs/file/#{CGI.escape(file_path)}/#{options[:is_path_shared]}?"
|
|
204
288
|
|
|
205
289
|
# query params are encrypted
|
|
206
290
|
query = []
|
|
@@ -220,11 +304,11 @@ module SafeNet
|
|
|
220
304
|
|
|
221
305
|
def self.update_file_content(file_path, contents, options = {})
|
|
222
306
|
# default values
|
|
223
|
-
options[:offset]
|
|
224
|
-
options[:is_path_shared]
|
|
307
|
+
options[:offset] = 0 if ! options.has_key?(:offset)
|
|
308
|
+
options[:is_path_shared] = false if ! options.has_key?(:is_path_shared)
|
|
225
309
|
|
|
226
310
|
# entry point
|
|
227
|
-
url = "#{@@
|
|
311
|
+
url = "#{@@LAUNCHER_SERVER}nfs/file/#{CGI.escape(file_path)}/#{options[:is_path_shared]}?offset=#{options[:offset]}"
|
|
228
312
|
|
|
229
313
|
# api call
|
|
230
314
|
uri = URI(url)
|
|
@@ -240,10 +324,10 @@ module SafeNet
|
|
|
240
324
|
|
|
241
325
|
def self.delete_file(file_path, options = {})
|
|
242
326
|
# default values
|
|
243
|
-
options[:is_path_shared]
|
|
327
|
+
options[:is_path_shared] = false if ! options.has_key?(:is_path_shared)
|
|
244
328
|
|
|
245
329
|
# entry point
|
|
246
|
-
url = "#{@@
|
|
330
|
+
url = "#{@@LAUNCHER_SERVER}nfs/file/#{CGI.escape(file_path)}/#{options[:is_path_shared]}"
|
|
247
331
|
|
|
248
332
|
# api call
|
|
249
333
|
uri = URI(url)
|
|
@@ -257,7 +341,8 @@ module SafeNet
|
|
|
257
341
|
|
|
258
342
|
|
|
259
343
|
def self.create_long_name(long_name)
|
|
260
|
-
|
|
344
|
+
# entry point
|
|
345
|
+
url = "#{@@LAUNCHER_SERVER}dns/#{CGI.escape(long_name)}"
|
|
261
346
|
|
|
262
347
|
# api call
|
|
263
348
|
uri = URI(url)
|
|
@@ -273,17 +358,18 @@ module SafeNet
|
|
|
273
358
|
|
|
274
359
|
# ex.: register_service("thegoogle", "www", "/www")
|
|
275
360
|
def self.register_service(long_name, service_name, service_home_dir_path, options = {})
|
|
276
|
-
|
|
361
|
+
# entry point
|
|
362
|
+
url = "#{@@LAUNCHER_SERVER}dns"
|
|
277
363
|
|
|
278
364
|
# default values
|
|
279
|
-
options[:is_path_shared]
|
|
365
|
+
options[:is_path_shared] = false if ! options.has_key?(:is_path_shared)
|
|
280
366
|
|
|
281
367
|
# payload
|
|
282
368
|
payload = {
|
|
283
369
|
longName: long_name,
|
|
284
370
|
serviceName: service_name,
|
|
285
371
|
serviceHomeDirPath: service_home_dir_path,
|
|
286
|
-
isPathShared: options[:is_path_shared]
|
|
372
|
+
isPathShared: options[:is_path_shared]
|
|
287
373
|
}
|
|
288
374
|
|
|
289
375
|
# optional
|
|
@@ -303,7 +389,8 @@ module SafeNet
|
|
|
303
389
|
|
|
304
390
|
|
|
305
391
|
def self.list_long_names
|
|
306
|
-
|
|
392
|
+
# entry point
|
|
393
|
+
url = "#{@@LAUNCHER_SERVER}dns"
|
|
307
394
|
|
|
308
395
|
# api call
|
|
309
396
|
uri = URI(url)
|
|
@@ -317,7 +404,8 @@ module SafeNet
|
|
|
317
404
|
|
|
318
405
|
|
|
319
406
|
def self.list_services(long_name)
|
|
320
|
-
|
|
407
|
+
# entry point
|
|
408
|
+
url = "#{@@LAUNCHER_SERVER}dns/#{CGI.escape(long_name)}"
|
|
321
409
|
|
|
322
410
|
# api call
|
|
323
411
|
uri = URI(url)
|
|
@@ -331,7 +419,8 @@ module SafeNet
|
|
|
331
419
|
|
|
332
420
|
|
|
333
421
|
def self.get_home_dir(long_name, service_name)
|
|
334
|
-
|
|
422
|
+
# entry point
|
|
423
|
+
url = "#{@@LAUNCHER_SERVER}dns/#{CGI.escape(service_name)}/#{CGI.escape(long_name)}"
|
|
335
424
|
|
|
336
425
|
# api call
|
|
337
426
|
uri = URI(url)
|
|
@@ -345,10 +434,10 @@ module SafeNet
|
|
|
345
434
|
# get_file_unauth("thegoogle", "www", "index.html", offset: 3, length: 5)
|
|
346
435
|
def self.get_file_unauth(long_name, service_name, file_path, options = {})
|
|
347
436
|
# default values
|
|
348
|
-
options[:offset]
|
|
437
|
+
options[:offset] = 0 if ! options.has_key?(:offset)
|
|
349
438
|
|
|
350
439
|
# entry point
|
|
351
|
-
url = "#{@@
|
|
440
|
+
url = "#{@@LAUNCHER_SERVER}dns/#{CGI.escape(service_name)}/#{CGI.escape(long_name)}/#{CGI.escape(file_path)}?"
|
|
352
441
|
|
|
353
442
|
# query params are encrypted
|
|
354
443
|
query = []
|
data/lib/safenet/version.rb
CHANGED