Page Menu
Home
WickedGov Phorge
Search
Configure Global Search
Log In
Files
F4070204
RsaJwtCodecTest.php
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Flag For Later
Award Token
Size
3 KB
Referenced Files
None
Subscribers
None
RsaJwtCodecTest.php
View Options
<?php
namespace
MediaWiki\Tests\Integration\Json
;
use
LogicException
;
use
MediaWiki\Json\JwtException
;
use
MediaWiki\Json\RsaJwtCodec
;
use
MediaWiki\MainConfigNames
;
use
MediaWikiIntegrationTestCase
;
use
Wikimedia\Timestamp\ConvertibleTimestamp
;
/**
* @covers \MediaWiki\Json\RsaJwtCodec
*/
class
RsaJwtCodecTest
extends
MediaWikiIntegrationTestCase
{
public
function
testEnabled
()
{
$this
->
overrideConfigValue
(
MainConfigNames
::
JwtPublicKey
,
false
);
$this
->
overrideConfigValue
(
MainConfigNames
::
JwtPrivateKey
,
false
);
/** @var RsaJwtCodec $jwtCodec */
$jwtCodec
=
$this
->
getServiceContainer
()->
get
(
'JwtCodec'
);
$this
->
assertFalse
(
$jwtCodec
->
isEnabled
()
);
try
{
$jwtCodec
->
create
(
[]
);
$this
->
fail
(
'Expected exception was not thrown'
);
}
catch
(
LogicException
$e
)
{
$this
->
assertSame
(
'JWT handling is not configured'
,
$e
->
getMessage
()
);
}
try
{
$jwtCodec
->
parse
(
''
);
$this
->
fail
(
'Expected exception was not thrown'
);
}
catch
(
LogicException
$e
)
{
$this
->
assertSame
(
'JWT handling is not configured'
,
$e
->
getMessage
()
);
}
$this
->
overrideConfigValue
(
MainConfigNames
::
JwtPublicKey
,
$this
->
key
(
1
,
'public'
)
);
$this
->
overrideConfigValue
(
MainConfigNames
::
JwtPrivateKey
,
$this
->
key
(
1
,
'private'
)
);
$this
->
getServiceContainer
()->
resetServiceForTesting
(
'JwtCodec'
);
/** @var RsaJwtCodec $jwtCodec */
$jwtCodec
=
$this
->
getServiceContainer
()->
get
(
'JwtCodec'
);
$this
->
assertTrue
(
$jwtCodec
->
isEnabled
()
);
}
public
function
testJwt
()
{
$this
->
overrideConfigValue
(
MainConfigNames
::
JwtPublicKey
,
$this
->
key
(
1
,
'public'
)
);
$this
->
overrideConfigValue
(
MainConfigNames
::
JwtPrivateKey
,
$this
->
key
(
1
,
'private'
)
);
ConvertibleTimestamp
::
setFakeTime
(
'20200101000000'
);
/** @var RsaJwtCodec $jwtCodec */
$jwtCodec
=
$this
->
getServiceContainer
()->
get
(
'JwtCodec'
);
// basic case
$jwt
=
$jwtCodec
->
create
(
[
'foo'
=>
'bar'
]
);
$claims
=
$jwtCodec
->
parse
(
$jwt
);
$this
->
assertCount
(
1
,
$claims
);
$this
->
assertSame
(
'bar'
,
$claims
[
'foo'
]
);
$jwt
=
$jwtCodec
->
create
(
[
'exp'
=>
ConvertibleTimestamp
::
time
()
+
1
]
);
$claims
=
$jwtCodec
->
parse
(
$jwt
);
$this
->
assertSame
(
ConvertibleTimestamp
::
time
()
+
1
,
$claims
[
'exp'
]
);
// 'exp' in the past
$jwt
=
$jwtCodec
->
create
(
[
'exp'
=>
ConvertibleTimestamp
::
time
()
-
1
]
);
try
{
$jwtCodec
->
parse
(
$jwt
);
$this
->
fail
(
'Expected exception was not thrown for expired token'
);
}
catch
(
JwtException
)
{
}
// wrong singing key
$jwt
=
$jwtCodec
->
create
(
[]
);
$this
->
overrideConfigValue
(
MainConfigNames
::
JwtPublicKey
,
$this
->
key
(
2
,
'public'
)
);
$this
->
overrideConfigValue
(
MainConfigNames
::
JwtPrivateKey
,
$this
->
key
(
2
,
'private'
)
);
/** @var RsaJwtCodec $jwtCodec */
$jwtCodec
=
$this
->
getServiceContainer
()->
get
(
'JwtCodec'
);
try
{
$jwtCodec
->
parse
(
$jwt
);
$this
->
fail
(
'Expected exception was not thrown for invalid signature'
);
}
catch
(
JwtException
)
{
}
}
private
function
key
(
int
$number
,
string
$type
):
string
{
$ext
=
match
(
$type
)
{
'public'
=>
'pem.pub'
,
'private'
=>
'pem'
,
};
return
file_get_contents
(
__DIR__
.
"/key{$number}.$ext"
);
}
}
File Metadata
Details
Attached
Mime Type
text/x-php
Expires
Aug 16 2026, 23:50 (11 w, 5 d ago)
Storage Engine
local-disk
Storage Format
Raw Data
Storage Handle
ff/4c/db840624bdb52be396934287c3e0
Default Alt Text
RsaJwtCodecTest.php (3 KB)
Attached To
Mode
rMWPROD MediaWiki Production
Attached
Detach File
Event Timeline
Log In to Comment