Class: Keycloak::Client

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

Overview

TODO: try adding functions to ffi class

Instance Method Summary collapse

Constructor Details

#initialize(json) ⇒ Client

Returns a new instance of Client.



354
355
356
357
358
359
360
361
# File 'lib/libkeycloak.rb', line 354

def initialize(json)
  client_ptr = FFI::LibC.malloc(Keycloak_FFI::KeycloakClient.size)
  @ptr = Keycloak_FFI::KeycloakClient.new(client_ptr)
  err = Keycloak_FFI.keycloak_create_client(@ptr, json)
  if err[:errcode] != :OK
    raise Keycloak::Error, err
  end
end

Instance Method Details

#auth_server_urlObject



371
372
373
# File 'lib/libkeycloak.rb', line 371

def auth_server_url
  @ptr[:auth_server_url]
end

#decode_and_validate_jwt(token, iss: nil, sub: nil, aud: nil, jti: nil, typ: nil, validate_exp: false, exp_tolerance: 0, validate_nbf: false, nbf_tolerance: 0, validate_iat: false, iat_tolerance: 0) ⇒ Object



513
514
515
516
517
518
519
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
550
551
552
553
554
555
556
557
# File 'lib/libkeycloak.rb', line 513

def decode_and_validate_jwt(
  token,
  iss: nil,
  sub: nil,
  aud: nil,
  jti: nil,
  typ: nil,
  validate_exp: false,
  exp_tolerance: 0,
  validate_nbf: false,
  nbf_tolerance: 0,
  validate_iat: false,
  iat_tolerance: 0
)
  valid = FFI::MemoryPointer.new(:uint8)
  jwt_ptr = FFI::LibC.malloc(Keycloak_FFI::KeycloakJWT.size)
  jwt = Keycloak_FFI::KeycloakJWT.new(jwt_ptr)
  err = Keycloak_FFI.keycloak_decode_and_validate_jwt_ex(
    @ptr,
    token,
    iss, iss.nil? ? 0 : iss.size,
    sub, sub.nil? ? 0 : sub.size,
    aud, aud.nil? ? 0 : aud.size,
    jti, jti.nil? ? 0 : jti.size,
    typ, typ.nil? ? 0 : typ.size,
    validate_exp, exp_tolerance,
    validate_nbf, nbf_tolerance,
    validate_iat, iat_tolerance,
    valid,
    jwt
  )

  if err[:errcode] != :OK
    raise err
  end

  reason = Keycloak_FFI::KeycloakJWTValidationResult[valid.read(:uint8).to_i]
  return JWTDecodeResult.new(
    JWTValidationResult.new(
      reason == :VALID,
      reason
    ),
    jwt
  )
end

#get_token(user, pass, scopes: nil) ⇒ Object



383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
# File 'lib/libkeycloak.rb', line 383

def get_token(user, pass, scopes: nil)
  tokens_ptr = FFI::LibC.malloc(Keycloak_FFI::KeycloakTokens.size)
  tokens = Keycloak_FFI::KeycloakTokens.new(tokens_ptr)
  err_response_ptr = FFI::MemoryPointer.new(:string)
  scopes_ptr = nil
  scopes_len = 0
  unless scopes.nil?
    scopes_len = scopes.size
    scopes_ptr = FFI::MemoryPointer.new(:pointer, scopes_len)
    ptr_arr = scopes.map do |scope, i|
      ptr = FFI::MemoryPointer.from_string scope
    end
    scopes_ptr.put_array_of_pointer 0, ptr_arr
  end
  err = Keycloak_FFI.keycloak_get_token(
    @ptr,
    user, pass,
    scopes_ptr, scopes_len,
    tokens,
    err_response_ptr
  )

  if err[:errcode] == :HTTP
    err = Keycloak::Error.new(err, err_response_ptr)
    raise err
  elsif err[:errcode] != :OK
    raise Keycloak::Error, err
  end

  return tokens
end

#get_userinfo(tokens) ⇒ Object



447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
# File 'lib/libkeycloak.rb', line 447

def get_userinfo(tokens)
  userinfo_ptr = FFI::LibC.malloc(Keycloak_FFI::KeycloakUserinfo.size)
  userinfo = Keycloak_FFI::KeycloakUserinfo.new(userinfo_ptr)

  err_response_ptr = FFI::MemoryPointer.new(:string)

  err = Keycloak_FFI.keycloak_get_userinfo(
    @ptr,
    tokens,
    userinfo,
    err_response_ptr
  )

  if err[:errcode] == :HTTP
    err = Keycloak::Error.new(err, err_response_ptr)
    raise err
  elsif err[:errcode] != :OK
    raise Keycloak::Error, err
  end

  return userinfo
end

#realmObject



363
364
365
# File 'lib/libkeycloak.rb', line 363

def realm
  @ptr[:realm]
end

#realm_infoObject



379
380
381
# File 'lib/libkeycloak.rb', line 379

def realm_info
  @ptr[:realm_info]
end

#refresh_token(refresh_token, scopes: nil) ⇒ Object



415
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
445
# File 'lib/libkeycloak.rb', line 415

def refresh_token(refresh_token, scopes: nil)
  tokens_ptr = FFI::LibC.malloc(Keycloak_FFI::KeycloakTokens.size)
  tokens = Keycloak_FFI::KeycloakTokens.new(tokens_ptr)
  err_response_ptr = FFI::MemoryPointer.new(:string)
  scopes_ptr = nil
  scopes_len = 0
  unless scopes.nil?
    scopes_len = scopes.size
    scopes_ptr = FFI::MemoryPointer.new(:pointer, scopes_len)
    ptr_arr = scopes.map do |scope, i|
      ptr = FFI::MemoryPointer.from_string scope
    end
    scopes_ptr.put_array_of_pointer 0, ptr_arr
  end
  err = Keycloak_FFI.keycloak_refresh_token(
    @ptr,
    refresh_token,
    scopes_ptr, scopes_len,
    tokens,
    err_response_ptr
  )

  if err[:errcode] == :HTTP
    err = Keycloak::Error.new(err, err_response_ptr)
    raise err
  elsif err[:errcode] != :OK
    raise Keycloak::Error, err
  end

  return tokens
end

#resourceObject



367
368
369
# File 'lib/libkeycloak.rb', line 367

def resource
  @ptr[:resource]
end

#secretObject



375
376
377
# File 'lib/libkeycloak.rb', line 375

def secret
  @ptr[:secret]
end

#validate_jwt(token, iss: nil, sub: nil, aud: nil, jti: nil, typ: nil, validate_exp: false, exp_tolerance: 0, validate_nbf: false, nbf_tolerance: 0, validate_iat: false, iat_tolerance: 0) ⇒ Object



470
471
472
473
474
475
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
507
508
509
510
511
# File 'lib/libkeycloak.rb', line 470

def validate_jwt(
  token,
  # strings
  iss: nil,
  sub: nil,
  aud: nil,
  jti: nil,
  typ: nil,
  # booleans
  validate_exp: false,
  # Tolerance in seconds
  exp_tolerance: 0,
  validate_nbf: false,
  nbf_tolerance: 0,
  validate_iat: false,
  iat_tolerance: 0
)
  valid = FFI::MemoryPointer.new(:uint8)
  err = Keycloak_FFI.keycloak_validate_jwt_ex(
    @ptr,
    token,
    iss, iss.nil? ? 0 : iss.size,
    sub, sub.nil? ? 0 : sub.size,
    aud, aud.nil? ? 0 : aud.size,
    jti, jti.nil? ? 0 : jti.size,
    typ, typ.nil? ? 0 : typ.size,
    validate_exp, exp_tolerance,
    validate_nbf, nbf_tolerance,
    validate_iat, iat_tolerance,
    valid
  )

  if err[:errcode] != :OK
    raise err
  end

  reason = Keycloak_FFI::KeycloakJWTValidationResult[valid.read(:uint8).to_i]
  return JWTValidationResult.new(
    reason == :VALID,
    reason
  )
end