Skip to content

Commit 6920484

Browse files
onairmarcEncoreBot
andauthored
Add OAuth Bearer Token Support (Experimental) (#77)
Co-authored-by: EncoreBot <[email protected]>
1 parent 9c1bca8 commit 6920484

File tree

3 files changed

+64
-4
lines changed

3 files changed

+64
-4
lines changed

.git-blame-ignore-revs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,5 @@ cbe36f817a31ddbeb1ef59120b2406f6e9d4a689
1010
fa4b60ada3c0013ece75995fe7d741e5ab3e80b4
1111
99bd1813b52dabee4f7a6e9fde1548dba008870c
1212
0301a888d15da0b7d5749d41a541e9d838ae8a5c
13+
ab97eabfaa74cf93c607153c1b128066cf5bc6ef
14+
17c9890cca2428bf9215a88a39da34d6fc57ab3c

src/Support/AuthType.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
/*
4+
* Encore Digital Group - Planning Center PHP SDK
5+
* Copyright (c) 2025. Encore Digital Group
6+
*/
7+
8+
namespace EncoreDigitalGroup\PlanningCenter\Support;
9+
10+
use EncoreDigitalGroup\StdLib\Objects\Support\Traits\HasEnumValue;
11+
12+
/** @experimental This could change. Use with caution. */
13+
enum AuthType: string
14+
{
15+
use HasEnumValue;
16+
17+
case Basic = "basic";
18+
case Token = "token";
19+
}

src/Traits/HasPlanningCenterClient.php

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@
22

33
/*
44
* Encore Digital Group - Planning Center PHP SDK
5-
* Copyright (c) 2023-2024. Encore Digital Group
5+
* Copyright (c) 2023-2025. Encore Digital Group
66
*/
77

88
namespace EncoreDigitalGroup\PlanningCenter\Traits;
99

1010
use EncoreDigitalGroup\PlanningCenter\Objects\SdkObjects\ClientResponse;
11+
use EncoreDigitalGroup\PlanningCenter\Support\AuthType;
1112
use EncoreDigitalGroup\PlanningCenter\Support\PlanningCenterApiVersion;
13+
use EncoreDigitalGroup\StdLib\Objects\Support\Types\Str;
1214
use Illuminate\Http\Client\PendingRequest;
1315
use Illuminate\Http\Client\Response;
1416
use PHPGenesis\Http\HttpClient;
@@ -18,22 +20,33 @@ trait HasPlanningCenterClient
1820
{
1921
protected const string HOSTNAME = "https://api.planningcenteronline.com";
2022

23+
/** @experimental This could change. Use with caution. */
24+
protected const string X_PCO_API_VERSION_HEADER = "X-PCO-API-Version";
25+
2126
protected string $clientId;
2227
protected string $clientSecret;
2328
protected string $apiVersion = "";
2429

30+
/** @experimental This could change. Use with caution. */
31+
protected AuthType $authType = AuthType::Basic;
32+
2533
public function __construct(?string $clientId = null, ?string $clientSecret = null)
2634
{
27-
$this->clientId = $clientId ?? "";
28-
$this->clientSecret = $clientSecret ?? "";
35+
$this->clientId = $clientId ?? Str::empty();
36+
$this->clientSecret = $clientSecret ?? Str::empty();
2937

3038
new HttpClientBuilder;
3139
}
3240

3341
public function client(): PendingRequest
3442
{
43+
if ($this->authType == AuthType::Token) {
44+
return HttpClient::withToken($this->clientId)
45+
->withHeader(self::X_PCO_API_VERSION_HEADER, $this->apiVersion);
46+
}
47+
3548
return HttpClient::withBasicAuth($this->clientId, $this->clientSecret)
36-
->withHeader("X-PCO-API-Version", $this->apiVersion);
49+
->withHeader(self::X_PCO_API_VERSION_HEADER, $this->apiVersion);
3750
}
3851

3952
public function hostname(): string
@@ -48,6 +61,32 @@ public function setApiVersion(string $apiVersion): static
4861
return $this;
4962
}
5063

64+
/** @experimental This could change. Use with caution. */
65+
public function setAuthType(AuthType $authType): static
66+
{
67+
$this->authType = $authType;
68+
69+
return $this;
70+
}
71+
72+
/** @experimental This could change. Use with caution. */
73+
public function withBasicAuth(string $clientId = "", string $clientSecret = ""): static
74+
{
75+
$this->clientId = $clientId;
76+
$this->clientSecret = $clientSecret;
77+
78+
return $this->setAuthType(AuthType::Basic);
79+
}
80+
81+
/** @experimental This could change. Use with caution. */
82+
public function withToken(string $token = ""): static
83+
{
84+
$this->clientId = $token;
85+
$this->clientSecret = Str::empty();
86+
87+
return $this->setAuthType(AuthType::Token);
88+
}
89+
5190
protected function processResponse(Response $http): ClientResponse
5291
{
5392
$clientResponse = new ClientResponse($http);

0 commit comments

Comments
 (0)