Class: Hydra::Keycloak::Tokens::Repo

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

Instance Method Summary collapse

Methods included from Mixin

args_inject, container, inject

Instance Method Details

#get_tokens(auth_code, code_verifier) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/hydra/keycloak/tokens/repo.rb', line 16

def get_tokens(auth_code, code_verifier)
  return Failure(code: :auth_code_was_not_received, context: { caller: caller }) unless auth_code

  result = tokens_gateway.post(
    urls.token_endpoint,
    urls.auth_code_token_request_body(auth_code, code_verifier)
  )

  result.fmap do |tokens|
    {
      access_token: ::Hydra::Keycloak::Token.new(tokens['access_token']),
      id_token: ::Hydra::Keycloak::Token.new(tokens['id_token']),
      refresh_token: ::Hydra::Keycloak::Token.new(tokens['refresh_token'])
    }
  end
end

#get_tokens_by_password(username, password) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/hydra/keycloak/tokens/repo.rb', line 33

def get_tokens_by_password(username, password)
  if username.nil? || password.nil?
    return Failure(code: :username_or_password_is_empty,
                   context: { caller: caller })
  end

  result = tokens_gateway.post(
    urls.token_endpoint,
    urls.password_token_request_body(username, password)
  )

  result.fmap do |tokens|
    {
      access_token: ::Hydra::Keycloak::Token.new(tokens['access_token']),
      id_token: ::Hydra::Keycloak::Token.new(tokens['id_token']),
      refresh_token: ::Hydra::Keycloak::Token.new(tokens['refresh_token'])
    }
  end
end

#introspect_token(token) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/hydra/keycloak/tokens/repo.rb', line 53

def introspect_token(token)
  tokens_gateway.post(
    urls.introspection_endpoint,
    urls.introspection_request_body(token)
  ).bind do |result|
    if result['active']
      Success(result)
    else
      Failure(code: :token_not_active, caller: caller)
    end
  end
end

#refresh_tokens(refresh_token) ⇒ Object



66
67
68
69
70
71
72
73
74
75
# File 'lib/hydra/keycloak/tokens/repo.rb', line 66

def refresh_tokens(refresh_token)
  tokens_gateway.post(
    urls.token_endpoint,
    urls.refresh_request_body(refresh_token)
  ).bind do |result|
    Success({ access_token: ::Hydra::Keycloak::Token.new(result['access_token']),
              id_token: ::Hydra::Keycloak::Token.new(result['id_token']),
              refresh_token: ::Hydra::Keycloak::Token.new(result['refresh_token']) })
  end
end