From 60a075238b66971c9b05689b7d954071bed81199 Mon Sep 17 00:00:00 2001
From: jano3 <jano@uafrica.com>
Date: Tue, 7 Feb 2023 15:05:07 +0200
Subject: [PATCH] Add function to get user and provider ID from JWT

---
 auth/jwt.go | 15 +++++++++++++++
 1 file changed, 15 insertions(+)

diff --git a/auth/jwt.go b/auth/jwt.go
index 705995f..55824e2 100644
--- a/auth/jwt.go
+++ b/auth/jwt.go
@@ -141,3 +141,18 @@ func GetUserIDFromJWTWithoutValidation(jsonWebTokenString string) string {
 	}
 	return jsonWebToken.UserID
 }
+
+// GetUserAndProviderIDFromJWTWithoutValidation gets the userID and providerID from the jsonWebTokenString without validating the
+// signature. Successful execution of this function DOES NOT indicate that the JWT is valid in any way.
+func GetUserAndProviderIDFromJWTWithoutValidation(jsonWebTokenString string) (string, int64) {
+	token, _, err := jwt.NewParser().ParseUnverified(jsonWebTokenString, jwt.MapClaims{})
+	if err != nil {
+		return "", 0
+	}
+
+	jsonWebToken, err := getJsonWebTokenFromTokenClaims(token, false)
+	if err != nil {
+		return "", 0
+	}
+	return jsonWebToken.UserID, jsonWebToken.ProviderID
+}
-- 
GitLab