3 final class PhabricatorAuthManagementRefreshWorkflow
4 extends PhabricatorAuthManagementWorkflow
{
6 protected function didConstruct() {
9 ->setExamples('**refresh**')
12 'Refresh OAuth access tokens. This is primarily useful for '.
13 'development and debugging.'))
19 'help' => pht('Refresh tokens for a given user.'),
24 public function execute(PhutilArgumentParser
$args) {
25 $console = PhutilConsole
::getConsole();
26 $viewer = $this->getViewer();
28 $query = id(new PhabricatorExternalAccountQuery())
30 ->requireCapabilities(
32 PhabricatorPolicyCapability
::CAN_VIEW
,
33 PhabricatorPolicyCapability
::CAN_EDIT
,
36 $username = $args->getArg('user');
37 if (strlen($username)) {
38 $user = id(new PhabricatorPeopleQuery())
40 ->withUsernames(array($username))
43 $query->withUserPHIDs(array($user->getPHID()));
45 throw new PhutilArgumentUsageException(
46 pht('No such user "%s"!', $username));
50 $accounts = $query->execute();
53 throw new PhutilArgumentUsageException(
54 pht('No accounts match the arguments!'));
59 'Found %s account(s) to refresh.',
60 phutil_count($accounts)));
63 $providers = PhabricatorAuthProvider
::getAllEnabledProviders();
64 $providers = mpull($providers, null, 'getProviderConfigPHID');
66 foreach ($accounts as $account) {
70 'Refreshing account #%d.',
73 $config_phid = $account->getProviderConfigPHID();
74 if (empty($providers[$config_phid])) {
77 pht('Skipping, provider is not enabled or does not exist.'));
81 $provider = $providers[$config_phid];
82 if (!($provider instanceof PhabricatorOAuth2AuthProvider
)) {
85 pht('Skipping, provider is not an OAuth2 provider.'));
89 $adapter = $provider->getAdapter();
90 if (!$adapter->supportsTokenRefresh()) {
93 pht('Skipping, provider does not support token refresh.'));
97 $refresh_token = $account->getProperty('oauth.token.refresh');
98 if (!$refresh_token) {
101 pht('Skipping, provider has no stored refresh token.'));
108 'Refreshing token, current token expires in %s seconds.',
110 $account->getProperty('oauth.token.access.expires') - time())));
112 $token = $provider->getOAuthAccessToken($account, $force_refresh = true);
116 pht('Unable to refresh token!'));
123 'Refreshed token, new token expires in %s seconds.',
125 $account->getProperty('oauth.token.access.expires') - time())));
129 $console->writeOut("%s\n", pht('Done.'));