Skip to content

Conversation

@GrandFelix
Copy link
Contributor

fixed wrong used config fields in some places when using custom fields
use tokenField from config if defined else IdentifierInterface::CREDENTIAL_JWT_SUBJECT.

this fixes this #353 (comment)

use tokenField from config if defined else IdentifierInterface::CREDENTIAL_JWT_SUBJECT
in conditions must be used dataField
Comment on lines 44 to 50
$dataField = $this->getConfig('dataField');
if (!isset($data[$dataField])) {
$tokenField = $this->getConfig('tokenField');
if (!isset($data[$tokenField])) {
return null;
}

$conditions = [
$this->getConfig('tokenField') => $data[$dataField],
$this->getConfig('dataField') => $data[$tokenField],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this change necessary? Unless I'm missing something, It just flips the config options, which breaks backwards compatibility.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you are right. I came out from this issue #353 (comment) I think data and token name is confusing. I expected that data is db field and token for token. But its other way arround. And also when you use custom token key name, here https://github.com/cakephp/authentication/pull/376/files/90a2daea03f470ef1294d9ac423bd42870a17ece#diff-b7a10d2c99be4b6528582b9471a0fb86R92 was fixed to 'sub' key name and because of that custom naming did not work and I did some debug. But now I see that naming is vice vera as I thought. But this https://github.com/cakephp/authentication/blob/master/src/Authenticator/JwtAuthenticator.php#L92 is still the issue.

Copy link
Member

@ADmad ADmad May 13, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

JwtAuthenticator should use JwtSubjectIdentifier, you don't need to mess around with the TokenIdentifier.

Scratch that, JwtSubjectIdentifier extends TokenIdentifier and just changes the default config.

Copy link
Member

@ADmad ADmad May 13, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@GrandFelix Switching the use of these config options would be a backwards compatibility break.

Since the issue is about making use of IdentifierInterface::CREDENTIAL_JWT_SUBJECT only JwtAuthenticator needs changes.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes... will fix that. thanks.

$result = json_decode(json_encode($result), true);

$key = IdentifierInterface::CREDENTIAL_JWT_SUBJECT;
$key = $this->getConfig('tokenField', IdentifierInterface::CREDENTIAL_JWT_SUBJECT);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This ties into the question why the config options are being flipped in the identifier... Should this use dataField instead?

@ADmad
Copy link
Member

ADmad commented May 13, 2020

Walking through the JwtAuthenticator I see why a lot of people seem to have problem using it and why they need they need the use of sub key (claim in JWT terms) configurable.

The authenticator has returnPayload option set to true by default which means the decoded JWT claims are directly returned as identity.

The way the JwtAuthenticate of my plugin for Cake 3.x works by default is it uses the value of sub claim of JWT to find user record with that id and use that as user/identity record.

Returning the JWT claims as identity isn't the best default IMO.

@GrandFelix
Copy link
Contributor Author

ok.. I have reverted almost everything except JwtAuthenticator $key definition.

$result = json_decode(json_encode($result), true);

$key = IdentifierInterface::CREDENTIAL_JWT_SUBJECT;
$key = $this->getConfig('dataField', IdentifierInterface::CREDENTIAL_JWT_SUBJECT);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should add the new key in the default config array using the constant for it's default value. Also dataKey might be a better name.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done


$key = IdentifierInterface::CREDENTIAL_JWT_SUBJECT;
if (empty($result[$key])) {
$dataKey = $this->getConfig('dataField');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was suggesting dataKey for the option name, not just the variable name 🙂.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pfff, ok. but this would break backward compatibility? IF anyone have already defined this with the default value of sub, when he will change the value this will not work anymore.

Copy link
Member

@ADmad ADmad May 13, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't really understand what backwards compatibility concern you have. We are adding a new config, so whether it's named dataField or dataKey really doesn't matter.

That's said let's see if anyone else has other suggestions regarding the new key name.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you are right. it looks its too much for today. I have looking at Authentication.JwtSubject config and have messed up. Yes, thats new config. The only concern that is is that all other components have dataField naming. If thats ok I can change to dataKey.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only concern that is is that all other components have dataField naming. If thats ok I can change to dataKey.

While naming consistency is important it should be contextual too. Personally I haven't seem the term "field" used in context of JWT. In JWT parlance the sub key is a "claim". So perhaps something more verbose like identityClaimKey might be ever better.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree. Maybe subjectClaimKey

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

claimKey or subjectKey are shorter names that are inline with other JWT jargon.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be good to add a test for this configuration option to help prevent regressions in the future.

@markstory
Copy link
Member

Looking good. Are you able to update the tests or do you need a hand with that?

@ADmad
Copy link
Member

ADmad commented May 15, 2020

Targeting the PR to next minor would help document this new option better.

@markstory markstory self-assigned this May 16, 2020
@markstory markstory merged commit 0ee1b43 into cakephp:master May 17, 2020
luuk-ou added a commit to luuk-ou/authentication that referenced this pull request Jun 8, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants