Skip to content

Commit

Permalink
[#66501] linux-client: Add functionality for extracting payloads out …
Browse files Browse the repository at this point in the history
…of JWT tokens

This commit introdcues a function that can extract payloads out of JSON
web tokens generated by the RDFM management server.
  • Loading branch information
Kacper Zienkiewicz committed Oct 3, 2024
1 parent 7cf0ced commit 2f065ec
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions devices/linux-client/daemon/net_utils/net_utils.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package netUtils

import (
"encoding/json"
"errors"
"github.com/golang-jwt/jwt/v5"
"net"
"net/url"
"regexp"
Expand Down Expand Up @@ -67,3 +69,31 @@ func GetMacAddr() (string, error) {
}
return "", errors.New("Failed to get MAC address")
}

type JwtPayload struct {
DeviceId string `json:"device_id"`
CreatedAt int64 `json:"created_at"`
Expires int64 `json:"expires"`
}

func ExtractJwtPayload(tokenStr string) (*JwtPayload, error) {
token, _, err := new(jwt.Parser).ParseUnverified(tokenStr, jwt.MapClaims{})
if err != nil {
return nil, err
}

if payload, ok := token.Claims.(jwt.MapClaims); ok {
var payloadStruct JwtPayload
claimsBytes, err := json.Marshal(payload)
if err != nil {
return nil, err
}
err = json.Unmarshal(claimsBytes, &payloadStruct)
if err != nil {
return nil, err
}
return &payloadStruct, nil
} else {
return nil, errors.New("Failed to extract JWT payload: false type assertion")
}
}

0 comments on commit 2f065ec

Please sign in to comment.