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

Commitd981a14

Browse files
committed
[AccessToken] allow user to set a default token lifetime via credentials url
1 parent6da39e5 commitd981a14

11 files changed

+50
-11
lines changed

‎src/Symfony/Component/AccessToken/AccessToken.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,25 @@ class AccessToken implements AccessTokenInterface
2121
*/
2222
publicconstIN_MEMORY ='in_memory';
2323

24+
/**
25+
* Default lifetime in use when the remote service does not expose the
26+
* token lifetime.
27+
*
28+
* @see CredentialsInterface::getDefaultLifetime() for overriding this value.
29+
*/
30+
publicconstDEFAULT_LIFETIME =600;
31+
2432
protected ?\DateTimeImmutable$expiresAt;
2533
protected ?bool$hasExpired =null;
2634

2735
/**
28-
* @param string $id Identifier of credentials used for generating it
36+
* @param string $id Identifier of credentials used for generating it
37+
* @param int $expiresIn Access token lifetime in seconds.
2938
*/
3039
publicfunction__construct(
3140
protectedreadonlystring$value,
3241
protectedreadonlystring$type ='Bearer',
33-
protectedreadonlyint$expiresIn =600,
42+
protectedreadonlyint$expiresIn =self::DEFAULT_LIFETIME,
3443
protectedreadonly\DateTimeImmutable$issuedAt =new \DateTimeImmutable(),
3544
protectedreadonlystring$id =self::IN_MEMORY,
3645
) {

‎src/Symfony/Component/AccessToken/Bridge/OAuth/AbstractOAuthCredentials.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ abstract class AbstractOAuthCredentials extends AbstractCredentials
2929
publicfunction__construct(
3030
#[\SensitiveParameter]protectedreadonly ?string$tenant =null,
3131
protectedreadonly ?string$endpoint =null,
32+
?int$defaultLifetime =null,
3233
) {
34+
parent::__construct($defaultLifetime);
3335
}
3436

3537
/**

‎src/Symfony/Component/AccessToken/Bridge/OAuth/ClientCredentials.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,9 @@ public function __construct(
3535
#[\SensitiveParameter] ?string$tenant =null,
3636
string|array|null$scope =null,
3737
?string$endpoint =null,
38+
?int$defaultLifetime =null,
3839
) {
39-
parent::__construct($tenant,$endpoint);
40+
parent::__construct($tenant,$endpoint,$defaultLifetime);
4041

4142
$this->scope =\is_string($scope) ?array_filter(explode('',$scope)) :$scope;
4243
}

‎src/Symfony/Component/AccessToken/Bridge/OAuth/ClientCredentialsProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ protected function parseResponse(ClientCredentials $credentials, string $body):
139139
returnnewAccessToken(
140140
value:$data['access_token'],
141141
type:$data['token_type'] ??'Bearer',
142-
expiresIn: (int) ($data['expires_in'] ??600),
142+
expiresIn: (int) ($data['expires_in'] ??$credentials->getDefaultLifetime()),
143143
id:$credentials->getId(),
144144
);
145145
}

‎src/Symfony/Component/AccessToken/Bridge/OAuth/OAuthFactory.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ public function createCredentials(Dsn $dsn): CredentialsInterface
4040
clientSecret:$clientSecret,
4141
tenant:$dsn->getOption('tenant'),
4242
scope:$dsn->getOption('scope'),
43-
endpoint:$dsn->toEndpointUrl(['grant_type','client_id','client_secret','tenant','scope']),
43+
endpoint:$dsn->toEndpointUrl(['grant_type','client_id','client_secret','tenant','scope','default_lifetime']),
44+
defaultLifetime: (int)$dsn->getOption('default_lifetime'),
4445
);
4546
}
4647

@@ -61,7 +62,8 @@ public function createCredentials(Dsn $dsn): CredentialsInterface
6162
clientSecret:$clientSecret,
6263
tenant:$dsn->getOption('tenant'),
6364
scope:$dsn->getOption('scope'),
64-
endpoint:$dsn->toEndpointUrl(['grant_type','refresh_token','client_id','client_secret','tenant','scope']),
65+
endpoint:$dsn->toEndpointUrl(['grant_type','refresh_token','client_id','client_secret','tenant','scope','default_lifetime']),
66+
defaultLifetime: (int)$dsn->getOption('default_lifetime'),
6567
);
6668
}
6769

‎src/Symfony/Component/AccessToken/Bridge/OAuth/RefreshTokenCredentials.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,9 @@ public function __construct(
3737
#[\SensitiveParameter] ?string$tenant =null,
3838
string|array|null$scope =null,
3939
?string$endpoint =null,
40+
?int$defaultLifetime =null,
4041
) {
41-
parent::__construct($tenant,$endpoint);
42+
parent::__construct($tenant,$endpoint,$defaultLifetime);
4243

4344
$this->scope =\is_string($scope) ?array_filter(explode('',$scope)) :$scope;
4445
}

‎src/Symfony/Component/AccessToken/Bridge/OAuth/RefreshTokenProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ protected function parseResponse(RefreshTokenCredentials $credentials, string $b
139139
returnnewAccessToken(
140140
value:$data['access_token'],
141141
type:$data['token_type'] ??'Bearer',
142-
expiresIn: (int) ($data['expires_in'] ??600),
142+
expiresIn: (int) ($data['expires_in'] ??$credentials->getDefaultLifetime()),
143143
id:$credentials->getId(),
144144
);
145145
}

‎src/Symfony/Component/AccessToken/Credentials/AbstractCredentials.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespaceSymfony\Component\AccessToken\Credentials;
1313

14+
useSymfony\Component\AccessToken\AccessToken;
1415
useSymfony\Component\AccessToken\CredentialsInterface;
1516

1617
/**
@@ -25,8 +26,18 @@ abstract class AbstractCredentials implements CredentialsInterface
2526
*/
2627
protectedabstractfunctioncomputeId():string;
2728

29+
publicfunction__construct(
30+
private ?int$defaultLifetime =null
31+
) {
32+
}
33+
2834
publicfunctiongetId():string
2935
{
3036
return$this->id ??=$this->computeId();
3137
}
38+
39+
publicfunctiongetDefaultLifetime():int
40+
{
41+
return$this->defaultLifetime ?? AccessToken::DEFAULT_LIFETIME;
42+
}
3243
}

‎src/Symfony/Component/AccessToken/Credentials/BasicAuthCredentials.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@ class BasicAuthCredentials extends AbstractCredentials
2121
publicfunction__construct(
2222
privatereadonlystring$username,
2323
privatereadonly ?string$password =null,
24-
) {}
24+
?int$defaultLifetime =null,
25+
) {
26+
parent::__construct($defaultLifetime);
27+
}
2528

2629
publicfunctiongetUsername():string
2730
{

‎src/Symfony/Component/AccessToken/CredentialsInterface.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,12 @@ interface CredentialsInterface
2929
* order to avoid cache pollution.
3030
*/
3131
publicfunctiongetId():string;
32+
33+
/**
34+
* Get default lifetime for this credentials.
35+
*
36+
* When the remote service does not give any information about token
37+
* lifetime, the value here will be used.
38+
*/
39+
publicfunctiongetDefaultLifetime():int;
3240
}

‎src/Symfony/Component/AccessToken/Tests/Unit/Bridge/OAuth/OAuthFactoryTest.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public function testClientCredentials(): void
5757
{
5858
$factory =newOAuthFactory();
5959

60-
$uri ='oauth://example.tld/oauth?grant_type=client_credentials&client_id=fooId&client_secret=fooSecret&tenant=fooTenant&scope=fooScope%20barScope';
60+
$uri ='oauth://example.tld/oauth?grant_type=client_credentials&client_id=fooId&client_secret=fooSecret&tenant=fooTenant&scope=fooScope%20barScope&default_lifetime=12';
6161
$credentials =$factory->createCredentials(Dsn::fromString($uri));
6262
\assert($credentialsinstanceof ClientCredentials);
6363

@@ -67,6 +67,7 @@ public function testClientCredentials(): void
6767
self::assertSame('fooSecret',$credentials->getClientSecret());
6868
self::assertSame('fooTenant',$credentials->getTenant());
6969
self::assertSame(['fooScope','barScope'],$credentials->getScope());
70+
self::assertSame(12,$credentials->getDefaultLifetime());
7071
}
7172

7273
publicfunctiontestClientCredentialsUserPassFallbackOnBasicAuth():void
@@ -105,7 +106,7 @@ public function testRefreshTokenCredentials(): void
105106
{
106107
$factory =newOAuthFactory();
107108

108-
$uri ='oauth://example.tld/oauth?grant_type=refresh_token&refresh_token=the_token&client_id=fooId&client_secret=fooSecret&tenant=fooTenant&scope=fooScope%20barScope';
109+
$uri ='oauth://example.tld/oauth?grant_type=refresh_token&refresh_token=the_token&client_id=fooId&client_secret=fooSecret&tenant=fooTenant&scope=fooScope%20barScope&default_lifetime=12';
109110
$credentials =$factory->createCredentials(Dsn::fromString($uri));
110111
\assert($credentialsinstanceof RefreshTokenCredentials);
111112

@@ -115,6 +116,7 @@ public function testRefreshTokenCredentials(): void
115116
self::assertSame('fooId',$credentials->getClientId());
116117
self::assertSame('fooSecret',$credentials->getClientSecret());
117118
self::assertSame('fooTenant',$credentials->getTenant());
119+
self::assertSame(12,$credentials->getDefaultLifetime());
118120
}
119121

120122
publicfunctiontestRefreshTokenCredentialsBareMinimum():void

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp