Path: blob/dev/pkg/authprovider/authx/cookies_auth.go
2070 views
package authx12import (3"net/http"4"slices"56"github.com/projectdiscovery/retryablehttp-go"7)89var (10_ AuthStrategy = &CookiesAuthStrategy{}11)1213// CookiesAuthStrategy is a strategy for cookies auth14type CookiesAuthStrategy struct {15Data *Secret16}1718// NewCookiesAuthStrategy creates a new cookies auth strategy19func NewCookiesAuthStrategy(data *Secret) *CookiesAuthStrategy {20return &CookiesAuthStrategy{Data: data}21}2223// Apply applies the cookies auth strategy to the request24func (s *CookiesAuthStrategy) Apply(req *http.Request) {25for _, cookie := range s.Data.Cookies {26c := &http.Cookie{27Name: cookie.Key,28Value: cookie.Value,29}30req.AddCookie(c)31}32}3334// ApplyOnRR applies the cookies auth strategy to the retryable request35func (s *CookiesAuthStrategy) ApplyOnRR(req *retryablehttp.Request) {36existingCookies := req.Cookies()3738for _, newCookie := range s.Data.Cookies {39for i, existing := range existingCookies {40if existing.Name == newCookie.Key {41existingCookies = slices.Delete(existingCookies, i, i+1)42break43}44}45}4647// Clear and reset remaining cookies48req.Header.Del("Cookie")49for _, cookie := range existingCookies {50req.AddCookie(cookie)51}52// Add new cookies53for _, cookie := range s.Data.Cookies {54req.AddCookie(&http.Cookie{55Name: cookie.Key,56Value: cookie.Value,57})58}59}606162