Skip to content

Commit 7538b2c

Browse files
committed
Added some composer authentication types
1 parent 1e738f2 commit 7538b2c

File tree

6 files changed

+253
-8
lines changed

6 files changed

+253
-8
lines changed

src/Adapter/Composer.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,4 +219,61 @@ public function addAuthenticationToken(string $url, string $token): void
219219
$token
220220
);
221221
}
222+
223+
public function addGitlabOauthAuthentication(string $token, string $url = 'gitlab.com'): void
224+
{
225+
$this->command(
226+
'composer',
227+
'config',
228+
'--auth',
229+
sprintf('gitlab-oauth.%s', $url),
230+
'token',
231+
$token
232+
);
233+
}
234+
235+
public function addGitlabTokenAuthentication(string $token, string $url = 'gitlab.com'): void
236+
{
237+
$this->command(
238+
'composer',
239+
'config',
240+
'--auth',
241+
sprintf('gitlab-token.%s', $url),
242+
$token
243+
);
244+
}
245+
246+
public function addGithubOauthAuthentication(string $token): void
247+
{
248+
$this->command(
249+
'composer',
250+
'config',
251+
'--auth',
252+
'github-oauth.github.com',
253+
$token
254+
);
255+
}
256+
257+
public function addHttpBasicAuthentication(string $url, string $username, string $password): void
258+
{
259+
$this->command(
260+
'composer',
261+
'config',
262+
'--auth',
263+
sprintf('http-basic.%s', $url),
264+
$username,
265+
$password,
266+
);
267+
}
268+
269+
public function addHttpBearerAuthentication(string $url, string $token): void
270+
{
271+
$this->command(
272+
'composer',
273+
'config',
274+
'--auth',
275+
sprintf('bearer.%s', $url),
276+
$token
277+
);
278+
}
222279
}

src/Adapter/Docker/Factory.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,14 @@ public function __invoke(array $configuration): Configurator\SatelliteBuilderInt
7676

7777
if (\array_key_exists('auth', $configuration['composer']) && (is_countable($configuration['composer']['auth']) ? \count($configuration['composer']['auth']) : 0) > 0) {
7878
foreach ($configuration['composer']['auth'] as $auth) {
79-
$builder->withComposerAuthenticationToken($auth['url'], $auth['token']);
79+
match ($auth['type']) {
80+
'gitlab-oauth' => $builder->withGitlabOauthAuthentication($auth['token'], $auth['url'] ?? 'gitlab.com'),
81+
'gitlab-token' => $builder->withGitlabTokenAuthentication($auth['token'], $auth['url'] ?? 'gitlab.com'),
82+
'github-oauth' => $builder->withGithubOauthAuthentication($auth['token'], $auth['url'] ?? 'github.com'),
83+
'http-basic' => $builder->withHttpBasicAuthentication($auth['url'], $auth['username'], $auth['password']),
84+
'http-bearer' => $builder->withHttpBearerAuthentication($auth['url'], $auth['token']),
85+
default => throw new \LogicException(),
86+
};
8087
}
8188
}
8289
}

src/Adapter/Docker/SatelliteBuilder.php

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,60 @@ public function withComposerAuthenticationToken(string $url, string $auth): self
132132
return $this;
133133
}
134134

135+
public function withGithubOauthAuthentication(string $token, string $url = 'github.com'): self
136+
{
137+
$this->authenticationTokens[$url] = [
138+
'type' => 'github-token',
139+
'url' => $url,
140+
'token' => $token
141+
];
142+
143+
return $this;
144+
}
145+
146+
public function withGitlabOauthAuthentication(string $token, string $url = 'gitlab.com'): self
147+
{
148+
$this->authenticationTokens[$url] = [
149+
'type' => 'gitlab-oauth',
150+
'url' => $url,
151+
'token' => $token
152+
];
153+
154+
return $this;
155+
}
156+
157+
public function withGitlabTokenAuthentication(string $token, string $url = 'gitlab.com'): self
158+
{
159+
$this->authenticationTokens[$url] = [
160+
'type' => 'gitlab-token',
161+
'url' => $url,
162+
'token' => $token
163+
];
164+
165+
return $this;
166+
}
167+
168+
public function withHttpBasicAuthentication(string $url, string $username, string $password): self
169+
{
170+
$this->authenticationTokens[$url] = [
171+
'type' => 'http-basic',
172+
'username' => $username,
173+
'password' => $password,
174+
];
175+
176+
return $this;
177+
}
178+
179+
public function withHttpBearerAuthentication(string $url, string $token): self
180+
{
181+
$this->authenticationTokens[$url] = [
182+
'type' => 'http-bearer',
183+
'token' => $token
184+
];
185+
186+
return $this;
187+
}
188+
135189
public function withTags(string ...$tags): self
136190
{
137191
$this->tags = $tags;
@@ -201,8 +255,15 @@ public function build(): Configurator\SatelliteInterface
201255
}
202256

203257
if (\count($this->authenticationTokens) > 0) {
204-
foreach ($this->authenticationTokens as $url => $token) {
205-
$dockerfile->push(new Dockerfile\PHP\ComposerAuthenticationToken($url, $token));
258+
foreach ($this->authenticationTokens as $url => $authentication) {
259+
match ($authentication['type']) {
260+
'gitlab-oauth' => $dockerfile->push(new Dockerfile\PHP\ComposerGitlabOauthAuthentication($authentication['token'])),
261+
'gitlab-token' => $dockerfile->push(new Dockerfile\PHP\ComposerGitlabTokenAuthentication($authentication['token'])),
262+
'github-oauth' => $dockerfile->push(new Dockerfile\PHP\ComposerGithubOauthAuthentication($authentication['token'])),
263+
'http-basic' => $dockerfile->push(new Dockerfile\PHP\ComposerHttpBasicAuthentication($url, $authentication['username'], $authentication['password'])),
264+
'http-bearer' => $dockerfile->push(new Dockerfile\PHP\ComposerHttpBearerAuthentication($url, $authentication['token'])),
265+
default => new \LogicException(),
266+
};
206267
}
207268
}
208269

src/Adapter/Filesystem/Factory.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,14 @@ public function __invoke(array $configuration): Configurator\SatelliteBuilderInt
5858

5959
if (\array_key_exists('auth', $configuration['composer']) && (is_countable($configuration['composer']['auth']) ? \count($configuration['composer']['auth']) : 0) > 0) {
6060
foreach ($configuration['composer']['auth'] as $auth) {
61-
$builder->withAuthenticationToken($auth['url'], $auth['token']);
61+
match ($auth['type']) {
62+
'gitlab-oauth' => $builder->withGitlabOauthAuthentication($auth['token'], $auth['url'] ?? 'gitlab.com'),
63+
'gitlab-token' => $builder->withGitlabTokenAuthentication($auth['token'], $auth['url'] ?? 'gitlab.com'),
64+
'github-oauth' => $builder->withGithubOauthAuthentication($auth['token'], $auth['url'] ?? 'github.com'),
65+
'http-basic' => $builder->withHttpBasicAuthentication($auth['url'], $auth['username'], $auth['password']),
66+
'http-bearer' => $builder->withHttpBearerAuthentication($auth['url'], $auth['token']),
67+
default => throw new \LogicException(),
68+
};
6269
}
6370
}
6471
}

src/Adapter/Filesystem/SatelliteBuilder.php

Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,62 @@ public function withAuthenticationToken(string $domain, string $auth): self
106106
return $this;
107107
}
108108

109+
public function withGitlabOauthAuthentication(string $token, string $domain = 'gitlab.com'): self
110+
{
111+
$this->authenticationTokens[$domain] = [
112+
'type' => 'gitlab-oauth',
113+
'url' => $domain,
114+
'token' => $token,
115+
];
116+
117+
return $this;
118+
}
119+
120+
public function withGitlabTokenAuthentication(string $token, string $domain = 'gitlab.com'): self
121+
{
122+
$this->authenticationTokens[$domain] = [
123+
'type' => 'gitlab-token',
124+
'url' => $domain,
125+
'token' => $token,
126+
];
127+
128+
return $this;
129+
}
130+
131+
public function withGithubOauthAuthentication(string $token, string $domain = 'github.com'): self
132+
{
133+
$this->authenticationTokens[$domain] = [
134+
'type' => 'github-oauth',
135+
'url' => $domain,
136+
'token' => $token,
137+
];
138+
139+
return $this;
140+
}
141+
142+
public function withHttpBasicAuthentication(string $domain, string $username, string $password): self
143+
{
144+
$this->authenticationTokens[$domain] = [
145+
'type' => 'http-basic',
146+
'url' => $domain,
147+
'username' => $username,
148+
'password' => $password,
149+
];
150+
151+
return $this;
152+
}
153+
154+
public function withHttpBearerAuthentication(string $domain, string $token): self
155+
{
156+
$this->authenticationTokens[$domain] = [
157+
'type' => 'http-basic',
158+
'url' => $domain,
159+
'token' => $token,
160+
];
161+
162+
return $this;
163+
}
164+
109165
public function build(): Configurator\SatelliteInterface
110166
{
111167
if (!file_exists($this->workdir)) {
@@ -153,8 +209,15 @@ public function build(): Configurator\SatelliteInterface
153209
}
154210

155211
if (\count($this->authenticationTokens) > 0) {
156-
foreach ($this->authenticationTokens as $url => $token) {
157-
$composer->addAuthenticationToken($url, $token);
212+
foreach ($this->authenticationTokens as $url => $authentication) {
213+
match ($authentication['type']) {
214+
'gitlab-oauth' => $composer->addGitlabOauthAuthentication($authentication['token']),
215+
'gitlab-token' => $composer->addGitlabTokenAuthentication($authentication['token']),
216+
'github-oauth' => $composer->addGithubOauthAuthentication($authentication['token']),
217+
'http-basic' => $composer->addHttpBasicAuthentication($url, $authentication['username'], $authentication['password']),
218+
'http-bearer' => $composer->addHttpBearerAuthentication($url, $authentication['token']),
219+
default => $composer->addAuthenticationToken($url, $authentication['token']),
220+
};
158221
}
159222
}
160223

src/Feature/Composer/Configuration.php

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,64 @@ public function getConfigTreeBuilder(): TreeBuilder
7171
->end()
7272
->arrayNode('auth')
7373
->arrayPrototype()
74+
->beforeNormalization()
75+
->always(function ($value) {
76+
if (isset($value['url']) && isset($value['token']) && !isset($value['type'])) {
77+
$value['type'] = 'http-basic';
78+
$value['url'] = substr($value['url'], strpos($value['url'], 'http-basic.') + strlen('http-basic.'));
79+
$value['username'] = $value['username'] ?? 'token';
80+
$value['password'] = $value['password'] ?? $value['token'];
81+
unset($value['token']);
82+
}
83+
84+
return $value;
85+
})
86+
->end()
87+
->validate()
88+
->always(function ($v) {
89+
switch ($v['type']) {
90+
case 'http-basic':
91+
if (empty($v['url']) || empty($v['username']) || empty($v['password'])) {
92+
throw new \InvalidArgumentException('For http-basic auth, url, username, and password are required.');
93+
}
94+
break;
95+
case 'http-bearer':
96+
if (empty($v['url']) || empty($v['token'])) {
97+
throw new \InvalidArgumentException('For http-bearer auth, url and token are required.');
98+
}
99+
break;
100+
default:
101+
if (empty($v['token'])) {
102+
throw new \InvalidArgumentException('For gitlab-oauth, gitlab-token or github-oauth, only token is required and url is optional.');
103+
}
104+
break;
105+
}
106+
return $v;
107+
})
108+
->end()
74109
->children()
75-
->scalarNode('url')->isRequired()->end()
110+
->enumNode('type')
111+
->isRequired()
112+
->values(['http-basic', 'http-bearer', 'gitlab-oauth', 'gitlab-token', 'github-oauth'])
113+
->end()
114+
->scalarNode('url')->end()
76115
->scalarNode('token')
77116
->validate()
78117
->ifTrue(isExpression())
79118
->then(asExpression())
80119
->end()
81-
->isRequired()
120+
->end()
121+
->scalarNode('username')
122+
->validate()
123+
->ifTrue(isExpression())
124+
->then(asExpression())
125+
->end()
126+
->end()
127+
->scalarNode('password')
128+
->validate()
129+
->ifTrue(isExpression())
130+
->then(asExpression())
131+
->end()
82132
->end()
83133
->end()
84134
->end()

0 commit comments

Comments
 (0)