Class: ForestAdminAgent::Auth::OAuth2::ForestProvider

Inherits:
OpenIDConnect::Client
  • Object
show all
Defined in:
lib/forest_admin_agent/auth/oauth2/forest_provider.rb,
sig/forest_admin_agent/auth/oauth2/forest_provider.rbs

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rendering_id, attributes = {}) ⇒ ForestProvider

Returns a new instance of ForestProvider.

Parameters:

  • (Integer)
  • (Array[String])


10
11
12
13
14
15
16
# File 'lib/forest_admin_agent/auth/oauth2/forest_provider.rb', line 10

def initialize(rendering_id, attributes = {})
  super(attributes)
  @rendering_id = rendering_id
  @authorization_endpoint = '/oidc/auth'
  @token_endpoint = '/oidc/token'
  self.userinfo_endpoint = "/liana/v2/renderings/#{rendering_id}/authorization"
end

Instance Attribute Details

#rendering_idInteger (readonly)

Returns the value of attribute rendering_id.

Returns:

  • (Integer)


8
9
10
# File 'lib/forest_admin_agent/auth/oauth2/forest_provider.rb', line 8

def rendering_id
  @rendering_id
end

Instance Method Details

#check_responseArray[String]

Returns:

  • (Array[String])


35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/forest_admin_agent/auth/oauth2/forest_provider.rb', line 35

def check_response
  response = yield
  case response.status
  when 200
    server_error = response.body.key?('errors') ? response.body['errors'][0] : nil
    if server_error &&
       server_error['name'] == Utils::ErrorMessages::TWO_FACTOR_AUTHENTICATION_REQUIRED
      raise ForestAdminAgent::Error, Utils::ErrorMessages::TWO_FACTOR_AUTHENTICATION_REQUIRED
    end

    response.body.with_indifferent_access
  when 400
    raise OpenIDConnect::BadRequest.new('API Access Failed', response)
  when 401
    raise OpenIDConnect::Unauthorized.new(Utils::ErrorMessages::AUTHORIZATION_FAILED, response)
  when 403
    error = response.body['errors'].first
    raise OpenIDConnect::Forbidden.new(error['name'], error['detail'])
  when 404
    raise OpenIDConnect::HttpError.new(response.status, Utils::ErrorMessages::SECRET_NOT_FOUND, response)
  when 422
    raise OpenIDConnect::HttpError.new(response.status,
                                       Utils::ErrorMessages::SECRET_AND_RENDERINGID_INCONSISTENT, response)
  else
    raise OpenIDConnect::HttpError.new(response.status, 'Unknown HttpError', response)
  end
end

#create_resource_owner(data) ⇒ ForestResourceOwner

Returns:



31
32
33
# File 'lib/forest_admin_agent/auth/oauth2/forest_provider.rb', line 31

def create_resource_owner(data)
  ForestResourceOwner.new data, rendering_id
end

#get_resource_owner(access_token) ⇒ ForestResourceOwner

Returns:



18
19
20
21
22
23
24
25
26
27
# File 'lib/forest_admin_agent/auth/oauth2/forest_provider.rb', line 18

def get_resource_owner(access_token)
  headers = { 'forest-token': access_token.access_token, 'forest-secret-key': secret }
  hash = check_response do
    OpenIDConnect.http_client.get access_token.client.userinfo_uri, {}, headers
  end

  response = OpenIDConnect::ResponseObject::UserInfo.new hash

  create_resource_owner response.raw_attributes[:data]
end