Class: KeycloakRails::User

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

Overview

User lvl access to sso server established session_token after auth with username and password min perms

Instance Method Summary collapse

Constructor Details

#initializeUser

Returns a new instance of User.



7
8
9
# File 'lib/keycloak_rails/user.rb', line 7

def initialize
  @curl = KeycloakRails::Curl.new
end

Instance Method Details

#access_tokenObject

private



75
76
77
# File 'lib/keycloak_rails/user.rb', line 75

def access_token
  KeycloakRails.current_session_cookie
end

#active_user_subObject



64
65
66
67
68
69
70
71
# File 'lib/keycloak_rails/user.rb', line 64

def active_user_sub
  return unless access_token

  case KeycloakRails.decode_token_strategy
  when :local then decode_active_user_sub
  when :cloud then fetch_active_user_sub
  end
end

#decode(token) ⇒ Object



87
88
89
# File 'lib/keycloak_rails/user.rb', line 87

def decode(token)
  JWT.decode token, KeycloakRails.public_key, false, { algorithm: KeycloakRails.signature_algo }
end

#decode_active_user_subObject

gets user sub by decoding the session_cookie



60
61
62
# File 'lib/keycloak_rails/user.rb', line 60

def decode_active_user_sub
  decoded_access_token.first['sub']
end

#decoded_access_tokenObject



79
80
81
# File 'lib/keycloak_rails/user.rb', line 79

def decoded_access_token
  decode(access_token)
end

#decoded_refresh_tokenObject



83
84
85
# File 'lib/keycloak_rails/user.rb', line 83

def decoded_refresh_token
  decode(refresh_token)
end

#end_session(redirect_uri) ⇒ Object

Raises:

  • (StandardError)


42
43
44
45
46
47
48
49
50
51
52
# File 'lib/keycloak_rails/user.rb', line 42

def end_session(redirect_uri)
  request = @curl.post(path: KeycloakRails.openid_config['end_session_endpoint'],
                       headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
                       body: { 'client_id': KeycloakRails.client_id,
                               'client_secret': KeycloakRails.secret,
                               'refresh_token': KeycloakRails.current_refresh_cookie,
                               'post_logout_redirect_uri': redirect_uri })
  raise StandardError, request[:response] unless request[:status] == :ok

  request[:response]
end

#fetch_active_user_subObject

gets user sub by making an api call to auth server



55
56
57
# File 'lib/keycloak_rails/user.rb', line 55

def fetch_active_user_sub
  ['sub']
end

#fetch_current_user_infoObject

Raises:

  • (StandardError)


33
34
35
36
37
38
39
40
# File 'lib/keycloak_rails/user.rb', line 33

def 
  request = @curl.post(path: KeycloakRails.openid_config['userinfo_endpoint'],
                       headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
                       body: { access_token: access_token })
  raise StandardError, request[:response] unless request[:status] == :ok

  request[:response]
end

#fetch_tokens(email:, password:, otp_password: nil) ⇒ Object

Raises:

  • (StandardError)


11
12
13
14
15
16
17
18
19
20
# File 'lib/keycloak_rails/user.rb', line 11

def fetch_tokens(email:, password:, otp_password: nil)
  request = @curl.post(path: KeycloakRails.openid_config['token_endpoint'],
                       headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
                       body: { 'grant_type': 'password', 'client_id': KeycloakRails.client_id,
                               'client_secret': KeycloakRails.secret, 'username': email,
                               'password': password }.merge((otp_password ? { 'totp': otp_password } : {})))
  raise StandardError, request[:response] unless request[:status] == :ok

  request[:response]
end

#fetch_tokens_by_handshake(code:, redirect_uri:) ⇒ Object

Raises:

  • (StandardError)


22
23
24
25
26
27
28
29
30
31
# File 'lib/keycloak_rails/user.rb', line 22

def fetch_tokens_by_handshake(code:, redirect_uri:)
  request = @curl.post(path: KeycloakRails.openid_config['token_endpoint'],
                       headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
                       body: { 'grant_type': 'authorization_code', 'client_id': KeycloakRails.client_id,
                               'client_secret': KeycloakRails.secret, code: code,
                               "redirect_uri": redirect_uri })
  raise StandardError, request[:response] unless request[:status] == :ok

  request[:response]
end