@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
3abstract class PassphraseAbstractKey extends Phobject {
4
5 private $credential;
6
7 protected function requireCredential() {
8 if (!$this->credential) {
9 throw new Exception(pht('Credential is required!'));
10 }
11 return $this->credential;
12 }
13
14 private function loadCredential(
15 $phid,
16 PhabricatorUser $viewer) {
17
18 $credential = id(new PassphraseCredentialQuery())
19 ->setViewer($viewer)
20 ->withPHIDs(array($phid))
21 ->needSecrets(true)
22 ->executeOne();
23
24 if (!$credential) {
25 throw new Exception(pht('Failed to load credential "%s"!', $phid));
26 }
27
28 return $credential;
29 }
30
31 private function validateCredential(
32 PassphraseCredential $credential,
33 $provides_type) {
34
35 $type = $credential->getImplementation();
36
37 if (!$type) {
38 throw new Exception(
39 pht(
40 'Credential "%s" is of unknown type "%s"!',
41 $credential->getMonogram(),
42 $credential->getCredentialType()));
43 }
44
45 if ($type->getProvidesType() !== $provides_type) {
46 throw new Exception(
47 pht(
48 'Credential "%s" must provide "%s", but provides "%s"!',
49 $credential->getMonogram(),
50 $provides_type,
51 $type->getProvidesType()));
52 }
53 }
54
55 protected function loadAndValidateFromPHID(
56 $phid,
57 PhabricatorUser $viewer,
58 $type) {
59
60 $credential = $this->loadCredential($phid, $viewer);
61
62 $this->validateCredential($credential, $type);
63
64 $this->credential = $credential;
65
66 return $this;
67 }
68
69 public function getUsernameEnvelope() {
70 $credential = $this->requireCredential();
71 return new PhutilOpaqueEnvelope($credential->getUsername());
72 }
73
74}