Updated Identity Support from SDK Core

- Moved PPModels required for Identity Support
This commit is contained in:
japatel
2014-10-14 14:15:41 -05:00
parent 0cb302326a
commit dc2ac0fd63
36 changed files with 2652 additions and 587 deletions

View File

@@ -0,0 +1,119 @@
<?php
namespace PayPal\Auth\Openid;
use PayPal\Common\PPModel;
/**
* End-User's preferred address.
*/
class PPOpenIdAddress extends PPModel
{
/**
* Full street address component, which may include house number, street name.
*
* @param string $street_address
*/
public function setStreetAddress($street_address)
{
$this->street_address = $street_address;
return $this;
}
/**
* Full street address component, which may include house number, street name.
*
* @return string
*/
public function getStreetAddress()
{
return $this->street_address;
}
/**
* City or locality component.
*
* @param string $locality
*/
public function setLocality($locality)
{
$this->locality = $locality;
return $this;
}
/**
* City or locality component.
*
* @return string
*/
public function getLocality()
{
return $this->locality;
}
/**
* State, province, prefecture or region component.
*
* @param string $region
*/
public function setRegion($region)
{
$this->region = $region;
return $this;
}
/**
* State, province, prefecture or region component.
*
* @return string
*/
public function getRegion()
{
return $this->region;
}
/**
* Zip code or postal code component.
*
* @param string $postal_code
*/
public function setPostalCode($postal_code)
{
$this->postal_code = $postal_code;
return $this;
}
/**
* Zip code or postal code component.
*
* @return string
*/
public function getPostalCode()
{
return $this->postal_code;
}
/**
* Country name component.
*
* @param string $country
*/
public function setCountry($country)
{
$this->country = $country;
return $this;
}
/**
* Country name component.
*
* @return string
*/
public function getCountry()
{
return $this->country;
}
}

View File

@@ -0,0 +1,76 @@
<?php
namespace PayPal\Auth\Openid;
use PayPal\Common\PPModel;
/**
* Error resource
*/
class PPOpenIdError extends PPModel
{
/**
* A single ASCII error code from the following enum.
*
* @param string $error
*/
public function setError($error)
{
$this->error = $error;
return $this;
}
/**
* A single ASCII error code from the following enum.
*
* @return string
*/
public function getError()
{
return $this->error;
}
/**
* A resource ID that indicates the starting resource in the returned results.
*
* @param string $error_description
*/
public function setErrorDescription($error_description)
{
$this->error_description = $error_description;
return $this;
}
/**
* A resource ID that indicates the starting resource in the returned results.
*
* @return string
*/
public function getErrorDescription()
{
return $this->error_description;
}
/**
* A URI identifying a human-readable web page with information about the error, used to provide the client developer with additional information about the error.
*
* @param string $error_uri
*/
public function setErrorUri($error_uri)
{
$this->error_uri = $error_uri;
return $this;
}
/**
* A URI identifying a human-readable web page with information about the error, used to provide the client developer with additional information about the error.
*
* @return string
*/
public function getErrorUri()
{
return $this->error_uri;
}
}

View File

@@ -0,0 +1,101 @@
<?php
namespace PayPal\Auth\Openid;
use PayPal\Core\PPConstants;
use PayPal\Rest\ApiContext;
class PPOpenIdSession
{
/**
* Returns the PayPal URL to which the user must be redirected to
* start the authentication / authorization process.
*
* @param string $redirectUri Uri on merchant website to where
* the user must be redirected to post paypal login
* @param array $scope The access privilges that you are requesting for
* from the user. Pass empty array for all scopes.
* @param string $clientId client id from developer portal
* See https://developer.paypal.com/webapps/developer/docs/integration/direct/log-in-with-paypal/detailed/#attributes for more
* @param ApiContext $apiContext Optional API Context
*/
public static function getAuthorizationUrl($redirectUri, $scope, $clientId, $nonce = null, $state = null, $apiContext = null)
{
$apiContext = $apiContext ? $apiContext : new ApiContext();
$config = $apiContext->getConfig();
if ($apiContext->get($clientId)) {
$clientId = $apiContext->get($clientId);
}
$scope = count($scope) != 0 ? $scope : array('openid', 'profile', 'address', 'email', 'phone',
'https://uri.paypal.com/services/paypalattributes', 'https://uri.paypal.com/services/expresscheckout');
if (!in_array('openid', $scope)) {
$scope[] = 'openid';
}
$params = array(
'client_id' => $clientId,
'response_type' => 'code',
'scope' => implode(" ", $scope),
'redirect_uri' => $redirectUri
);
if ($nonce) {
$params['nonce'] = $nonce;
}
if ($state) {
$params['state'] = $state;
}
return sprintf("%s/v1/authorize?%s", self::getBaseUrl($config), http_build_query($params));
}
/**
* Returns the URL to which the user must be redirected to
* logout from the OpenID provider (i.e. PayPal)
*
* @param string $redirectUri Uri on merchant website to where
* the user must be redirected to post logout
* @param string $idToken id_token from the TokenInfo object
* @param ApiContext $apiContext Optional API Context
* @return string logout URL
*/
public static function getLogoutUrl($redirectUri, $idToken, $apiContext = null)
{
if (is_null($apiContext)) {
$apiContext = new ApiContext();
}
$config = $apiContext->getConfig();
$params = array(
'id_token' => $idToken,
'redirect_uri' => $redirectUri,
'logout' => 'true'
);
return sprintf("%s/v1/endsession?%s", self::getBaseUrl($config), http_build_query($params));
}
/**
* Gets the base URL for the Redirect URI
*
* @param $config
* @return null|string
*/
private static function getBaseUrl($config)
{
if (array_key_exists('openid.RedirectUri', $config)) {
return $config['openid.RedirectUri'];
} else if (array_key_exists('mode', $config)) {
switch (strtoupper($config['mode'])) {
case 'SANDBOX':
return PPConstants::OPENID_REDIRECT_SANDBOX_URL;
case 'LIVE':
return PPConstants::OPENID_REDIRECT_LIVE_URL;
}
}
return null;
}
}

View File

@@ -0,0 +1,230 @@
<?php
namespace PayPal\Auth\Openid;
use PayPal\Common\ResourceModel;
use PayPal\Rest\ApiContext;
use PayPal\Transport\PPRestCall;
/**
* Token grant resource
*/
class PPOpenIdTokeninfo extends ResourceModel
{
/**
* OPTIONAL, if identical to the scope requested by the client; otherwise, REQUIRED.
*
* @param string $scope
*/
public function setScope($scope)
{
$this->scope = $scope;
return $this;
}
/**
* OPTIONAL, if identical to the scope requested by the client; otherwise, REQUIRED.
*
* @return string
*/
public function getScope()
{
return $this->scope;
}
/**
* The access token issued by the authorization server.
*
* @param string $access_token
*/
public function setAccessToken($access_token)
{
$this->access_token = $access_token;
return $this;
}
/**
* The access token issued by the authorization server.
*
* @return string
*/
public function getAccessToken()
{
return $this->access_token;
}
/**
* The refresh token, which can be used to obtain new access tokens using the same authorization grant as described in OAuth2.0 RFC6749 in Section 6.
*
* @param string $refresh_token
*/
public function setRefreshToken($refresh_token)
{
$this->refresh_token = $refresh_token;
return $this;
}
/**
* The refresh token, which can be used to obtain new access tokens using the same authorization grant as described in OAuth2.0 RFC6749 in Section 6.
*
* @return string
*/
public function getRefreshToken()
{
return $this->refresh_token;
}
/**
* The type of the token issued as described in OAuth2.0 RFC6749 (Section 7.1). Value is case insensitive.
*
* @param string $token_type
*/
public function setTokenType($token_type)
{
$this->token_type = $token_type;
return $this;
}
/**
* The type of the token issued as described in OAuth2.0 RFC6749 (Section 7.1). Value is case insensitive.
*
* @return string
*/
public function getTokenType()
{
return $this->token_type;
}
/**
* The id_token is a session token assertion that denotes the user's authentication status
*
* @param string $id_token
*/
public function setIdToken($id_token)
{
$this->id_token = $id_token;
return $this;
}
/**
* The id_token is a session token assertion that denotes the user's authentication status
*
* @return string
*/
public function getIdToken()
{
return $this->id_token;
}
/**
* The lifetime in seconds of the access token.
*
* @param integer $expires_in
*/
public function setExpiresIn($expires_in)
{
$this->expires_in = $expires_in;
return $this;
}
/**
* The lifetime in seconds of the access token.
*
* @return integer
*/
public function getExpiresIn()
{
return $this->expires_in;
}
/**
* Creates an Access Token from an Authorization Code.
*
* @path /v1/identity/openidconnect/tokenservice
* @method POST
* @param array $params (allowed values are client_id, client_secret, grant_type, code and redirect_uri)
* (required) client_id from developer portal
* (required) client_secret from developer portal
* (required) code is Authorization code previously received from the authorization server
* (required) redirect_uri Redirection endpoint that must match the one provided during the
* authorization request that ended in receiving the authorization code.
* (optional) grant_type is the Token grant type. Defaults to authorization_code
* @param string $clientId
* @param string $clientSecret
* @param ApiContext $apiContext Optional API Context
* @param PPRestCall $restCall
* @return PPOpenIdTokeninfo
*/
public static function createFromAuthorizationCode($params, $clientId = null, $clientSecret = null, $apiContext = null, $restCall = null)
{
static $allowedParams = array('grant_type' => 1, 'code' => 1, 'redirect_uri' => 1);
if (!array_key_exists('grant_type', $params)) {
$params['grant_type'] = 'authorization_code';
}
if ($apiContext->get('client_id')) {
$clientId = $apiContext->get('client_id');
}
if ($apiContext->get('client_secret')) {
$clientSecret = $apiContext->get('client_secret');
}
$json = self::executeCall(
"/v1/identity/openidconnect/tokenservice",
"POST",
http_build_query(array_intersect_key($params, $allowedParams)),
array(
'Content-Type' => 'application/x-www-form-urlencoded',
'Authorization' => 'Basic ' . base64_encode($clientId . ":" . $clientSecret)
),
$apiContext,
$restCall
);
$token = new PPOpenIdTokeninfo();
$token->fromJson($json);
return $token;
}
/**
* Creates an Access Token from an Refresh Token.
*
* @path /v1/identity/openidconnect/tokenservice
* @method POST
* @param array $params (allowed values are grant_type and scope)
* (required) client_id from developer portal
* (required) client_secret from developer portal
* (optional) refresh_token refresh token. If one is not passed, refresh token from the current object is used.
* (optional) grant_type is the Token grant type. Defaults to refresh_token
* (optional) scope is an array that either the same or a subset of the scope passed to the authorization request
* @param APIContext $apiContext Optional API Context
* @return PPOpenIdTokeninfo
*/
public function createFromRefreshToken($params, $apiContext = null)
{
static $allowedParams = array('grant_type' => 1, 'refresh_token' => 1, 'scope' => 1);
if (!array_key_exists('grant_type', $params)) {
$params['grant_type'] = 'refresh_token';
}
if (!array_key_exists('refresh_token', $params)) {
$params['refresh_token'] = $this->getRefreshToken();
}
$json = self::executeCall(
"/v1/identity/openidconnect/tokenservice",
"POST",
http_build_query(array_intersect_key($params, $allowedParams)),
array(
'Content-Type' => 'application/x-www-form-urlencoded',
'Authorization' => 'Basic ' . base64_encode($params['client_id'] . ":" . $params['client_secret'])
),
$apiContext
);
$this->fromJson($json);
return $this;
}
}

View File

@@ -0,0 +1,491 @@
<?php
namespace PayPal\Auth\Openid;
use PayPal\Common\ResourceModel;
use PayPal\Rest\ApiContext;
/**
* OpenIdConnect UserInfo Resource
*/
class PPOpenIdUserinfo extends ResourceModel
{
/**
* Subject - Identifier for the End-User at the Issuer.
*
* @param string $user_id
*/
public function setUserId($user_id)
{
$this->user_id = $user_id;
return $this;
}
/**
* Subject - Identifier for the End-User at the Issuer.
*
* @return string
*/
public function getUserId()
{
return $this->user_id;
}
/**
* Subject - Identifier for the End-User at the Issuer.
*
* @param string $sub
*/
public function setSub($sub)
{
$this->sub = $sub;
return $this;
}
/**
* Subject - Identifier for the End-User at the Issuer.
*
* @return string
*/
public function getSub()
{
return $this->sub;
}
/**
* End-User's full name in displayable form including all name parts, possibly including titles and suffixes, ordered according to the End-User's locale and preferences.
*
* @param string $name
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* End-User's full name in displayable form including all name parts, possibly including titles and suffixes, ordered according to the End-User's locale and preferences.
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Given name(s) or first name(s) of the End-User
*
* @param string $given_name
*/
public function setGivenName($given_name)
{
$this->given_name = $given_name;
return $this;
}
/**
* Given name(s) or first name(s) of the End-User
*
* @return string
*/
public function getGivenName()
{
return $this->given_name;
}
/**
* Surname(s) or last name(s) of the End-User.
*
* @param string $family_name
*/
public function setFamilyName($family_name)
{
$this->family_name = $family_name;
return $this;
}
/**
* Surname(s) or last name(s) of the End-User.
*
* @return string
*/
public function getFamilyName()
{
return $this->family_name;
}
/**
* Middle name(s) of the End-User.
*
* @param string $middle_name
*/
public function setMiddleName($middle_name)
{
$this->middle_name = $middle_name;
return $this;
}
/**
* Middle name(s) of the End-User.
*
* @return string
*/
public function getMiddleName()
{
return $this->middle_name;
}
/**
* URL of the End-User's profile picture.
*
* @param string $picture
*/
public function setPicture($picture)
{
$this->picture = $picture;
return $this;
}
/**
* URL of the End-User's profile picture.
*
* @return string
*/
public function getPicture()
{
return $this->picture;
}
/**
* End-User's preferred e-mail address.
*
* @param string $email
*/
public function setEmail($email)
{
$this->email = $email;
return $this;
}
/**
* End-User's preferred e-mail address.
*
* @return string
*/
public function getEmail()
{
return $this->email;
}
/**
* True if the End-User's e-mail address has been verified; otherwise false.
*
* @param boolean $email_verified
*/
public function setEmailVerified($email_verified)
{
$this->email_verified = $email_verified;
return $this;
}
/**
* True if the End-User's e-mail address has been verified; otherwise false.
*
* @return boolean
*/
public function getEmailVerified()
{
return $this->email_verified;
}
/**
* End-User's gender.
*
* @param string $gender
*/
public function setGender($gender)
{
$this->gender = $gender;
return $this;
}
/**
* End-User's gender.
*
* @return string
*/
public function getGender()
{
return $this->gender;
}
/**
* End-User's birthday, represented as an YYYY-MM-DD format. They year MAY be 0000, indicating it is omited. To represent only the year, YYYY format would be used.
*
* @param string $birthday
*/
public function setBirthday($birthday)
{
$this->birthday = $birthday;
return $this;
}
/**
* End-User's birthday, represented as an YYYY-MM-DD format. They year MAY be 0000, indicating it is omited. To represent only the year, YYYY format would be used.
*
* @return string
*/
public function getBirthday()
{
return $this->birthday;
}
/**
* Time zone database representing the End-User's time zone
*
* @param string $zoneinfo
*/
public function setZoneinfo($zoneinfo)
{
$this->zoneinfo = $zoneinfo;
return $this;
}
/**
* Time zone database representing the End-User's time zone
*
* @return string
*/
public function getZoneinfo()
{
return $this->zoneinfo;
}
/**
* End-User's locale.
*
* @param string $locale
*/
public function setLocale($locale)
{
$this->locale = $locale;
return $this;
}
/**
* End-User's locale.
*
* @return string
*/
public function getLocale()
{
return $this->locale;
}
/**
* End-User's language.
*
* @param string $language
*/
public function setLanguage($language)
{
$this->language = $language;
return $this;
}
/**
* End-User's language.
*
* @return string
*/
public function getLanguage()
{
return $this->language;
}
/**
* End-User's verified status.
*
* @param boolean $verified
*/
public function setVerified($verified)
{
$this->verified = $verified;
return $this;
}
/**
* End-User's verified status.
*
* @return boolean
*/
public function getVerified()
{
return $this->verified;
}
/**
* End-User's preferred telephone number.
*
* @param string $phone_number
*/
public function setPhoneNumber($phone_number)
{
$this->phone_number = $phone_number;
return $this;
}
/**
* End-User's preferred telephone number.
*
* @return string
*/
public function getPhoneNumber()
{
return $this->phone_number;
}
/**
* End-User's preferred address.
*
* @param \PayPal\Auth\Openid\PPOpenIdAddress $address
*/
public function setAddress($address)
{
$this->address = $address;
return $this;
}
/**
* End-User's preferred address.
*
* @return \PayPal\Auth\Openid\PPOpenIdAddress
*/
public function getAddress()
{
return $this->address;
}
/**
* Verified account status.
*
* @param boolean $verified_account
*/
public function setVerifiedAccount($verified_account)
{
$this->verified_account = $verified_account;
return $this;
}
/**
* Verified account status.
*
* @return boolean
*/
public function getVerifiedAccount()
{
return $this->verified_account;
}
/**
* Account type.
*
* @param string $account_type
*/
public function setAccountType($account_type)
{
$this->account_type = $account_type;
return $this;
}
/**
* Account type.
*
* @return string
*/
public function getAccountType()
{
return $this->account_type;
}
/**
* Account holder age range.
*
* @param string $age_range
*/
public function setAgeRange($age_range)
{
$this->age_range = $age_range;
return $this;
}
/**
* Account holder age range.
*
* @return string
*/
public function getAgeRange()
{
return $this->age_range;
}
/**
* Account payer identifier.
*
* @param string $payer_id
*/
public function setPayerId($payer_id)
{
$this->payer_id = $payer_id;
return $this;
}
/**
* Account payer identifier.
*
* @return string
*/
public function getPayerId()
{
return $this->payer_id;
}
/**
* returns user details
*
* @path /v1/identity/openidconnect/userinfo
* @method GET
* @param array $params (allowed values are access_token)
* access_token - access token from the createFromAuthorizationCode / createFromRefreshToken calls
* @param ApiContext $apiContext Optional API Context
* @return PPOpenIdUserinfo
*/
public static function getUserinfo($params, $apiContext = null)
{
static $allowedParams = array('schema' => 1);
if (!array_key_exists('schema', $params)) {
$params['schema'] = 'openid';
}
$requestUrl = "/v1/identity/openidconnect/userinfo?"
. http_build_query(array_intersect_key($params, $allowedParams));
$json = self::executeCall(
$requestUrl,
"GET",
"",
array(
'Authorization' => "Bearer " . $params['access_token'],
'Content-Type' => 'x-www-form-urlencoded'
),
$apiContext
);
$ret = new PPOpenIdUserinfo();
$ret->fromJson($json);
return $ret;
}
}