@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 PhabricatorKeyring extends Phobject {
4
5 private static $hasReadConfiguration;
6 private static $keyRing = array();
7
8 public static function addKey($spec) {
9 self::$keyRing[$spec['name']] = $spec;
10 }
11
12 public static function getKey($name, $type) {
13 self::readConfiguration();
14
15 if (empty(self::$keyRing[$name])) {
16 throw new Exception(
17 pht(
18 'No key "%s" exists in keyring.',
19 $name));
20 }
21
22 $spec = self::$keyRing[$name];
23
24 $material = base64_decode($spec['material.base64'], true);
25 return new PhutilOpaqueEnvelope($material);
26 }
27
28 public static function getDefaultKeyName($type) {
29 self::readConfiguration();
30
31 foreach (self::$keyRing as $name => $key) {
32 if (!empty($key['default'])) {
33 return $name;
34 }
35 }
36
37 return null;
38 }
39
40 private static function readConfiguration() {
41 if (self::$hasReadConfiguration) {
42 return true;
43 }
44
45 self::$hasReadConfiguration = true;
46
47 foreach (PhabricatorEnv::getEnvConfig('keyring') as $spec) {
48 self::addKey($spec);
49 }
50 }
51
52}