-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: replace map claims with built-in jwt struct
- Loading branch information
Showing
3 changed files
with
67 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,45 @@ | ||
package utils | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"os" | ||
|
||
"github.com/golang-jwt/jwt" | ||
) | ||
|
||
// Given a JSON file, map the contents into any struct dest | ||
func FileMapper[T any](filename string, dest T) error { | ||
file, err := os.ReadFile(filename) | ||
if err != nil { | ||
return fmt.Errorf("%s not found", filename) | ||
} | ||
if err = json.Unmarshal(file, dest); err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
// Given a raw jwt token and an encryption key return the mapped jwt claims or an error | ||
func GetJwtClaims(jwt_token string, key string) (jwt.MapClaims, error) { | ||
token, token_err := jwt.Parse(jwt_token, func(t *jwt.Token) (interface{}, error) { | ||
if _, ok := t.Method.(*jwt.SigningMethodHMAC); !ok { | ||
return nil, fmt.Errorf("invalid signing method") | ||
} | ||
return []byte(key), nil | ||
}) | ||
if token_err != nil { | ||
return nil, fmt.Errorf("could not parse jwt token") | ||
} | ||
|
||
// Get claims stored in parsed JWT token | ||
claims, ok := token.Claims.(jwt.MapClaims) | ||
if !ok { | ||
return nil, fmt.Errorf("could not fetch jwt claims") | ||
} | ||
return claims, nil | ||
} | ||
package utils | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"os" | ||
|
||
"github.com/golang-jwt/jwt" | ||
) | ||
|
||
// Given a JSON file, map the contents into any struct dest | ||
func FileMapper[T any](filename string, dest T) error { | ||
file, err := os.ReadFile(filename) | ||
if err != nil { | ||
return fmt.Errorf("%s not found", filename) | ||
} | ||
if err = json.Unmarshal(file, dest); err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
// Given a raw jwt token and an encryption key return the mapped jwt claims or an error | ||
func GetJwtClaims(jwt_token string, key string) (claims jwt.StandardClaims, err error) { | ||
token, token_err := jwt.Parse(jwt_token, func(t *jwt.Token) (interface{}, error) { | ||
if _, ok := t.Method.(*jwt.SigningMethodHMAC); !ok { | ||
return nil, fmt.Errorf("invalid signing method") | ||
} | ||
return []byte(key), nil | ||
}) | ||
if token_err != nil { | ||
return jwt.StandardClaims{}, fmt.Errorf("could not parse jwt token") | ||
} | ||
|
||
// Get claims_map stored in parsed JWT token | ||
claims_map, ok := token.Claims.(jwt.MapClaims) | ||
if !ok { | ||
return jwt.StandardClaims{}, fmt.Errorf("could not transform claim map to desired object") | ||
} | ||
marshalled_claims, err := json.Marshal(claims_map) | ||
if err != nil { | ||
return jwt.StandardClaims{}, err | ||
} | ||
return claims, json.Unmarshal(marshalled_claims, &claims) | ||
} |