Skip to content

Allow CookieLogin to set a cookie when an HTTP request is made #135

Description

@klsoft-web

Description

The 'secure' parameter of the Cookie constructor has a default value of true.
Since CookieLogin does not allow this value to be changed, always add the Secure attribute to prevent the cookie from being sent to the server when an HTTP request is made.
Please note that this issue does not occur on localhost.

Proposed solution:

final class CookieLogin
{
    // ...
    private bool $cookieSecure = true;

    /**
     * Returns a new instance with the specified auto-login cookie name.
     *
     * @param bool $secure Whether the client should send back the cookie only over HTTPS connection.
     */
    public function withCookieSecure(bool $secure): self
    {
        $new = clone $this;
        $new->cookieSecure = $secure;
        return $new;
    }


    public function addCookie(
        CookieLoginIdentityInterface $identity,
        ResponseInterface            $response,
        DateInterval|null|false      $duration = false,
    ): ResponseInterface
    {  
        // ...
        return (new Cookie(name: $this->cookieName, value: $cookieValue, expires: $expires))->withSecure($this->cookieSecure)->addToResponse($response);
    }


    public function expireCookie(ResponseInterface $response): ResponseInterface
    {
        return (new Cookie($this->cookieName))
            ->withSecure($this->cookieSecure)
            ->expire()
            ->addToResponse($response);
    }
}

Package version

2.3.2

PHP version

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions