Class: Talis::Authentication::Client

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/talis/authentication/client.rb

Overview

Represents an OAuth client

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(token, opts = {}) ⇒ Client

Returns a new instance of Client.



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/talis/authentication/client.rb', line 10

def initialize(token, opts = {})
  @token = token
  acquire_host!(opts)
  acquire_credentials!

  response = authenticate!
  body = JSON.parse response.body
  @scopes = body['scope']
  raise Talis::AuthenticationFailedError unless
      response.code == 200
end

Instance Attribute Details

#client_idObject (readonly)

Returns the value of attribute client_id.



9
10
11
# File 'lib/talis/authentication/client.rb', line 9

def client_id
  @client_id
end

#client_secretObject (readonly)

Returns the value of attribute client_secret.



9
10
11
# File 'lib/talis/authentication/client.rb', line 9

def client_secret
  @client_secret
end

#hostObject (readonly)

Returns the value of attribute host.



9
10
11
# File 'lib/talis/authentication/client.rb', line 9

def host
  @host
end

#scopesObject (readonly)

Returns the value of attribute scopes.



9
10
11
# File 'lib/talis/authentication/client.rb', line 9

def scopes
  @scopes
end

#tokenObject (readonly)

Returns the value of attribute token.



9
10
11
# File 'lib/talis/authentication/client.rb', line 9

def token
  @token
end

Instance Method Details

#add_scope(scope) ⇒ Object



22
23
24
25
26
27
# File 'lib/talis/authentication/client.rb', line 22

def add_scope(scope)
  if scope.is_a? String
    response = modify_scope(:add, scope)
    @scopes << scope if response.code == 204
  end
end

#modify_scope(action, scope) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/talis/authentication/client.rb', line 36

def modify_scope(action, scope)
  action = case action
           when :add
             '$add'
           when :remove
             '$remove'
           else
             raise 'Unknown action'
           end
  patch_client_scope(action, scope)
end

#remove_scope(scope) ⇒ Object



29
30
31
32
33
34
# File 'lib/talis/authentication/client.rb', line 29

def remove_scope(scope)
  if scope.is_a? String
    response = modify_scope(:remove, scope)
    @scopes.delete(scope) if response.code == 204
  end
end