Skip to content
Toggle navigation
Toggle navigation
This project
Loading...
Sign in
moqui
/
moqui-keycloak
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Network
Create a new issue
Builds
Commits
Issue Boards
Files
Commits
Network
Compare
Branches
Tags
89286c2a
authored
2023-03-09 19:04:48 -0600
by
Adam Heath
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
Use a ToolFactory to build a working Keycloak at startup.
1 parent
af3ad96c
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
94 additions
and
12 deletions
MoquiConf.xml
service/keycloak/KeycloakServices.groovy
src/main/groovy/org/moqui/keycloak/KeycloakToolFactory.groovy
MoquiConf.xml
0 → 100644
View file @
89286c2
<?xml version="1.0" encoding="UTF-8" ?>
<!-- No copyright or license for configuration file, details here are not considered a creative work. -->
<moqui-conf
xmlns:xsi=
"http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation=
"http://moqui.org/xsd/moqui-conf-3.xsd"
>
<default-property
name=
"moqui_keycloak_realm"
value=
"master"
/>
<default-property
name=
"moqui_keycloak_server_url"
value=
"http://keycloak"
/>
<default-property
name=
"moqui_keycloak_client_id"
value=
"moqui"
/>
<default-property
name=
"moqui_keycloak_client_secret"
is-secret=
"true"
/>
<tools>
<tool-factory
class=
"org.moqui.keycloak.KeycloakToolFactory"
init-priority=
"20"
disabled=
"false"
/>
</tools>
</moqui-conf>
service/keycloak/KeycloakServices.groovy
View file @
89286c2
...
...
@@ -23,6 +23,9 @@ import org.moqui.entity.EntityFind
import
org.moqui.entity.EntityList
import
org.moqui.entity.EntityValue
import
org.moqui.keycloak.KeycloakToolFactory
import
org.keycloak.OAuth2Constants
import
org.keycloak.admin.client.Keycloak
import
org.keycloak.admin.client.KeycloakBuilder
...
...
@@ -50,16 +53,6 @@ String keycloakToJson(Object o) {
return
JsonSerialization
.
writeValueAsString
(
o
)
}
Keycloak
buildKeycloak
()
{
return
KeycloakBuilder
.
builder
()
.
serverUrl
(
'http://keycloak'
)
.
realm
(
'master'
)
.
grantType
(
OAuth2Constants
.
CLIENT_CREDENTIALS
)
.
clientId
(
'moqui'
)
.
clientSecret
(
'iXsnjGEbIVT8DQky2yCU9NQhnqDYyi7g'
)
.
build
()
}
Map
<
String
,
Object
>
buildClientConsent
()
{
return
[
clientId:
null
,
...
...
@@ -137,7 +130,7 @@ Map<String, Object> joinUserAccountToKeycloak() {
return
[:]
}
Keycloak
keycloak
=
buildKeycloak
()
Keycloak
keycloak
=
KeycloakToolFactory
.
getInstance
()
try
{
RealmResource
realm
=
keycloak
.
realm
(
'master'
)
...
...
@@ -279,7 +272,7 @@ Map<String, Object> pushKeycloakUser() {
Map
<
String
,
Object
>
getKeycloakUsers
()
{
String
keycloakClientId
=
'moqui'
Keycloak
keycloak
=
buildKeycloak
(
)
Keycloak
keycloak
=
ec
.
getTool
(
'Keycloak'
,
Keycloak
.
class
)
try
{
RealmResource
realm
=
keycloak
.
realm
(
'master'
)
...
...
src/main/groovy/org/moqui/keycloak/KeycloakToolFactory.groovy
0 → 100644
View file @
89286c2
/*
* This software is in the public domain under CC0 1.0 Universal plus a
* Grant of Patent License.
*
* To the extent possible under law, the author(s) have dedicated all
* copyright and related and neighboring rights to this software to the
* public domain worldwide. This software is distributed without any
* warranty.
*
* You should have received a copy of the CC0 Public Domain Dedication
* along with this software (see the LICENSE.md file). If not, see
* <http://creativecommons.org/publicdomain/zero/1.0/>.
*/
package
org.moqui.keycloak
import
groovy.transform.CompileStatic
import
org.moqui.context.ExecutionContextFactory
import
org.moqui.context.ToolFactory
import
org.slf4j.Logger
import
org.slf4j.LoggerFactory
import
org.keycloak.OAuth2Constants
import
org.keycloak.admin.client.Keycloak
import
org.keycloak.admin.client.KeycloakBuilder
import
org.keycloak.representations.info.ServerInfoRepresentation
/**
*
*/
@CompileStatic
class
KeycloakToolFactory
implements
ToolFactory
<
Keycloak
>
{
protected
final
static
Logger
logger
=
LoggerFactory
.
getLogger
(
KeycloakToolFactory
.
class
)
final
static
String
TOOL_NAME
=
"Keycloak"
protected
Keycloak
keycloak
=
null
KeycloakToolFactory
()
{
}
@Override
String
getName
()
{
return
TOOL_NAME
}
@Override
void
init
(
ExecutionContextFactory
ecf
)
{
logger
.
info
(
"Starting Keycloak"
)
try
{
ServerInfoRepresentation
serverInfo
=
keycloak
.
serverInfo
().
getInfo
()
}
catch
(
Exception
e
)
{
logger
.
error
(
"Could not connect to keycloak server"
)
throw
e
}
}
@Override
void
preFacadeInit
(
ExecutionContextFactory
ecf
)
{
this
.
keycloak
=
KeycloakBuilder
.
builder
()
.
serverUrl
((
String
)
System
.
getProperty
(
"moqui_keycloak_server_url"
))
.
realm
((
String
)
System
.
getProperty
(
"moqui_keycloak_realm"
))
.
grantType
((
String
)
OAuth2Constants
.
CLIENT_CREDENTIALS
)
.
clientId
((
String
)
System
.
getProperty
(
"moqui_keycloak_client_id"
))
.
clientSecret
((
String
)
System
.
getProperty
(
"moqui_keycloak_client_secret"
))
.
build
()
}
@Override
Keycloak
getInstance
(
Object
...
parameters
)
{
return
keycloak
}
@Override
void
destroy
()
{
if
(
keycloak
!=
null
)
{
keycloak
.
close
()
}
}
}
Please
register
or
sign in
to post a comment