14require_once __DIR__ .
'/Maintenance.php';
26 parent::__construct();
27 $this->
addDescription(
'Generate a JWT token from a JSON file or a JSON string' );
28 $this->
addOption(
'file',
'A filename with claims stored as JSON',
false,
true );
29 $this->
addOption(
'json',
'A json string with claims',
false,
true );
30 $this->
addOption(
'include-default-claims',
'Inject default claims: iss, iat, jti and sxp',
false );
31 $this->
addOption(
'validate',
'Validate if JWT has all required fields (iss, sub)',
false );
32 $this->
addOption(
'verbose',
'Be verbose and output the claims array',
false );
43 'iat' => MWTimestamp::time(),
44 'jti' => base64_encode( random_bytes( 16 ) ),
45 'sxp' => MWTimestamp::time() + 3600,
55 private function validateClaims( array $claims ) {
56 foreach ( [
'iss',
'sub' ] as $requiredClaim ) {
57 if ( !array_key_exists( $requiredClaim, $claims ) ) {
58 $this->
fatalError(
'Missing required claim: ' . $requiredClaim );
68 private function readClaimsFromInput(): array {
71 if ( $file ===
null && $json ===
null ) {
72 $this->
fatalError(
'Either --file or --json must be specified' );
75 if ( !file_exists( $file ) ) {
76 $this->
fatalError(
'File does not exist: ' . $file );
78 $this->
output(
'Reading claims from file: ' . $file . PHP_EOL );
79 $content = file_get_contents( $file );
83 if ( strlen( $content ) == 0 ) {
84 $this->
fatalError(
'Empty content, cannot decode' );
86 $claims = json_decode( $content,
true );
87 $lastError = json_last_error();
88 if ( $lastError !== JSON_ERROR_NONE ) {
89 $this->
fatalError(
'Invalid JSON: ' . json_last_error_msg() );
91 if ( !is_array( $claims ) ) {
92 $this->
fatalError(
'Decoded claims structure is not an array' );
98 $jwtCodec = $this->getServiceContainer()->getJwtCodec();
99 if ( !$jwtCodec->isEnabled() ) {
100 $this->fatalError(
'JWT is not enabled on this wiki. Please setup JwtPublicKey and JwtPrivateKey' );
103 $claims = $this->readClaimsFromInput();
105 if ( $this->hasOption(
'include-default-claims' ) ) {
106 $claims = $this->getDefaultClaims() + $claims;
109 if ( $this->getOption(
'verbose' ) ) {
110 $this->output(
'Decoded Claims: ' . PHP_EOL );
111 $this->output( json_encode( $claims, JSON_PRETTY_PRINT ) . PHP_EOL );
114 if ( $this->getOption(
'validate' ) ) {
115 $this->validateClaims( $claims );
118 $token = $jwtCodec->create( $claims );
119 $this->output( $token . PHP_EOL );
125require_once RUN_MAINTENANCE_IF_MAIN;
Maintenance script to generate a JWT token.
__construct()
Default constructor.
execute()
Do the actual work.
getDefaultClaims()
Retrieve default claims to inject into the JWT token.
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
output( $out, $channel=null)
Throw some output to the user.
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
getOption( $name, $default=null)
Get an option, or return the default.
getServiceContainer()
Returns the main service container.
addDescription( $text)
Set the description text.