-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
83 lines (57 loc) · 1.72 KB
/
main.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
80
81
82
83
package main
import (
"bytes"
"encoding/base64"
"encoding/json"
"flag"
"fmt"
"os"
"strings"
)
func main() {
accessToken := grabAccessToken()
headerAndPayLoad := getHeaderAndPayLoadFrom(accessToken)
printDecodedHeaderAndPayload(headerAndPayLoad)
}
func printDecodedHeaderAndPayload(headerAndPayLoad []string) {
headerDecoded, err := base64.RawStdEncoding.DecodeString(headerAndPayLoad[0])
if err != nil {
fmt.Println("Not a base64 encoded string ", headerAndPayLoad[0])
os.Exit(1)
}
var prettyJSONHeader bytes.Buffer
json.Indent(&prettyJSONHeader, headerDecoded, "", "\t")
fmt.Println("------------------------------")
fmt.Println("Token Header")
fmt.Println("------------------------------")
fmt.Println(string(prettyJSONHeader.Bytes()))
fmt.Println("------------------------------")
payloadDecoded, err := base64.RawStdEncoding.DecodeString(headerAndPayLoad[1])
if err != nil {
fmt.Println("Not a base64 encoded string ", headerAndPayLoad[1])
os.Exit(1)
}
var prettyJSONPayload bytes.Buffer
json.Indent(&prettyJSONPayload, payloadDecoded, "", "\t")
fmt.Println("------------------------------")
fmt.Println("Token payload")
fmt.Println("------------------------------")
fmt.Println(string(prettyJSONPayload.Bytes()))
}
func getHeaderAndPayLoadFrom(accessToken string) []string {
splittedAccessToken := strings.Split(accessToken, ".")
if len(splittedAccessToken) != 3 {
fmt.Println("Invalid Access Token")
os.Exit(1)
}
return splittedAccessToken[0:2]
}
func grabAccessToken() string {
accessToken := flag.String("accessToken", "", "Gets the Decoded Access Token")
flag.Parse()
if *accessToken == "" {
fmt.Println("--accessToken flag is mandatory")
os.Exit(1)
}
return *accessToken
}