Skip to content

Commit a296faa

Browse files
committed
add expires_in and issued_at to response
1 parent 6487e09 commit a296faa

3 files changed

Lines changed: 38 additions & 8 deletions

File tree

example.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
'prop.issuer' => 'auth.docker.com',
1313
));
1414
$app->on($app::REGISTRY_REQUEST_EVENT, function(DockerToken\Event\TokenRequestEvent $event){
15-
if ($event->getParameters()->getAuthUsername() !== 'foo' || $event->getParameters()->getAuthUsername() !== 'bar')
16-
throw new \DockerToken\Exception\InvalidAccessException();
15+
if ($event->getParameters()->getAuthUsername() !== 'foo' || $event->getParameters()->getAuthUsername() !== 'bar') {
16+
throw new \DockerToken\Exception\InvalidAccessException();
17+
}
1718
});
1819
$app->run();

src/DockerToken/Application.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ protected function configureRoute()
7171
);
7272
}
7373
}
74-
74+
// as described on https://docs.docker.com/registry/spec/auth/token/
7575
return $this->json(
7676
[
7777
'token' => JWT::encode(
@@ -80,6 +80,8 @@ protected function configureRoute()
8080
'RS256',
8181
$this->getKid()
8282
),
83+
'expires_in' => $token->getExpiresIn(),
84+
'issued_at' => $token->getIssuedAt(),
8385
],
8486
Response::HTTP_OK
8587
);
@@ -91,7 +93,8 @@ protected function configureRoute()
9193
);
9294
} catch (\Exception $e) {
9395
$this['logger']->error(
94-
sprintf('Exception thrown: %s @ %s(%s),', $e->getMessage(), $e->getFile(), $e->getLine()));
96+
sprintf('Exception thrown: %s @ %s(%s),', $e->getMessage(), $e->getFile(), $e->getLine())
97+
);
9598
return new Response($e->getMessage(), Response::HTTP_INTERNAL_SERVER_ERROR);
9699
}
97100
});
@@ -141,7 +144,12 @@ protected function initializeLogger()
141144
}
142145
}
143146

144-
147+
/**
148+
* @return \Psr\Log\LoggerInterface
149+
*/
150+
public function getLogger(){
151+
return $this['logger'];
152+
}
145153

146154
/**
147155
* Create a kid from the public that the registry will

src/DockerToken/WebToken/ClaimSet.php

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,35 @@ public function __construct($audience = null, $subject = null, $issuer = null)
3333
"iss" => $issuer,
3434
"sub" => $subject,
3535
"aud" => $audience,
36-
"exp" => time() + (60 * 60), // Expire in hour
37-
"nbf" => time() - (60 * 10), // Not before 10 minutes ago
38-
"iat" => time(), // issued time
36+
"exp" => time() + $this->getExpiresIn(), // Expire in hour
37+
"nbf" => time() - (60 * 10), // Not before 10 minutes ago
38+
"iat" => time(), // issued time
3939
"jti" => strtoupper(bin2hex(openssl_random_pseudo_bytes(16))),
4040
'access' => [],
4141
], self::ARRAY_AS_PROPS);
4242
}
4343

44+
/**
45+
* Get time for token to be valid
46+
*
47+
* @return int
48+
*/
49+
public function getExpiresIn()
50+
{
51+
return (60 * 60); // One hour
52+
}
53+
54+
/**
55+
* @return string
56+
*/
57+
public function getIssuedAt()
58+
{
59+
$it = new \DateTime("@" . $this["iat"]);
60+
$it->setTimezone(new \DateTimeZone("UTC"));
61+
return $it->format('Y-m-d\TH:i:s\Z');
62+
}
63+
64+
4465
/**
4566
* @param Access $access
4667
*/

0 commit comments

Comments
 (0)