mirror of
https://github.com/foomo/sesamy-go.git
synced 2025-10-16 12:35:43 +00:00
41 lines
864 B
Go
41 lines
864 B
Go
package session
|
|
|
|
import (
|
|
"net/http"
|
|
"strings"
|
|
|
|
"github.com/pkg/errors"
|
|
)
|
|
|
|
func ParseGAClientID(r *http.Request) (string, error) {
|
|
cookie, err := r.Cookie("_ga")
|
|
if err != nil {
|
|
return "", errors.Wrap(err, "failed to retrieve _ga cookie")
|
|
}
|
|
|
|
parts := strings.Split(cookie.Value, ".")
|
|
|
|
// validate
|
|
if !strings.HasPrefix(cookie.Value, "GA1.1") || len(parts) < 4 {
|
|
return "", errors.New("invalid _ga cookie value")
|
|
}
|
|
|
|
return parts[2] + "." + parts[3], nil
|
|
}
|
|
|
|
func ParseGASessionID(r *http.Request, id string) (string, error) {
|
|
cookie, err := r.Cookie("_ga_" + id)
|
|
if err != nil {
|
|
return "", errors.Wrap(err, "failed to retrieve _ga cookie")
|
|
}
|
|
|
|
parts := strings.Split(cookie.Value, ".")
|
|
|
|
// validate
|
|
if !strings.HasPrefix(cookie.Value, "GS1.1") || len(parts) < 3 {
|
|
return "", errors.New("invalid _ga cookie value")
|
|
}
|
|
|
|
return parts[2], nil
|
|
}
|