Class: SafeNet::Auth

Inherits:
Object
  • Object
show all
Defined in:
lib/safenet.rb

Instance Method Summary collapse

Constructor Details

#initialize(client_obj) ⇒ Auth

Returns a new instance of Auth.



119
120
121
# File 'lib/safenet.rb', line 119

def initialize(client_obj)
  @client = client_obj
end

Instance 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: my_client.auth() Fail: nil Success: “1222”, encryptedKey: “232”, “publicKey”: “4323”, “permissions”: []

Reference: maidsafe.readme.io/docs/auth



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/safenet.rb', line 146

def auth
  # entry point
  url = "#{@client.app_info[:launcher_server]}auth"

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

  # payload
  payload = {
    app: {
      name: @client.app_info[:name],
      version: @client.app_info[:version],
      vendor: @client.app_info[:vendor],
      id: @client.app_info[: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(@client.app_info[:conf_path], "w") { |f| f << JSON.pretty_generate(conf) }

    # invalidates @keys
    @client.key_helper.invalidate()
  else
    # puts "ERROR #{res.code}: #{res.message}"
    response = nil
  end

  # return
  response
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



206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/safenet.rb', line 206

def is_token_valid
  # entry point
  url = "#{@client.app_info[: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 #{@client.key_helper.get_token()}"
  })
  res = http.request(req)
  res.code == "200"
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



230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/safenet.rb', line 230

def revoke_token
  # entry point
  url = "#{@client.app_info[: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 #{@client.key_helper.get_valid_token()}"
  })
  res = http.request(req)
  res.code == "200"
end