Code
Explorer
You are a developer and looking for Shopware projects?
Apply Now!
jwtGenerationRequiresCustomerLoggedIn example
$appJWTGenerateRoute
=
$this
->
createMock
(
AppJWTGenerateRoute::
class
)
;
$appJWTGenerateRoute
->
expects
(
static
::
once
(
)
)
->
method
(
'generate'
)
->
with
(
'test'
)
;
$controller
=
new
AppController
(
$appJWTGenerateRoute
)
;
$controller
->
generateToken
(
'test', Generator::
createSalesChannelContext
(
)
)
;
}
public
function
testGenerateFails
(
)
: void
{
$appJWTGenerateRoute
=
$this
->
createMock
(
AppJWTGenerateRoute::
class
)
;
$appJWTGenerateRoute
->
expects
(
static
::
once
(
)
)
->
method
(
'generate'
)
->
willThrowException
(
AppException::
jwtGenerationRequiresCustomerLoggedIn
(
)
)
;
$controller
=
new
AppController
(
$appJWTGenerateRoute
)
;
$response
=
$controller
->
generateToken
(
'test', Generator::
createSalesChannelContext
(
)
)
;
static
::
assertSame
(
Response::HTTP_BAD_REQUEST,
$response
->
getStatusCode
(
)
)
;
$data
=
json_decode
(
(string)
$response
->
getContent
(
)
, true, 512, \JSON_THROW_ON_ERROR
)
;
static
::
assertArrayHasKey
(
'message',
$data
)
;
static
::
assertSame
(
'JWT generation requires customer to be logged in',
$data
[
'message'
]
)
;
}
}
public
function
__construct
(
private
readonly Connection
$connection
,
private
readonly ShopIdProvider
$shopIdProvider
)
{
}
#[Route('/store-api/app-system/{name}/generate-token', name: 'store-api.app-system.generate-token', methods: ['POST'])]
public
function
generate
(
string
$name
, SalesChannelContext
$context
)
: JsonResponse
{
if
(
$context
->
getCustomer
(
)
=== null
)
{
throw
AppException::
jwtGenerationRequiresCustomerLoggedIn
(
)
;
}
[
'app_secret' =>
$appSecret
, 'privileges' =>
$privileges
]
=
$this
->
fetchAppDetails
(
$name
)
;
$key
= InMemory::
plainText
(
$appSecret
)
;
$configuration
= Configuration::
forSymmetricSigner
(
new
Sha256
(
)
,
$key
)
;