Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
projectdiscovery
GitHub Repository: projectdiscovery/nuclei
Path: blob/dev/pkg/authprovider/authx/cookies_auth.go
2070 views
1
package authx
2
3
import (
4
"net/http"
5
"slices"
6
7
"github.com/projectdiscovery/retryablehttp-go"
8
)
9
10
var (
11
_ AuthStrategy = &CookiesAuthStrategy{}
12
)
13
14
// CookiesAuthStrategy is a strategy for cookies auth
15
type CookiesAuthStrategy struct {
16
Data *Secret
17
}
18
19
// NewCookiesAuthStrategy creates a new cookies auth strategy
20
func NewCookiesAuthStrategy(data *Secret) *CookiesAuthStrategy {
21
return &CookiesAuthStrategy{Data: data}
22
}
23
24
// Apply applies the cookies auth strategy to the request
25
func (s *CookiesAuthStrategy) Apply(req *http.Request) {
26
for _, cookie := range s.Data.Cookies {
27
c := &http.Cookie{
28
Name: cookie.Key,
29
Value: cookie.Value,
30
}
31
req.AddCookie(c)
32
}
33
}
34
35
// ApplyOnRR applies the cookies auth strategy to the retryable request
36
func (s *CookiesAuthStrategy) ApplyOnRR(req *retryablehttp.Request) {
37
existingCookies := req.Cookies()
38
39
for _, newCookie := range s.Data.Cookies {
40
for i, existing := range existingCookies {
41
if existing.Name == newCookie.Key {
42
existingCookies = slices.Delete(existingCookies, i, i+1)
43
break
44
}
45
}
46
}
47
48
// Clear and reset remaining cookies
49
req.Header.Del("Cookie")
50
for _, cookie := range existingCookies {
51
req.AddCookie(cookie)
52
}
53
// Add new cookies
54
for _, cookie := range s.Data.Cookies {
55
req.AddCookie(&http.Cookie{
56
Name: cookie.Key,
57
Value: cookie.Value,
58
})
59
}
60
}
61
62