@mherod/get-cookie 4.4.1 → 4.4.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.husky/commit-msg +0 -0
- package/.husky/pre-commit +10 -0
- package/.husky/pre-push +15 -0
- package/dist/cli.cjs +10 -10
- package/dist/cli.cjs.map +1 -1
- package/dist/index.cjs +11 -11
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +11 -11
- package/dist/index.js.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/examples/README.md +40 -6
- package/examples/cli-examples.sh +0 -0
- package/examples/curl-integration.sh +195 -160
- package/examples/features-demo.sh +260 -0
- package/examples/github-auth.sh +295 -0
- package/examples/quick-start.sh +115 -0
- package/package.json +36 -45
- package/tsup.cli.ts +1 -0
- package/tsup.lib.ts +9 -1
- package/examples/chrome-cookies-demo.sh +0 -117
- package/examples/chrome-profile-demo.sh +0 -126
- package/examples/curl-demo.sh +0 -102
- package/examples/curl-with-url.sh +0 -136
- package/examples/deduplication-demo.sh +0 -110
- package/examples/final-chrome-demo.sh +0 -115
- package/examples/github-api.sh +0 -101
- package/examples/github-private-access.sh +0 -118
- package/examples/list-profiles-demo.sh +0 -120
- package/examples/proper-curl-usage.sh +0 -120
- package/examples/simple-curl.sh +0 -95
- package/examples/test-expired-filtering.sh +0 -68
- package/examples/test-github-access.sh +0 -245
- package/examples/test-github-auth-improved.sh +0 -136
- package/examples/working-curl.sh +0 -117
- package/examples/working-github-auth.sh +0 -87
package/examples/README.md
CHANGED
|
@@ -2,24 +2,49 @@
|
|
|
2
2
|
|
|
3
3
|
This directory contains example scripts demonstrating various ways to use get-cookie.
|
|
4
4
|
|
|
5
|
-
##
|
|
5
|
+
## Shell Script Examples
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
### Quick Start
|
|
8
|
+
- **`quick-start.sh`**: Essential patterns for immediate use. Perfect for getting started quickly with basic cookie extraction and one-liner curl commands. Includes a reusable shell function for your `.bashrc/.zshrc`.
|
|
9
|
+
|
|
10
|
+
### Comprehensive Guides
|
|
11
|
+
- **`curl-integration.sh`**: Complete guide for using get-cookie with curl. Covers everything from basic single-cookie requests to complex multi-cookie authentication, POST requests, and cookie validation. Includes colored output and reusable functions.
|
|
12
|
+
|
|
13
|
+
- **`github-auth.sh`**: Complete GitHub authentication reference. Demonstrates session cookie filtering, endpoint testing, private repository access patterns, and clarifies the difference between web and API authentication.
|
|
14
|
+
|
|
15
|
+
- **`features-demo.sh`**: All CLI features in one place. Demonstrates profile listing and selection, cookie deduplication, expired cookie filtering, browser-specific extraction, and how to combine features.
|
|
16
|
+
|
|
17
|
+
### Development Examples
|
|
18
|
+
- **`cli-examples.sh`**: Basic reference for development. Shows how to use the command-line interface for common tasks using `pnpm tsx` for local development.
|
|
8
19
|
|
|
9
20
|
## TypeScript/Node.js Examples
|
|
10
21
|
|
|
11
|
-
-
|
|
12
|
-
-
|
|
22
|
+
- **`basic-usage.ts`**: Demonstrates basic Node.js module usage
|
|
23
|
+
- **`advanced-usage.ts`**: Shows advanced usage with browser-specific strategies
|
|
13
24
|
|
|
14
25
|
## Running the Examples
|
|
15
26
|
|
|
16
|
-
###
|
|
27
|
+
### Shell Script Examples
|
|
17
28
|
|
|
18
29
|
```bash
|
|
19
30
|
# Make the script executable
|
|
20
|
-
chmod +x
|
|
31
|
+
chmod +x quick-start.sh
|
|
32
|
+
chmod +x curl-integration.sh
|
|
33
|
+
chmod +x github-auth.sh
|
|
34
|
+
chmod +x features-demo.sh
|
|
21
35
|
|
|
22
36
|
# Run the examples
|
|
37
|
+
./quick-start.sh # Quick start guide
|
|
38
|
+
./curl-integration.sh # Comprehensive curl integration
|
|
39
|
+
./github-auth.sh # GitHub authentication patterns
|
|
40
|
+
./features-demo.sh # All CLI features
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
### Development Examples
|
|
44
|
+
|
|
45
|
+
```bash
|
|
46
|
+
# For cli-examples.sh (uses pnpm tsx)
|
|
47
|
+
chmod +x cli-examples.sh
|
|
23
48
|
./cli-examples.sh
|
|
24
49
|
```
|
|
25
50
|
|
|
@@ -34,4 +59,13 @@ ts-node basic-usage.ts
|
|
|
34
59
|
ts-node advanced-usage.ts
|
|
35
60
|
```
|
|
36
61
|
|
|
62
|
+
## Example File Organization
|
|
63
|
+
|
|
64
|
+
The examples are organized by use case:
|
|
65
|
+
|
|
66
|
+
1. **Quick Start** → Start here for immediate practical patterns
|
|
67
|
+
2. **curl Integration** → For HTTP/API automation workflows
|
|
68
|
+
3. **GitHub Auth** → For GitHub-specific authentication scenarios
|
|
69
|
+
4. **Features Demo** → For understanding all CLI capabilities
|
|
70
|
+
|
|
37
71
|
Note: Make sure you have get-cookie installed either globally or as a project dependency before running the examples.
|
package/examples/cli-examples.sh
CHANGED
|
File without changes
|
|
@@ -1,13 +1,10 @@
|
|
|
1
1
|
#!/bin/bash
|
|
2
2
|
|
|
3
3
|
# ============================================================================
|
|
4
|
-
# get-cookie + curl Integration
|
|
5
|
-
#
|
|
6
|
-
#
|
|
7
|
-
#
|
|
8
|
-
# - Testing APIs with real authentication
|
|
9
|
-
# - Automating tasks that require browser sessions
|
|
10
|
-
# - Debugging authentication issues
|
|
4
|
+
# get-cookie + curl Integration Guide
|
|
5
|
+
# ============================================================================
|
|
6
|
+
# Comprehensive guide for using get-cookie with curl for authenticated
|
|
7
|
+
# HTTP requests. This covers all common patterns from basic to advanced.
|
|
11
8
|
# ============================================================================
|
|
12
9
|
|
|
13
10
|
# Colors for output
|
|
@@ -17,73 +14,118 @@ YELLOW='\033[1;33m'
|
|
|
17
14
|
BLUE='\033[0;34m'
|
|
18
15
|
NC='\033[0m' # No Color
|
|
19
16
|
|
|
20
|
-
|
|
21
|
-
GET_COOKIE="get-cookie"
|
|
22
|
-
|
|
23
|
-
echo -e "${BLUE}🍪 get-cookie + curl Integration Examples${NC}"
|
|
17
|
+
echo -e "${BLUE}🍪 get-cookie + curl Integration Guide${NC}"
|
|
24
18
|
echo "================================================"
|
|
19
|
+
echo ""
|
|
25
20
|
|
|
26
21
|
# ============================================================================
|
|
27
|
-
# Example 1:
|
|
22
|
+
# Example 1: Basic Single Cookie with curl
|
|
28
23
|
# ============================================================================
|
|
29
|
-
echo -e "
|
|
30
|
-
echo "
|
|
24
|
+
echo -e "${YELLOW}1️⃣ Basic Single Cookie Extraction${NC}"
|
|
25
|
+
echo "─────────────────────────────────────"
|
|
31
26
|
|
|
32
|
-
# Extract GitHub session cookie
|
|
33
27
|
echo -e "${GREEN}Extracting GitHub session cookie...${NC}"
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
if [ -n "$
|
|
37
|
-
echo "✓ Found GitHub session cookie"
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
echo -e "${GREEN}✓
|
|
28
|
+
USER_SESSION=$(get-cookie user_session github.com --render 2>/dev/null | head -1)
|
|
29
|
+
|
|
30
|
+
if [ -n "$USER_SESSION" ]; then
|
|
31
|
+
echo -e "${GREEN}✓ Found GitHub session cookie${NC}"
|
|
32
|
+
echo " Value: ${USER_SESSION:0:30}... (truncated for security)"
|
|
33
|
+
|
|
34
|
+
# Test with curl
|
|
35
|
+
echo -e "\n${GREEN}Testing with GitHub API...${NC}"
|
|
36
|
+
RESPONSE=$(curl -s \
|
|
37
|
+
-H "Cookie: $USER_SESSION" \
|
|
38
|
+
-H "User-Agent: get-cookie-demo" \
|
|
39
|
+
"https://api.github.com/user")
|
|
40
|
+
|
|
41
|
+
LOGIN=$(echo "$RESPONSE" | jq -r '.login // empty' 2>/dev/null)
|
|
42
|
+
if [ -n "$LOGIN" ]; then
|
|
43
|
+
echo -e "${GREEN}✓ Authenticated as: @$LOGIN${NC}"
|
|
50
44
|
else
|
|
51
|
-
echo -e "${
|
|
45
|
+
echo -e "${YELLOW}⚠ API may not accept browser cookies (use tokens for API)${NC}"
|
|
52
46
|
fi
|
|
53
47
|
else
|
|
54
48
|
echo -e "${RED}✗ No GitHub session cookie found${NC}"
|
|
55
|
-
echo " Make sure you're logged into GitHub in
|
|
49
|
+
echo " Make sure you're logged into GitHub in your browser"
|
|
56
50
|
fi
|
|
51
|
+
echo ""
|
|
57
52
|
|
|
58
53
|
# ============================================================================
|
|
59
|
-
# Example 2:
|
|
54
|
+
# Example 2: The Perfect Pattern - Using --url flag
|
|
60
55
|
# ============================================================================
|
|
61
|
-
echo -e "
|
|
62
|
-
echo "
|
|
56
|
+
echo -e "${YELLOW}2️⃣ The Perfect Pattern: --url flag${NC}"
|
|
57
|
+
echo "─────────────────────────────────────────────"
|
|
63
58
|
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
echo -e "${
|
|
59
|
+
echo -e "${GREEN}The --url flag automatically gets all cookies for a URL:${NC}"
|
|
60
|
+
echo ""
|
|
61
|
+
echo -e "${BLUE}curl -H \"Cookie: \$(get-cookie --url <URL> --render)\" <URL>${NC}"
|
|
62
|
+
echo ""
|
|
63
|
+
|
|
64
|
+
# Reusable function for authenticated requests
|
|
65
|
+
authenticated_curl() {
|
|
66
|
+
local url=$1
|
|
67
|
+
shift # Remove first argument, pass the rest to curl
|
|
67
68
|
|
|
68
|
-
# Get cookies
|
|
69
|
-
|
|
69
|
+
# Get cookies for this specific URL
|
|
70
|
+
local cookies=$(get-cookie --url "$url" --render 2>/dev/null)
|
|
71
|
+
|
|
72
|
+
if [ -n "$cookies" ]; then
|
|
73
|
+
curl -H "Cookie: $cookies" "$@" "$url"
|
|
74
|
+
else
|
|
75
|
+
echo "No cookies found for $url" >&2
|
|
76
|
+
curl "$@" "$url"
|
|
77
|
+
fi
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
echo -e "${GREEN}Reusable function for authenticated requests:${NC}"
|
|
81
|
+
cat << 'EOF'
|
|
82
|
+
authenticated_curl() {
|
|
83
|
+
local url=$1
|
|
84
|
+
shift
|
|
85
|
+
curl -H "Cookie: $(get-cookie --url "$url" --render 2>/dev/null)" "$@" "$url"
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
# Usage:
|
|
89
|
+
# authenticated_curl https://github.com/settings/profile -s | grep username
|
|
90
|
+
# authenticated_curl https://example.com/file.pdf -o file.pdf
|
|
91
|
+
EOF
|
|
92
|
+
echo ""
|
|
93
|
+
|
|
94
|
+
# Test the function
|
|
95
|
+
echo -e "${GREEN}Testing authenticated_curl function:${NC}"
|
|
96
|
+
HTTP_CODE=$(authenticated_curl "https://github.com/settings/profile" -s -o /dev/null -w "%{http_code}" 2>/dev/null)
|
|
97
|
+
if [ "$HTTP_CODE" = "200" ]; then
|
|
98
|
+
echo -e "${GREEN}✓ Successfully accessed protected page (HTTP 200)${NC}"
|
|
99
|
+
elif [ "$HTTP_CODE" = "302" ]; then
|
|
100
|
+
echo -e "${YELLOW}⚠ Redirected to login (HTTP 302)${NC}"
|
|
101
|
+
else
|
|
102
|
+
echo -e "${BLUE}ℹ HTTP $HTTP_CODE${NC}"
|
|
103
|
+
fi
|
|
104
|
+
echo ""
|
|
105
|
+
|
|
106
|
+
# ============================================================================
|
|
107
|
+
# Example 3: Multiple Cookies for Complex Authentication
|
|
108
|
+
# ============================================================================
|
|
109
|
+
echo -e "${YELLOW}3️⃣ Multiple Cookie Authentication${NC}"
|
|
110
|
+
echo "───────────────────────────────────────"
|
|
111
|
+
|
|
112
|
+
echo -e "${GREEN}Extracting all GitHub cookies...${NC}"
|
|
113
|
+
COOKIES_JSON=$(get-cookie % github.com --output json 2>/dev/null)
|
|
70
114
|
|
|
71
115
|
if [ -n "$COOKIES_JSON" ]; then
|
|
72
|
-
# Count cookies
|
|
73
116
|
COOKIE_COUNT=$(echo "$COOKIES_JSON" | jq 'length')
|
|
74
|
-
echo "✓ Found $COOKIE_COUNT GitHub cookies"
|
|
75
|
-
|
|
117
|
+
echo -e "${GREEN}✓ Found $COOKIE_COUNT GitHub cookies${NC}"
|
|
118
|
+
|
|
76
119
|
# Build cookie header with multiple cookies
|
|
77
|
-
COOKIE_HEADER=$(echo "$COOKIES_JSON" | jq -r '.[] | "\(.name)=\(.value)"' |
|
|
78
|
-
|
|
120
|
+
COOKIE_HEADER=$(echo "$COOKIES_JSON" | jq -r '.[] | "\(.name)=\(.value)"' | paste -sd '; ')
|
|
121
|
+
|
|
79
122
|
if [ -n "$COOKIE_HEADER" ]; then
|
|
80
123
|
echo -e "\n${GREEN}Testing with multiple cookies...${NC}"
|
|
81
|
-
# Make request with multiple cookies
|
|
82
124
|
STATUS=$(curl -s -o /dev/null -w "%{http_code}" \
|
|
83
125
|
-H "Cookie: $COOKIE_HEADER" \
|
|
84
126
|
-H "User-Agent: get-cookie-demo" \
|
|
85
|
-
https://github.com/settings/profile)
|
|
86
|
-
|
|
127
|
+
"https://github.com/settings/profile")
|
|
128
|
+
|
|
87
129
|
if [ "$STATUS" = "200" ]; then
|
|
88
130
|
echo -e "${GREEN}✓ Successfully accessed protected page (HTTP $STATUS)${NC}"
|
|
89
131
|
else
|
|
@@ -93,141 +135,78 @@ if [ -n "$COOKIES_JSON" ]; then
|
|
|
93
135
|
else
|
|
94
136
|
echo -e "${RED}✗ Failed to extract cookies${NC}"
|
|
95
137
|
fi
|
|
138
|
+
echo ""
|
|
96
139
|
|
|
97
140
|
# ============================================================================
|
|
98
|
-
# Example
|
|
141
|
+
# Example 4: Download Protected Content
|
|
99
142
|
# ============================================================================
|
|
100
|
-
echo -e "
|
|
101
|
-
echo "
|
|
102
|
-
|
|
103
|
-
# Example: Download a file that requires authentication
|
|
104
|
-
echo -e "${GREEN}Preparing authenticated download...${NC}"
|
|
105
|
-
|
|
106
|
-
# Get session cookie for a specific domain
|
|
107
|
-
DOMAIN="github.com"
|
|
108
|
-
SESSION=$($GET_COOKIE user_session $DOMAIN 2>/dev/null | head -1)
|
|
109
|
-
|
|
110
|
-
if [ -n "$SESSION" ]; then
|
|
111
|
-
echo "✓ Authentication cookie ready"
|
|
112
|
-
|
|
113
|
-
# Example: Download user's avatar (requires authentication for private profiles)
|
|
114
|
-
echo -e "\n${GREEN}Example curl command for authenticated download:${NC}"
|
|
115
|
-
echo -e "${BLUE}curl -H \"Cookie: user_session=\$SESSION\" \\
|
|
116
|
-
-H \"User-Agent: YourApp\" \\
|
|
117
|
-
-o avatar.jpg \\
|
|
118
|
-
https://github.com/username.png${NC}"
|
|
119
|
-
else
|
|
120
|
-
echo -e "${RED}✗ No session cookie available${NC}"
|
|
121
|
-
fi
|
|
122
|
-
|
|
123
|
-
# ============================================================================
|
|
124
|
-
# Example 4: Function for easy cookie extraction
|
|
125
|
-
# ============================================================================
|
|
126
|
-
echo -e "\n${YELLOW}Example 4: Reusable Shell Function${NC}"
|
|
127
|
-
echo "----------------------------------------"
|
|
128
|
-
|
|
129
|
-
# Define a reusable function
|
|
130
|
-
get_cookie_for_curl() {
|
|
131
|
-
local cookie_name=$1
|
|
132
|
-
local domain=$2
|
|
133
|
-
local cookie_value=$($GET_COOKIE "$cookie_name" "$domain" 2>/dev/null | head -1)
|
|
134
|
-
|
|
135
|
-
if [ -n "$cookie_value" ]; then
|
|
136
|
-
echo "$cookie_name=$cookie_value"
|
|
137
|
-
return 0
|
|
138
|
-
else
|
|
139
|
-
echo ""
|
|
140
|
-
return 1
|
|
141
|
-
fi
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
# Demonstrate the function
|
|
145
|
-
echo -e "${GREEN}Using reusable function to build cookie header...${NC}"
|
|
146
|
-
|
|
147
|
-
# Build a cookie header with multiple specific cookies
|
|
148
|
-
COOKIE_HEADER=""
|
|
149
|
-
for cookie in "user_session" "dotcom_user" "_gh_sess"; do
|
|
150
|
-
if cookie_value=$(get_cookie_for_curl "$cookie" "github.com"); then
|
|
151
|
-
if [ -n "$COOKIE_HEADER" ]; then
|
|
152
|
-
COOKIE_HEADER="$COOKIE_HEADER; $cookie_value"
|
|
153
|
-
else
|
|
154
|
-
COOKIE_HEADER="$cookie_value"
|
|
155
|
-
fi
|
|
156
|
-
echo " ✓ Added $cookie"
|
|
157
|
-
else
|
|
158
|
-
echo " ✗ Skipped $cookie (not found)"
|
|
159
|
-
fi
|
|
160
|
-
done
|
|
143
|
+
echo -e "${YELLOW}4️⃣ Download Protected Content${NC}"
|
|
144
|
+
echo "───────────────────────────────────"
|
|
161
145
|
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
146
|
+
echo -e "${GREEN}Example: Download a file that requires authentication${NC}"
|
|
147
|
+
echo ""
|
|
148
|
+
echo -e "${BLUE}curl -H \"Cookie: \$(get-cookie --url <URL> --render)\" \\"
|
|
149
|
+
echo " -H \"User-Agent: YourApp\" \\"
|
|
150
|
+
echo " -o file.pdf \\"
|
|
151
|
+
echo " https://example.com/protected/file.pdf${NC}"
|
|
152
|
+
echo ""
|
|
166
153
|
|
|
167
154
|
# ============================================================================
|
|
168
|
-
# Example 5: POST
|
|
155
|
+
# Example 5: POST Request with Authentication
|
|
169
156
|
# ============================================================================
|
|
170
|
-
echo -e "
|
|
171
|
-
echo "
|
|
157
|
+
echo -e "${YELLOW}5️⃣ POST Request with Authentication${NC}"
|
|
158
|
+
echo "─────────────────────────────────────────"
|
|
172
159
|
|
|
173
160
|
echo -e "${GREEN}Example: Creating a GitHub gist with authentication${NC}"
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
SESSION=$($GET_COOKIE user_session github.com 2>/dev/null | head -1)
|
|
177
|
-
CSRF=$($GET_COOKIE _gh_sess github.com 2>/dev/null | head -1)
|
|
178
|
-
|
|
179
|
-
if [ -n "$SESSION" ]; then
|
|
180
|
-
echo -e "\n${BLUE}Example POST command:${NC}"
|
|
181
|
-
cat << 'EOF'
|
|
161
|
+
echo ""
|
|
162
|
+
cat << 'EOF'
|
|
182
163
|
curl -X POST \
|
|
183
|
-
-H "Cookie:
|
|
164
|
+
-H "Cookie: $(get-cookie --url https://api.github.com/gists --render)" \
|
|
184
165
|
-H "Content-Type: application/json" \
|
|
185
166
|
-H "User-Agent: get-cookie-demo" \
|
|
186
167
|
-d '{
|
|
187
|
-
"description": "Created via
|
|
168
|
+
"description": "Created via get-cookie + curl",
|
|
188
169
|
"public": false,
|
|
189
170
|
"files": {
|
|
190
|
-
"test.
|
|
191
|
-
"content": "Hello from get-cookie
|
|
171
|
+
"test.md": {
|
|
172
|
+
"content": "# Hello from get-cookie!"
|
|
192
173
|
}
|
|
193
174
|
}
|
|
194
175
|
}' \
|
|
195
176
|
https://api.github.com/gists
|
|
196
177
|
EOF
|
|
197
|
-
|
|
198
|
-
echo -e "${RED}✗ Authentication cookies not available${NC}"
|
|
199
|
-
fi
|
|
178
|
+
echo ""
|
|
200
179
|
|
|
201
180
|
# ============================================================================
|
|
202
|
-
# Example 6: Cookie
|
|
181
|
+
# Example 6: Cookie Validation Function
|
|
203
182
|
# ============================================================================
|
|
204
|
-
echo -e "
|
|
205
|
-
echo "
|
|
183
|
+
echo -e "${YELLOW}6️⃣ Cookie Validation${NC}"
|
|
184
|
+
echo "───────────────────────"
|
|
206
185
|
|
|
207
186
|
validate_cookie() {
|
|
208
187
|
local domain=$1
|
|
209
188
|
local cookie_name=$2
|
|
210
|
-
|
|
189
|
+
|
|
211
190
|
echo -e "${GREEN}Validating $cookie_name for $domain...${NC}"
|
|
212
|
-
|
|
191
|
+
|
|
213
192
|
# Get cookie with metadata
|
|
214
|
-
COOKIE_JSON=$(
|
|
215
|
-
|
|
193
|
+
COOKIE_JSON=$(get-cookie "$cookie_name" "$domain" --output json 2>/dev/null)
|
|
194
|
+
|
|
216
195
|
if [ -n "$COOKIE_JSON" ] && [ "$COOKIE_JSON" != "[]" ]; then
|
|
217
196
|
# Parse cookie details
|
|
218
197
|
EXPIRY=$(echo "$COOKIE_JSON" | jq -r '.[0].expiry' 2>/dev/null)
|
|
219
198
|
BROWSER=$(echo "$COOKIE_JSON" | jq -r '.[0].meta.browser' 2>/dev/null)
|
|
220
199
|
DECRYPTED=$(echo "$COOKIE_JSON" | jq -r '.[0].meta.decrypted' 2>/dev/null)
|
|
221
|
-
|
|
200
|
+
|
|
222
201
|
echo " ✓ Cookie found"
|
|
223
202
|
echo " Browser: $BROWSER"
|
|
224
203
|
echo " Decrypted: $DECRYPTED"
|
|
225
|
-
|
|
204
|
+
|
|
226
205
|
# Check if expired
|
|
227
206
|
if [ "$EXPIRY" != "null" ] && [ "$EXPIRY" != "Infinity" ]; then
|
|
228
207
|
EXPIRY_TIMESTAMP=$(date -d "$EXPIRY" +%s 2>/dev/null || date -j -f "%Y-%m-%dT%H:%M:%S" "${EXPIRY%%.*}" +%s 2>/dev/null)
|
|
229
208
|
CURRENT_TIMESTAMP=$(date +%s)
|
|
230
|
-
|
|
209
|
+
|
|
231
210
|
if [ -n "$EXPIRY_TIMESTAMP" ]; then
|
|
232
211
|
if [ "$EXPIRY_TIMESTAMP" -gt "$CURRENT_TIMESTAMP" ]; then
|
|
233
212
|
DAYS_LEFT=$(( ($EXPIRY_TIMESTAMP - $CURRENT_TIMESTAMP) / 86400 ))
|
|
@@ -250,27 +229,83 @@ validate_cookie() {
|
|
|
250
229
|
|
|
251
230
|
# Validate some common cookies
|
|
252
231
|
validate_cookie "github.com" "user_session"
|
|
232
|
+
echo ""
|
|
253
233
|
|
|
254
234
|
# ============================================================================
|
|
255
|
-
#
|
|
235
|
+
# Example 7: Reusable Shell Function for Multiple Cookies
|
|
256
236
|
# ============================================================================
|
|
257
|
-
echo -e "
|
|
258
|
-
echo "
|
|
259
|
-
echo "
|
|
260
|
-
1. ${YELLOW}Security:${NC} Never log or save cookie values in scripts
|
|
261
|
-
2. ${YELLOW}Expiration:${NC} Check cookie expiry before using them
|
|
262
|
-
3. ${YELLOW}User-Agent:${NC} Always set a User-Agent header with curl
|
|
263
|
-
4. ${YELLOW}CSRF Tokens:${NC} Some APIs require CSRF tokens in addition to session cookies
|
|
264
|
-
5. ${YELLOW}Rate Limiting:${NC} Respect API rate limits when using automation
|
|
237
|
+
echo -e "${YELLOW}7️⃣ Reusable Function for Multiple Cookies${NC}"
|
|
238
|
+
echo "───────────────────────────────────────────────"
|
|
265
239
|
|
|
266
|
-
|
|
267
|
-
|
|
240
|
+
cat << 'EOF'
|
|
241
|
+
get_cookie_for_curl() {
|
|
242
|
+
local cookie_name=$1
|
|
243
|
+
local domain=$2
|
|
244
|
+
local cookie_value=$(get-cookie "$cookie_name" "$domain" --render 2>/dev/null | head -1)
|
|
268
245
|
|
|
269
|
-
|
|
270
|
-
|
|
246
|
+
if [ -n "$cookie_value" ]; then
|
|
247
|
+
echo "$cookie_value"
|
|
248
|
+
return 0
|
|
249
|
+
else
|
|
250
|
+
echo ""
|
|
251
|
+
return 1
|
|
252
|
+
fi
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
# Build a cookie header with multiple specific cookies
|
|
256
|
+
COOKIE_HEADER=""
|
|
257
|
+
for cookie in "user_session" "dotcom_user" "_gh_sess"; do
|
|
258
|
+
if cookie_value=$(get_cookie_for_curl "$cookie" "github.com"); then
|
|
259
|
+
if [ -n "$COOKIE_HEADER" ]; then
|
|
260
|
+
COOKIE_HEADER="$COOKIE_HEADER; $cookie_value"
|
|
261
|
+
else
|
|
262
|
+
COOKIE_HEADER="$cookie_value"
|
|
263
|
+
fi
|
|
264
|
+
fi
|
|
265
|
+
done
|
|
266
|
+
|
|
267
|
+
curl -H "Cookie: $COOKIE_HEADER" https://github.com/settings/profile
|
|
268
|
+
EOF
|
|
269
|
+
echo ""
|
|
270
|
+
|
|
271
|
+
# ============================================================================
|
|
272
|
+
# Example 8: Using with Other Tools
|
|
273
|
+
# ============================================================================
|
|
274
|
+
echo -e "${YELLOW}8️⃣ Using with Other HTTP Tools${NC}"
|
|
275
|
+
echo "─────────────────────────────────────"
|
|
271
276
|
|
|
272
|
-
${GREEN}
|
|
273
|
-
${BLUE}wget --header=\"Cookie: \$(get-cookie
|
|
274
|
-
"
|
|
277
|
+
echo -e "${GREEN}With wget:${NC}"
|
|
278
|
+
echo -e "${BLUE}wget --header=\"Cookie: \$(get-cookie --url <URL> --render)\" <URL>${NC}"
|
|
279
|
+
echo ""
|
|
275
280
|
|
|
276
|
-
echo -e "${GREEN}
|
|
281
|
+
echo -e "${GREEN}With HTTPie:${NC}"
|
|
282
|
+
echo -e "${BLUE}http GET <URL> Cookie:\"\$(get-cookie --url <URL> --render)\"${NC}"
|
|
283
|
+
echo ""
|
|
284
|
+
|
|
285
|
+
echo -e "${GREEN}With jq for JSON APIs:${NC}"
|
|
286
|
+
echo -e "${BLUE}curl -H \"Cookie: \$(get-cookie --url <URL> --render)\" <URL> | jq .${NC}"
|
|
287
|
+
echo ""
|
|
288
|
+
|
|
289
|
+
# ============================================================================
|
|
290
|
+
# Tips and Best Practices
|
|
291
|
+
# ============================================================================
|
|
292
|
+
echo -e "${BLUE}📚 Tips and Best Practices${NC}"
|
|
293
|
+
echo "================================================"
|
|
294
|
+
echo ""
|
|
295
|
+
echo "1. ${YELLOW}Security:${NC} Never log or save cookie values in scripts"
|
|
296
|
+
echo "2. ${YELLOW}Expiration:${NC} Check cookie expiry before using them"
|
|
297
|
+
echo "3. ${YELLOW}User-Agent:${NC} Always set a User-Agent header with curl"
|
|
298
|
+
echo "4. ${YELLOW}CSRF Tokens:${NC} Some APIs require CSRF tokens in addition to session cookies"
|
|
299
|
+
echo "5. ${YELLOW}Rate Limiting:${NC} Respect API rate limits when using automation"
|
|
300
|
+
echo ""
|
|
301
|
+
echo -e "${GREEN}Example one-liner to test authentication:${NC}"
|
|
302
|
+
echo -e "${BLUE}get-cookie --url <URL> --render | xargs -I {} curl -s -H \"Cookie: {}\" <URL> | jq .login${NC}"
|
|
303
|
+
echo ""
|
|
304
|
+
echo -e "${GREEN}Example to save cookies to a file:${NC}"
|
|
305
|
+
echo -e "${BLUE}get-cookie % domain.com --output json > cookies.json${NC}"
|
|
306
|
+
echo ""
|
|
307
|
+
echo -e "${GREEN}Example to use with wget instead of curl:${NC}"
|
|
308
|
+
echo -e "${BLUE}wget --header=\"Cookie: \$(get-cookie --url <URL> --render)\" <URL>${NC}"
|
|
309
|
+
echo ""
|
|
310
|
+
|
|
311
|
+
echo -e "${GREEN}✅ Integration guide complete!${NC}"
|