Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# PCO API OAuth Slim PHP Example
# PCO API OAuth / OIDC / PKCE Example - Slim + PHP

This is an example Slim PHP app for demonstrating how one might build an app to authenticate any PCO user
and then subsequently use that authentication to query the API.
Expand Down Expand Up @@ -30,4 +30,3 @@ You can learn more about Planning Center's API [here](https://developer.planning
## Copyright & License

Copyright Ministry Centered Technologies. Licensed MIT.

87 changes: 16 additions & 71 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 15 additions & 2 deletions public/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
"scopes" => ["openid", "people"],
"urlAccessToken" => "{$container->get("apiUrl")}/oauth/token",
"urlAuthorize" => "{$container->get("apiUrl")}/oauth/authorize",
"urlResourceOwnerDetails" => "{$container->get("apiUrl")}/oauth/userinfo"
"urlResourceOwnerDetails" => "{$container->get("apiUrl")}/oauth/userinfo",
"pkceMethod" => \League\OAuth2\Client\Provider\GenericProvider::PKCE_METHOD_S256
]);
});
AppFactory::setContainer($container);
Expand Down Expand Up @@ -103,10 +104,14 @@
$app->get("/auth", function (Request $request, Response $response, $args) {
// Build the authorization URL and redirect to it
$oauth = $this->get("oauth");
$session = $this->get("session");

$authorizationUrl = $oauth->getAuthorizationUrl([
'prompt' => 'select_account' // to allow user account selection or "login" to force re-authentication
]);

$session->set("code_verifier", $oauth->getPkceCode());

return $response
->withHeader("Location", $authorizationUrl)
->withStatus(302);
Expand All @@ -117,9 +122,17 @@
$code = $request->getQueryParams()["code"];
// Use the code to fetch our access token
$oauth = $this->get("oauth");
$token = $oauth->getAccessToken("authorization_code", ["code" => $code]);

// Restore the PKCE code for the token exchange
$session = $this->get("session");
if ($session->exists("code_verifier")) {
$oauth->setPkceCode($session->get("code_verifier"));
}

$token = $oauth->getAccessToken("authorization_code", ["code" => $code]);

$session->delete("code_verifier");

// Set the token in our session
$session->set("token", $token);

Expand Down