1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195
<?php
namespace Gedcomx\Rs\Client;
use Gedcomx\Common\Attributable;
use Gedcomx\Common\ResourceReference;
use Gedcomx\Gedcomx;
use Gedcomx\Links\Link;
use Gedcomx\Rs\Client\Exception\GedcomxApplicationException;
use Gedcomx\Rs\Client\Options\HeaderParameter;
use Gedcomx\Rs\Client\Options\StateTransitionOption;
use Gedcomx\Rs\Client\Util\EmbeddedLinkLoader;
use Gedcomx\Rs\Client\Util\Embedding;
use Gedcomx\Rs\Client\Util\HttpStatus;
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Response;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
/**
* This is the base class for all state instances.
*
* Class GedcomxApplicationState
*
* @package Gedcomx\Rs\Client
*/
abstract class GedcomxApplicationState
{
/**
* The REST API client to use with all API calls.
*
* @var Client
*/
protected $client;
/**
* Gets or sets the REST API request.
*
* @var Request
*/
protected $request;
/**
* Gets or sets the REST API response.
*
* @var Response
*/
protected $response;
/**
* Gets or sets the current access token (the OAuth2 token), see {@link https://familysearch.org/developers/docs/api/authentication/Access_Token_resource}.
*
* @var string
*/
protected $accessToken;
/**
* The factory responsible for creating new state instances from REST API response data.
*
* @var StateFactory
*/
protected $stateFactory;
/**
* The list of hypermedia links. Links are not specified by GEDCOM X core, but as extension elements by GEDCOM X RS.
*
* @var array
*/
protected $links;
/**
* Gets the entity represented by this state (if applicable). Not all responses produce entities.
*
* @var object
*/
protected $entity;
/**
* The last embedded request (from a previous call to GedcomxApplicationState embed()).
*
* @var \GuzzleHttp\Psr7\Request
*/
private $lastEmbeddedRequest;
/**
* Gets or sets the last embedded response (from a previous call to GedcomxApplicationState embed()).
*
* @var \GuzzleHttp\Psr7\Response
*/
private $lastEmbeddedResponse;
/**
* Constructs a new GedcomxApplicationState using the specified client, request, response, access token, and state factory.
* @param Client $client
* @param Request $request
* @param Response $response
* @param string $accessToken
* @param StateFactory $stateFactory
*/
function __construct(Client $client, Request $request, Response $response, $accessToken, StateFactory $stateFactory)
{
$this->client = $client;
$this->request = $request;
$this->response = $response;
$this->accessToken = $accessToken;
$this->stateFactory = $stateFactory;
$this->entity = $this->loadEntityConditionally();
$this->links = $this->loadLinks();
}
/**
* Loads the entity from the REST API response if the response should have data.
*
* @return null
*/
protected function loadEntityConditionally()
{
if ( ($this->request->getMethod() != 'HEAD' && $this->request->getMethod() != 'OPTIONS')
&& ($this->response->getStatusCode() == HttpStatus::OK || $this->response->getStatusCode() == HttpStatus::GONE)
|| $this->response->getStatusCode() == HttpStatus::PRECONDITION_FAILED
) {
return $this->loadEntity();
}
else {
return null;
}
}
/**
* Clones the current state instance.
*
* @param \GuzzleHttp\Psr7\Request $request
* @param \GuzzleHttp\Psr7\Response $response
*
* @return mixed
*/
protected abstract function reconstruct(Request $request, Response $response);
/**
* Returns the entity from the REST API response.
*
* @return mixed
*/
protected abstract function loadEntity();
/**
* Gets the main data element represented by this state instance.
*
* @return mixed
*/
protected abstract function getScope();
/**
* Invokes the specified REST API request and returns a state instance of the REST API response.
*
* @param \GuzzleHttp\Psr7\Request $request
*
* @return mixed
*/
public function inject(Request $request)
{
return $this->reconstruct($request, $this->passOptionsTo('invoke',array($request)));
}
/**
* Loads all links from a REST API response and entity object, whether from the header, response body, or any other properties available to extract useful links for this state instance.
*
* @return array
*/
protected function loadLinks()
{
$links = array();
//if there's a location, we'll consider it a "self" link.
$myLocation = $this->response->getHeader('Location');
if (isset($myLocation[0])) {
$links['self'] = new Link();
$links['self']->setRel('self');
$links['self']->setHref($myLocation[0]);
}
//load link headers
$linkHeaders = \GuzzleHttp\Psr7\parse_header($this->response->getHeader('Link'));
foreach ($linkHeaders as $linkHeader) {
$linkHeader['href'] = trim($linkHeader[0], '<>');
if (isset($linkHeader['rel'])) {
$link = new Link($linkHeader);
$links[$linkHeader['rel']] = $link;
}
}
//load links from the entity.
if (isset($this->entity) && $this->entity->getLinks() != null) {
$links = array_merge($links, $this->entity->getLinks());
}
$scope = $this->getScope();
if (isset($scope) && is_object($scope)) {
$links = array_merge($links, $scope->getLinks());
}
return $links;
}
/**
* Gets sets the main REST API client to use with all API calls.
*
* @return \GuzzleHttp\Client
*/
public function getClient()
{
return $this->client;
}
/**
* Gets the current access token (the OAuth2 token), see https://familysearch.org/developers/docs/api/authentication/Access_Token_resource.
*
* @return string
*/
public function getAccessToken()
{
return $this->accessToken;
}
/**
* Gets the REST API request.
*
* @return \GuzzleHttp\Psr7\Request
*/
public function getRequest()
{
return $this->request;
}
/**
* Gets the REST API response.
*
* @return \GuzzleHttp\Psr7\Response
*/
public function getResponse()
{
return $this->response;
}
/**
* Gets the last embedded request (from a previous call to GedcomxApplicationState embed()).
*
* @return \GuzzleHttp\Psr7\Request
*/
public function getLastEmbeddedRequest()
{
return $this->lastEmbeddedRequest;
}
/**
* Gets the last embedded response (from a previous call to GedcomxApplicationState embed()).
*
* @return \GuzzleHttp\Psr7\Response
*/
public function getLastEmbeddedResponse()
{
return $this->lastEmbeddedResponse;
}
/**
* Gets the entity represented by this state (if applicable). Not all responses produce entities.
*
* @return object
*/
public function getEntity()
{
return $this->entity;
}
/**
* Gets a value indicating whether this instance is authenticated.
*
* @return boolean whether this state is authenticated.
*/
public function isAuthenticated()
{
return isset($this->accessToken);
}
/**
* Gets the URI of the REST API request associated to this state instance.
*
* @return string The URI for this application state.
*/
public function getUri()
{
return $this->request->getUri();
}
/**
* Determines whether the server response status code indicates a client side error (status code >= 400 and < 500).
*
* @return bool Whether this state is a client-side error.
*/
public function hasClientError()
{
$statusCode = intval($this->getStatus());
return $statusCode >= 400 && $statusCode < 500;
}
/**
* Determines whether the server response status code indicates a server side error (status code >= 500 and < 600).
*
* @return bool Whether this state is a server-side error.
*/
public function hasServerError()
{
$statusCode = intval($this->getStatus());
return $statusCode >= 500 && $statusCode < 600;
}
/**
* Determines whether this instance has error (server [code >= 500 and < 600] or client [code >= 400 and < 500]).
*
* @return bool Whether this state has an error.
*/
public function hasError()
{
return $this->hasClientError() || $this->hasServerError();
}
/**
* Determines whether the current REST API response has the specified status.
*
* @param int $status
*
* @return bool
*/
public function hasStatus($status)
{
return $status == $this->getStatus() ? true : false;
}
/**
* Get the HTTP status code of the response.
*
* @return int
*/
public function getStatus()
{
return intval($this->getResponse()->getStatusCode());
}
/**
* Gets the collection of REST API response headers.
*
* @return array The headers for this state.
*/
public function getHeaders()
{
return $this->response->getHeaders();
}
/**
* Gets the REST API response header (by name) if present.
*
* @param string $name The name of the header to retrieve.
*
* @return array return a specific header for this state.
*/
public function getHeader($name)
{
return $this->response->getHeader($name);
}
/**
* Gets the URI representing this current state instance.
*
* @return string The self-URI for this state.
*/
public function getSelfUri()
{
$selfRel = $this->getSelfRel();
$link = null;
if ($selfRel != null) {
$link = $this->getLink($selfRel);
}
if ($link == null){
$link = $this->getLink(Rel::SELF);
}
$self = null;
if ($link != null){
if ($link->getHref() != null){
$self = $link->getHref();
}
}
if ($self == null){
return $this->getUri();
}else{
return $self;
}
}
/**
* Gets the rel name for the current state instance. This is expected to be overridden.
*
* @return null
*/
public function getSelfRel(){
return null;
}
/**
* Gets the entity tag of the entity represented by this instance.
*
* @return array
*/
public function getETag() {
return $this->response->getHeader(HeaderParameter::ETAG);
}
/**
* Gets the last modified date of the entity represented by this instance.
*
* @return array
*/
public function getLastModified() {
return $this->response->getHeader(HeaderParameter::LAST_MODIFIED);
}
/**
* Gets the resource reference represented by this instance.
*
* @return ResourceReference
*/
public function getResourceReference(){
$args = array(
'resource' => $this->getSelfUri()
);
if ($this->getScope() != null) {
$args['resourceId'] = $this->getScope()->getId();
}
return new ResourceReference($args);
}
/**
* Executes a HEAD verb request against the current REST API request and returns a state instance with the response.
*
* @param \Gedcomx\Rs\Client\Options\StateTransitionOption $option,...
*
* @return \Gedcomx\Rs\Client\GedcomxApplicationState
*/
public function head(StateTransitionOption $option = null)
{
$headers = [];
$accept = $this->request->getHeader("Accept");
if (isset($accept)) {
$headers["Accept"] = $accept;
}
$request = $this->createAuthenticatedRequest('HEAD', $this->getSelfUri(), $headers);
return $this->reconstruct($request, $this->passOptionsTo('invoke',array($request),func_get_args()));
}
/**
* Executes a'GET' verb request against the current REST API request and returns a state instance with the response.
*
* @param \Gedcomx\Rs\Client\Options\StateTransitionOption $option,...
*
* @return mixed
*/
public function get(StateTransitionOption $option = null)
{
$headers = [];
$accept = $this->request->getHeader("Accept");
if (isset($accept)) {
$headers["Accept"] = $accept;
}
$request = $this->createAuthenticatedRequest('GET', $this->getSelfUri(), $headers);
return $this->reconstruct($request, $this->passOptionsTo('invoke',array($request),func_get_args()));
}
/**
* Executes an DELETE verb request against the current REST API request and returns a state instance with the response.
*
* @param \Gedcomx\Rs\Client\Options\StateTransitionOption $option,...
*
* @return \Gedcomx\Rs\Client\GedcomxApplicationState
*/
public function delete(StateTransitionOption $option = null)
{
$headers = [];
$accept = $this->request->getHeader("Accept");
if (isset($accept)) {
$headers["Accept"] = $accept;
}
$request = $this->createAuthenticatedRequest('DELETE', $this->getSelfUri(), $headers);
return $this->reconstruct($request, $this->passOptionsTo('invoke',array($request),func_get_args()));
}
/**
* Executes an OPTIONS verb request against the current REST API request and returns a state instance with the response.
*
* @param \Gedcomx\Rs\Client\Options\StateTransitionOption $option,...
*
* @return \Gedcomx\Rs\Client\GedcomxApplicationState
*/
public function options(StateTransitionOption $option = null)
{
$headers = [];
$accept = $this->request->getHeader("Accept");
if (isset($accept)) {
$headers["Accept"] = $accept;
}
$request = $this->createAuthenticatedRequest('OPTIONS', $this->getSelfUri(), $headers);
return $this->reconstruct($request, $this->passOptionsTo('invoke',array($request),func_get_args()));
}
/**
* Executes a PUT verb request against the current REST API request and returns a state instance with the response.
*
* @param $entity
* @param \Gedcomx\Rs\Client\Options\StateTransitionOption $option,...
*
* @return \Gedcomx\Rs\Client\GedcomxApplicationState
* @throws GedcomxApplicationException
*/
public function put($entity, StateTransitionOption $option = null)
{
$headers = [];
$accept = $this->request->getHeader("Accept");
if (isset($accept)) {
$headers["Accept"] = $accept;
}
$contentType = $this->request->getHeader("Content-Type");
if (isset($contentType)) {
$headers["Content-Type"] = $contentType;
}
$request = $this->createAuthenticatedRequest('PUT', $this->getSelfUri(), $headers, null, $entity->toJson());
return $this->reconstruct($request, $this->passOptionsTo('invoke',array($request),func_get_args()));
}
/**
* Executes a POST verb request against the current REST API request and returns a state instance with the response.
*
* @param \Gedcomx\Gedcomx $entity
* @param \Gedcomx\Rs\Client\Options\StateTransitionOption $option,...
*
* @return mixed
*/
public function post(Gedcomx $entity, StateTransitionOption $option = null)
{
$headers = [];
$accept = $this->request->getHeader("Accept");
if (count($accept) > 0) {
$headers["Accept"] = $accept[0];
}
$contentType = $this->request->getHeader("Content-Type");
if (count($contentType) > 0) {
$headers["Content-Type"] = $contentType[0];
}
if ($entity instanceof Gedcomx && !isset($headers["Content-Type"])){
$headers["Content-Type"] = Gedcomx::JSON_MEDIA_TYPE;
}
$request = $this->createAuthenticatedRequest('POST', $this->getSelfUri(), $headers, null, $entity->toJson());
return $this->reconstruct($request, $this->passOptionsTo('invoke', array($request), func_get_args()));
}
/**
* Get a link by its rel. Links are not specified by GEDCOM X core, but as extension elements by GEDCOM X RS.
*
* @param string $rel The link rel.
*
* @return \Gedcomx\Links\Link
*/
public function getLink($rel)
{
if( isset($this->links[$rel]) ){
return $this->links[$rel];
}
return null;
}
/**
* Gets the list of hypermedia links. Links are not specified by GEDCOM X core, but as extension elements by GEDCOM X RS.
*
* @return array
*/
public function getLinks()
{
return $this->links == null ? array() : $this->links;
}
/**
* Returns the current state instance if there are no errors in the current REST API response; otherwise, it throws an exception with the response details.
*
* @throws GedcomxApplicationException If this state captures an error.
* @return GedcomxApplicationState $this
*/
public function ifSuccessful()
{
if ($this->hasError()) {
throw new GedcomxApplicationException($this->buildFailureMessage($this->request, $this->response), $this->response);
}
return $this;
}
/**
* Sets the current access token to the one specified. The server is not contacted during this operation.
*
* @param $accessToken
*
* @return $this
*/
public function authenticateWithAccessToken($accessToken) {
$this->accessToken = $accessToken;
return $this;
}
/**
* Authenticates this session via OAuth2.
*
* @param array $formData The form parameters.
*
* @return \Gedcomx\Rs\Client\GedcomxApplicationState $this
* @throws GedcomxApplicationException If there are problems.
*/
protected function authenticateViaOAuth2(array $formData)
{
$tokenLink = $this->getLink(Rel::OAUTH2_TOKEN);
if (!isset($tokenLink)) {
$here = $this->getUri();
throw new GedcomxApplicationException("No OAuth2 token URI supplied for resource at {$here}");
}
$href = $tokenLink->getHref();
if (!isset($href)) {
$here = $this->getUri();
throw new GedcomxApplicationException("No OAuth2 token URI supplied for resource at {$here}");
}
$request = $this->createRequest('POST', $href, ['Accept' => 'application/json'], $formData);
$response = $this->invoke($request);
$statusCode = intval($response->getStatusCode());
if ($statusCode >= 200 && $statusCode < 300) {
$tokens = json_decode($response->getBody(), true);
$accessToken = $tokens['access_token'];
if (!isset($accessToken)) {
//workaround to accommodate providers that were built on an older version of the oauth2 specification.
$accessToken = $tokens['token'];
}
if (!isset($accessToken)) {
throw new GedcomxApplicationException('Illegal access token response: no access_token provided.', $response);
}
$this->accessToken = $accessToken;
return $this;
}
else {
throw new GedcomxApplicationException('Unable to obtain an access token.', $response);
}
}
/**
* Authenticates this session via OAuth2 password.
*
* @param string $username The username.
* @param string $password The password.
* @param string $clientId The client id.
* @param string $clientSecret The client secret.
* @return GedcomxApplicationState $this
*/
public function authenticateViaOAuth2Password($username, $password, $clientId, $clientSecret = NULL)
{
$formData = array(
'grant_type' => 'password',
'username' => $username,
'password' => $password,
'client_id' => $clientId
);
if (isset($clientSecret)) {
$formData['client_secret'] = $clientSecret;
}
return $this->authenticateViaOAuth2($formData);
}
/**
* Authenticates this session via OAuth2 authentication code.
*
* @param string $authCode The auth code.
* @param string $redirect The redirect URI.
* @param string $clientId The client id.
* @param string $clientSecret The client secret.
* @return GedcomxApplicationState $this
*/
public function authenticateViaOAuth2AuthCode($authCode, $redirect, $clientId, $clientSecret = NULL)
{
$formData = array(
'grant_type' => 'authorization_code',
'code' => $authCode,
'redirect_uri' => $redirect,
'client_id' => $clientId
);
if (isset($clientSecret)) {
$formData['client_secret'] = $clientSecret;
}
return $this->authenticateViaOAuth2($formData);
}
/**
* Authenticates this session via OAuth2 client credentials.
*
* @param string $clientId The client id.
* @param string $clientSecret The client secret.
* @return GedcomxApplicationState $this
*/
public function authenticateViaOAuth2ClientCredentials($clientId, $clientSecret)
{
$formData = array(
'grant_type' => 'client_credentials',
'client_id' => $clientId,
'client_secret' => $clientSecret
);
return $this->authenticateViaOAuth2($formData);
}
/**
* Creates a state instance without authentication. It will produce an access token, but only good for requests that do not need authentication.
*
* @param string $clientId The client id.
* @param string $ipAddress The client's ipaddress.
* @return GedcomxApplicationState $this
*/
public function authenticateViaOAuth2WithoutCredentials($ipAddress, $clientId)
{
$formData = array(
'grant_type' => 'unauthenticated_session',
'ip_address' => $ipAddress,
'client_id' => $clientId
);
return $this->authenticateViaOAuth2($formData);
}
/**
* Generate the secret required for authentication via client credentials.
*
* @param mixed $key The key parameter of openssl_private_encrypt which is
* the same as documented for openssl_pkey_get_private. May be the
* text of a key file or a file reference.
* @timestamp mixed $timestamp The time in milliseconds. This parameter
* should only be used for testing. A current timestamp is generated when this
* parameter is not provided.
*/
public function generateClientSecret($key, $timestamp = null)
{
if($timestamp === null)
{
// Generate a timestamp in milliseconds
// http://stackoverflow.com/a/11424665
$timestamp = round(microtime(true) * 1000);
}
// Encrypt the timestamp
openssl_private_encrypt($timestamp, $secret, $key);
return base64_encode($secret);
}
/**
* Invalidate the current access token and end the session.
*
* @return GedcomxApplicationState $this
*/
public function logout()
{
$link = $this->getLink('logout');
if ($link === null || $link->getHref() === null) {
return null;
}
$request = $this->createRequest('GET', $link->getHref() . '?access_token=' . $this->accessToken);
$response = $this->invoke($request);
$this->accessToken = null;
return $this;
}
/**
* Builds a pretty failure message from the specified response's warning headers, using the specified request for
* additional information.
* @param \GuzzleHttp\Psr7\Request $request HTTP request object
* @param \GuzzleHttp\Psr7\Response $response HTTP response object
* @return string
*/
protected function buildFailureMessage( Request $request, Response $response ) {
$message = "Unsuccessful " . $request->getMethod() . " to " . $request->getUri() . " (" . $response->getStatusCode() . ")";
$warnings = $response->getHeader('Warning');
foreach( $warnings as $w ) {
$message .= "\nWarning: " . $w;
}
return $message;
}
/**
* Reads a page of results, usually of type \Gedcomx\Atom\Feed.
*
* @param string $rel The rel
* @param \Gedcomx\Rs\Client\Options\StateTransitionOption $option,... zero or more StateTransitionOption objects
*
* @return \Gedcomx\Rs\Client\GedcomxApplicationState The requested page.
*/
protected function readPage($rel, StateTransitionOption $option = null)
{
$link = $this->getLink($rel);
if ($link === null || $link->getHref() === null) {
return null;
}
$headers = [
'Accept' => count($this->request->getHeader("Accept")) > 0 ? $this->request->getHeader("Accept")[0] : null,
'Content-Type' => count($this->request->getHeader("Content-Type")) > 0 ? $this->request->getHeader("Content-Type")[0] : null
];
$request = $this->createAuthenticatedRequest($this->request->getMethod(), $link->getHref(), $headers);
$class = get_class($this);
return new $class(
$this->client,
$request,
$this->passOptionsTo('invoke',array($request), func_get_args()),
$this->accessToken,
$this->stateFactory
);
}
/**
* Reads the next page of results, usually of type \Gedcomx\Atom\Feed.
*
* @param \Gedcomx\Rs\Client\Options\StateTransitionOption $option,... zero or more StateTransitionOption objects
*
* @return \Gedcomx\Rs\Client\GedcomxApplicationState The requested page.
*/
protected function readNextPage( StateTransitionOption $option = null )
{
return $this->passOptionsTo('readPage',array(Rel::NEXT), func_get_args());
}
/**
* Reads the previous page of results, usually of type \Gedcomx\Atom\Feed.
*
* @param \Gedcomx\Rs\Client\Options\StateTransitionOption $option,... zero or more StateTransitionOption objects
*
* @return \Gedcomx\Rs\Client\GedcomxApplicationState The requested page.
*/
protected function readPreviousPage(StateTransitionOption $option = null)
{
return $this->passOptionsTo('readPage',array(Rel::PREVIOUS), func_get_args());
}
/**
* Reads the first page of results, usually of type \Gedcomx\Atom\Feed.
*
* @param \Gedcomx\Rs\Client\Options\StateTransitionOption $option,... zero or more StateTransitionOption objects
*
* @return \Gedcomx\Rs\Client\GedcomxApplicationState The requested page.
*/
protected function readFirstPage(StateTransitionOption $option = null)
{
return $this->passOptionsTo('readPage',array(Rel::FIRST), func_get_args());
}
/**
* Reads the last page of results, usually of type \Gedcomx\Atom\Feed.
*
* @param \Gedcomx\Rs\Client\Options\StateTransitionOption $option,... zero or more StateTransitionOption objects
*
* @return \Gedcomx\Rs\Client\GedcomxApplicationState The requested page.
*/
protected function readLastPage(StateTransitionOption $option = null)
{
return $this->passOptionsTo('readPage',array(Rel::LAST), func_get_args());
}
/**
* @param string $method The HTTP method.
* @param string|array $uri optional: string with an href, or an array with template info
* @param array $headers optional: Associative array of HTTP headers
* @param array $formData optional: Array of form data
* @param string $body optional: body of the request
*
* @return Request The request.
*/
protected function createRequest($method, $uri = null, $headers = array(), $formData = null, $body = null)
{
if(is_array($formData)){
$body = http_build_query($formData, null, '&');
$headers['Content-Type'] = 'application/x-www-form-urlencoded';
}
if(is_array($uri)){
if(!isset($uri[1])){
$uri = $uri[0];
} else {
$uri = \GuzzleHttp\uri_template($uri[0], $uri[1]);
}
}
return new Request($method, $uri, $headers, $body);
}
/**
* Creates a request object (with authorization when present) for use with REST API requests.
*
* @param string $method The HTTP method.
* @param string|array $uri optional: string with an href, or an array with template info
* @param array $headers optional: Associative array of HTTP headers
* @param array $formData optional: Array of form data
* @param string $body optional: body of the request
*
* @return Request The request.
*/
protected function createAuthenticatedRequest($method, $uri = null, $headers = array(), $formData = null, $body = null)
{
if (isset($this->accessToken)) {
$headers['Authorization'] = "Bearer {$this->accessToken}";
}
$request = $this->createRequest($method, $uri, $headers, $formData, $body);
return $request;
}
/**
* Creates a request object that expects a "application/x-gedcomx-atom+json" response (with authorization when present) for use with REST API requests.
*
* @param string $method The HTTP method.
* @param string|array $uri optional: string with an href, or an array with template info
* @param array $headers optional: Associative array of HTTP headers
* @param array $formData optional: Array of form data
* @param string $body optional: body of the request
*
* @return Request The request.
*/
protected function createAuthenticatedFeedRequest($method, $uri = null, $headers = array(), $formData = null, $body = null)
{
$headers['Accept'] = Gedcomx::ATOM_JSON_MEDIA_TYPE;
$request = $this->createAuthenticatedRequest($method, $uri, $headers, $formData, $body);
return $request;
}
/**
* Creates a request object that will send and expect "application/x-gedcomx-v1+json" data (with authorization when present) for use with REST API requests.
*
* @param string $method The HTTP method.
* @param string|array $uri optional: string with an href, or an array with template info
* @param array $headers optional: Associative array of HTTP headers
* @param array $formData optional: Array of form data
* @param string $body optional: body of the request
*
* @return Request The request.
*/
protected function createAuthenticatedGedcomxRequest($method, $uri = null, $headers = array(), $formData = null, $body = null)
{
if(!isset($headers['Accept'])){
$headers['Accept'] = Gedcomx::JSON_MEDIA_TYPE;
}
if(!isset($headers['Content-Type'])){
$headers['Content-Type'] = Gedcomx::JSON_MEDIA_TYPE;
}
$request = $this->createAuthenticatedRequest($method, $uri, $headers, $formData, $body);
return $request;
}
/**
* Creates a REST API request (with appropriate authentication headers).
*
* @param $method
* @param \Gedcomx\Links\Link $link
*
* @return \GuzzleHttp\Psr7\Request
*/
protected function createRequestForEmbeddedResource($method, Link $link) {
return $this->createAuthenticatedGedcomxRequest($method, $link->getHref());
}
/**
* Executes the specified link and embeds the response in the current instance entity.
*
* @param \Gedcomx\Links\Link $link
* @param \Gedcomx\Rs\Client\Options\StateTransitionOption $option,...
*
* @throws Exception\GedcomxApplicationException
*/
protected function embed(Link $link, StateTransitionOption $option = null ){
if ($link->getHref() != null) {
$this->lastEmbeddedRequest = $this->createRequestForEmbeddedResource('GET', $link);
$this->lastEmbeddedResponse = $this->passOptionsTo('invoke',array($this->lastEmbeddedRequest), func_get_args());
if ($this->lastEmbeddedResponse->getStatusCode() == 200) {
$json = json_decode($this->lastEmbeddedResponse->getBody(), true);
$entityClass = get_class($this->entity);
Embedding::embed($this->entity, new $entityClass($json));
}
else if (floor($this->lastEmbeddedResponse->getStatusCode()/100) == 5 ) {
throw new GedcomxApplicationException(sprintf("Unable to load embedded resources: server says \"%s\" at %s.", $this->lastEmbeddedResponse.getClientResponseStatus().getReasonPhrase(), $this->lastEmbeddedRequest.getURI()), $this->lastEmbeddedResponse);
}
else {
//todo: log a warning? throw an error?
}
}
}
/**
* Load all external resources such as notes, media, and evidence. See
* EmbeddedLinkLoader for a complete list.
*
* @param \Gedcomx\Rs\Client\Options\StateTransitionOption $option,...
*
* @return \Gedcomx\Rs\Client\PersonState $this
*/
public function loadAllEmbeddedResources(StateTransitionOption $option = null)
{
$loader = new EmbeddedLinkLoader();
$links = $loader->loadEmbeddedLinks($this->entity);
foreach ($links as $link) {
$this->passOptionsTo('embed', array($link), func_get_args());
}
return $this;
}
/**
* Extracts embedded links from the current instance entity, calls each one, and embeds the response into the current instance entity.
*
* @param \Gedcomx\Rs\Client\Options\StateTransitionOption $options
*/
protected function includeEmbeddedResources(StateTransitionOption $options = null) {
$this->passOptionsTo('loadAllEmbeddedResources', array(), func_get_args());
}
/**
* @param array $args expects the results from func_get_args()
*
* @return array
*/
private function findTransitionOptions( array $args )
{
while (! empty($args) && ! $args[0] instanceof StateTransitionOption){
array_shift($args);
}
return $args;
}
/**
* This function emulates optional parameters present in other languages.
*
* @param string $functionName The function to call. Assumed to be in $this scope
* @param array $args New arguments to pass to the function
* @param array $passed_args Possible optional arguments from the calling function
* @param null $scope
*
* @return mixed
*/
protected function passOptionsTo( $functionName, array $args, array $passed_args = array(), $scope = null ){
$func_args = array_merge($args, $this->findTransitionOptions($passed_args));
if( $scope == null ){
$func = 'static::' . $functionName;
} elseif (is_string($scope)) {
$func = $scope . "::" . $functionName;
} else {
$func = array($scope, $functionName);
}
return call_user_func_array(
$func,
$func_args
);
}
/**
* Reads the contributor for the current state instance.
*
* @param \Gedcomx\Rs\Client\Options\StateTransitionOption $option,...
*
* @return \Gedcomx\Rs\Client\AgentState|null
*/
public function readContributor(StateTransitionOption $option = null)
{
$scope = $this->getScope();
if ($scope instanceof Attributable) {
return $this->passOptionsTo('readAttributableContributor', array($scope), func_get_args());
} else {
return null;
}
}
/**
* Reads the contributor for the specified attributable.
*
* @param \Gedcomx\Common\Attributable $attributable
* @param \Gedcomx\Rs\Client\Options\StateTransitionOption $option,...
*
* @return \Gedcomx\Rs\Client\AgentState|null
*/
public function readAttributableContributor(Attributable $attributable, StateTransitionOption $option = null)
{
$attribution = $attributable->getAttribution();
if ($attribution == null) {
return null;
}
$reference = $attribution->getContributor();
return $this->passOptionsTo('readReferenceContributor', array($reference), func_get_args());
}
/**
* Reads the contributor for the specified resource reference.
*
* @param \Gedcomx\Common\ResourceReference $contributor
* @param \Gedcomx\Rs\Client\Options\StateTransitionOption $option,...
*
* @return \Gedcomx\Rs\Client\AgentState|null
*/
public function readReferenceContributor(ResourceReference $contributor, StateTransitionOption $option = null)
{
if ($contributor == null || $contributor->getResource() == null) {
return null;
}
$request = $this->createAuthenticatedGedcomxRequest('GET', $contributor->getResource());
return $this->stateFactory->createState(
'AgentState',
$this->client,
$request,
$this->passOptionsTo('invoke', array($request), func_get_args()),
$this->accessToken
);
}
/**
* Applies the specified options before before executing the request.
*
* @param \GuzzleHttp\Psr7\Request $request the request to send.
* @param \Gedcomx\Rs\Client\Options\StateTransitionOption $option,... StateTransitionOptions to be applied before sending
*
* @throws Exception\GedcomxApplicationException
* @return Response The response.
*/
protected function invoke(Request $request, StateTransitionOption $option = null)
{
$options = func_get_args();
array_shift($options);
if( $options !== null && !empty($options) ){
foreach( $options as $opt ){
$request = $opt->apply($request);
}
}
return self::send($this->client, $request);
}
/**
* Sends a request via the given client. This centralizes options
* that all clients need such as disabling exceptions on errors
* and catching redirects.
*
* @param \GuzzleHttp\Client $client
* @param \GuzzleHttp\Psr7\Request $request
*
* @return \GuzzleHttp\Psr7\Response $response
*/
public static function send(Client $client, Request $request){
$actualUri = (string) $request->getUri();
try {
$response = $client->send($request, [
'curl' => ['body_as_string' => true],
'allow_redirects' => [
'on_redirect' => function(RequestInterface $request, ResponseInterface $response, $uri) use (&$actualUri) {
$actualUri = (string) $uri;
}
]
]);
$response->effectiveUri = $actualUri;
return $response;
}
// Exceptions are thrown if http_errors is set to true on the client.
catch(\GuzzleHttp\Exception\BadResponseException $e) {
$response = null;
$reason = 'Bad response.';
if($e->hasResponse()){
$response = $e->getResponse();
$reason = $response->getReasonPhrase();
}
throw new \Gedcomx\Rs\Client\Exception\GedcomxApplicationException($reason, $response);
}
}
}