private function findJwk(array $jwks, $kid = null)
    {
        if ($this->subArrayHasEmptyFirstItem($jwks, 'keys')) {
            return null;
        }

        if (! $kid) {
            return $jwks['keys'][0];
        }

        foreach ($jwks['keys'] as $key) {
            if (isset($key['kid']) && $key['kid'] === $kid) {
                return $key;
            }
        }

        return null;
    }