Validate ID Token

@param $idToken
@return mixed
@throws Exception
6652
protected function validateIdToken($idToken) {
        $segments = explode('.', $idToken);
        if (count($segments) != 3) {
            throw new InvalidIdTokenException('Invalid Token');
        }
        $header = json_decode($this->decodeBase64Url($segments[0]), true);
        if (empty($header)) {
            throw new InvalidIdTokenException('Invalid Token');
        }
        $payload = json_decode($this->decodeBase64Url($segments[1]), true);
        if (empty($payload)) {
            throw new InvalidIdTokenException('Invalid Token');
        }
        $signature = $this->decodeBase64Url($segments[2]);

        $signingInput = implode('.', array($segments[0], $segments[1]));
        $kid = isset($header['kid']) ? $header['kid'] : null;
        switch ($header['alg']) {
            case 'RS256':
            case 'RS384':
            case 'RS512':
                // 署名検証用に公開鍵を取得する
                $publicKey = $this->getPublicKey($kid);

                $algo = 'sha' . substr($header['alg'], 2);
                if (openssl_verify($signingInput, $signature, $publicKey, $algo) != 1) {
                    openssl_free_key($publicKey);
                    throw new InvalidIdTokenException('Signature Mismatch');
                }
                openssl_free_key($publicKey);
                break;
            default:
                throw new InvalidIdTokenException('Unsupported Algorithm: ' . $header['alg']);
        }

        // Check Issuer
        if ($payload['iss'] != 'minecraft.jp') {
            throw new InvalidIdTokenException('Invalid Issuer.');
        }

        // Check Client Id
        if ($payload['aud'] != $this->getClientId()) {
            throw new InvalidIdTokenException('Client ID Mismatch.');
        }

        // Check expired
        $now = time();
        if ($payload['exp'] < $now || $payload['iat'] < $now - 600) {
            throw new InvalidIdTokenException('ID Token expired.');
        }

        // Check nonce
        if ($payload['nonce'] != $this->sessionStorage->read('nonce')) {
            throw new InvalidIdTokenException('Nonce Mismatch.');
        }

        $this->sessionStorage->remove('nonce');

        return $payload;
    }
1156080
function validateToken(bearerToken) {
    var validatedToken = bearerToken;
    if (bearerToken.startsWith('bearer ')) {
        validatedToken = bearerToken.replace('bearer ', '');
    }
    return validatedToken;
}
300398
public function getToken($id)
    {
        if ($id) {
            $ownerClass = $this->tokenConfig['owner'];
            $owner = DataObject::get_by_id($ownerClass, $id);

            if ($owner) {
                $tokenDBColumn = $this->tokenConfig['DBColumn'];
                return $owner->{$tokenDBColumn};
            } else {
                user_error("API Token owner '$ownerClass' not found with ID = $id", E_USER_WARNING);
            }
        } else {
            user_error("TokenAuthenticator::getToken() requires an ID as argument.", E_USER_WARNING);
        }
    }
1323492
private function validate()
    {
        if (false === $this->options['admin'] && !array_key_exists('uid', $this->data)) {
            throw new TokenException('No uid provided in data and admin option not set.');
        }

        if (array_key_exists('uid', $this->data)) {
            $this->validateUid($this->data['uid']);
        }
    }
1109281
private function validateToken(string $id)
    {
        $token = (new Parser())->parse((string)$id);
        //Verifica se o token expirou
        if ($token->isExpired()) {
            return NULL;
        }
        //Verifica a assinatura
        $signer = new Sha256();
        $key = new Key('file://' . config('openid.key'));
        if (!$token->verify($signer, $key)) {
            return NULL;
        }
        //Verifica os dados
        $validation = new ValidationData();
        $validation->setIssuer(config('openid.server'));
        $validation->setAudience(config('openid.client.id'));
        if (!$token->validate($validation)) {
            return NULL;
        }

        return $token;
    }
981650
public function validate($token)
    {
        $autologin = $this->provider->findByToken($token);

        if ($autologin && $this->autologinValid($autologin)) {
            if (config('autologin.count')) {
                $autologin->incrementCount();
            }

            return $autologin;
        }

        return null;
    }
706262
public function getByApiToken(int $id, string $token) : ?Authenticatable
    {
        $model = $this->getModel();

        $user = $model->newQuery()
                      ->where($model->getAuthIdentifierName(), $id)
                      ->first();

        if ($user === null) {
            return null;
        }

        $apiToken = $user->getApiToken();

        return !empty($apiToken) && hash_equals($apiToken, $token) ? $user : null;
    }
313294
private function validateUid($uid)
    {
        if (!is_string($uid)) {
            throw new TokenException(sprintf('The uid must be a string, %s given.', gettype($uid)));
        }

        $uidSize = mb_strlen($uid, '8bit');

        if ($uidSize > static::MAX_UID_SIZE) {
            throw new TokenException(
                sprintf('The provided uid is longer than %d bytes (%d).', static::MAX_UID_SIZE, $uidSize)
            );
        }

        if (0 === $uidSize) {
            throw new TokenException('The provided uid is empty.');
        }
    }
619008
public function setIdToken($id_token)
    {
        $key = array_search('id_token', $this->persistantMap);
        if ($key !== false) {
            $this->store->set('id_token', $id_token);
        }

        $this->id_token = $id_token;

        return $this;
    }
373928
public function validate($authTokenPayload) {

    if($authTokenPayload == null) {
      return false;
    }

    $tokenResponse = $this->tokens->find($authTokenPayload);

    if($tokenResponse == null) {
      return false;
    }

    $user = $this->users->retrieveByID( $tokenResponse->getAuthIdentifier() );

    if($user == null) {
      return false;
    }

    return $user;
  }
1249941
private function isUserTokenAuthenticated()
    {
        try
        {
            $token = $this->TokenUser->getTokenInterface()->get();
            $this->TokenUser->validate($token);
        }
        catch (Exception $exception)
        {
            // We will re-throw this error later if no other authentication types give a successful result
            return $exception;
        }

        return true;
    }
1012838
public function validateAccessToken($accessToken)
    {
        $keys        = $this->getJwtVerificationKeys();
        $tokenClaims = (array)JWT::decode($accessToken, $keys, ['RS256']);

        if ($this->getClientId() != $tokenClaims['aud'] && $this->getClientId() != $tokenClaims['appid']) {
            throw new \RuntimeException('The client_id / audience is invalid!');
        }
        if ($tokenClaims['nbf'] > time() || $tokenClaims['exp'] < time()) {
            // Additional validation is being performed in firebase/JWT itself
            throw new \RuntimeException('The id_token is invalid!');
        }

        if ('common' == $this->tenant) {
            $this->tenant = $tokenClaims['tid'];

            $tenant = $this->getTenantDetails($this->tenant);
            if ($tokenClaims['iss'] != $tenant['issuer']) {
                throw new \RuntimeException('Invalid token issuer!');
            }
        } else {
            $tenant = $this->getTenantDetails($this->tenant);
            if ($tokenClaims['iss'] != $tenant['issuer']) {
                throw new \RuntimeException('Invalid token issuer!');
            }
        }

        return $tokenClaims;
    }
1075589
protected function validateToken($token)
  {
    $hash   = explode(':', config('app.key'))[0];
    $secret = explode(':', config('app.key'))[1];

    if ($hash == 'base64') $secret = base64_decode($secret);

    $validator = new TokenValidator;

    $validator->splitToken($token)
        ->validateExpiration()
        ->validateSignature($secret);

    $payLoad = $validator->getPayload();

    return json_decode($payLoad, true);
  }
468824
public function validateOrFail($secret = null, $algo = null)
    {
        if( ! $this->validate($secret, $algo)) {
            throw new InvalidTokenException('Token is not valid.');
        }

        return true;
    }
949683
public function checkPreviousToken($id, $text = false) {
		$token = $this->peekPrvsToken();
		return $this->checkToken($token, $id, $text);
	}
636896
public function validateToken($userToken, $name = null)
    {
        $token = $this->getToken($name);
        return !is_null($userToken) && $token === $userToken;
    }
631479
public function validateToken($token)
    {
        if (isset($_SESSION[$this->session_name])) {
            if ($_SESSION[$this->session_name] === $token) {
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }