|
|
@@ -0,0 +1,334 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+namespace ElkenGit\OneAuthSDK;
|
|
|
+
|
|
|
+use Exception;
|
|
|
+use GuzzleHttp\Exception\GuzzleException;
|
|
|
+use Psr\Http\Message\ResponseInterface;
|
|
|
+
|
|
|
+class Client
|
|
|
+{
|
|
|
+ public ?string $bearerToken;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @param string|null $bearerToken
|
|
|
+ */
|
|
|
+ public function __construct(
|
|
|
+ ?string $bearerToken = null
|
|
|
+ )
|
|
|
+ {
|
|
|
+ $this->setBearerToken($bearerToken);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @return string|null
|
|
|
+ */
|
|
|
+ public function getBearerToken(): ?string
|
|
|
+ {
|
|
|
+ return $this->bearerToken ?? null;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @param string|null $value
|
|
|
+ * @return void
|
|
|
+ */
|
|
|
+ public function setBearerToken(
|
|
|
+ ?string $value
|
|
|
+ ): void
|
|
|
+ {
|
|
|
+ $this->bearerToken = $value;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @return bool
|
|
|
+ * @throws GuzzleException
|
|
|
+ */
|
|
|
+ public function verifyAccessToken(): bool
|
|
|
+ {
|
|
|
+ try {
|
|
|
+ $response = $this->makeOneAuthHttpRequest(
|
|
|
+ 'GET',
|
|
|
+ config('one_auth.url_paths.auth.verify_access_token')
|
|
|
+ );
|
|
|
+
|
|
|
+ if ($response->getStatusCode() == 200) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ } catch (Exception $e) {
|
|
|
+ //
|
|
|
+ }
|
|
|
+
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @param string $clientId
|
|
|
+ * @param string $clientSecret
|
|
|
+ * @return array|null
|
|
|
+ * @throws GuzzleException
|
|
|
+ */
|
|
|
+ public function loginUsingClient(
|
|
|
+ string $clientId,
|
|
|
+ string $clientSecret
|
|
|
+ ): ?array
|
|
|
+ {
|
|
|
+ try {
|
|
|
+ $response = $this->makeOneAuthHttpRequest(
|
|
|
+ 'POST',
|
|
|
+ config('one_auth.url_paths.auth.logout'),
|
|
|
+ [
|
|
|
+ 'grant_type' => 'client_credentials',
|
|
|
+ 'client_id' => $clientId,
|
|
|
+ 'client_secret' => $clientSecret
|
|
|
+ ]
|
|
|
+ );
|
|
|
+
|
|
|
+ if ($response->getStatusCode() == 200) {
|
|
|
+ return json_decode(
|
|
|
+ $response->getBody(),
|
|
|
+ true
|
|
|
+ );
|
|
|
+ } else {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ } catch (Exception $e) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @param string $userUuid
|
|
|
+ * @param string $password
|
|
|
+ * @return array|null
|
|
|
+ * @throws GuzzleException
|
|
|
+ */
|
|
|
+ public function login(
|
|
|
+ string $userUuid,
|
|
|
+ string $password
|
|
|
+ ): ?array
|
|
|
+ {
|
|
|
+ try {
|
|
|
+ $response = $this->makeOneAuthHttpRequest(
|
|
|
+ 'POST',
|
|
|
+ config('one_auth.url_paths.auth.login'),
|
|
|
+ [
|
|
|
+ 'user_uuid' => $userUuid,
|
|
|
+ 'password' => $password
|
|
|
+ ]
|
|
|
+ );
|
|
|
+
|
|
|
+ if ($response->getStatusCode() == 200) {
|
|
|
+ return json_decode(
|
|
|
+ $response->getBody(),
|
|
|
+ true
|
|
|
+ );
|
|
|
+ } else {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ } catch (Exception $e) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @param string $userUuid
|
|
|
+ * @param string $loginAsUserUuid
|
|
|
+ * @return array|null
|
|
|
+ * @throws GuzzleException
|
|
|
+ */
|
|
|
+ public function loginAs(
|
|
|
+ string $userUuid,
|
|
|
+ string $loginAsUserUuid
|
|
|
+ ): ?array
|
|
|
+ {
|
|
|
+ try {
|
|
|
+ $response = $this->makeOneAuthHttpRequest(
|
|
|
+ 'POST',
|
|
|
+ config('one_auth.url_paths.auth.login_as'),
|
|
|
+ [
|
|
|
+ 'user_uuid' => $userUuid,
|
|
|
+ 'login_as_user_uuid' => $loginAsUserUuid
|
|
|
+ ]
|
|
|
+ );
|
|
|
+
|
|
|
+ if ($response->getStatusCode() == 200) {
|
|
|
+ return json_decode(
|
|
|
+ $response->getBody(),
|
|
|
+ true
|
|
|
+ );
|
|
|
+ } else {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ } catch (Exception $e) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @param bool $revokeAllToken
|
|
|
+ * @return bool
|
|
|
+ * @throws GuzzleException
|
|
|
+ */
|
|
|
+ public function logout(
|
|
|
+ bool $revokeAllToken = false
|
|
|
+ ): bool
|
|
|
+ {
|
|
|
+ try {
|
|
|
+ $response = $this->makeOneAuthHttpRequest(
|
|
|
+ 'POST',
|
|
|
+ config('one_auth.url_paths.auth.logout'),
|
|
|
+ [
|
|
|
+ 'revoke_all' => $revokeAllToken
|
|
|
+ ]
|
|
|
+ );
|
|
|
+
|
|
|
+ if ($response->getStatusCode() == 200) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ } catch (Exception $e) {
|
|
|
+ //
|
|
|
+ }
|
|
|
+
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @param string $name
|
|
|
+ * @param string $email
|
|
|
+ * @param string $password
|
|
|
+ * @param string $appName
|
|
|
+ * @param int $appUserId
|
|
|
+ * @param string|null $appMemberId
|
|
|
+ * @param bool $isActive
|
|
|
+ * @return bool
|
|
|
+ * @throws GuzzleException
|
|
|
+ */
|
|
|
+ public function createUser(
|
|
|
+ string $name,
|
|
|
+ string $email,
|
|
|
+ string $password,
|
|
|
+ string $appName,
|
|
|
+ int $appUserId,
|
|
|
+ ?string $appMemberId,
|
|
|
+ bool $isActive = true
|
|
|
+ ): bool
|
|
|
+ {
|
|
|
+ try {
|
|
|
+ $response = $this->makeOneAuthHttpRequest(
|
|
|
+ 'POST',
|
|
|
+ config('one_auth.url_paths.users.create'),
|
|
|
+ [
|
|
|
+ 'name' => $name,
|
|
|
+ 'email' => $email,
|
|
|
+ 'password' => $password,
|
|
|
+ 'app_name' => $appName,
|
|
|
+ 'app_user_id' => $appUserId,
|
|
|
+ 'app_member_id' => $appMemberId,
|
|
|
+ 'is_active' => $isActive
|
|
|
+ ]
|
|
|
+ );
|
|
|
+
|
|
|
+ if ($response->getStatusCode() == 200) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ } catch (Exception $e) {
|
|
|
+ //
|
|
|
+ }
|
|
|
+
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @param string $userUuid
|
|
|
+ * @return array|null
|
|
|
+ * @throws GuzzleException
|
|
|
+ */
|
|
|
+ public function showUser(
|
|
|
+ string $userUuid
|
|
|
+ ): ?array
|
|
|
+ {
|
|
|
+ try {
|
|
|
+ $response = $this->makeOneAuthHttpRequest(
|
|
|
+ 'GET',
|
|
|
+ sprintf(
|
|
|
+ config('one_auth.url_paths.users.show'),
|
|
|
+ $userUuid
|
|
|
+ )
|
|
|
+ );
|
|
|
+
|
|
|
+ if ($response->getStatusCode() == 200) {
|
|
|
+ return json_decode(
|
|
|
+ $response->getBody(),
|
|
|
+ true
|
|
|
+ );
|
|
|
+ } else {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ } catch (Exception $e) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @return array|null
|
|
|
+ * @throws GuzzleException
|
|
|
+ */
|
|
|
+ public function getAuthUser(): ?array
|
|
|
+ {
|
|
|
+ try {
|
|
|
+ $response = $this->makeOneAuthHttpRequest(
|
|
|
+ 'GET',
|
|
|
+ config('one_auth.url_paths.auth.show_auth_user')
|
|
|
+ );
|
|
|
+
|
|
|
+ return json_decode(
|
|
|
+ $response->getBody(),
|
|
|
+ true
|
|
|
+ );
|
|
|
+ } catch (Exception $e) {
|
|
|
+ //
|
|
|
+ }
|
|
|
+
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @param string $httpMethod
|
|
|
+ * @param string $urlPath
|
|
|
+ * @param array $formParams
|
|
|
+ * @return ResponseInterface
|
|
|
+ * @throws GuzzleException
|
|
|
+ */
|
|
|
+ private function makeOneAuthHttpRequest(
|
|
|
+ string $httpMethod,
|
|
|
+ string $urlPath,
|
|
|
+ array $formParams = []
|
|
|
+ ): ResponseInterface
|
|
|
+ {
|
|
|
+ $httpClient = new \GuzzleHttp\Client();
|
|
|
+
|
|
|
+ $options = [
|
|
|
+ 'headers' => [
|
|
|
+ 'Accept' => 'application/json'
|
|
|
+ ]
|
|
|
+ ];
|
|
|
+
|
|
|
+ if (!empty($this->getBearerToken())) {
|
|
|
+ $options['headers']['Authorization'] = $this->getBearerToken();
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!empty($formParams)) {
|
|
|
+ $options['form_params'] = $formParams;
|
|
|
+ }
|
|
|
+
|
|
|
+ return $httpClient->request(
|
|
|
+ $httpMethod,
|
|
|
+ sprintf(
|
|
|
+ '%s%s',
|
|
|
+ config('one_auth.base_url'),
|
|
|
+ $urlPath
|
|
|
+ ),
|
|
|
+ $options
|
|
|
+ );
|
|
|
+ }
|
|
|
+}
|