Class: OneLogin::Api::Client

Inherits:
Object
  • Object
show all
Includes:
HTTParty, Util
Defined in:
lib/onelogin/api/client.rb

Overview

Constant Summary collapse

NOKOGIRI_OPTIONS =
Nokogiri::XML::ParseOptions::STRICT |
Nokogiri::XML::ParseOptions::NONET
DEFAULT_USER_AGENT =
"onelogin-ruby-sdk v#{OneLogin::VERSION}".freeze

Constants included from Util::Constants

Util::Constants::ACTIVATE_FACTOR_URL, Util::Constants::ADD_ROLE_TO_USER_URL, Util::Constants::CREATE_EVENT_URL, Util::Constants::CREATE_GROUP_URL, Util::Constants::CREATE_ROLE_URL, Util::Constants::CREATE_USER_URL, Util::Constants::DELETE_ROLE_TO_USER_URL, Util::Constants::DELETE_USER_URL, Util::Constants::EMBED_APP_URL, Util::Constants::ENROLL_FACTOR_URL, Util::Constants::GENERATE_INVITE_LINK_URL, Util::Constants::GET_APPS_FOR_USER_URL, Util::Constants::GET_CUSTOM_ATTRIBUTES_URL, Util::Constants::GET_ENROLLED_FACTORS_URL, Util::Constants::GET_EVENTS_URL, Util::Constants::GET_EVENT_TYPES_URL, Util::Constants::GET_EVENT_URL, Util::Constants::GET_FACTORS_URL, Util::Constants::GET_GROUPS_URL, Util::Constants::GET_GROUP_URL, Util::Constants::GET_RATE_URL, Util::Constants::GET_ROLES_FOR_USER_URL, Util::Constants::GET_ROLES_URL, Util::Constants::GET_ROLE_URL, Util::Constants::GET_SAML_ASSERTION_URL, Util::Constants::GET_SAML_VERIFY_FACTOR, Util::Constants::GET_TOKEN_VERIFY_FACTOR, Util::Constants::GET_USERS_URL, Util::Constants::GET_USER_URL, Util::Constants::LOCK_USER_URL, Util::Constants::LOG_USER_OUT_URL, Util::Constants::REMOVE_FACTOR_URL, Util::Constants::SEND_INVITE_LINK_URL, Util::Constants::SESSION_LOGIN_TOKEN_URL, Util::Constants::SET_CUSTOM_ATTRIBUTE_TO_USER_URL, Util::Constants::SET_PW_CLEARTEXT, Util::Constants::SET_PW_SALT, Util::Constants::SET_USER_STATE_URL, Util::Constants::TOKEN_REFRESH_URL, Util::Constants::TOKEN_REQUEST_URL, Util::Constants::TOKEN_REVOKE_URL, Util::Constants::UPDATE_USER_URL, Util::Constants::VERIFY_FACTOR_URL

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Util::UrlBuilder

#url_for

Constructor Details

#initialize(config) ⇒ Client

Create a new instance of the Client.



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/onelogin/api/client.rb', line 33

def initialize(config)
  options = Hash[config.map { |(k, v)| [k.to_sym, v] }]

  @client_id = options[:client_id]
  @client_secret = options[:client_secret]
  @region = options[:region] || 'us'
  @max_results = options[:max_results] || 1000

  if options[:proxy_host]
    self.class.http_proxy options[:proxy_host], options[:proxy_port], options[:proxy_user], options[:proxy_pass]
  end

  validate_config

  @user_agent = DEFAULT_USER_AGENT
end

Instance Attribute Details

#client_idObject

Returns the value of attribute client_id.



21
22
23
# File 'lib/onelogin/api/client.rb', line 21

def client_id
  @client_id
end

#client_secretObject

Returns the value of attribute client_secret.



21
22
23
# File 'lib/onelogin/api/client.rb', line 21

def client_secret
  @client_secret
end

#errorObject

Returns the value of attribute error.



22
23
24
# File 'lib/onelogin/api/client.rb', line 22

def error
  @error
end

#error_attributeObject

Returns the value of attribute error_attribute.



22
23
24
# File 'lib/onelogin/api/client.rb', line 22

def error_attribute
  @error_attribute
end

#error_descriptionObject

Returns the value of attribute error_description.



22
23
24
# File 'lib/onelogin/api/client.rb', line 22

def error_description
  @error_description
end

#regionObject

Returns the value of attribute region.



21
22
23
# File 'lib/onelogin/api/client.rb', line 21

def region
  @region
end

#user_agentObject

Returns the value of attribute user_agent.



22
23
24
# File 'lib/onelogin/api/client.rb', line 22

def user_agent
  @user_agent
end

Instance Method Details

#access_tokenOneLoginToken

Generates an access token and refresh token that you may use to call Onelogin’s API methods.



187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/onelogin/api/client.rb', line 187

def access_token
  clean_error

  begin
    url = url_for(TOKEN_REQUEST_URL)

    data = {
      'grant_type' => 'client_credentials'
    }

    response = self.class.post(
      url,
      headers: authorized_headers(false),
      body: data.to_json
    )

    if response.code == 200
      json_data = JSON.parse(response.body)
      token = OneLogin::Api::Models::OneLoginToken.new(json_data)
      @access_token = token.access_token
      @refresh_token = token.refresh_token
      @expiration = token.created_at + token.expires_in
      return token
    else
      @error = response.code.to_s
      @error_description = extract_error_message_from_response(response)
    end
  rescue Exception => e
    @error = '500'
    @error_description = e.message
  end

  nil
end

#activate_factor(user_id, device_id) ⇒ FactorEnrollmentResponse

Triggers an SMS or Push notification containing a One-Time Password (OTP) that can be used to authenticate a user with the Verify Factor call.



1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
# File 'lib/onelogin/api/client.rb', line 1579

def activate_factor(user_id, device_id)
  clean_error
  prepare_token

  begin
    url = url_for(ACTIVATE_FACTOR_URL, user_id, device_id)

    response = self.class.post(
      url,
      headers: authorized_headers
    )

    if response.code == 200
      json_data = JSON.parse(response.body)
      if json_data && json_data['data']
        return OneLogin::Api::Models::FactorEnrollmentResponse.new(json_data['data'][0])
      end
    else
      @error = response.code.to_s
      @error_description = extract_error_message_from_response(response)
    end
  rescue Exception => e
    @error = '500'
    @error_description = e.message
  end

  nil
end

#assign_role_to_user(user_id, role_ids) ⇒ Boolean

Assigns Roles to User



603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
# File 'lib/onelogin/api/client.rb', line 603

def assign_role_to_user(user_id, role_ids)
  clean_error
  prepare_token

  begin
    url = url_for(ADD_ROLE_TO_USER_URL, user_id)

    data = {
      'role_id_array' => role_ids
    }

    response = self.class.put(
      url,
      headers: authorized_headers,
      body: data.to_json
    )

    if response.code == 200
      return handle_operation_response(response)
    else
      @error = response.code.to_s
      @error_description = extract_error_message_from_response(response)
      @error_attribute = extract_error_attribute_from_response(response)
    end
  rescue Exception => e
    @error = '500'
    @error_description = e.message
  end

  false
end

#authorized_headers(bearer = true) ⇒ Object



165
166
167
168
169
170
171
172
173
174
175
# File 'lib/onelogin/api/client.rb', line 165

def authorized_headers(bearer = true)
  authorization = if bearer
    "bearer:#{@access_token}"
  else
    "client_id:#{@client_id},client_secret:#{@client_secret}"
  end

  headers.merge({
    'Authorization' => authorization
  })
end

#clean_errorObject

Clean any previous error registered at the client.



56
57
58
59
60
# File 'lib/onelogin/api/client.rb', line 56

def clean_error
  @error = nil
  @error_description = nil
  @error_attribute = nil
end

#create_event(event_params) ⇒ Boolean

Create an event in the OneLogin event log.



1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
# File 'lib/onelogin/api/client.rb', line 1237

def create_event(event_params)
  clean_error
  prepare_token

  begin
    url = url_for(CREATE_EVENT_URL)

    response = self.class.post(
      url,
      headers: authorized_headers,
      body: event_params.to_json
    )

    if response.code == 200
      return handle_operation_response(response)
    else
      @error = response.code.to_s
      @error_description = extract_error_message_from_response(response)
      @error_attribute = extract_error_attribute_from_response(response)
    end
  rescue Exception => e
    @error = '500'
    @error_description = e.message
  end

  false
end

#create_session_login_token(query_params, allowed_origin = '') ⇒ SessionTokenInfo|SessionTokenMFAInfo

Generates a session login token in scenarios in which MFA may or may not be required. A session login token expires two minutes after creation.



970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
# File 'lib/onelogin/api/client.rb', line 970

def (query_params, allowed_origin='')
  clean_error
  prepare_token

  begin
    url = url_for()

    if query_params.nil? || !query_params.has_key?('username_or_email') || !query_params.has_key?('password') || !query_params.has_key?('subdomain')
      raise "username_or_email, password and subdomain are required parameters"
    end

    headers = authorized_headers
    if allowed_origin
      headers = headers.merge({ 'Custom-Allowed-Origin-Header-1' => allowed_origin })
    end

    response = self.class.post(
      url,
      headers: headers,
      body: query_params.to_json
    )

    if response.code == 200
      return handle_session_token_response(response)
    else
      @error = response.code.to_s
      @error_description = extract_error_message_from_response(response)
    end
  rescue Exception => e
    @error = '500'
    @error_description = e.message
  end

  nil
end

#create_user(user_params) ⇒ User

Creates an user



520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
# File 'lib/onelogin/api/client.rb', line 520

def create_user(user_params)
  clean_error
  prepare_token

  begin
    url = url_for(CREATE_USER_URL)

    response = self.class.post(
      url,
      headers: authorized_headers,
      body: user_params.to_json
    )

    if response.code == 200
      json_data = JSON.parse(response.body)
      if json_data && json_data['data']
        return OneLogin::Api::Models::User.new(json_data['data'][0])
      end
    else
      @error = response.code.to_s
      @error_description = extract_error_message_from_response(response)
      @error_attribute = extract_error_attribute_from_response(response)
    end
  rescue Exception => e
    @error = '500'
    @error_description = e.message
  end

  nil
end

#delete_user(user_id) ⇒ Boolean

Deletes an user



931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
# File 'lib/onelogin/api/client.rb', line 931

def delete_user(user_id)
  clean_error
  prepare_token

  begin
    url = url_for(DELETE_USER_URL, user_id)

    response = self.class.delete(
      url,
      headers: authorized_headers
    )

    if response.code == 200
      return handle_operation_response(response)
    else
      @error = response.code.to_s
      @error_description = extract_error_message_from_response(response)
      @error_attribute = extract_error_attribute_from_response(response)
    end
  rescue Exception => e
    @error = '500'
    @error_description = e.message
  end

  false
end

#enroll_factor(user_id, factor_id, display_name, number) ⇒ OTPDevice

Enroll a user with a given authentication factor.



1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
# File 'lib/onelogin/api/client.rb', line 1494

def enroll_factor(user_id, factor_id, display_name, number)
  clean_error
  prepare_token

  begin
    url = url_for(ENROLL_FACTOR_URL, user_id)

    data = {
      'factor_id'=> factor_id.to_i,
      'display_name'=> display_name,
      'number'=> number
    }

    response = self.class.post(
      url,
      :headers => authorized_headers,
      body: data.to_json
    )

    if response.code == 200
      json_data = JSON.parse(response.body)
      if json_data and json_data['data']
        return OneLogin::Api::Models::OTPDevice.new(json_data['data'][0])
      end
    else
      @error = response.code.to_s
      @error_description = extract_error_message_from_response(response)
    end
  rescue Exception => e
    @error = '500'
    @error_description = e.message
  end

  nil
end

#expired?Boolean



96
97
98
# File 'lib/onelogin/api/client.rb', line 96

def expired?
  Time.now.utc > @expiration
end

#extract_error_attribute_from_response(response) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/onelogin/api/client.rb', line 82

def extract_error_attribute_from_response(response)
  attribute = nil
  content = JSON.parse(response.body)
  if content && content.has_key?('status')
    status = content['status']
    if status.has_key?('message') && status['message'].instance_of?(Hash)
      if status['message'].has_key?('attribute')
        attribute = status['message']['attribute']
      end
    end
  end
  attribute
end

#extract_error_message_from_response(response) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/onelogin/api/client.rb', line 62

def extract_error_message_from_response(response)
  message = ''
  content = JSON.parse(response.body)
  if content && content.has_key?('status')
    status = content['status']
    if status.has_key?('message')
      if status['message'].instance_of?(Hash)
        if status['message'].has_key?('description')
          message = status['message']['description']
        end
      else
        message = status['message']
      end
    elsif status.has_key?('type')
      message = status['type']
    end
  end
  message
end

Generates an invite link for a user that you have already created in your OneLogin account.



1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
# File 'lib/onelogin/api/client.rb', line 1709

def generate_invite_link(email)
  clean_error
  prepare_token

  begin
    url = url_for(GENERATE_INVITE_LINK_URL)

    data = {
      'email'=> email
    }

    response = self.class.post(
      url,
      headers: authorized_headers,
      body: data.to_json
    )

    if response.code == 200
      json_data = JSON.parse(response.body)
      if json_data && json_data['data']
        return json_data['data'][0]
      end
    else
      @error = response.code.to_s
      @error_description = extract_error_message_from_response(response)
    end
  rescue Exception => e
    @error = '500'
    @error_description = e.message
  end

  nil
end

#get_custom_attributesArray

Gets a list of all custom attribute fields (also known as custom user fields) that have been defined for OL account.



476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
# File 'lib/onelogin/api/client.rb', line 476

def get_custom_attributes
  clean_error
  prepare_token

  begin
    url = url_for(GET_CUSTOM_ATTRIBUTES_URL)

    response = self.class.get(
      url,
      headers: authorized_headers
    )

    custom_attributes = []
    if response.code == 200
      json_data = JSON.parse(response.body)
      if json_data && json_data['data']
        custom_attributes = json_data['data'][0]
      end
    else
      @error = response.code.to_s
      @error_description = extract_error_message_from_response(response)
    end

    return custom_attributes
  rescue Exception => e
    @error = '500'
    @error_description = e.message
  end

  nil
end

#get_embed_apps(token, email) ⇒ Array

Lists apps accessible by a OneLogin user.



1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
# File 'lib/onelogin/api/client.rb', line 1796

def get_embed_apps(token, email)
  clean_error

  begin
    response = self.class.get(
      EMBED_APP_URL,
      headers: {
        'User-Agent' => @user_agent
      },
      query: {
        token: token,
        email: email
      }
    )

    if response.code == 200 && !(response.body.nil? || response.body.empty?)
      return retrieve_apps_from_xml(response.body)
    else
      @error = response.code.to_s
      unless response.body.nil? || response.body.empty?
        @error_description = response.body
      end
    end
  rescue Exception => e
    @error = '500'
    @error_description = e.message
  end

  nil
end

#get_enrolled_factors(user_id) ⇒ Array

Return a list of authentication factors registered to a particular user for multifactor authentication (MFA)



1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
# File 'lib/onelogin/api/client.rb', line 1537

def get_enrolled_factors(user_id)
  clean_error
  prepare_token

  begin
    url = url_for(GET_ENROLLED_FACTORS_URL, user_id)

    response = self.class.get(
      url,
      :headers => authorized_headers
    )

    otp_devices = []
    if response.code == 200
      json_data = JSON.parse(response.body)
      if json_data and json_data['data'] and json_data['data']['otp_devices']
        json_data['data']['otp_devices'].each do |otp_device_data|
          otp_devices << OneLogin::Api::Models::OTPDevice.new(otp_device_data)
        end
      end
    else
      @error = response.code.to_s
      @error_description = extract_error_message_from_response(response)
    end
    return otp_devices
  rescue Exception => e
    @error = '500'
    @error_description = e.message
  end

  nil
end

#get_event(event_id) ⇒ Event

Gets Event by ID.



1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
# File 'lib/onelogin/api/client.rb', line 1195

def get_event(event_id)
  clean_error
  prepare_token

  begin
    url = url_for(GET_EVENT_URL, event_id)

    response = self.class.get(
      url,
      headers: authorized_headers
    )

    if response.code == 200
      json_data = JSON.parse(response.body)
      if json_data && json_data['data']
        return OneLogin::Api::Models::Event.new(json_data['data'][0])
      end
    else
      @error = response.code.to_s
      @error_description = extract_error_message_from_response(response)
    end
  rescue Exception => e
    @error = '500'
    @error_description = e.message
  end

  nil
end

#get_event_typesArray

List of all OneLogin event types available to the Events API.



1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
# File 'lib/onelogin/api/client.rb', line 1138

def get_event_types
  clean_error
  prepare_token

  begin
  options = {
    model: OneLogin::Api::Models::EventType,
    headers: authorized_headers,
    max_results: @max_results
  }

  return Cursor.new(self.class, url_for(GET_EVENT_TYPES_URL), options)

  rescue Exception => e
    @error = '500'
    @error_description = e.message
  end

  nil
end

#get_events(params = {}) ⇒ Array

Gets a list of Event resources. (if no limit provided, by default get 50 elements)



1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
# File 'lib/onelogin/api/client.rb', line 1166

def get_events(params={})
  clean_error
  prepare_token

  begin
  options = {
    model: OneLogin::Api::Models::Event,
    headers: authorized_headers,
    max_results: @max_results,
    params: params
  }

  return Cursor.new(self.class, url_for(GET_EVENTS_URL), options)

  rescue Exception => e
    @error = '500'
    @error_description = e.message
  end

  nil
end

#get_factors(user_id) ⇒ Array

Returns a list of authentication factors that are available for user enrollment via API.



1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
# File 'lib/onelogin/api/client.rb', line 1451

def get_factors(user_id)
  clean_error
  prepare_token

  begin
    url = url_for(GET_FACTORS_URL, user_id)

    response = self.class.get(
      url,
      :headers => authorized_headers
    )

    factors = []
    if response.code == 200
      json_data = JSON.parse(response.body)
      if json_data and json_data['data'] and json_data['data']['auth_factors']
        json_data['data']['auth_factors'].each do |factor_data|
          factors << OneLogin::Api::Models::AuthFactor.new(factor_data)
        end
      end
    else
      @error = response.code.to_s
      @error_description = extract_error_message_from_response(response)
    end
    return factors
  rescue Exception => e
    @error = '500'
    @error_description = e.message
  end

  nil
end

#get_group(group_id) ⇒ Group

Gets Group by ID.



1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
# File 'lib/onelogin/api/client.rb', line 1303

def get_group(group_id)
  clean_error
  prepare_token

  begin
    url = url_for(GET_GROUP_URL, group_id)

    response = self.class.get(
      url,
      headers: authorized_headers
    )

    if response.code == 200
      json_data = JSON.parse(response.body)
      if json_data && json_data['data']
        return OneLogin::Api::Models::Group.new(json_data['data'][0])
      end
    else
      @error = response.code.to_s
      @error_description = extract_error_message_from_response(response)
    end
  rescue Exception => e
    @error = '500'
    @error_description = e.message
  end

  nil
end

#get_groups(params = {}) ⇒ Array

Gets a list of Group resources (element of groups limited with the limit parameter).



1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
# File 'lib/onelogin/api/client.rb', line 1274

def get_groups(params = {})
  clean_error
  prepare_token

  begin
    options = {
      model: OneLogin::Api::Models::Group,
      headers: authorized_headers,
      max_results: @max_results,
      params: params
    }

    return Cursor.new(self.class, url_for(GET_GROUPS_URL), options)

  rescue Exception => e
    @error = '500'
    @error_description = e.message
  end

  nil
end

#get_rate_limitsRateLimit

Gets current rate limit details about an access token.



307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
# File 'lib/onelogin/api/client.rb', line 307

def get_rate_limits
  clean_error
  prepare_token

  begin
    url = url_for(GET_RATE_URL)

    response = self.class.get(
      url,
      headers: authorized_headers
    )

    if response.code == 200
      json_data = JSON.parse(response.body)
      if json_data && json_data['data']
        return OneLogin::Api::Models::RateLimit.new(json_data['data'])
      end
    else
      @error = response.code.to_s
      @error_description = extract_error_message_from_response(response)
    end
  rescue Exception => e
    @error = '500'
    @error_description = e.message
  end

  nil
end

#get_role(role_id) ⇒ Role

Gets Role by ID.



1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
# File 'lib/onelogin/api/client.rb', line 1100

def get_role(role_id)
  clean_error
  prepare_token

  begin
    url = url_for(GET_ROLE_URL, role_id)

    response = self.class.get(
      url,
      headers: authorized_headers
    )

    if response.code == 200
      json_data = JSON.parse(response.body)
      if json_data && json_data['data']
        return OneLogin::Api::Models::Role.new(json_data['data'][0])
      end
    else
      @error = response.code.to_s
      @error_description = extract_error_message_from_response(response)
    end
  rescue Exception => e
    @error = '500'
    @error_description = e.message
  end

  nil
end

#get_roles(params = {}) ⇒ Array

Gets a list of Role resources. (if no limit provided, by default get 50 elements)



1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
# File 'lib/onelogin/api/client.rb', line 1071

def get_roles(params = {})
  clean_error
  prepare_token

  begin
    options = {
      model: OneLogin::Api::Models::Role,
      headers: authorized_headers,
      max_results: @max_results,
      params: params
    }

    return Cursor.new(self.class, url_for(GET_ROLES_URL), options)

  rescue Exception => e
    @error = '500'
    @error_description = e.message
  end

  nil
end

#get_saml_assertion(username_or_email, password, app_id, subdomain, ip_address = nil) ⇒ SAMLEndpointResponse

Generates a SAML Assertion.



1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
# File 'lib/onelogin/api/client.rb', line 1347

def get_saml_assertion(username_or_email, password, app_id, subdomain, ip_address=nil)
  clean_error
  prepare_token

  begin
    url = url_for(GET_SAML_ASSERTION_URL)

    data = {
      'username_or_email'=> username_or_email,
      'password'=> password,
      'app_id'=> app_id,
      'subdomain'=> subdomain,
    }

    unless ip_address.nil? || ip_address.empty?
      data['ip_address'] = ip_address
    end

    response = self.class.post(
      url,
      headers: authorized_headers,
      body: data.to_json
    )

    if response.code == 200
      return handle_saml_endpoint_response(response)
    else
      @error = response.code.to_s
      @error_description = extract_error_message_from_response(response)
    end
  rescue Exception => e
    @error = '500'
    @error_description = e.message
  end

  nil
end

#get_saml_assertion_verifying(app_id, device_id, state_token, otp_token = nil, url_endpoint = nil, do_not_notify = false) ⇒ SAMLEndpointResponse

Verify a one-time password (OTP) value provided for a second factor when multi-factor authentication (MFA) is required for SAML authentication.



1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
# File 'lib/onelogin/api/client.rb', line 1397

def get_saml_assertion_verifying(app_id, device_id, state_token, otp_token=nil, url_endpoint=nil, do_not_notify=false)
  clean_error
  prepare_token

  begin

    if url_endpoint.nil? || url_endpoint.empty?
      url = url_for(GET_SAML_VERIFY_FACTOR)
    else
      url = url_endpoint
    end

    data = {
      'app_id'=> app_id,
      'device_id'=> device_id.to_s,
      'state_token'=> state_token,
      'do_not_notify'=> do_not_notify
    }

    unless otp_token.nil? || otp_token.empty?
      data['otp_token'] = otp_token
    end

    response = self.class.post(
      url,
      headers: authorized_headers,
      body: data.to_json
    )

    if response.code == 200
      return handle_saml_endpoint_response(response)
    else
      @error = response.code.to_s
      @error_description = extract_error_message_from_response(response)
    end
  rescue Exception => e
    @error = '500'
    @error_description = e.message
  end

  nil
end

#get_session_token_verified(device_id, state_token, otp_token = nil, allowed_origin = '', do_not_notify = false) ⇒ SessionTokenInfo

Verify a one-time password (OTP) value provided for multi-factor authentication (MFA).



1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
# File 'lib/onelogin/api/client.rb', line 1017

def get_session_token_verified(device_id, state_token, otp_token=nil, allowed_origin='', do_not_notify=false)
  clean_error
  prepare_token

  begin
    url = url_for(GET_TOKEN_VERIFY_FACTOR)

    data = {
      'device_id'=> device_id.to_s,
      'state_token'=> state_token,
      'do_not_notify'=> do_not_notify
    }

    unless otp_token.nil? || otp_token.empty?
      data['otp_token'] = otp_token
    end

    headers = authorized_headers
    if allowed_origin
      headers = headers.merge({ 'Custom-Allowed-Origin-Header-1' => allowed_origin })
    end

    response = self.class.post(
      url,
      headers: headers,
      body: data.to_json
    )

    if response.code == 200
      return handle_session_token_response(response)
    else
      @error = response.code.to_s
      @error_description = extract_error_message_from_response(response)
    end
  rescue Exception => e
    @error = '500'
    @error_description = e.message
  end

  nil
end

#get_user(user_id) ⇒ User

Gets User by ID.



376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
# File 'lib/onelogin/api/client.rb', line 376

def get_user(user_id)
  clean_error
  prepare_token

  begin

    url = url_for(GET_USER_URL, user_id)

    response = self.class.get(
      url,
      headers: authorized_headers
    )

    if response.code == 200
      json_data = JSON.parse(response.body)
      if json_data && json_data['data']
        return OneLogin::Api::Models::User.new(json_data['data'][0])
      end
    else
      @error = response.code.to_s
      @error_description = extract_error_message_from_response(response)
    end
  rescue Exception => e
    @error = '500'
    @error_description = e.message
  end

  nil
end

#get_user_apps(user_id) ⇒ Array

Gets a list of apps accessible by a user, not including personal apps.



413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
# File 'lib/onelogin/api/client.rb', line 413

def get_user_apps(user_id)
  clean_error
  prepare_token

  begin
    options = {
      model: OneLogin::Api::Models::App,
      headers: authorized_headers,
      max_results: @max_results
    }

    return Cursor.new(self.class, url_for(GET_APPS_FOR_USER_URL, user_id), options)

  rescue Exception => e
    @error = '500'
    @error_description = e.message
  end

  nil
end

#get_user_roles(user_id) ⇒ Array

Gets a list of role IDs that have been assigned to a user.



441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
# File 'lib/onelogin/api/client.rb', line 441

def get_user_roles(user_id)
  clean_error
  prepare_token

  begin
    url = url_for(GET_ROLES_FOR_USER_URL, user_id)

    response = self.class.get(
      url,
      headers: authorized_headers
    )

    role_ids = []
    if response.code == 200
      json_data = JSON.parse(response.body)
      role_ids = json_data['data'][0] if json_data && json_data['data']
    else
      @error = response.code.to_s
      @error_description = extract_error_message_from_response(response)
    end

    return role_ids
  rescue Exception => e
    @error = '500'
    @error_description = e.message
  end

  nil
end

#get_users(params = {}) ⇒ Array

Gets a list of User resources. (if no limit provided, by default gt 50 elements)



347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
# File 'lib/onelogin/api/client.rb', line 347

def get_users(params = {})
  clean_error
  prepare_token

  begin
    options = {
      model: OneLogin::Api::Models::User,
      headers: authorized_headers,
      max_results: @max_results,
      params: params
    }

    return Cursor.new(self.class, url_for(GET_USERS_URL), options)

  rescue Exception => e
    @error = '500'
    @error_description = e.message
  end

  nil
end

#handle_operation_response(response) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/onelogin/api/client.rb', line 108

def handle_operation_response(response)
  result = false
  begin
    content = JSON.parse(response.body)
    if content && content.has_key?('status') && content['status'].has_key?('type') && content['status']['type'] == "success"
      result = true
    end
  rescue Exception => e
    result = false
  end

  result
end

#handle_saml_endpoint_response(response) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/onelogin/api/client.rb', line 137

def handle_saml_endpoint_response(response)
  content = JSON.parse(response.body)
  if content && content.has_key?('status') && content['status'].has_key?('message') && content['status'].has_key?('type')
    status_type = content['status']['type']
    status_message = content['status']['message']
    saml_endpoint_response = OneLogin::Api::Models::SAMLEndpointResponse.new(status_type, status_message)
    if content.has_key?('data')
      if status_message == 'Success'
        saml_endpoint_response.saml_response = content['data']
      else
        mfa = OneLogin::Api::Models::MFA.new(content['data'][0])
        saml_endpoint_response.mfa = mfa
      end
    end

    return saml_endpoint_response
  end

  nil
end

#handle_session_token_response(response) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/onelogin/api/client.rb', line 122

def handle_session_token_response(response)
  content = JSON.parse(response.body)
  if content && content.has_key?('status') && content['status'].has_key?('message') && content.has_key?('data')
    if content['status']['message'] == "Success"
      return OneLogin::Api::Models::SessionTokenInfo.new(content['data'][0])
    elsif content['status']['message'] == "MFA is required for this user"
      return OneLogin::Api::Models::SessionTokenMFAInfo.new(content['data'][0])
    else
      raise "Status Message type not reognized: %s" % content['status']['message']
    end
  end

  nil
end

#headersObject



158
159
160
161
162
163
# File 'lib/onelogin/api/client.rb', line 158

def headers
  {
    'Content-Type' => 'application/json',
    'User-Agent' => @user_agent
  }
end

#lock_user(user_id, minutes) ⇒ Boolean

Use this call to lock a user’s account based on the policy assigned to the user, for a specific time you define in the request, or until you unlock it.



892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
# File 'lib/onelogin/api/client.rb', line 892

def lock_user(user_id, minutes)
  clean_error
  prepare_token

  begin
    url = url_for(LOCK_USER_URL, user_id)

    data = {
      'locked_until' => minutes
    }

    response = self.class.put(
      url,
      headers: authorized_headers,
      body: data.to_json
    )

    if response.code == 200
      return handle_operation_response(response)
    else
      @error = response.code.to_s
      @error_description = extract_error_message_from_response(response)
      @error_attribute = extract_error_attribute_from_response(response)
    end
  rescue Exception => e
    @error = '500'
    @error_description = e.message
  end

  false
end

#log_user_out(user_id) ⇒ Boolean

Log a user out of any and all sessions.



855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
# File 'lib/onelogin/api/client.rb', line 855

def log_user_out(user_id)
  clean_error
  prepare_token

  begin
    url = url_for(LOG_USER_OUT_URL, user_id)

    response = self.class.put(
      url,
      headers: authorized_headers
    )

    if response.code == 200
      return handle_operation_response(response)
    else
      @error = response.code.to_s
      @error_description = extract_error_message_from_response(response)
      @error_attribute = extract_error_attribute_from_response(response)
    end
  rescue Exception => e
    @error = '500'
    @error_description = e.message
  end

  false
end

#prepare_tokenObject



100
101
102
103
104
105
106
# File 'lib/onelogin/api/client.rb', line 100

def prepare_token
  if @access_token.nil?
    access_token
  elsif expired?
    regenerate_token
  end
end

#regenerate_tokenOneLoginToken

Refreshing tokens provides a new set of access and refresh tokens.



227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
# File 'lib/onelogin/api/client.rb', line 227

def regenerate_token
  clean_error

  begin
    url = url_for(TOKEN_REQUEST_URL)

    data = {
      'grant_type' => 'refresh_token',
      'access_token' => @access_token,
      'refresh_token' => @refresh_token
    }

    response = self.class.post(
      url,
      headers: headers,
      body: data.to_json
    )

    if response.code == 200
      json_data = JSON.parse(response.body)
      token = OneLogin::Api::Models::OneLoginToken.new(json_data)
      @access_token = token.access_token
      @refresh_token = token.refresh_token
      @expiration = token.created_at + token.expires_in
      return token
    else
      @error = response.code.to_s
      @error_description = extract_error_message_from_response(response)
    end
  rescue Exception => e
    @error = '500'
    @error_description = e.message
  end

  nil
end

#remove_factor(user_id, device_id) ⇒ Boolean

Remove an enrolled factor from a user.



1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
# File 'lib/onelogin/api/client.rb', line 1671

def remove_factor(user_id, device_id)
  clean_error
  prepare_token

  begin
    url = url_for(REMOVE_FACTOR_URL, user_id, device_id)

    response = self.class.delete(
      url,
      :headers => authorized_headers
    )

    if response.code == 200
      return true
    else
      @error = response.code.to_s
      @error_description = extract_error_message_from_response(response)
      return false
    end
  rescue Exception => e
    @error = '500'
    @error_description = e.message
  end

  nil
end

#remove_role_from_user(user_id, role_ids) ⇒ Boolean

Removes Role from User



643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
# File 'lib/onelogin/api/client.rb', line 643

def remove_role_from_user(user_id, role_ids)
  clean_error
  prepare_token

  begin
    url = url_for(DELETE_ROLE_TO_USER_URL, user_id)

    data = {
      'role_id_array' => role_ids
    }

    response = self.class.put(
      url,
      headers: authorized_headers,
      body: data.to_json
    )

    if response.code == 200
      return handle_operation_response(response)
    else
      @error = response.code.to_s
      @error_description = extract_error_message_from_response(response)
      @error_attribute = extract_error_attribute_from_response(response)
    end
  rescue Exception => e
    @error = '500'
    @error_description = e.message
  end

  false
end

#retrieve_apps_from_xml(xml_content) ⇒ Object



1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
# File 'lib/onelogin/api/client.rb', line 1827

def retrieve_apps_from_xml(xml_content)
  doc = Nokogiri::XML(xml_content) do |config|
    config.options = NOKOGIRI_OPTIONS
  end

  node_list = doc.xpath("/apps/app")
  attributes = ['id', 'icon', 'name', 'provisioned', 'extension_required', 'personal', 'login_id']
  apps = []
  node_list.each do |node|
    app_data = {}
    node.children.each do |children|
      if attributes.include? children.name
        app_data[children.name] = children.content
      end
    end
    apps << OneLogin::Api::Models::EmbedApp.new(app_data)
  end

  apps
end

#revoke_tokenBoolean

Revokes an access token and refresh token pair.



269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
# File 'lib/onelogin/api/client.rb', line 269

def revoke_token
  clean_error

  begin
    url = url_for(TOKEN_REVOKE_URL)

    data = {
      access_token: @access_token
    }

    response = self.class.post(
      url,
      headers: authorized_headers(false),
      body: data.to_json
    )

    if response.code == 200
      @access_token = nil
      @refresh_token = nil
      @expiration = nil
      return true
    else
      @error = response.code.to_s
      @error_description = extract_error_message_from_response(response)
    end
  rescue Exception => e
    @error = '500'
    @error_description = e.message
  end

  false
end

Sends an invite link to a user that you have already created in your OneLogin account.



1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
# File 'lib/onelogin/api/client.rb', line 1753

def send_invite_link(email, personal_email=nil)
  clean_error
  prepare_token

  begin
    url = url_for(SEND_INVITE_LINK_URL)

    data = {
      'email'=> email
    }

    unless personal_email.nil? || personal_email.empty?
      data['personal_email'] = personal_email
    end

    response = self.class.post(
      url,
      headers: authorized_headers,
      body: data.to_json
    )

    if response.code == 200
      return handle_operation_response(response)
    else
      @error = response.code.to_s
      @error_description = extract_error_message_from_response(response)
    end
  rescue Exception => e
    @error = '500'
    @error_description = e.message
  end

  false
end

#set_custom_attribute_to_user(user_id, custom_attributes) ⇒ Boolean

Set Custom Attribute Value



816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
# File 'lib/onelogin/api/client.rb', line 816

def set_custom_attribute_to_user(user_id, custom_attributes)
  clean_error
  prepare_token

  begin
    url = url_for(SET_CUSTOM_ATTRIBUTE_TO_USER_URL, user_id)

    data = {
      'custom_attributes' => custom_attributes
    }

    response = self.class.put(
      url,
      headers: authorized_headers,
      body: data.to_json
    )

    if response.code == 200
      return handle_operation_response(response)
    else
      @error = response.code.to_s
      @error_description = extract_error_message_from_response(response)
      @error_attribute = extract_error_attribute_from_response(response)
    end
  rescue Exception => e
    @error = '500'
    @error_description = e.message
  end

  false
end

#set_password_using_clear_text(user_id, password, password_confirmation, validate_policy = false) ⇒ Boolean

Sets Password by ID Using Cleartext



685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
# File 'lib/onelogin/api/client.rb', line 685

def set_password_using_clear_text(user_id, password, password_confirmation, validate_policy=false)
  clean_error
  prepare_token

  begin
    url = url_for(SET_PW_CLEARTEXT, user_id)

    data = {
      'password' => password,
      'password_confirmation' => password_confirmation,
      'validate_policy' => validate_policy
    }

    response = self.class.put(
      url,
      headers: authorized_headers,
      body: data.to_json
    )

    if response.code == 200
      return handle_operation_response(response)
    else
      @error = response.code.to_s
      @error_description = extract_error_message_from_response(response)
      @error_attribute = extract_error_attribute_from_response(response)
    end
  rescue Exception => e
    @error = '500'
    @error_description = e.message
  end

  false
end

#set_password_using_hash_salt(user_id, password, password_confirmation, password_algorithm, password_salt = nil) ⇒ Boolean

Set Password by ID Using Salt and SHA-256



730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
# File 'lib/onelogin/api/client.rb', line 730

def set_password_using_hash_salt(user_id, password, password_confirmation, password_algorithm, password_salt=nil)
  clean_error
  prepare_token

  begin
    url = url_for(SET_PW_SALT, user_id)

    data = {
      'password' => password,
      'password_confirmation' => password_confirmation,
      'password_algorithm' => password_algorithm
    }

    unless password_salt.nil?
      data['password_salt'] = password_salt
    end

    response = self.class.put(
      url,
      headers: authorized_headers,
      body: data.to_json
    )

    if response.code == 200
      return handle_operation_response(response)
    else
      @error = response.code.to_s
      @error_description = extract_error_message_from_response(response)
      @error_attribute = extract_error_attribute_from_response(response)
    end
  rescue Exception => e
    @error = '500'
    @error_description = e.message
  end

  false
end

#set_state_to_user(user_id, state) ⇒ Boolean

Set User State



776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
# File 'lib/onelogin/api/client.rb', line 776

def set_state_to_user(user_id, state)
  clean_error
  prepare_token

  begin
    url = url_for(SET_USER_STATE_URL, user_id)

    data = {
      'state' => state
    }

    response = self.class.put(
      url,
      headers: authorized_headers,
      body: data.to_json
    )

    if response.code == 200
      return handle_operation_response(response)
    else
      @error = response.code.to_s
      @error_description = extract_error_message_from_response(response)
      @error_attribute = extract_error_attribute_from_response(response)
    end
  rescue Exception => e
    @error = '500'
    @error_description = e.message
  end

  false
end

#update_user(user_id, user_params) ⇒ User

Updates an user



564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
# File 'lib/onelogin/api/client.rb', line 564

def update_user(user_id, user_params)
  clean_error
  prepare_token

  begin
    url = url_for(UPDATE_USER_URL, user_id)

    response = self.class.put(
      url,
      headers: authorized_headers,
      body: user_params.to_json
    )

    if response.code == 200
      json_data = JSON.parse(response.body)
      if json_data && json_data['data']
        return OneLogin::Api::Models::User.new(json_data['data'][0])
      end
    else
      @error = response.code.to_s
      @error_description = extract_error_message_from_response(response)
      @error_attribute = extract_error_attribute_from_response(response)
    end
  rescue Exception => e
    @error = '500'
    @error_description = e.message
  end

  nil
end

#validate_configObject

Raises:

  • (ArgumentError)


50
51
52
# File 'lib/onelogin/api/client.rb', line 50

def validate_config
  raise ArgumentError, 'client_id & client_secret are required' unless @client_id && @client_secret
end

#verify_factor(user_id, device_id, otp_token = nil, state_token = nil) ⇒ Boolean

Authenticates a one-time password (OTP) code provided by a multifactor authentication (MFA) device.



1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
# File 'lib/onelogin/api/client.rb', line 1623

def verify_factor(user_id, device_id, otp_token=nil, state_token=nil)
  clean_error
  prepare_token

  begin
    url = url_for(VERIFY_FACTOR_URL, user_id, device_id)

    data = {
      'user_id'=> user_id,
      'device_id'=> device_id
    }

    unless otp_token.nil? || otp_token.empty?
      data['otp_token'] = otp_token
    end

    unless state_token.nil? || state_token.empty?
      data['state_token'] = state_token
    end

    response = self.class.post(
      url,
      headers: authorized_headers,
      body: data.to_json
    )

    if response.code == 200
      return handle_operation_response(response)
    else
      @error = response.code.to_s
      @error_description = extract_error_message_from_response(response)
    end
  rescue Exception => e
    @error = '500'
    @error_description = e.message
  end

  false
end