Class: RailsKeycloakAuthorization::KeycloakAdminRubyAgent

Inherits:
Object
  • Object
show all
Defined in:
app/services/rails_keycloak_authorization/keycloak_admin_ruby_agent.rb

Constant Summary collapse

POLICY_NAME =
"RKA-Policy"

Class Method Summary collapse

Class Method Details

.attach_scope_to_resource(keycloak_scope_name, keycloak_resource_id) ⇒ Object



138
139
140
141
142
# File 'app/services/rails_keycloak_authorization/keycloak_admin_ruby_agent.rb', line 138

def self.attach_scope_to_resource(keycloak_scope_name, keycloak_resource_id)
  KeycloakAdmin.realm(realm_name)
               .authz_resources(openid_client.id)
               .update(keycloak_resource_id, scopes: [{name: keycloak_scope_name}])
end

.create_keycloak_policy(keycloak_realm_role_id, policy_name) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'app/services/rails_keycloak_authorization/keycloak_admin_ruby_agent.rb', line 38

def create_keycloak_policy(keycloak_realm_role_id, policy_name)
  KeycloakAdmin
    .realm(realm_name)
    .authz_policies(openid_client.id, 'role')
    .create!(policy_name,
             "#{POLICY_NAME} default policy",
             "role",
             "POSITIVE",
             "UNANIMOUS",
             true,
             [{id: keycloak_realm_role_id, required: true}]
    )
end

.create_keycloak_resource(route_id) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'app/services/rails_keycloak_authorization/keycloak_admin_ruby_agent.rb', line 71

def create_keycloak_resource(route_id)
  route = WithRoutesReader.route(route_id)
  resource_name = resource_name_for(route.defaults[:controller])

  KeycloakAdmin
    .realm(realm_name)
    .authz_resources(openid_client.id)
    .create!(
      resource_name,
      resource_type_for_controller,
      [],
      true,
      "RKA #{resource_name}",
      [])
end

.create_keycloak_scope(keycloak_scope_name) ⇒ Object



60
61
62
63
64
65
66
67
68
69
# File 'app/services/rails_keycloak_authorization/keycloak_admin_ruby_agent.rb', line 60

def create_keycloak_scope(keycloak_scope_name)
  KeycloakAdmin
    .realm(realm_name)
    .authz_scopes(openid_client.id)
    .create!(
      keycloak_scope_name,
      "RKA #{keycloak_scope_name}",
      ""
    )
end

.initializeObject



7
8
9
10
# File 'app/services/rails_keycloak_authorization/keycloak_admin_ruby_agent.rb', line 7

def initialize
  super
  keycloak_admin_configure
end

.keycloak_admin_configureObject



124
125
126
127
128
129
130
131
132
133
134
135
# File 'app/services/rails_keycloak_authorization/keycloak_admin_ruby_agent.rb', line 124

def keycloak_admin_configure
  KeycloakAdmin.configure do |config|
    config. = true
    config.server_url          = RailsKeycloakAuthorization.keycloak_server_url
    config.server_domain       = RailsKeycloakAuthorization.keycloak_server_domain
    config.client_realm_name   = RailsKeycloakAuthorization.keycloak_admin_realm_name
    config.client_id           = RailsKeycloakAuthorization.keycloak_admin_client_id
    config.client_secret       = RailsKeycloakAuthorization.keycloak_admin_client_secret
    config.logger              = Rails.logger
    config.rest_client_options = { timeout: 3, verify_ssl: Rails.env.production? }
  end
end

.keycloak_resource(controller_name) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'app/services/rails_keycloak_authorization/keycloak_admin_ruby_agent.rb', line 87

def keycloak_resource(controller_name)
  resource_name = resource_name_for(controller_name)
  KeycloakAdmin
    .realm(realm_name)
    .authz_resources(openid_client.id)
    .find_by(resource_name,
             resource_type_for_controller,
             "",
             "",
             "")
    .first
rescue
  nil
end

.list_keycloak_permissionsObject



26
27
28
29
30
# File 'app/services/rails_keycloak_authorization/keycloak_admin_ruby_agent.rb', line 26

def list_keycloak_permissions
  KeycloakAdmin.realm(realm_name)
               .authz_permissions(openid_client.id, "scope")
               .find_by(nil, nil)
end

.list_keycloak_policiesObject



32
33
34
35
36
# File 'app/services/rails_keycloak_authorization/keycloak_admin_ruby_agent.rb', line 32

def list_keycloak_policies
  KeycloakAdmin.realm(realm_name)
               .authz_policies(openid_client.id, 'role')
               .find_by(POLICY_NAME, "role")
end

.list_keycloak_resources_for_controllersObject



16
17
18
19
20
21
22
23
24
# File 'app/services/rails_keycloak_authorization/keycloak_admin_ruby_agent.rb', line 16

def list_keycloak_resources_for_controllers
  KeycloakAdmin.realm(realm_name)
               .authz_resources(openid_client.id)
               .find_by("",
                        resource_type_for_controller,
                        "",
                        "",
                        "")
end

.list_policiesObject



52
53
54
# File 'app/services/rails_keycloak_authorization/keycloak_admin_ruby_agent.rb', line 52

def list_policies

end

.list_rolesObject



56
57
58
# File 'app/services/rails_keycloak_authorization/keycloak_admin_ruby_agent.rb', line 56

def list_roles
  KeycloakAdmin.realm(realm_name).roles.list
end

.openid_clientObject



106
107
108
109
110
111
# File 'app/services/rails_keycloak_authorization/keycloak_admin_ruby_agent.rb', line 106

def openid_client
  KeycloakAdmin
    .realm(realm_name)
    .clients
    .find_by_client_id(RailsKeycloakAuthorization.keycloak_auth_client_id)
end

.policy_nameObject



12
13
14
# File 'app/services/rails_keycloak_authorization/keycloak_admin_ruby_agent.rb', line 12

def policy_name
  POLICY_NAME
end

.realm_nameObject



102
103
104
# File 'app/services/rails_keycloak_authorization/keycloak_admin_ruby_agent.rb', line 102

def realm_name
  RailsKeycloakAuthorization.keycloak_auth_client_realm_name
end

.resource_name_for(controller_name) ⇒ Object



120
121
122
# File 'app/services/rails_keycloak_authorization/keycloak_admin_ruby_agent.rb', line 120

def resource_name_for(controller_name)
  "#{controller_name}_controller"
end

.resource_type_for_controllerObject



113
114
115
# File 'app/services/rails_keycloak_authorization/keycloak_admin_ruby_agent.rb', line 113

def resource_type_for_controller
  type_for(openid_client.client_id)
end

.type_for(openid_client_id) ⇒ Object



117
118
119
# File 'app/services/rails_keycloak_authorization/keycloak_admin_ruby_agent.rb', line 117

def type_for(openid_client_id)
  "urn:#{openid_client_id}:rka:resources:controllers"
end