Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
dbp-relay-core-bundle
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
This is an archived project. Repository and other project resources are read-only.
Show more breadcrumbs
digital blueprint
Relay API Gateway
dbp-relay-core-bundle
Commits
f19d42d4
Commit
f19d42d4
authored
2 years ago
by
Tobias Gross-Vogt
Browse files
Options
Downloads
Patches
Plain Diff
new helper class Http\ApiConnection communicating with an OIDC server and API server
parent
7db6c405
No related branches found
No related tags found
No related merge requests found
Pipeline
#202245
passed
2 years ago
Stage: test
Changes
2
Pipelines
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/Http/ApiConnection.php
+157
-0
157 additions, 0 deletions
src/Http/ApiConnection.php
src/Http/ConnectionException.php
+1
-0
1 addition, 0 deletions
src/Http/ConnectionException.php
with
158 additions
and
0 deletions
src/Http/ApiConnection.php
0 → 100644
+
157
−
0
View file @
f19d42d4
<?php
declare
(
strict_types
=
1
);
namespace
Dbp\Relay\CoreBundle\Http
;
use
Dbp\Relay\CoreBundle\Helpers\Tools
;
use
Psr\Cache\CacheItemPoolInterface
;
use
Psr\Http\Message\ResponseInterface
;
use
Psr\Log\LoggerInterface
;
class
ApiConnection
{
public
const
API_URL_CONFIG_PARAMETER
=
'api_url'
;
public
const
KEYCLOAK_SERVER_URL_CONFIG_PARAMETER
=
'keycloak_server_url'
;
public
const
KEYCLOAK_REALM_CONFIG_PARAMETER
=
'keycloak_realm'
;
public
const
CLIENT_ID_CONFIG_PARAMETER
=
'client_id'
;
public
const
CLIENT_SECRET_CONFIG_PARAMETER
=
'client_secret'
;
/** @var Connection */
private
$connection
;
/** @var array */
private
$config
;
/** @var object|null */
private
$clientHandler
;
/** @var CacheItemPoolInterface|null */
private
$cachePool
;
/** @var int */
private
$cacheTTL
;
/** @var LoggerInterface|null */
private
$logger
;
/** @var string */
private
$accessToken
;
public
function
__construct
()
{
$this
->
config
=
[];
}
public
function
setCache
(
?CacheItemPoolInterface
$cachePool
,
int
$ttl
):
void
{
$this
->
cachePool
=
$cachePool
;
$this
->
cacheTTL
=
$ttl
;
if
(
$this
->
connection
!==
null
)
{
$this
->
connection
->
setCache
(
$cachePool
,
$ttl
);
}
}
public
function
setConfig
(
array
$config
):
void
{
$this
->
config
=
$config
;
}
/*
* Used to mock up server for unit testing.
*/
public
function
setClientHandler
(
?object
$handler
):
void
{
$this
->
clientHandler
=
$handler
;
if
(
$this
->
connection
!==
null
)
{
$this
->
connection
->
setClientHandler
(
$handler
);
}
}
public
function
setLogger
(
LoggerInterface
$logger
):
void
{
$this
->
logger
=
$logger
;
if
(
$this
->
connection
!==
null
)
{
$this
->
connection
->
setLogger
(
$logger
);
}
}
/**
* @throws ConnectionException
*/
public
function
get
(
string
$uri
,
array
$options
):
ResponseInterface
{
$requestOptions
=
[
Connection
::
REQUEST_OPTION_HEADERS
=>
[
'Authorization'
=>
'Bearer '
.
$this
->
getAccessToken
(),
],
];
return
$this
->
getApiConnection
()
->
get
(
$uri
,
$options
,
$requestOptions
);
}
/**
* @throws ConnectionException
*/
private
function
getAccessToken
():
string
{
if
(
$this
->
accessToken
===
null
)
{
$idServerUrl
=
$this
->
config
[
self
::
KEYCLOAK_SERVER_URL_CONFIG_PARAMETER
];
$idServerRealm
=
$this
->
config
[
self
::
KEYCLOAK_REALM_CONFIG_PARAMETER
];
$clientId
=
$this
->
config
[
self
::
CLIENT_ID_CONFIG_PARAMETER
];
$clientSecret
=
$this
->
config
[
self
::
CLIENT_SECRET_CONFIG_PARAMETER
];
$tokenUrl
=
$idServerUrl
.
'/realms/'
.
$idServerRealm
.
'/protocol/openid-connect/token'
;
$response
=
$this
->
getIdServerConnection
()
->
postForm
(
$tokenUrl
,
[
'client_id'
=>
$clientId
,
'client_secret'
=>
$clientSecret
,
'grant_type'
=>
'client_credentials'
,
]);
$responseData
=
$response
->
getBody
()
->
getContents
();
try
{
$token
=
Tools
::
decodeJSON
(
$responseData
,
true
);
}
catch
(
\JsonException
$exception
)
{
throw
new
ConnectionException
(
sprintf
(
'Failed to JSON decode access token: '
.
$exception
->
getMessage
()),
ConnectionException
::
JSON_EXCEPTION
);
}
$this
->
accessToken
=
$token
[
'access_token'
];
}
return
$this
->
accessToken
;
}
private
function
getApiConnection
():
Connection
{
if
(
$this
->
connection
===
null
)
{
$connection
=
new
Connection
(
$this
->
config
[
self
::
API_URL_CONFIG_PARAMETER
]);
if
(
$this
->
cachePool
!==
null
)
{
$connection
->
setCache
(
$this
->
cachePool
,
$this
->
cacheTTL
);
}
if
(
$this
->
clientHandler
!==
null
)
{
$connection
->
setClientHandler
(
$this
->
clientHandler
);
}
if
(
$this
->
logger
!==
null
)
{
$connection
->
setLogger
(
$this
->
logger
);
}
$this
->
connection
=
$connection
;
}
return
$this
->
connection
;
}
private
function
getIdServerConnection
():
Connection
{
$idServerConnection
=
new
Connection
();
if
(
$this
->
clientHandler
!==
null
)
{
$idServerConnection
->
setClientHandler
(
$this
->
clientHandler
);
}
return
$idServerConnection
;
}
}
This diff is collapsed.
Click to expand it.
src/Http/ConnectionException.php
+
1
−
0
View file @
f19d42d4
...
...
@@ -10,6 +10,7 @@ use Psr\Http\Message\ResponseInterface;
class
ConnectionException
extends
\RuntimeException
{
public
const
REQUEST_EXCEPTION
=
1
;
public
const
JSON_EXCEPTION
=
2
;
/** @var RequestInterface|null */
private
$request
;
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment