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

fix: claims.update_at is float64 #3867

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
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
46 changes: 45 additions & 1 deletion selfservice/strategy/oidc/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@ package oidc
import (
"context"
"encoding/json"
"fmt"
"math"
"net/http"
"net/url"
"strconv"
"strings"
"time"

"github.com/dghubble/oauth1"
"github.com/pkg/errors"
Expand Down Expand Up @@ -73,14 +77,54 @@ type Claims struct {
Locale Locale `json:"locale,omitempty"`
PhoneNumber string `json:"phone_number,omitempty"`
PhoneNumberVerified bool `json:"phone_number_verified,omitempty"`
UpdatedAt int64 `json:"updated_at,omitempty"`
UpdatedAt Timestamp `json:"updated_at,omitempty"`
HD string `json:"hd,omitempty"`
Team string `json:"team,omitempty"`
Nonce string `json:"nonce,omitempty"`
NonceSupported bool `json:"nonce_supported,omitempty"`
RawClaims map[string]interface{} `json:"raw_claims,omitempty"`
}

type Timestamp int64

func (t *Timestamp) UnmarshalJSON(data []byte) error {
var v interface{}
if err := json.Unmarshal(data, &v); err != nil {
return err
}

switch vv := v.(type) {
case float64:
*t = Timestamp(vv)
return nil
case int64:
*t = Timestamp(vv)
return nil
case string:
fv, err := strconv.ParseFloat(vv, 64)
if err == nil && !math.IsNaN(fv) && !math.IsInf(fv, 0) {
*t = Timestamp(fv)
return nil
}

iv, err := strconv.ParseInt(vv, 10, 64)
if err == nil {
*t = Timestamp(iv)
return nil
}

date, err := time.Parse(time.RFC3339, vv)
if err == nil {
*t = Timestamp(date.UnixMilli())
return nil
}

return fmt.Errorf("unable to parse timestamp: %s", vv)
default:
return fmt.Errorf("unable to parse timestamp: %T", vv)
}
}

type Locale string

func (l *Locale) UnmarshalJSON(data []byte) error {
Expand Down
2 changes: 1 addition & 1 deletion selfservice/strategy/oidc/provider_github.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func (g *ProviderGitHub) Claims(ctx context.Context, exchange *oauth2.Token, que
Website: user.GetBlog(),
Picture: user.GetAvatarURL(),
Profile: user.GetHTMLURL(),
UpdatedAt: user.GetUpdatedAt().Unix(),
UpdatedAt: Timestamp(user.GetUpdatedAt().UnixMilli()),
}

// GitHub does not provide the user's private emails in the call to `/user`. Therefore, if scope "user:email" is set,
Expand Down
2 changes: 1 addition & 1 deletion selfservice/strategy/oidc/provider_github_app.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func (g *ProviderGitHubApp) Claims(ctx context.Context, exchange *oauth2.Token,
Website: user.GetBlog(),
Picture: user.GetAvatarURL(),
Profile: user.GetHTMLURL(),
UpdatedAt: user.GetUpdatedAt().Unix(),
UpdatedAt: Timestamp(user.GetUpdatedAt().UnixMilli()),
}

// GitHub does not provide the user's private emails in the call to `/user`. Therefore, if scope "user:email" is set,
Expand Down
Loading