Skip to content
This repository was archived by the owner on Mar 7, 2025. It is now read-only.

Commit 05bbfa9

Browse files
committed
Certificates management
1 parent cca939d commit 05bbfa9

14 files changed

+996
-4
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
88
### Added
99
- Configuration (nginx and ENV) files management;
1010
- Recipes management;
11+
- SSL Certificates management.
1112

1213
## [1.0.1] - 2017-03-14
1314
### Added

docs/certificates.md

+150
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
# Overview
2+
3+
SDK provides Certificates Manager that allows you to create, list, clone, install, activate, delete certificates and obtain new Let's Encrypt certificate for your Forge sites.
4+
5+
# Usage
6+
7+
Documentation assumes that you've already retrieved site instance from `SitesManager` class.
8+
9+
All operations are performed via `Laravel\Forge\Certificates\CertificatesManager` instance.
10+
11+
All methods return either instance of `Laravel\Forge\Certificates\Certificate` or array of `Laravel\Forge\Certificates\Certificate` instances.
12+
13+
## Create new certificate
14+
15+
```php
16+
<?php
17+
18+
use Laravel\Forge\Certificates\CertificatesManager;
19+
20+
$certificates = new CertificatesManager();
21+
22+
$certificate = $certificates->create('example.org')
23+
->ownedBy('My Company Name')
24+
->locatedAt('US', 'NY', 'New York')
25+
->assignedTo('Department Name')
26+
->on($site);
27+
```
28+
29+
## Installing an existed certificate
30+
31+
```php
32+
<?php
33+
34+
use Laravel\Forge\Certificates\CertificatesManager;
35+
36+
$certificates = new CertificatesManager();
37+
38+
$certificate = $certificates->install('private-key', 'certificate-content')
39+
->on($site);
40+
```
41+
42+
## Cloning an existed certificate
43+
44+
```php
45+
<?php
46+
47+
use Laravel\Forge\Certificates\CertificatesManager;
48+
49+
$certificates = new CertificatesManager();
50+
51+
$certificateId = 1234;
52+
$certificate = $certificates->clone($certificateId)->on($site);
53+
```
54+
55+
## Obtain a Let's Encrypt certificate
56+
57+
```php
58+
<?php
59+
60+
use Laravel\Forge\Certificates\CertificatesManager;
61+
62+
$certificates = new CertificatesManager();
63+
64+
$domains = ['example.org', 'www.example.org'];
65+
$certificate = $certificates->obtain($domains)->on($site);
66+
```
67+
68+
## List certificates
69+
70+
```php
71+
<?php
72+
73+
use Laravel\Forge\Certificates\CertificatesManager;
74+
75+
$certificates = new CertificatesManager();
76+
77+
$siteCertificates = $certificates->list()->from($site);
78+
```
79+
80+
## Get single certificate by ID
81+
82+
```php
83+
<?php
84+
85+
use Laravel\Forge\Certificates\CertificatesManager;
86+
87+
$certificates = new CertificatesManager();
88+
89+
$certificateId = 1234;
90+
$certificate = $certificates->get($certificateId)->from($site);
91+
```
92+
93+
## Get Signing Request
94+
95+
This method returns the full certificate signing request content.
96+
97+
```php
98+
<?php
99+
100+
use Laravel\Forge\Certificates\CertificatesManager;
101+
102+
$certificates = new CertificatesManager();
103+
$certificate = $certificates->get(1234)->from($site);
104+
105+
$csr = $certificate->csr();
106+
```
107+
108+
## Install certificate
109+
110+
```php
111+
<?php
112+
113+
use Laravel\Forge\Certificates\CertificatesManager;
114+
115+
$certificates = new CertificatesManager();
116+
$certificate = $certificates->get(1234)->from($site);
117+
118+
$certificateContent = 'content-here';
119+
$addIntermediates = false;
120+
121+
$result = $certificate->install($certificateContent, $addIntermediates);
122+
```
123+
124+
## Activate certificate
125+
126+
```php
127+
<?php
128+
129+
use Laravel\Forge\Certificates\CertificatesManager;
130+
131+
$certificates = new CertificatesManager();
132+
$certificate = $certificates->get(1234)->from($site);
133+
134+
$result = $certificate->activate();
135+
```
136+
137+
## Delete certificate
138+
139+
```php
140+
<?php
141+
142+
use Laravel\Forge\Certificates\CertificatesManager;
143+
144+
$certificates = new CertificatesManager();
145+
$certificate = $certificates->get(1234)->from($site);
146+
147+
$result = $certificate->delete();
148+
```
149+
150+
[Back to Table of Contents](./readme.md)

docs/getting-started.md

+1-4
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,7 @@ This SDK supports following Forge API features:
1919
- Firewall rules;
2020
- Scheduled jobs;
2121
- Workers;
22-
- Recipes.
23-
24-
## Coming soon
25-
22+
- Recipes;
2623
- SSL certificates management.
2724

2825
# Requirements

docs/readme.md

+1
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,4 @@
1717
+ [Deployment](./deployment.md)
1818
+ [Workers](./workers.md)
1919
- [Recipes](./recipes.md)
20+
- [SSL Certificates](./certificates.md)

src/Certificates/Certificate.php

+142
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
<?php
2+
3+
namespace Laravel\Forge\Certificates;
4+
5+
use Laravel\Forge\ApiResource;
6+
7+
class Certificate extends ApiResource
8+
{
9+
/**
10+
* Resource type.
11+
*
12+
* @return string
13+
*/
14+
public static function resourceType()
15+
{
16+
return 'certificate';
17+
}
18+
19+
/**
20+
* Resource path (relative to owner or API root).
21+
*
22+
* @return string
23+
*/
24+
public function resourcePath()
25+
{
26+
return 'certificates';
27+
}
28+
29+
/**
30+
* Resource name.
31+
*
32+
* @return string|null
33+
*/
34+
public function name()
35+
{
36+
return $this->domain();
37+
}
38+
39+
/**
40+
* Certificate domain.
41+
*
42+
* @return string|null
43+
*/
44+
public function domain()
45+
{
46+
return $this->getData('domain');
47+
}
48+
49+
/**
50+
* Certificate type.
51+
*
52+
* @return string|null
53+
*/
54+
public function type()
55+
{
56+
return $this->getData('type');
57+
}
58+
59+
/**
60+
* Request status.
61+
*
62+
* @return string|null
63+
*/
64+
public function requestStatus()
65+
{
66+
return $this->getData('request_status');
67+
}
68+
69+
/**
70+
* Determines if certificate is active.
71+
*
72+
* @return bool
73+
*/
74+
public function active(): bool
75+
{
76+
return intval($this->getData('active')) === 1;
77+
}
78+
79+
/**
80+
* Determines if current certificate was installed from existing certificate.
81+
*
82+
* @return bool
83+
*/
84+
public function existing(): bool
85+
{
86+
return intval($this->getData('existing')) === 1;
87+
}
88+
89+
/**
90+
* Determines if current certificate is Let's Encrypt certificate.
91+
*
92+
* @return bool
93+
*/
94+
public function letsencrypt(): bool
95+
{
96+
return $this->type() === 'letsencrypt';
97+
}
98+
99+
/**
100+
* Get Certificate Signing Request value.
101+
*
102+
* @return string
103+
*/
104+
public function csr()
105+
{
106+
$response = $this->getHttpClient()->request('GET', $this->apiUrl('/csr'));
107+
108+
return (string) $response->getBody();
109+
}
110+
111+
/**
112+
* Install certificate.
113+
*
114+
* @param string $content
115+
* @param bool $addIntermediates = false
116+
*
117+
* @return string|null
118+
*/
119+
public function install(string $content, bool $addIntermediates = false): bool
120+
{
121+
$response = $this->getHttpClient()->request('POST', $this->apiUrl('/install'), [
122+
'form_params' => [
123+
'certificate' => $content,
124+
'add_intermediates' => $addIntermediates,
125+
],
126+
]);
127+
128+
return true;
129+
}
130+
131+
/**
132+
* Activate certificate.
133+
*
134+
* @return bool
135+
*/
136+
public function activate(): bool
137+
{
138+
$this->getHttpClient()->request('POST', $this->apiUrl('/activate'));
139+
140+
return true;
141+
}
142+
}

0 commit comments

Comments
 (0)