yii\authclient\OpenId::validate PHP Method

validate() public method

Performs OpenID verification with the OP.
public validate ( boolean $validateRequiredAttributes = true ) : boolean
$validateRequiredAttributes boolean whether to validate required attributes.
return boolean whether the verification was successful.
    public function validate($validateRequiredAttributes = true)
    {
        $claimedId = $this->getClaimedId();
        if (empty($claimedId)) {
            return false;
        }
        $params = ['openid.assoc_handle' => $this->data['openid_assoc_handle'], 'openid.signed' => $this->data['openid_signed'], 'openid.sig' => $this->data['openid_sig']];
        if (isset($this->data['openid_ns'])) {
            /* We're dealing with an OpenID 2.0 server, so let's set an ns
               Even though we should know location of the endpoint,
               we still need to verify it by discovery, so $server is not set here*/
            $params['openid.ns'] = 'http://specs.openid.net/auth/2.0';
        } elseif (isset($this->data['openid_claimed_id']) && $this->data['openid_claimed_id'] != $this->data['openid_identity']) {
            // If it's an OpenID 1 provider, and we've got claimed_id,
            // we have to append it to the returnUrl, like authUrlV1 does.
            $this->returnUrl .= (strpos($this->returnUrl, '?') ? '&' : '?') . 'openid.claimed_id=' . $claimedId;
        }
        if (!$this->compareUrl($this->data['openid_return_to'], $this->returnUrl)) {
            // The return_to url must match the url of current request.
            return false;
        }
        $serverInfo = $this->discover($claimedId);
        foreach (explode(',', $this->data['openid_signed']) as $item) {
            $value = $this->data['openid_' . str_replace('.', '_', $item)];
            $params['openid.' . $item] = $value;
        }
        $params['openid.mode'] = 'check_authentication';
        $response = $this->sendRequest($serverInfo['url'], 'POST', $params);
        if (preg_match('/is_valid\\s*:\\s*true/i', $response)) {
            if ($validateRequiredAttributes) {
                return $this->validateRequiredAttributes();
            } else {
                return true;
            }
        } else {
            return false;
        }
    }

Usage Example

 /**
  * Performs OpenID auth flow.
  * @param OpenId $client auth client instance.
  * @return Response action response.
  * @throws Exception on failure.
  * @throws HttpException on failure.
  */
 protected function authOpenId($client)
 {
     if (!empty($_REQUEST['openid_mode'])) {
         switch ($_REQUEST['openid_mode']) {
             case 'id_res':
                 if ($client->validate()) {
                     return $this->authSuccess($client);
                 } else {
                     throw new HttpException(400, 'Unable to complete the authentication because the required data was not received.');
                 }
                 break;
             case 'cancel':
                 $this->redirectCancel();
                 break;
             default:
                 throw new HttpException(400);
                 break;
         }
     } else {
         $url = $client->buildAuthUrl();
         return Yii::$app->getResponse()->redirect($url);
     }
     return $this->redirectCancel();
 }