Skip to content

Commit 20bd508

Browse files
author
Martin Brecht-Precht
committed
Added some Authentications.
1 parent 31443d4 commit 20bd508

File tree

3 files changed

+201
-1
lines changed

3 files changed

+201
-1
lines changed
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?php
2+
3+
namespace BasicHttpClient\Request\Authentication;
4+
5+
/**
6+
* Class BasicAuthentication
7+
*
8+
* @package BasicHttpClient\Request\Authentication
9+
*/
10+
class BasicAuthentication implements AuthenticationInterface
11+
{
12+
13+
/**
14+
* @var string
15+
*/
16+
private $username;
17+
18+
/**
19+
* @var string
20+
*/
21+
private $password;
22+
23+
/**
24+
* BasicAuthentication constructor.
25+
*
26+
* @param string $username
27+
* @param string $password
28+
*/
29+
public function __construct($username, $password)
30+
{
31+
32+
$this->username = $username;
33+
$this->password = $password;
34+
}
35+
36+
/**
37+
* @return string
38+
*/
39+
public function getUsername()
40+
{
41+
return $this->username;
42+
}
43+
44+
/**
45+
* @param string $username
46+
* @return $this
47+
*/
48+
public function setUsername($username)
49+
{
50+
$this->username = $username;
51+
return $this;
52+
}
53+
54+
/**
55+
* @return string
56+
*/
57+
public function getPassword()
58+
{
59+
return $this->password;
60+
}
61+
62+
/**
63+
* @param string $password
64+
* @return $this
65+
*/
66+
public function setPassword($password)
67+
{
68+
$this->password = $password;
69+
return $this;
70+
}
71+
72+
/**
73+
* @param resource $curl
74+
* @return $this
75+
*/
76+
public function configureCurl($curl)
77+
{
78+
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
79+
curl_setopt($curl, CURLOPT_USERPWD, $this->username . ':' . $this->password);
80+
return $this;
81+
}
82+
83+
}
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
<?php
2+
3+
namespace BasicHttpClient\Request\Authentication;
4+
5+
use CommonException\IoException\FileReadableException;
6+
7+
/**
8+
* Class ClientCertificateAuthentication
9+
*
10+
* @package BasicHttpClient\Request\Authentication
11+
*/
12+
class ClientCertificateAuthentication implements AuthenticationInterface
13+
{
14+
15+
/**
16+
* @var string
17+
*/
18+
private $caCertificatePath;
19+
/**
20+
* @var string
21+
*/
22+
private $clientCertificatePath;
23+
/**
24+
* @var string
25+
*/
26+
private $clientCertificatePassword;
27+
28+
/**
29+
* ClientCertificateAuthentication constructor.
30+
*
31+
* @param string $caCertificatePath
32+
* @param string $clientCertificatePath
33+
* @param string $clientCertificatePassword
34+
*/
35+
public function __construct($caCertificatePath, $clientCertificatePath, $clientCertificatePassword)
36+
{
37+
$this->setCaCertificatePath($caCertificatePath);
38+
$this->setClientCertificatePath($clientCertificatePath);
39+
$this->setClientCertificatePassword($clientCertificatePassword);
40+
}
41+
42+
/**
43+
* @return mixed
44+
*/
45+
public function getCaCertificatePath()
46+
{
47+
return $this->caCertificatePath;
48+
}
49+
50+
/**
51+
* @param mixed $caCertificatePath
52+
* @return $this
53+
* @throws FileReadableException
54+
*/
55+
public function setCaCertificatePath($caCertificatePath)
56+
{
57+
if (!is_file($caCertificatePath)) {
58+
throw new FileReadableException('CA certificate file not readable.');
59+
}
60+
$this->caCertificatePath = $caCertificatePath;
61+
return $this;
62+
}
63+
64+
/**
65+
* @return mixed
66+
*/
67+
public function getClientCertificatePath()
68+
{
69+
return $this->clientCertificatePath;
70+
}
71+
72+
/**
73+
* @param mixed $clientCertificatePath
74+
* @return $this
75+
* @throws FileReadableException
76+
*/
77+
public function setClientCertificatePath($clientCertificatePath)
78+
{
79+
if (!is_file($clientCertificatePath)) {
80+
throw new FileReadableException('Client certificate file not readable.');
81+
}
82+
$this->clientCertificatePath = $clientCertificatePath;
83+
return $this;
84+
}
85+
86+
/**
87+
* @return mixed
88+
*/
89+
public function getClientCertificatePassword()
90+
{
91+
return $this->clientCertificatePassword;
92+
}
93+
94+
/**
95+
* @param mixed $clientCertificatePassword
96+
* @return $this
97+
*/
98+
public function setClientCertificatePassword($clientCertificatePassword)
99+
{
100+
$this->clientCertificatePassword = $clientCertificatePassword;
101+
return $this;
102+
}
103+
104+
/**
105+
* @param resource $curl
106+
* @return mixed
107+
*/
108+
public function configureCurl($curl)
109+
{
110+
curl_setopt($curl, CURLOPT_CAINFO, $this->caCertificatePath);
111+
curl_setopt($curl, CURLOPT_SSLCERT, $this->clientCertificatePath);
112+
curl_setopt($curl, CURLOPT_SSLCERTPASSWD, $this->clientCertificatePassword);
113+
return $this;
114+
}
115+
116+
}

test.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,10 @@
3737
}
3838
*/
3939

40-
$client = new \BasicHttpClient\JsonHttpClient('https://api-yourapihere-com-98yq3775xff0.runscope.net/path/');
40+
$client = new \BasicHttpClient\BasicHttpClient('https://api-yourapihere-com-98yq3775xff0.runscope.net/path/');
4141
$client
4242
->getRequest()
43+
->addAuthentication(new \BasicHttpClient\Request\Authentication\BasicAuthentication('username', 'password'))
4344
->getMessage()
4445
->addHeader(new \BasicHttpClient\Request\Message\Header\Header('Runscope-Bucket-Auth', array('7a64dde7-74d5-4eed-b170-a2ab406eff08')))
4546
->addCookie(new \BasicHttpClient\Request\Message\Cookie\Cookie('SESSION_ID', 'abc'));

0 commit comments

Comments
 (0)