Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[Security] Add a JSON authentication listener#18952

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Closed

Conversation

@dunglas
Copy link
Member

@dunglasdunglas commentedJun 2, 2016
edited
Loading

QA
Branch?master
Bug fix?no
New feature?yes
BC breaks?no
Deprecations?no
Tests pass?yes
Fixed ticketsn/a
LicenseMIT
Doc PRsymfony/symfony-docs#7081

Add a new authentication listener allowing to login by sending a JSON document like:

{"_username": "dunglas", "_password": "foo"}.

It is similar to the traditional form login (but take a JSON document as entry) and is convenient for APIs, especially used in combination with JWT.

Seeapi-platform/core#563 andlexik/LexikJWTAuthenticationBundle#123 (comment) for previous discussions.

  • Add functional tests in security bundle

eXtreme, polc, jrjohnson, chalasr, sstok, Shine-neko, SofHad, ogizanagi, DavidBadura, theofidry, and 8 more reacted with thumbs up emoji
@chalasr
Copy link
Member

👍 A great help for JSON APis!

@GromNaN
Copy link
Member

GromNaN commentedJun 2, 2016
edited
Loading

Unless you have to pass the JWT and the login/password in the same request, the standard way for password authentication is HTTP Basic. This is supported out of the box in Symfony and easy to do withjQuery.ajax.
Then a simple controller that return a new JWT can be created as an entry point.

teohhanhui reacted with thumbs up emoji

@chalasr
Copy link
Member

chalasr commentedJun 2, 2016
edited
Loading

@GromNaN I agree about that HTTP Basic and form-data stay the standard way for getting a JWT (& simpler for most cases).
But that's not an absolute rule and, depending on the client's platform/language/framework or simply out of preference, JSON can be the way to go.
Also I think that this could be helpful for a large number of use cases therefore good to have in Symfony

teohhanhui and alexdee2007 reacted with thumbs up emoji

@teohhanhui
Copy link
Contributor

Does this makehttps://github.com/gfreeau/GfreeauGetJWTBundle obsolete?

@dunglas
Copy link
MemberAuthor

dunglas commentedJun 3, 2016
edited
Loading

@teohhanhui AFAIK, GfreeauGetJWTBundle is for x-www-formencoded data, not for JSON documents.
@GromNaN it's more common to send JSON documents (or form data) than using HTTP Basic auth to get a JWT token. Most client-side and server-side libs work that way.

@teohhanhui
Copy link
Contributor

teohhanhui commentedJun 3, 2016
edited
Loading

@dunglas:

GfreeauGetJWTBundle is for x-www-formencoded data, not for JSON documents

But the usual intention is to avoidform_login, which uses cookies. The fact that it usesx-www-form-urlencoded seems to me simply due to ease of implementation (the code seems to be adapted from Symfony'sUsernamePasswordFormAuthenticationListener).

@jorge07
Copy link

Why not a Request body listener like FOSRestBundle has? But that way you can use also XML for example... I'm already using exactly this way to login with Json on 3.0.6 with FOSRestBundle 2

teohhanhui reacted with thumbs up emojichalasr, dunglas, polc, and sstok reacted with thumbs down emoji

throw new BadCredentialsException(sprintf('Missing key "%s".', $this->options['password_parameter']));
}

$username = $data[$this->options['username_parameter']];
Copy link
Member

Choose a reason for hiding this comment

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

What about using property paths here? One might not always have the required information on the top level, but may want to wrap it in some other structure.

@dunglas
Copy link
MemberAuthor

dunglas commentedJun 8, 2016
edited
Loading

I've done some changes to this PR:

  • The provider is now stateless (it doesn't use the session)
  • It uses the PropertyAccess component to access the username and the password as brilliantly suggested by@xabbuh
  • Functional tests added

It is ready for review.

chalasr, DavidBadura, Simperfit, and alexdee2007 reacted with thumbs up emoji

{
public function loginCheckAction()
{
throw new \RuntimeException('loginAction() should never be called.');
Copy link
Member

Choose a reason for hiding this comment

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

Should beloginCheckAction() should never be called..

dunglas reacted with thumbs up emoji
Copy link
Contributor

Choose a reason for hiding this comment

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

could make use of__FUNCTION__ actually

@Simperfit
Copy link
Contributor

LGTM

@xabbuh
Copy link
Member

The min version in thecomposer.json file of the SecurityBundle needs to be updated.

@dunglas Can you rebase here to hopefully let Travis run the job on PHP 7 again.

@dunglasdunglasforce-pushed thejson_authentication_listener branch from8475884 tocd86c9eCompareJuly 10, 2016 08:33
@dunglas
Copy link
MemberAuthor

Rebased against master and comments fixed.

@dunglasdunglasforce-pushed thejson_authentication_listener branch from163a36a to653bebbCompareJuly 17, 2016 10:02
@dunglas
Copy link
MemberAuthor

ping @symfony/deciders (HHVM error not related)

@xabbuh
Copy link
Member

xabbuh commentedJul 18, 2016
edited
Loading

Imo you should also add a functional test case covering a config where theusername_path andpassword_path are actually used.

@dunglas
Copy link
MemberAuthor

@xabbuh test updated.

@dunglasdunglasforce-pushed thejson_authentication_listener branch from5c085a6 toee40718CompareOctober 25, 2016 11:05
public function handle(GetResponseEvent $event)
{
$request = $event->getRequest();
$data = json_decode($request->getContent());

Choose a reason for hiding this comment

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

Should we protect here ourselves by checking the max. length of the request content in the same way that we check the max username (Security::MAX_USERNAME_LENGTH) and password a bit later?

Copy link
MemberAuthor

@dunglasdunglasOct 25, 2016
edited
Loading

Choose a reason for hiding this comment

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

@javiereguiluz but what is the max length of the request? The login and password can be stored in any JSON document. It would be similar to checking the length of the form in the traditional login form and AFAIK we don't do it currently.

Copy link
Member

@javiereguiluzjaviereguiluzOct 25, 2016
edited
Loading

Choose a reason for hiding this comment

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

I'm not an expert about this topic. I just saw thisjson_decode() that accepts the raw user's input and I was thinking about sending a 100MB request to make the application explode ;)

Copy link
MemberAuthor

Choose a reason for hiding this comment

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

I'm not a security expert either, but it's the same for all endpoints in this case :) There are some configurations in PHP to avoid such problems (likepost_max_size,max_execution_time andmemory_limit). The check on the username length isn't related to DDOS attacks.

javiereguiluz and jvasseur reacted with thumbs up emoji
Copy link
MemberAuthor

Choose a reason for hiding this comment

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

@dunglas
Copy link
MemberAuthor

Docs addedsymfony/symfony-docs#7081

}

if (!is_string($username)) {
throw new BadCredentialsException(sprintf('The key "%s" must contain a string.', $this->options['username_path']));
Copy link
Member

Choose a reason for hiding this comment

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

The key "%s" must be a string.?

}

if (!is_string($password)) {
throw new BadCredentialsException(sprintf('The key "%s" must contain a string.', $this->options['password_path']));
Copy link
Member

Choose a reason for hiding this comment

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

The key "%s" must be a string.?

try {
$username = $this->propertyAccessor->getValue($data, $this->options['username_path']);
} catch (AccessException $e) {
throw new BadCredentialsException(sprintf('Missing key "%s".', $this->options['username_path']));
Copy link
Member

Choose a reason for hiding this comment

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

The "%s" key must be provided.?

try {
$password = $this->propertyAccessor->getValue($data, $this->options['password_path']);
} catch (AccessException $e) {
throw new BadCredentialsException(sprintf('Missing key "%s".', $this->options['password_path']));
Copy link
Member

Choose a reason for hiding this comment

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

The "%s" key must be provided.?

@dunglasdunglasforce-pushed thejson_authentication_listener branch fromee40718 to8fd5666CompareNovember 2, 2016 14:54
@fabpot
Copy link
Member

👍 for 3.3

@javiereguiluzjaviereguiluz added this to the3.3 milestoneNov 6, 2016
@dunglasdunglasforce-pushed thejson_authentication_listener branch from8fd5666 toe0bb4e4CompareDecember 2, 2016 08:22
@dunglasdunglasforce-pushed thejson_authentication_listener branch frome0bb4e4 to10ecbe1CompareDecember 2, 2016 08:27
@fabpot
Copy link
Member

Thank you@dunglas.

chalasr, andreybolonin, and alexdee2007 reacted with hooray emoji

@fabpotfabpot closed thisDec 3, 2016
fabpot added a commit that referenced this pull requestDec 3, 2016
This PR was squashed before being merged into the 3.3-dev branch (closes#18952).Discussion----------[Security] Add a JSON authentication listener| Q | A || --- | --- || Branch? | master || Bug fix? | no || New feature? | yes || BC breaks? | no || Deprecations? | no || Tests pass? | yes || Fixed tickets | n/a || License | MIT || Doc PR |symfony/symfony-docs#7081 |Add a new authentication listener allowing to login by sending a JSON document like: `{"_username": "dunglas", "_password": "foo"}`.It is similar to the traditional form login (but take a JSON document as entry) and is convenient for APIs, especially used in combination with JWT.Seeapi-platform/core#563 andlexik/LexikJWTAuthenticationBundle#123 (comment) for previous discussions.- [x] Add functional tests in security bundleCommits-------02178bc [Security] Add a JSON authentication listener
@dunglasdunglas deleted the json_authentication_listener branchDecember 3, 2016 14:23
xabbuh added a commit to symfony/symfony-docs that referenced this pull requestDec 14, 2016
This PR was merged into the master branch.Discussion----------JSON authentication listener docsDocs forsymfony/symfony#18952.Commits-------b192ab3 Fixed a syntax issue16ae3f6 Reworded and simplified the article820f28e Fixed the name of the "username" property6d018f6 Fixed a syntax errorab5259b Removed a tip which is not too relevant for the articled2dd895 Fixed the JSON format (this time for real)6ff24da Fixed the JSON format0961128 Show the simple example first and then explain the complex use case68bd9a5 JSON authentication listener docs
@nicolas-grekasnicolas-grekas modified the milestones:3.x,3.3Mar 24, 2017
@fabpotfabpot mentioned this pull requestMay 1, 2017
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@fabpotfabpotfabpot left review comments

@javiereguiluzjaviereguiluzjaviereguiluz left review comments

Assignees

No one assigned

Projects

None yet

Milestone

3.3

Development

Successfully merging this pull request may close these issues.

13 participants

@dunglas@chalasr@GromNaN@teohhanhui@jorge07@Simperfit@xabbuh@DavidBadura@fabpot@javiereguiluz@theofidry@nicolas-grekas@carsonbot

[8]ページ先頭

©2009-2025 Movatter.jp