Class: OneLogin::Api::Client

Inherits:
Object
  • Object
show all
Includes:
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::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::GENERATE_INVITE_LINK_URL, Util::Constants::GET_APPS_FOR_USER_URL, Util::Constants::GET_CUSTOM_ATTRIBUTES_URL, Util::Constants::GET_EVENTS_URL, Util::Constants::GET_EVENT_TYPES_URL, Util::Constants::GET_EVENT_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::SEND_INVITE_LINK_URL, Util::Constants::SESSION_API_TOKEN_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::TOKEN_REFRESH_URL, Util::Constants::TOKEN_REQUEST_URL, Util::Constants::TOKEN_REVOKE_URL, Util::Constants::UPDATE_USER_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.


32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/onelogin/api/client.rb', line 32

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

  validate_config

  @user_agent = DEFAULT_USER_AGENT
end

Instance Attribute Details

#client_idObject

Returns the value of attribute client_id


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

def client_id
  @client_id
end

#client_secretObject

Returns the value of attribute client_secret


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

def client_secret
  @client_secret
end

#errorObject

Returns the value of attribute error


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

def error
  @error
end

#error_descriptionObject

Returns the value of attribute error_description


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

def error_description
  @error_description
end

#regionObject

Returns the value of attribute region


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

def region
  @region
end

#user_agentObject

Returns the value of attribute user_agent


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

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.


159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/onelogin/api/client.rb', line 159

def access_token
  clean_error

  begin
    url = url_for(TOKEN_REQUEST_URL)

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

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

    if response.code == 200
      json_data = JSON.parse(response.body)
      if json_data && json_data['data']
        token = OneLogin::Api::Models::OneLoginToken.new(json_data['data'][0])
        @access_token = token.access_token
        @refresh_token = token.refresh_token
        @expiration = token.created_at + token.expires_in
        return token
      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


576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
# File 'lib/onelogin/api/client.rb', line 576

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 = HTTParty.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)
    end
  rescue Exception => e
    @error = '500'
    @error_description = e.message
  end

  false
end

#authorized_headers(bearer = true) ⇒ Object


137
138
139
140
141
142
143
144
145
146
147
# File 'lib/onelogin/api/client.rb', line 137

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.


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

def clean_error
  @error = nil
  @error_description = nil
end

#create_event(event_params) ⇒ Boolean

Create an event in the OneLogin event log.


1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
# File 'lib/onelogin/api/client.rb', line 1151

def create_event(event_params)
  clean_error
  prepare_token

  begin
    url = url_for(CREATE_EVENT_URL)

    response = HTTParty.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)
    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.


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
923
924
925
926
927
928
# File 'lib/onelogin/api/client.rb', line 895

def (query_params, allowed_origin='')
  clean_error
  prepare_token

  begin
    url = url_for()

    unless allowed_origin.nil? || allowed_origin.empty?
      headers['Custom-Allowed-Origin-Header-1'] = allowed_origin
    end

    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

    response = HTTParty.post(
      url,
      headers: authorized_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


495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
# File 'lib/onelogin/api/client.rb', line 495

def create_user(user_params)
  clean_error
  prepare_token

  begin
    url = url_for(CREATE_USER_URL)

    response = HTTParty.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)
    end
  rescue Exception => e
    @error = '500'
    @error_description = e.message
  end

  nil
end

#delete_user(user_id) ⇒ Boolean

Deletes an user


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

def delete_user(user_id)
  clean_error
  prepare_token

  begin
    url = url_for(DELETE_USER_URL, user_id)

    response = HTTParty.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)
    end
  rescue Exception => e
    @error = '500'
    @error_description = e.message
  end

  false
end

#expired?Boolean


70
71
72
# File 'lib/onelogin/api/client.rb', line 70

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

#extract_error_message_from_response(response) ⇒ Object


56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/onelogin/api/client.rb', line 56

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')
      message = status['message']
    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.


1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
# File 'lib/onelogin/api/client.rb', line 1362

def generate_invite_link(email)
  clean_error
  prepare_token

  begin
    url = url_for(GENERATE_INVITE_LINK_URL)

    data = {
      'email'=> email
    }

    response = HTTParty.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.


451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
# File 'lib/onelogin/api/client.rb', line 451

def get_custom_attributes
  clean_error
  prepare_token

  begin
    url = url_for(GET_CUSTOM_ATTRIBUTES_URL)

    response = HTTParty.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.


1449
1450
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
# File 'lib/onelogin/api/client.rb', line 1449

def get_embed_apps(token, email)
  clean_error

  begin
    response = HTTParty.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_event(event_id) ⇒ Event

Gets Event by ID.


1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
# File 'lib/onelogin/api/client.rb', line 1109

def get_event(event_id)
  clean_error
  prepare_token

  begin
    url = url_for(GET_EVENT_URL, event_id)

    response = HTTParty.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.


1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
# File 'lib/onelogin/api/client.rb', line 1053

def get_event_types
  clean_error
  prepare_token

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

  return Cursor.new(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)


1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
# File 'lib/onelogin/api/client.rb', line 1080

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(url_for(GET_EVENTS_URL), options)

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

  nil
end

#get_group(group_id) ⇒ Group

Gets Group by ID.


1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
# File 'lib/onelogin/api/client.rb', line 1216

def get_group(group_id)
  clean_error
  prepare_token

  begin
    url = url_for(GET_GROUP_URL, group_id)

    response = HTTParty.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).


1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
# File 'lib/onelogin/api/client.rb', line 1187

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(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.


283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
# File 'lib/onelogin/api/client.rb', line 283

def get_rate_limits
  clean_error
  prepare_token

  begin
    url = url_for(GET_RATE_URL)

    response = HTTParty.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.


1015
1016
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
# File 'lib/onelogin/api/client.rb', line 1015

def get_role(role_id)
  clean_error
  prepare_token

  begin
    url = url_for(GET_ROLE_URL, role_id)

    response = HTTParty.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)


986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
# File 'lib/onelogin/api/client.rb', line 986

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(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.


1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
# File 'lib/onelogin/api/client.rb', line 1260

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 = HTTParty.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) ⇒ SAMLEndpointResponse

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


1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
# File 'lib/onelogin/api/client.rb', line 1309

def get_saml_assertion_verifying(app_id, device_id, state_token, otp_token=nil, url_endpoint=nil)
  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
    }

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

    response = HTTParty.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) ⇒ SessionTokenInfo

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


939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
# File 'lib/onelogin/api/client.rb', line 939

def get_session_token_verified(device_id, state_token, otp_token=nil)
  clean_error
  prepare_token

  begin
    url = url_for(GET_TOKEN_VERIFY_FACTOR)

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

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

    response = HTTParty.post(
      url,
      headers: authorized_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.


352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
# File 'lib/onelogin/api/client.rb', line 352

def get_user(user_id)
  clean_error
  prepare_token

  begin

    url = url_for(GET_USER_URL, user_id)

    response = HTTParty.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.


389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
# File 'lib/onelogin/api/client.rb', line 389

def get_user_apps(user_id)
  clean_error
  prepare_token

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

    return Cursor.new(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.


416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
# File 'lib/onelogin/api/client.rb', line 416

def get_user_roles(user_id)
  clean_error
  prepare_token

  begin
    url = url_for(GET_ROLES_FOR_USER_URL, user_id)

    response = HTTParty.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)


323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
# File 'lib/onelogin/api/client.rb', line 323

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(url_for(GET_USERS_URL), options)

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

  nil
end

#handle_operation_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 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


111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/onelogin/api/client.rb', line 111

def handle_saml_endpoint_response(response)
  content = JSON.parse(response.body)
  if content && content.has_key?('status') && content.has_key?('data') && 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 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

    return saml_endpoint_response
  end

  nil
end

#handle_session_token_response(response) ⇒ Object


96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/onelogin/api/client.rb', line 96

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


130
131
132
133
134
135
# File 'lib/onelogin/api/client.rb', line 130

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.


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
847
848
# File 'lib/onelogin/api/client.rb', line 819

def lock_user(user_id, minutes)
  clean_error
  prepare_token

  begin
    url = url_for(LOCK_USER_URL, user_id)

    data = {
      'locked_until' => minutes
    }

    response = HTTParty.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)
    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.


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

def log_user_out(user_id)
  clean_error
  prepare_token

  begin
    url = url_for(LOG_USER_OUT_URL, user_id)

    response = HTTParty.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)
    end
  rescue Exception => e
    @error = '500'
    @error_description = e.message
  end

  false
end

#prepare_tokenObject


74
75
76
77
78
79
80
# File 'lib/onelogin/api/client.rb', line 74

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.


201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# File 'lib/onelogin/api/client.rb', line 201

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 = HTTParty.post(
      url,
      headers: headers,
      body: data.to_json
    )

    if response.code == 200
      json_data = JSON.parse(response.body)
      if json_data && json_data['data']
        token = OneLogin::Api::Models::OneLoginToken.new(json_data['data'][0])
        @access_token = token.access_token
        @refresh_token = token.refresh_token
        @expiration = token.created_at + token.expires_in
        return token
      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

#remove_role_from_user(user_id, role_ids) ⇒ Boolean

Removes Role from User


615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
# File 'lib/onelogin/api/client.rb', line 615

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 = HTTParty.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)
    end
  rescue Exception => e
    @error = '500'
    @error_description = e.message
  end

  false
end

#retrieve_apps_from_xml(xml_content) ⇒ Object


1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
# File 'lib/onelogin/api/client.rb', line 1480

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.


245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
# File 'lib/onelogin/api/client.rb', line 245

def revoke_token
  clean_error

  begin
    url = url_for(TOKEN_REVOKE_URL)

    data = {
      access_token: @access_token
    }

    response = HTTParty.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.


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
1439
# File 'lib/onelogin/api/client.rb', line 1406

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 = HTTParty.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


745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
# File 'lib/onelogin/api/client.rb', line 745

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 = HTTParty.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)
    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


656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
# File 'lib/onelogin/api/client.rb', line 656

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 = HTTParty.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)
    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


700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
# File 'lib/onelogin/api/client.rb', line 700

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 = HTTParty.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)
    end
  rescue Exception => e
    @error = '500'
    @error_description = e.message
  end

  false
end

#update_user(user_id, user_params) ⇒ User

Updates an user


538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
# File 'lib/onelogin/api/client.rb', line 538

def update_user(user_id, user_params)
  clean_error
  prepare_token

  begin
    url = url_for(UPDATE_USER_URL, user_id)

    response = HTTParty.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)
    end
  rescue Exception => e
    @error = '500'
    @error_description = e.message
  end

  nil
end

#validate_configObject

Raises:

  • (ArgumentError)

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

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