|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Incapsula\Credentials; |
|
6
|
|
|
|
|
7
|
|
|
use Exception; |
|
8
|
|
|
|
|
9
|
|
|
class CredentialProvider |
|
10
|
|
|
{ |
|
11
|
|
|
const ENV_PROFILE = 'INCAPSULA_PROFILE'; |
|
12
|
|
|
|
|
13
|
|
|
const ENV_ID = 'INCAPSULA_API_ID'; |
|
14
|
|
|
|
|
15
|
|
|
const ENV_KEY = 'INCAPSULA_API_KEY'; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Get the default profile, either defined as ENV_PROFILE or |
|
19
|
|
|
* falling back to "default". |
|
20
|
|
|
*/ |
|
21
|
|
|
public static function defaultProfile(): string |
|
22
|
|
|
{ |
|
23
|
|
|
return getenv(self::ENV_PROFILE) ?: 'default'; |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Provider that creates credentials from environment variables. |
|
28
|
|
|
* |
|
29
|
|
|
* @return null|Credentials |
|
30
|
|
|
*/ |
|
31
|
|
|
public static function env() |
|
32
|
|
|
{ |
|
33
|
|
|
$key = getenv(self::ENV_ID); |
|
34
|
|
|
$secret = getenv(self::ENV_KEY); |
|
35
|
|
|
if ($key && $secret) { |
|
36
|
|
|
return new Credentials($key, $secret); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
return null; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Provider that creates credentials using an ini file stored |
|
44
|
|
|
* in the current user's home directory. |
|
45
|
|
|
* |
|
46
|
|
|
* @param null|string $profile Profile to use. If not specified will use |
|
47
|
|
|
* the "default" profile in "~/.incapsula/credentials". |
|
48
|
|
|
* @param null|string $filename if provided, uses a custom filename rather |
|
49
|
|
|
* than looking in the home directory |
|
50
|
|
|
* |
|
51
|
|
|
* @throws Exception |
|
52
|
|
|
*/ |
|
53
|
|
|
public static function ini($profile = null, $filename = null): Credentials |
|
54
|
|
|
{ |
|
55
|
|
|
$filename = $filename ?: sprintf('%s/.incapsula/credentials', self::getHomeDir()); |
|
56
|
|
|
$profile = $profile ?: self::defaultProfile(); |
|
57
|
|
|
|
|
58
|
|
|
if (!is_readable($filename)) { |
|
59
|
|
|
throw new Exception(sprintf('Cannot read credentials from %s', $filename)); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
$data = parse_ini_file($filename, true); |
|
63
|
|
|
if (false === $data) { |
|
64
|
|
|
throw new Exception(sprintf('Invalid credentials file: %s', $filename)); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
if (!isset($data[$profile])) { |
|
68
|
|
|
throw new Exception(sprintf('Profile "%s" not found in credentials file', $profile)); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
if ( |
|
72
|
|
|
!isset($data[$profile]['incapsula_api_id']) |
|
73
|
|
|
|| !isset($data[$profile]['incapsula_api_key']) |
|
74
|
|
|
) { |
|
75
|
|
|
throw new Exception(sprintf('Profile "%s" contains no credentials', $profile)); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
return new Credentials( |
|
79
|
|
|
$data[$profile]['incapsula_api_id'], |
|
80
|
|
|
$data[$profile]['incapsula_api_key'] |
|
81
|
|
|
); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Gets the environment's HOME directory if available. |
|
86
|
|
|
* |
|
87
|
|
|
* @return null|string |
|
88
|
|
|
*/ |
|
89
|
|
|
private static function getHomeDir() |
|
90
|
|
|
{ |
|
91
|
|
|
// On Linux/Unix-like systems, use the HOME environment variable |
|
92
|
|
|
if (getenv('HOME')) { |
|
93
|
|
|
return getenv('HOME'); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
// Get the HOMEDRIVE and HOMEPATH values for Windows hosts |
|
97
|
|
|
$homeDrive = getenv('HOMEDRIVE'); |
|
98
|
|
|
$homePath = getenv('HOMEPATH'); |
|
99
|
|
|
|
|
100
|
|
|
return ($homeDrive && $homePath) ? sprintf('%s%s', $homeDrive, $homePath) : null; |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|