@recaptime-dev's working patches + fork for Phorge, a community fork of Phabricator. (Upstream dev and stable branches are at upstream/main and upstream/stable respectively.)
hq.recaptime.dev/wiki/Phorge
phorge
phabricator
1<?php
2
3final class PhabricatorOAuthClientTestController
4 extends PhabricatorOAuthClientController {
5
6 public function handleRequest(AphrontRequest $request) {
7 $viewer = $this->getViewer();
8 $id = $request->getURIData('id');
9
10 $client = id(new PhabricatorOAuthServerClientQuery())
11 ->setViewer($viewer)
12 ->withIDs(array($id))
13 ->executeOne();
14 if (!$client) {
15 return new Aphront404Response();
16 }
17
18 $done_uri = $client->getViewURI();
19
20 if ($request->isFormPost()) {
21 $server = id(new PhabricatorOAuthServer())
22 ->setUser($viewer)
23 ->setClient($client);
24
25 // Create an authorization if we don't already have one.
26 $authorization = id(new PhabricatorOAuthClientAuthorizationQuery())
27 ->setViewer($viewer)
28 ->withUserPHIDs(array($viewer->getPHID()))
29 ->withClientPHIDs(array($client->getPHID()))
30 ->executeOne();
31 if (!$authorization) {
32 $scope = array();
33 $authorization = $server->authorizeClient($scope);
34 }
35
36 $access_token = $server->generateAccessToken();
37
38 Javelin::initBehavior('select-on-click');
39 $form = id(new AphrontFormView())
40 ->setViewer($viewer)
41 ->appendInstructions(
42 pht(
43 'Keep this token private, it allows any bearer to access '.
44 'your account on behalf of this application.'))
45 ->appendChild(
46 id(new AphrontFormTextControl())
47 ->setLabel(pht('Token'))
48 ->setReadOnly(true)
49 ->setSigil('select-on-click')
50 ->setHasCopyButton(true)
51 ->setValue($access_token->getToken()));
52
53 return $this->newDialog()
54 ->setTitle(pht('OAuth Access Token'))
55 ->appendForm($form)
56 ->addCancelButton($done_uri, pht('Close'));
57 }
58
59 // TODO: It would be nice to put scope options in this dialog, maybe?
60
61 return $this->newDialog()
62 ->setTitle(pht('Authorize Application?'))
63 ->appendParagraph(
64 pht(
65 'This will create an authorization and OAuth token, permitting %s '.
66 'to access your account.',
67 phutil_tag('strong', array(), $client->getName())))
68 ->addCancelButton($done_uri)
69 ->addSubmitButton(pht('Authorize Application'));
70 }
71}