Module: SafeNet

Defined in:
lib/safenet.rb

Constant Summary collapse

@@NAME =

default values

"Ruby Demo App"
@@VERSION =
"0.0.1"
@@VENDOR =
"Vendor's Name"
@@ID =
"org.thevendor.demo"
@@LAUNCHER_SERVER =
"http://localhost:8100/"
@@CONF_PATH =
File.join(File.expand_path('..', __FILE__), "conf.json")

Class Method Summary collapse

Class Method Details

.authObject

An application exchanges data with the SAFE Launcher using symmetric key

encryption. The symmetric key is session based and is securely transferred
from the SAFE Launcher to the application using ECDH Key Exchange.

Applications will generate an asymmetric key pair and a nonce for ECDH Key

Exchange with the SAFE Launcher.

The application will initiate the authorisation request with the generated

nonce and public key, along with information about the application and the
required permissions.

The SAFE Launcher will prompt to the user with the application information

along with the requested permissions. Once the user authorises the
request, the symmetric keys for encryption are received. If the user
denies the request then the SAFE Launcher sends an unauthorised error
response.

Usage: SafeNet.auth() Fail: nil Success: “1222”, encryptedKey: “232”, “publicKey”: “4323”, “permissions”: []

Reference: maidsafe.readme.io/docs/auth



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/safenet.rb', line 50

def self.auth
  # entry point
  url = "#{@@LAUNCHER_SERVER}auth"

  # new random key
  private_key = RbNaCl::PrivateKey.generate
  nonce = RbNaCl::Random.random_bytes(24)

  # payload
  payload = {
    app: {
      name: @@NAME,
      version: @@VERSION,
      vendor: @@VENDOR,
      id: @@ID
    },
    publicKey: Base64.strict_encode64(private_key.public_key),
    nonce: Base64.strict_encode64(nonce),
    permissions: []
  }

  # api call
  uri = URI(url)
  http = Net::HTTP.new(uri.host, uri.port)
  req = Net::HTTP::Post.new(uri.path, {'Content-Type' => 'application/json'})
  req.body = payload.to_json
  res = http.request(req)

  # return's parser
  if res.code == "200"
    response = JSON.parse(res.body)

    # save it in conf.json
    conf = response.dup
    conf["nonce"] = Base64.strict_encode64(nonce)
    conf["privateKey"] = Base64.strict_encode64(private_key)
    File.open(@@CONF_PATH, "w") { |f| f << JSON.pretty_generate(conf) }

    # invalidates @@keys
    @@keys = {}
  else
    # puts "ERROR #{res.code}: #{res.message}"
    response = nil
  end

  # return
  response
end

.create_directory(dir_path, options = {}) ⇒ Object

Create a directory using the NFS API. Only authorised requests can create a directory.

Usage: SafeNet.create_directory(“/photos”, metadata: “2323”, is_private: true, is_path_shared: false, is_versioned: false) Fail: “description”=>“NfsError::FileAlreadyExistsWithSameName” Success: true

Reference: maidsafe.readme.io/docs/nfs-create-directory



229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# File 'lib/safenet.rb', line 229

def self.create_directory(dir_path, options = {})
  # entry point
  url = "#{@@LAUNCHER_SERVER}nfs/directory"

  # default values
  options[:is_private]     = true  if ! options.has_key?(:is_private)
  options[:is_versioned]   = false if ! options.has_key?(:is_versioned)
  options[:is_path_shared] = false if ! options.has_key?(:is_path_shared)

  # payload
  payload = {
    dirPath: dir_path,
    isPrivate: options[:is_private],
    isVersioned: options[:is_versioned],
    isPathShared: options[:is_path_shared]
  }

  # optional
  payload["metadata"] = options[:metadata] if options.has_key?(:metadata)

  # api call
  uri = URI(url)
  http = Net::HTTP.new(uri.host, uri.port)
  req = Net::HTTP::Post.new(uri.path, {
    'Authorization' => "Bearer #{self.get_valid_token()}",
    'Content-Type' => 'text/plain'
  })
  req.body = self.encrypt(payload.to_json)
  res = http.request(req)
  res.code == "200" ? true : JSON.parse(self.decrypt(res.body))
end

.create_long_name(long_name) ⇒ Object



343
344
345
346
347
348
349
350
351
352
353
354
355
356
# File 'lib/safenet.rb', line 343

def self.create_long_name(long_name)
  # entry point
  url = "#{@@LAUNCHER_SERVER}dns/#{CGI.escape(long_name)}"

  # api call
  uri = URI(url)
  http = Net::HTTP.new(uri.host, uri.port)
  req = Net::HTTP::Post.new(uri.path, {
    'Authorization' => "Bearer #{self.get_valid_token()}",
    'Content-Type' => 'text/plain'
  })
  res = http.request(req)
  res.code == "200" ? true : JSON.parse(self.decrypt(res.body))
end

.delete_directory(dir_path, options = {}) ⇒ Object

ex.: delete_directory(“/photos”)



263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
# File 'lib/safenet.rb', line 263

def self.delete_directory(dir_path, options = {})
  # default values
  options[:is_path_shared] = false if ! options.has_key?(:is_path_shared)

  # entry point
  url = "#{@@LAUNCHER_SERVER}nfs/directory/#{CGI.escape(dir_path)}/#{options[:is_path_shared]}"

  # api call
  uri = URI(url)
  http = Net::HTTP.new(uri.host, uri.port)
  req = Net::HTTP::Delete.new(uri.path, {
    'Authorization' => "Bearer #{self.get_valid_token()}",
  })
  res = http.request(req)
  res.code == "200" ? true : JSON.parse(self.decrypt(res.body))
end

.delete_file(file_path, options = {}) ⇒ Object



325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
# File 'lib/safenet.rb', line 325

def self.delete_file(file_path, options = {})
  # default values
  options[:is_path_shared] = false if ! options.has_key?(:is_path_shared)

  # entry point
  url = "#{@@LAUNCHER_SERVER}nfs/file/#{CGI.escape(file_path)}/#{options[:is_path_shared]}"

  # api call
  uri = URI(url)
  http = Net::HTTP.new(uri.host, uri.port)
  req = Net::HTTP::Delete.new(uri.path, {
    'Authorization' => "Bearer #{self.get_valid_token()}",
  })
  res = http.request(req)
  res.code == "200" ? true : JSON.parse(self.decrypt(res.body))
end

.file(file_path, options = {}) ⇒ Object

Create a File using the NFS API. Only authorised requests can invoke the API.

Usage: SafeNet.file(“/photos/cat.jpg”, metadata: “ad sad”, is_path_shared: false, is_private: true, is_versioned: false) Fail: “description”=>“NfsError::FileAlreadyExistsWithSameName” Success: true

Reference: maidsafe.readme.io/docs/nfsfile



187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/safenet.rb', line 187

def self.file(file_path, options = {})
  url = "#{@@LAUNCHER_SERVER}nfs/file"

  # default values
  options[:is_private]     = true  if ! options.has_key?(:is_private)
  options[:is_versioned]   = false if ! options.has_key?(:is_versioned)
  options[:is_path_shared] = false if ! options.has_key?(:is_path_shared)

  # payload
  payload = {
    filePath: file_path,
    isPrivate: options[:is_private],
    isVersioned: options[:is_versioned],
    isPathShared: options[:is_path_shared]
  }

  # optional
  payload["metadata"] = options[:metadata] if options.has_key?(:metadata)

  # api call
  uri = URI(url)
  http = Net::HTTP.new(uri.host, uri.port)
  req = Net::HTTP::Post.new(uri.path, {
    'Authorization' => "Bearer #{self.get_valid_token()}",
    'Content-Type' => 'text/plain'
  })
  req.body = self.encrypt(payload.to_json)
  res = http.request(req)
  res.code == "200" ? true : JSON.parse(self.decrypt(res.body))
end

.get_directory(dir_path, options = {}) ⇒ Object

Create a directory using the NFS API. Only authorised requests can create a directory.

Usage: SafeNet.get_directory(“/photos”, is_path_shared: false) Fail: “description”=>“FfiError::PathNotFound” Success: {“name”=> “Ruby Demo App-Root-Dir”, …, …}

Reference: maidsafe.readme.io/docs/nfs-create-directory



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/safenet.rb', line 159

def self.get_directory(dir_path, options = {})
  # default values
  options[:is_path_shared] = false if ! options.has_key?(:is_path_shared)

  # entry point
  url = "#{@@LAUNCHER_SERVER}nfs/directory/#{CGI.escape(dir_path)}/#{options[:is_path_shared]}"

  # api call
  uri = URI(url)
  http = Net::HTTP.new(uri.host, uri.port)
  req = Net::HTTP::Get.new(uri.path, {
    'Authorization' => "Bearer #{self.get_valid_token()}",
  })
  res = http.request(req)
  JSON.parse(self.decrypt(res.body))
end

.get_file(file_path, options = {}) ⇒ Object

options: offset, length, is_path_shared



281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
# File 'lib/safenet.rb', line 281

def self.get_file(file_path, options = {})
  # default values
  options[:offset]         = 0     if ! options.has_key?(:offset)
  options[:is_path_shared] = false if ! options.has_key?(:is_path_shared)

  # entry point
  url = "#{@@LAUNCHER_SERVER}nfs/file/#{CGI.escape(file_path)}/#{options[:is_path_shared]}?"

  # query params are encrypted
  query = []
  query << "offset=#{options[:offset]}"
  query << "length=#{options[:length]}" if options.has_key?(:length) # length is optional
  url = "#{url}#{self.encrypt(query.join('&'))}"

  # api call
  uri = URI(url)
  http = Net::HTTP.new(uri.host, uri.port)
  req = Net::HTTP::Get.new(uri.path, {
    'Authorization' => "Bearer #{self.get_valid_token()}",
  })
  res = http.request(req)
  res.code == "200" ? self.decrypt(res.body) : JSON.parse(self.decrypt(res.body))
end

.get_file_unauth(long_name, service_name, file_path, options = {}) ⇒ Object

get_file_unauth(“thegoogle”, “www”, “index.html”, offset: 3, length: 5)



435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
# File 'lib/safenet.rb', line 435

def self.get_file_unauth(long_name, service_name, file_path, options = {})
  # default values
  options[:offset] = 0 if ! options.has_key?(:offset)

  # entry point
  url = "#{@@LAUNCHER_SERVER}dns/#{CGI.escape(service_name)}/#{CGI.escape(long_name)}/#{CGI.escape(file_path)}?"

  # query params are encrypted
  query = []
  query << "offset=#{options[:offset]}"
  query << "length=#{options[:length]}" if options.has_key?(:length) # length is optional
  url = "#{url}#{self.encrypt(query.join('&'))}"

  # api call
  uri = URI(url)
  http = Net::HTTP.new(uri.host, uri.port)
  req = Net::HTTP::Get.new(uri.path)
  res = http.request(req)
  res.code == "200" ? res.body : JSON.parse(res.body)
end

.get_home_dir(long_name, service_name) ⇒ Object



421
422
423
424
425
426
427
428
429
430
431
# File 'lib/safenet.rb', line 421

def self.get_home_dir(long_name, service_name)
  # entry point
  url = "#{@@LAUNCHER_SERVER}dns/#{CGI.escape(service_name)}/#{CGI.escape(long_name)}"

  # api call
  uri = URI(url)
  http = Net::HTTP.new(uri.host, uri.port)
  req = Net::HTTP::Get.new(uri.path)
  res = http.request(req)
  JSON.parse(res.body)
end

.is_token_validObject

To check whether the authorisation token obtained is valid. The Authorization header must be present in the request.

Usage: SafeNet.is_token_valid() Fail: false Success: true

Reference: maidsafe.readme.io/docs/is-token-valid



110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/safenet.rb', line 110

def self.is_token_valid
  # entry point
  url = "#{@@LAUNCHER_SERVER}auth"

  # api call
  uri = URI(url)
  http = Net::HTTP.new(uri.host, uri.port)
  req = Net::HTTP::Get.new(uri.path, {
    'Authorization' => "Bearer #{self.get_token()}"
  })
  res = http.request(req)
  res.code == "200"
end

.list_long_namesObject



391
392
393
394
395
396
397
398
399
400
401
402
403
# File 'lib/safenet.rb', line 391

def self.list_long_names
  # entry point
  url = "#{@@LAUNCHER_SERVER}dns"

  # api call
  uri = URI(url)
  http = Net::HTTP.new(uri.host, uri.port)
  req = Net::HTTP::Get.new(uri.path, {
    'Authorization' => "Bearer #{self.get_valid_token()}",
  })
  res = http.request(req)
  JSON.parse(self.decrypt(res.body))
end

.list_services(long_name) ⇒ Object



406
407
408
409
410
411
412
413
414
415
416
417
418
# File 'lib/safenet.rb', line 406

def self.list_services(long_name)
  # entry point
  url = "#{@@LAUNCHER_SERVER}dns/#{CGI.escape(long_name)}"

  # api call
  uri = URI(url)
  http = Net::HTTP.new(uri.host, uri.port)
  req = Net::HTTP::Get.new(uri.path, {
    'Authorization' => "Bearer #{self.get_valid_token()}",
  })
  res = http.request(req)
  JSON.parse(self.decrypt(res.body))
end

.register_service(long_name, service_name, service_home_dir_path, options = {}) ⇒ Object

ex.: register_service(“thegoogle”, “www”, “/www”)



360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
# File 'lib/safenet.rb', line 360

def self.register_service(long_name, service_name, service_home_dir_path, options = {})
  # entry point
  url = "#{@@LAUNCHER_SERVER}dns"

  # default values
  options[:is_path_shared] = false if ! options.has_key?(:is_path_shared)

  # payload
  payload = {
    longName: long_name,
    serviceName: service_name,
    serviceHomeDirPath: service_home_dir_path,
    isPathShared: options[:is_path_shared]
  }

  # optional
  payload["metadata"] = options[:metadata] if options.has_key?(:metadata)

  # api call
  uri = URI(url)
  http = Net::HTTP.new(uri.host, uri.port)
  req = Net::HTTP::Post.new(uri.path, {
    'Authorization' => "Bearer #{self.get_valid_token()}",
    'Content-Type' => 'text/plain'
  })
  req.body = self.encrypt(payload.to_json)
  res = http.request(req)
  res.code == "200" ? true : JSON.parse(self.decrypt(res.body))
end

.revoke_tokenObject

Removes the token from the SAFE Launcher.

Usage: SafeNet.revoke_token() Fail: false Success: true

Reference: maidsafe.readme.io/docs/revoke-token



134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/safenet.rb', line 134

def self.revoke_token
  # entry point
  url = "#{@@LAUNCHER_SERVER}auth"

  # api call
  uri = URI(url)
  http = Net::HTTP.new(uri.host, uri.port)
  req = Net::HTTP::Delete.new(uri.path, {
    'Authorization' => "Bearer #{self.get_valid_token()}"
  })
  res = http.request(req)
  res.code == "200"
end

.set_app_info(options) ⇒ Object



18
19
20
21
22
23
24
25
# File 'lib/safenet.rb', line 18

def self.set_app_info(options)
  @@NAME = options[:name] if options.has_key?(:name)
  @@VERSION = options[:version] if options.has_key?(:version)
  @@VENDOR = options[:vendor] if options.has_key?(:vendor)
  @@ID = options[:id] if options.has_key?(:id)
  @@LAUNCHER_SERVER = options[:server] if options.has_key?(:server)
  @@CONF_PATH = options[:conf_file] if options.has_key?(:conf_file)
end

.update_file_content(file_path, contents, options = {}) ⇒ Object



305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
# File 'lib/safenet.rb', line 305

def self.update_file_content(file_path, contents, options = {})
  # default values
  options[:offset]         = 0     if ! options.has_key?(:offset)
  options[:is_path_shared] = false if ! options.has_key?(:is_path_shared)

  # entry point
  url = "#{@@LAUNCHER_SERVER}nfs/file/#{CGI.escape(file_path)}/#{options[:is_path_shared]}?offset=#{options[:offset]}"

  # api call
  uri = URI(url)
  http = Net::HTTP.new(uri.host, uri.port)
  req = Net::HTTP::Put.new(uri.path, {
    'Authorization' => "Bearer #{self.get_valid_token()}",
    'Content-Type' => 'text/plain'
  })
  req.body = self.encrypt(contents)
  res = http.request(req)
  res.code == "200" ? true : JSON.parse(self.decrypt(res.body))
end