-
Notifications
You must be signed in to change notification settings - Fork 0
/
handle.go
79 lines (67 loc) · 1.97 KB
/
handle.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package cmd
import (
"bufio"
"encoding/json"
"fmt"
"os"
"strings"
"github.com/dgrijalva/jwt-go"
)
// tokenInfo struct represents the structure of JWT token information
type tokenInfo struct {
Header interface{} `json:"header"` // Header of the JWT token
Payload interface{} `json:"payload"` // Payload (claims) of the JWT token
Signature string `json:"signature"` // Signature of the JWT token
}
// ParseFile reads tokens from a file and returns them as a slice
func ParseFile(fn string) ([]string, error) {
f, err := os.Open(fn)
if err != nil {
return nil, fmt.Errorf("[-] File provided not found\n")
}
defer f.Close()
var tokens []string
scanner := bufio.NewScanner(f)
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
if line != "" {
tokens = append(tokens, line)
}
}
return tokens, nil
}
// ParseTokenInfo parses the header, payload, and signature of a JWT token
// and returns the token info as a JSON string.
func ParseTokenInfo(token string) (string, error) {
parts := strings.Split(token, ".")
if len(parts) != 3 {
return "", fmt.Errorf("invalid token format")
}
headerBytes, err := jwt.DecodeSegment(parts[0])
if err != nil {
return "", fmt.Errorf("error decoding header: %v", err)
}
var header interface{}
if err := json.Unmarshal(headerBytes, &header); err != nil {
return "", fmt.Errorf("error unmarshalling header: %v", err)
}
payloadBytes, err := jwt.DecodeSegment(parts[1])
if err != nil {
return "", fmt.Errorf("error decoding payload: %v", err)
}
var payload interface{}
if err := json.Unmarshal(payloadBytes, &payload); err != nil {
return "", fmt.Errorf("error unmarshalling payload: %v", err)
}
signature := parts[2]
tokenInfo := tokenInfo{
Header: header,
Payload: payload,
Signature: signature,
}
tokenInfoJSON, err := json.MarshalIndent(tokenInfo, "", " ")
if err != nil {
return "", fmt.Errorf("error encoding token info to JSON: %v", err)
}
return string(tokenInfoJSON), nil
}