1- <?php declare (strict_types=1 );
2-
3- namespace Pdsinterop \Solid ;
4-
5- class ServerConfig {
6- private $ path ;
7- private $ serverConfig ;
8- private $ userConfig ;
9-
10- public function __construct ($ path ) {
11- $ this ->path = $ path ;
12- $ this ->serverConfigFile = $ this ->path . "serverConfig.json " ;
13- $ this ->userConfigFile = $ this ->path . "user.json " ;
14- $ this ->serverConfig = $ this ->loadConfig ();
15- $ this ->userConfig = $ this ->loadUserConfig ();
16-
17- }
18- private function loadConfig () {
19- if (!file_exists ($ this ->serverConfigFile )) {
20- $ keySet = $ this ->generateKeySet ();
21- $ this ->serverConfig = array (
22- "encryptionKey " => $ keySet ['encryptionKey ' ],
23- "privateKey " => $ keySet ['privateKey ' ]
24- );
25- $ this ->saveConfig ();
26- }
27- return json_decode (file_get_contents ($ this ->serverConfigFile ), true );
28- }
29- private function saveConfig () {
30- file_put_contents ($ this ->serverConfigFile , json_encode ($ this ->serverConfig , JSON_PRETTY_PRINT ));
31- }
32- private function loadUserConfig () {
33- if (!file_exists ($ this ->userConfigFile )) {
34- $ this ->userConfig = array (
35- "allowedClients " => array ()
36- );
37- $ this ->saveUserConfig ();
38- }
39- return json_decode (file_get_contents ($ this ->userConfigFile ), true );
40- }
41- private function saveUserConfig () {
42- file_put_contents ($ this ->userConfigFile , json_encode ($ this ->userConfig , JSON_PRETTY_PRINT ));
43- }
44-
45- /* Server data */
46- public function getPrivateKey () {
47- return $ this ->serverConfig ['privateKey ' ];
48- }
49-
50- public function getEncryptionKey () {
51- return $ this ->serverConfig ['encryptionKey ' ];
52- }
53-
54- public function getClientConfigById ($ clientId ) {
55- $ clients = (array )$ this ->serverConfig ['clients ' ];
56-
57- if (array_key_exists ($ clientId , $ clients )) {
58- return $ clients [$ clientId ];
59- }
60- return null ;
61- }
62-
63- public function saveClientConfig ($ clientConfig ) {
64- $ clientId = uuidv4 ();
65- $ this ->serverConfig ['clients ' ][$ clientId ] = $ clientConfig ;
66- $ this ->saveConfig ();
67- return $ clientId ;
68- }
69-
70- public function saveClientRegistration ($ origin , $ clientData ) {
71- $ originHash = md5 ($ origin );
72- $ existingRegistration = $ this ->getClientRegistration ($ originHash );
73- if ($ existingRegistration && isset ($ existingRegistration ['client_name ' ])) {
74- return $ originHash ;
75- }
76-
77- $ clientData ['client_name ' ] = $ origin ;
78- $ clientData ['client_secret ' ] = md5 (random_bytes (32 ));
79- $ this ->serverConfig ['client- ' . $ originHash ] = $ clientData ;
80- $ this ->saveConfig ();
81- return $ originHash ;
82- }
83-
84- public function getClientRegistration ($ clientId ) {
85- if (isset ($ this ->serverConfig ['client- ' . $ clientId ])) {
86- return $ this ->serverConfig ['client- ' . $ clientId ];
87- } else {
88- return array ();
89- }
90- }
91-
92- /* User specific data */
93- public function getAllowedClients ($ userId ) {
94- return $ this ->userConfig ['allowedClients ' ];
95- }
96-
97- public function addAllowedClient ($ userId , $ clientId ) {
98- $ this ->userConfig ['allowedClients ' ][] = $ clientId ;
99- $ this ->userConfig ['allowedClients ' ] = array_unique ($ this ->userConfig ['allowedClients ' ]);
100- $ this ->saveUserConfig ();
101- }
102- public function removeAllowedClient ($ userId , $ clientId ) {
103- $ this ->userConfig ['allowedClients ' ] = array_diff ($ this ->userConfig ['allowedClients ' ], array ($ clientId ));
104- $ this ->saveUserConfig ();
105- }
106-
107- /* Helper functions */
108- private function generateKeySet () {
109- $ config = array (
110- "digest_alg " => "sha256 " ,
111- "private_key_bits " => 2048 ,
112- "private_key_type " => OPENSSL_KEYTYPE_RSA ,
113- );
114- // Create the private and public key
115- $ key = openssl_pkey_new ($ config );
116-
117- // Extract the private key from $key to $privateKey
118- openssl_pkey_export ($ key , $ privateKey );
119- $ encryptionKey = base64_encode (random_bytes (32 ));
120- $ result = array (
121- "privateKey " => $ privateKey ,
122- "encryptionKey " => $ encryptionKey
123- );
124- return $ result ;
125- }
126- }
127- ?>
1+ <?php declare (strict_types=1 );
2+
3+ namespace Pdsinterop \Solid ;
4+
5+ class ServerConfig {
6+ private $ path ;
7+ private $ serverConfig ;
8+ private $ userConfig ;
9+
10+ public function __construct ($ path ) {
11+ $ this ->path = $ path ;
12+ $ this ->serverConfigFile = $ this ->path . "serverConfig.json " ;
13+ $ this ->userConfigFile = $ this ->path . "user.json " ;
14+ $ this ->serverConfig = $ this ->loadConfig ();
15+ $ this ->userConfig = $ this ->loadUserConfig ();
16+
17+ }
18+
19+ public function getAllowedOrigins ()
20+ {
21+ $ allowedOrigins = [];
22+
23+ $ serverConfig = $ this ->serverConfig ;
24+ foreach ($ serverConfig as $ value ) {
25+ if (isset ($ value ['redirect_uris ' ])) {
26+ foreach ($ value ['redirect_uris ' ] as $ url ) {
27+ $ allowedOrigins [] = parse_url ($ url )['host ' ];
28+ }
29+ }
30+ }
31+
32+ return array_unique ($ allowedOrigins );
33+ }
34+
35+ private function loadConfig () {
36+ if (!file_exists ($ this ->serverConfigFile )) {
37+ $ keySet = $ this ->generateKeySet ();
38+ $ this ->serverConfig = array (
39+ "encryptionKey " => $ keySet ['encryptionKey ' ],
40+ "privateKey " => $ keySet ['privateKey ' ]
41+ );
42+ $ this ->saveConfig ();
43+ }
44+ return json_decode (file_get_contents ($ this ->serverConfigFile ), true );
45+ }
46+ private function saveConfig () {
47+ file_put_contents ($ this ->serverConfigFile , json_encode ($ this ->serverConfig , JSON_PRETTY_PRINT ));
48+ }
49+ private function loadUserConfig () {
50+ if (!file_exists ($ this ->userConfigFile )) {
51+ $ this ->userConfig = array (
52+ "allowedClients " => array ()
53+ );
54+ $ this ->saveUserConfig ();
55+ }
56+ return json_decode (file_get_contents ($ this ->userConfigFile ), true );
57+ }
58+ private function saveUserConfig () {
59+ file_put_contents ($ this ->userConfigFile , json_encode ($ this ->userConfig , JSON_PRETTY_PRINT ));
60+ }
61+
62+ /* Server data */
63+ public function getPrivateKey () {
64+ return $ this ->serverConfig ['privateKey ' ];
65+ }
66+
67+ public function getEncryptionKey () {
68+ return $ this ->serverConfig ['encryptionKey ' ];
69+ }
70+
71+ public function getClientConfigById ($ clientId ) {
72+ $ clients = (array )$ this ->serverConfig ['clients ' ];
73+
74+ if (array_key_exists ($ clientId , $ clients )) {
75+ return $ clients [$ clientId ];
76+ }
77+ return null ;
78+ }
79+
80+ public function saveClientConfig ($ clientConfig ) {
81+ $ clientId = uuidv4 ();
82+ $ this ->serverConfig ['clients ' ][$ clientId ] = $ clientConfig ;
83+ $ this ->saveConfig ();
84+ return $ clientId ;
85+ }
86+
87+ public function saveClientRegistration ($ origin , $ clientData ) {
88+ $ originHash = md5 ($ origin );
89+ $ existingRegistration = $ this ->getClientRegistration ($ originHash );
90+ if ($ existingRegistration && isset ($ existingRegistration ['client_name ' ])) {
91+ return $ originHash ;
92+ }
93+
94+ $ clientData ['client_name ' ] = $ origin ;
95+ $ clientData ['client_secret ' ] = md5 (random_bytes (32 ));
96+ $ this ->serverConfig ['client- ' . $ originHash ] = $ clientData ;
97+ $ this ->saveConfig ();
98+ return $ originHash ;
99+ }
100+
101+ public function getClientRegistration ($ clientId ) {
102+ if (isset ($ this ->serverConfig ['client- ' . $ clientId ])) {
103+ return $ this ->serverConfig ['client- ' . $ clientId ];
104+ } else {
105+ return array ();
106+ }
107+ }
108+
109+ /* User specific data */
110+ public function getAllowedClients ($ userId ) {
111+ return $ this ->userConfig ['allowedClients ' ];
112+ }
113+
114+ public function addAllowedClient ($ userId , $ clientId ) {
115+ $ this ->userConfig ['allowedClients ' ][] = $ clientId ;
116+ $ this ->userConfig ['allowedClients ' ] = array_unique ($ this ->userConfig ['allowedClients ' ]);
117+ $ this ->saveUserConfig ();
118+ }
119+ public function removeAllowedClient ($ userId , $ clientId ) {
120+ $ this ->userConfig ['allowedClients ' ] = array_diff ($ this ->userConfig ['allowedClients ' ], array ($ clientId ));
121+ $ this ->saveUserConfig ();
122+ }
123+
124+ /* Helper functions */
125+ private function generateKeySet () {
126+ $ config = array (
127+ "digest_alg " => "sha256 " ,
128+ "private_key_bits " => 2048 ,
129+ "private_key_type " => OPENSSL_KEYTYPE_RSA ,
130+ );
131+ // Create the private and public key
132+ $ key = openssl_pkey_new ($ config );
133+
134+ // Extract the private key from $key to $privateKey
135+ openssl_pkey_export ($ key , $ privateKey );
136+ $ encryptionKey = base64_encode (random_bytes (32 ));
137+ $ result = array (
138+ "privateKey " => $ privateKey ,
139+ "encryptionKey " => $ encryptionKey
140+ );
141+ return $ result ;
142+ }
143+ }
0 commit comments