diff --git a/textfilters.go b/textfilters.go index 1bd4334..e3ef6d7 100644 --- a/textfilters.go +++ b/textfilters.go @@ -4,6 +4,7 @@ import ( "bufio" "bytes" "context" + "encoding/base64" "encoding/json" "errors" "fmt" @@ -123,6 +124,32 @@ var TextFilters = map[string]TextProcessor{ return TextFilterResponse{Output: result}, nil }, }, + "compact-json": { + Label: "JSON: Compact", + Filter: func(ctx context.Context, input string) (resp TextFilterResponse, err error) { + var ( + dst bytes.Buffer + inBfr bytes.Buffer + outBfr bytes.Buffer + ) + + scnr := bufio.NewScanner(strings.NewReader(input)) + for scnr.Scan() { + line := scnr.Text() + inBfr.WriteString(line) + inBfr.WriteString("\n") + + if err := json.Compact(&outBfr, inBfr.Bytes()); err == nil { + dst.WriteString(outBfr.String()) + dst.WriteString("\n") + inBfr.Reset() + outBfr.Reset() + } + } + + return TextFilterResponse{Output: dst.String() + inBfr.String()}, nil + }, + }, "format-json": { Label: "JSON: Format", Filter: func(ctx context.Context, input string) (resp TextFilterResponse, err error) { @@ -173,6 +200,48 @@ var TextFilters = map[string]TextProcessor{ return TextFilterResponse{Output: string(jsonBytes)}, nil }, }, + "jwt-decode": { + Label: "JWT: Decode", + Filter: func(ctx context.Context, input string) (resp TextFilterResponse, err error) { + tokens := strings.Split(input, ".") + if len(tokens) != 3 { + return TextFilterResponse{}, errors.New("invalid JWT format") + } + + header, err := base64.RawURLEncoding.DecodeString(tokens[0]) + if err != nil { + return TextFilterResponse{}, err + } + payload, err := base64.RawURLEncoding.DecodeString(tokens[1]) + if err != nil { + return TextFilterResponse{}, err + } + + var headerJSON, payloadJSON bytes.Buffer + json.Indent(&headerJSON, header, "", " ") + json.Indent(&payloadJSON, payload, "", " ") + + return TextFilterResponse{Output: headerJSON.String() + "\n" + payloadJSON.String()}, nil + }, + }, + "base64-decode": { + Label: "Base64: Decode", + Filter: func(ctx context.Context, input string) (resp TextFilterResponse, err error) { + dst, err := base64.StdEncoding.DecodeString(input) + if err != nil { + return TextFilterResponse{}, err + } + + return TextFilterResponse{Output: string(dst)}, nil + }, + }, + "base64-encode": { + Label: "Base64: Encode", + Filter: func(ctx context.Context, input string) (resp TextFilterResponse, err error) { + dst := base64.StdEncoding.EncodeToString([]byte(input)) + return TextFilterResponse{Output: dst}, nil + }, + }, "lorem-ipsum": { Label: "Generate: Lorem Ipsum", Filter: func(ctx context.Context, input string) (resp TextFilterResponse, err error) {