Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chore: Add json tags to Claims #130

Merged
merged 5 commits into from
Jan 21, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 43 additions & 1 deletion authn/verifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package authn

import (
"context"
"encoding/json"
"errors"
"time"

Expand All @@ -21,7 +22,48 @@ type Claims[T any] struct {
Rest T

// The original raw token
token string
token string `json:"-"`
}

func (c Claims[T]) MarshalJSON() ([]byte, error) {
// Create a combined map with fields from both Claims and Rest
combined := make(map[string]interface{})

// Marshal jwt.Claims to get standard claims
standardClaims, err := json.Marshal(c.Claims)
if err != nil {
return nil, err
}

// Marshal Rest to get custom claims
restClaims, err := json.Marshal(c.Rest)
if err != nil {
return nil, err
}

// Unmarshal both into the combined map
if err := json.Unmarshal(standardClaims, &combined); err != nil {
return nil, err
}
if err := json.Unmarshal(restClaims, &combined); err != nil {
return nil, err
}

return json.Marshal(combined)
}

func (c *Claims[T]) UnmarshalJSON(data []byte) error {
// Unmarshal into jwt.Claims
if err := json.Unmarshal(data, &c.Claims); err != nil {
return err
}

// Unmarshal into Rest
if err := json.Unmarshal(data, &c.Rest); err != nil {
return err
}

return nil
}

type Verifier[T any] interface {
Expand Down
Loading