Class: Hydra::Keycloak::Client

Inherits:
Object
  • Object
show all
Extended by:
Mixin
Defined in:
lib/hydra/keycloak/client.rb

Instance Method Summary collapse

Methods included from Mixin

args_inject, container, inject

Instance Method Details

#access_token(session_state) ⇒ Object



178
179
180
# File 'lib/hydra/keycloak/client.rb', line 178

def access_token(session_state)
  fetch_token(session_state, 'access_token')
end

#access_token_jti(session_state) ⇒ Object



193
194
195
# File 'lib/hydra/keycloak/client.rb', line 193

def access_token_jti(session_state)
  fetch_token(session_state, 'access_token').fmap { |token| token[:jti] }
end

#auth_urlObject



137
138
139
140
# File 'lib/hydra/keycloak/client.rb', line 137

def auth_url
  code_verifier.generate
  urls.auth_url(code_verifier.code_challenge)
end

#authenticate!(auth_code) ⇒ Object



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/hydra/keycloak/client.rb', line 142

def authenticate!(auth_code)
  tokens_repo.get_tokens(auth_code, code_verifier.value).fmap do |tokens|
    access_token  = tokens[:access_token]
    id_token      = tokens[:id_token]
    refresh_token = tokens[:refresh_token]

    session_state = access_token.session_state

    save_token(session_state, 'access_token',  access_token)
    save_token(session_state, 'id_token',      id_token)
    save_token(session_state, 'refresh_token', refresh_token)

    session_state
  end
end

#authenticate_by_password!(username, password) ⇒ Object



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/hydra/keycloak/client.rb', line 158

def authenticate_by_password!(username, password)
  tokens_repo.get_tokens_by_password(username, password).fmap do |tokens|
    access_token  = tokens[:access_token]
    id_token      = tokens[:id_token]
    refresh_token = tokens[:refresh_token]

    session_state = access_token.session_state

    save_token(session_state, 'access_token',  access_token)
    save_token(session_state, 'id_token',      id_token)
    save_token(session_state, 'refresh_token', refresh_token)

    session_state
  end
end

#authenticated?(session_state) ⇒ Boolean

Returns:

  • (Boolean)


174
175
176
# File 'lib/hydra/keycloak/client.rb', line 174

def authenticated?(session_state)
  fetch_token(session_state, 'access_token').success?
end

#authorize!(session_state) ⇒ Object



182
183
184
185
186
187
188
189
190
191
# File 'lib/hydra/keycloak/client.rb', line 182

def authorize!(session_state)
  access_token = yield fetch_token(session_state, 'access_token')
  if token_expired?(access_token)
    yield refresh_tokens(session_state)

    access_token = yield fetch_token(session_state, 'access_token')
  end

  tokens_repo.introspect_token(access_token.source)
end

#introspect_token(token) ⇒ Object



205
206
207
# File 'lib/hydra/keycloak/client.rb', line 205

def introspect_token(token)
  tokens_repo.introspect_token(token)
end

#logout!(session_state) {|clear_tokens(session_state)| ... } ⇒ Object

Yields:

  • (clear_tokens(session_state))


197
198
199
200
201
202
203
# File 'lib/hydra/keycloak/client.rb', line 197

def logout!(session_state)
  id_token = yield fetch_token(session_state, 'id_token')

  yield clear_tokens(session_state)

  Success(urls.end_session_url(id_token.source))
end