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