Class: Respoke::Client

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

Overview

Contains methods to make API calls.

Examples:

One-step endpoint authentication


require 'respoke'

client = Respoke::Client.new(app_secret: '77269d84-d7f3-49da-8eab-bd6686160035')
client.app_token(
  appId: '0cdf7bc1-45d1-420a-963e-c797a6f7ba61',
  roleId: '47ea573f-5a78-42f4-927c-fe658bc00f91',
  endpointId: 'foo-bar-user'
)
#=> '3c022dbd-0a82-4382-bd0d-5af6e11b8d67'

Manual endpoint authentication


require 'respoke'

client = Respoke::Client.new(app_secret: '77269d84-d7f3-49da-8eab-bd6686160035')

session_id_response = client.request_session_token_id(
  appId: '0cdf7bc1-45d1-420a-963e-c797a6f7ba61',
  roleId: '47ea573f-5a78-42f4-927c-fe658bc00f91',
  endpointId: 'foo-bar-user'
)
session = client.request_session_token(
  appId: session_id_response.appId,
  tokenId: session_id_response.tokenId
)

session.token #=> '3c022dbd-0a82-4382-bd0d-5af6e11b8d67'

# OR you can just use Client#app_token since Client#request_session_token
# sets `@app_token`.
client.app_token #=> '3c022dbd-0a82-4382-bd0d-5af6e11b8d67'

Constant Summary collapse

DEFAULT_BASE_URL =

Default base_url

'https://api.respoke.io/v1'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app_secret: nil, base_url: DEFAULT_BASE_URL) ⇒ Respoke::Client

Creates a new Client instance.

Parameters:

  • app_secret (String) (defaults to: nil)

    The application App-Secret token. (Defaults to nil)

  • base_url (String) (defaults to: DEFAULT_BASE_URL)

    Overrides the DEFAULT_BASE_URL constant. (Defaults to DEFAULT_BASE_URL)


64
65
66
67
# File 'lib/respoke/client.rb', line 64

def initialize(app_secret: nil, base_url: DEFAULT_BASE_URL)
  @base_url = base_url
  @app_secret = app_secret
end

Instance Attribute Details

#app_secretObject (readonly)

Sets the App-Secret token


54
55
56
# File 'lib/respoke/client.rb', line 54

def app_secret
  @app_secret
end

#app_token(token_request_params = {}) ⇒ String (readonly)

Either returns the current `@app_token` or sets it based on the parameter Hash `token_request_params`.

Parameters:

Options Hash (token_request_params):

  • appId (String)

    The application ID.

  • roleId (String)

    Role ID to use for permissions on given endpoint ID.

  • endpointId (String)

    The endpoint ID to authenticate.

  • ttl (Number) — default: 86400

    Length of time token is valid.

Returns:

  • (String)

    App-Token value.


45
46
47
# File 'lib/respoke/client.rb', line 45

def app_token
  @app_token
end

#base_urlObject (readonly)

Base URL used for API requests


51
52
53
# File 'lib/respoke/client.rb', line 51

def base_url
  @base_url
end

Instance Method Details

#create_role(name:, rules: {}) ⇒ Respoke::Role

Create a role

Parameters:

  • name (String)

    The name of the role

  • rules (Hash) (defaults to: {})

    The permissions for the role

Returns:

Raises:


179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/respoke/client.rb', line 179

def create_role(name:, rules: {})
  response = connection.post 'roles' do |request|
    request.body = rules.merge( name: name )
  end

  if response.status != 200
    raise Respoke::Errors::UnexpectedServerError, <<-ERR
      request failed with status #{response.status}:
      #{response.body}
    ERR
  else
    Respoke::Role.new(self, response.body.each_with_object({}) { |(k,v), h| h[k.to_sym] = v} )
  end
end

#delete_role(id:) ⇒ Boolean

Delete a role

Parameters:

  • id (String)

    The id of the role to retrieve

Returns:

  • (Boolean)

    true if the role was deleted, false otherwise


243
244
245
246
# File 'lib/respoke/client.rb', line 243

def delete_role(id:)
  response = connection.delete "roles/#{id}"
  response.success?
end

#find_role(id:) ⇒ Respoke::Role

Find a role

Parameters:

  • id (String)

    The id of the role to retrieve

Returns:

  • (Respoke::Role)

    The role that was retrieved, nil if none found

Raises:


201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/respoke/client.rb', line 201

def find_role(id:)
  response = connection.get "roles/#{id}"

  if response.status == 404
    nil
  elsif !response.success?
    raise Respoke::Errors::UnexpectedServerError, <<-ERR
      request failed with status #{response.status}:
      #{response.body}
    ERR
  else
    Respoke::Role.new(self, response.body.each_with_object({}) { |(k,v), h| h[k.to_sym] = v} )
  end
end

#request_session_token(appId:, tokenId:) ⇒ Respoke::Response::SessionToken

TODO:

test setting of `@app_token`.

Request the session token using the tokenId retrived with #request_session_token_id. This method sets the `app_token` attribute.

Parameters:

  • appId (String)

    The application ID used in the token request.

  • tokenId (String)

    The token ID requested from #request_session_token_id.

Returns:


136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/respoke/client.rb', line 136

def request_session_token(appId:, tokenId:)
  response = connection.post 'session-tokens' do |request|
    request.body = {
      appId: appId,
      tokenId: tokenId
    }
  end
  @app_token = response.body.fetch('token', nil)

  if response.status != 200
    raise Respoke::Errors::UnexpectedServerError, <<-ERR
      request failed with status #{response.status}:
      #{response.body}
    ERR
  else
    Respoke::Response::SessionToken.new(response.body)
  end
end

#request_session_token_id(appId:, roleId:, endpointId:, ttl: 86400) ⇒ Respoke::Response::SessionTokenId

TODO:

test return value

Request a token ID for use in requesting the App-Token value.

Parameters:

  • appId (String)

    The application ID that matches the App-Secret.

  • roleId (String)

    The role ID to use for the given endpoint.

  • endpointId (String)

    The endpoint ID that is being authenticated.

  • ttl (Number) (defaults to: 86400)

    The amount of time in seconds the App-Token is valid. (Defaults to 86400)

Returns:


106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/respoke/client.rb', line 106

def request_session_token_id(appId:, roleId:, endpointId:, ttl: 86400)
  response = connection.post 'tokens' do |request|
    request.body = {
      appId: appId,
      endpointId: endpointId,
      roleId: roleId,
      ttl: 86400
    }
  end

  if response.status != 200
    raise Respoke::Errors::UnexpectedServerError, <<-ERR
      request failed with status #{response.status}:
      #{response.body}
    ERR
  else
    Respoke::Response::SessionTokenId.new(response.body)
  end
end

#rolesArray<Respoke::Role>

Get the roles

Returns:

Raises:


159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/respoke/client.rb', line 159

def roles()
  response = connection.get 'roles'

  if response.status != 200
    raise Respoke::Errors::UnexpectedServerError, <<-ERR
      request failed with status #{response.status}:
      #{response.body}
    ERR
  else
    response.body.map { |r| Respoke::Role.new(self, r.each_with_object({}) { |(k,v), h| h[k.to_sym] = v} ) }
  end
end

#update_role(id:, rules:) ⇒ Boolean

Update a role

Parameters:

  • id (String)

    The id of the role to update

  • rules (Hash)

    The new permissions for the role

Returns:

  • (Boolean)

    true if successfully updated

Raises:


223
224
225
226
227
228
229
230
231
232
233
234
235
236
# File 'lib/respoke/client.rb', line 223

def update_role(id:, rules:)
  response = connection.put "roles/#{id}" do |request|
    request.body = rules
  end

  if !response.success?
    raise Respoke::Errors::UnexpectedServerError, <<-ERR
      request failed with status #{response.status}:
      #{response.body}
    ERR
  else
    true
  end
end