Class: SafeNet::Auth
- Inherits:
-
Object
- Object
- SafeNet::Auth
- Defined in:
- lib/safenet.rb
Instance Method Summary collapse
-
#auth ⇒ Object
Any application that wants to access API endpoints that require authorised access must receive an authorisation token from SAFE Launcher.
-
#initialize(client_obj) ⇒ Auth
constructor
A new instance of Auth.
-
#is_token_valid ⇒ Object
Check whether the authorisation token obtained from SAFE Launcher is still valid.
-
#revoke_token ⇒ Object
Revoke the authorisation token obtained from SAFE Launcher.
Constructor Details
#initialize(client_obj) ⇒ Auth
Returns a new instance of Auth.
80 81 82 |
# File 'lib/safenet.rb', line 80 def initialize(client_obj) @client = client_obj end |
Instance Method Details
#auth ⇒ Object
Any application that wants to access API endpoints that require authorised
access must receive an authorisation token from SAFE Launcher.
Reading public data using the DNS API does not require an authorisation
token. All other API endpoints require authorised access.
The application will initiate the authorisation request with information
about the application itself and the required permissions. SAFE Launcher
will then display a prompt to the user with the application information
along with the requested permissions. Once the user authorises the
request, the application will receive an authorisation token. If the user
denies the request, the application will receive an unauthorised error
response.
Usage: my_client.auth.auth() Fail: nil Success: “1222”, “permissions”: []
Reference: maidsafe.readme.io/docs/auth
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/safenet.rb', line 105 def auth # entry point url = "#{@client.app_info[:launcher_server]}#{API_VERSION}/auth" # payload payload = { app: { name: @client.app_info[:name], version: @client.app_info[:version], vendor: @client.app_info[:vendor], id: @client.app_info[:id] }, permissions: @client.app_info[: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 File.open(@client.app_info[:conf_path], "w") { |f| f << JSON.pretty_generate(conf) } else # puts "ERROR #{res.code}: #{res.message}" response = nil end # return response end |
#is_token_valid ⇒ Object
Check whether the authorisation token obtained from SAFE Launcher is still
valid.
Usage: my_client.auth.is_token_valid() Fail: false Success: true
Reference: maidsafe.readme.io/docs/is-token-valid
154 155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/safenet.rb', line 154 def is_token_valid # entry point url = "#{@client.app_info[:launcher_server]}#{API_VERSION}/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_token ⇒ Object
Revoke the authorisation token obtained from SAFE Launcher.
Usage: my_client.auth.revoke_token() Fail: false Success: true
Reference: maidsafe.readme.io/docs/revoke-token
178 179 180 181 182 183 184 185 186 187 188 189 190 |
# File 'lib/safenet.rb', line 178 def revoke_token # entry point url = "#{@client.app_info[:launcher_server]}#{API_VERSION}/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 |