commit 04112f46ff71e5df6aea7a26189757f000d4c70c
parent 1dc59d3ea11042317891142c69303b1fbc340ea4
Author: Vetle Haflan <vetle@haflan.dev>
Date: Thu, 21 Oct 2021 18:24:43 +0200
Add JWT experiment
Diffstat:
A | go/jwt/main.go | | | 63 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1 file changed, 63 insertions(+), 0 deletions(-)
diff --git a/go/jwt/main.go b/go/jwt/main.go
@@ -0,0 +1,63 @@
+package main
+
+import (
+ "crypto/hmac"
+ "crypto/sha256"
+ "encoding/base64"
+ "strings"
+
+ //"encoding/json"
+ "fmt"
+ "os"
+)
+
+var secretKey []byte
+var encodedJWTHeader string
+
+func ComputeHmac256(message string) string {
+ h := hmac.New(sha256.New, secretKey)
+ h.Write([]byte(message))
+ return base64.RawURLEncoding.EncodeToString(h.Sum(nil))
+}
+
+func ComputeJWT(payload string) string {
+ data := encodedJWTHeader + "." + base64.RawURLEncoding.EncodeToString([]byte(payload))
+ return fmt.Sprintf("%v.%v", data, ComputeHmac256(data))
+}
+
+func JWTValid(jwt string) bool {
+ parts := strings.Split(jwt, ".")
+ if len(parts) != 3 {
+ return false
+ }
+ return parts[2] == ComputeHmac256(parts[0]+"."+parts[1])
+}
+
+const useMessage = `Commands:
+verify <jwt> : Verify JWT
+gen <payload> : Generate JWP from arbitrary payload
+`
+
+func main() {
+ key := os.Getenv("JWTEX_KEY")
+ if key == "" {
+ fmt.Println("Please define env var JWTEX_KEY")
+ os.Exit(1)
+ }
+ secretKey = []byte(key)
+ encodedJWTHeader = base64.RawURLEncoding.EncodeToString([]byte(`{"alg":"HS256","typ":"JWT"}`))
+
+ if len(os.Args) < 3 {
+ fmt.Println(useMessage)
+ os.Exit(0)
+ }
+ if os.Args[1] == "gen" {
+ fmt.Println(ComputeJWT(os.Args[2]))
+ } else {
+ if JWTValid(os.Args[2]) {
+ fmt.Println("JWT valid")
+ } else {
+ fmt.Println("JWT invalid!")
+ }
+ }
+}