From 9db15a648ba923cd588205bc7afefdc6a9cd67f6 Mon Sep 17 00:00:00 2001 From: Daniel Ponte Date: Sun, 13 Nov 2022 12:03:58 -0500 Subject: [PATCH] move common types --- internal/common/types.go | 37 ++++++++++++++++++++++++++ pkg/auth/session.go | 56 +++++++++++----------------------------- 2 files changed, 52 insertions(+), 41 deletions(-) create mode 100644 internal/common/types.go diff --git a/internal/common/types.go b/internal/common/types.go new file mode 100644 index 0000000..9edcc89 --- /dev/null +++ b/internal/common/types.go @@ -0,0 +1,37 @@ +package common + +// Convenience types + +import ( + "fmt" + "strconv" + "strings" + "time" +) + +type ( + PyTimestamp time.Time + KeepZero float64 +) + +func (f KeepZero) MarshalJSON() ([]byte, error) { + if float64(f) == float64(int(f)) { + return []byte(strconv.FormatFloat(float64(f), 'f', 1, 32)), nil + } + return []byte(strconv.FormatFloat(float64(f), 'f', -1, 32)), nil +} + +const PytTimeFormat = "2006-01-02T15:04:05.999999-07:00" + +func (t *PyTimestamp) MarshalJSON() ([]byte, error) { + rv := fmt.Sprintf("%q", time.Time(*t).Format(PytTimeFormat)) + return []byte(rv), nil +} + +func (t *PyTimestamp) UnmarshalJSON(b []byte) error { + s := strings.Trim(string(b), `"`) + tm, err := time.Parse(PytTimeFormat, s) + *t = PyTimestamp(tm) + + return err +} diff --git a/pkg/auth/session.go b/pkg/auth/session.go index 9532ebd..fd047e5 100644 --- a/pkg/auth/session.go +++ b/pkg/auth/session.go @@ -2,62 +2,36 @@ package auth import ( "encoding/json" - "fmt" "net/http" - "strconv" - "strings" "time" "github.com/labstack/echo/v4" + "dynatron.me/x/blasphem/internal/common" "dynatron.me/x/blasphem/internal/generate" "dynatron.me/x/blasphem/pkg/auth/provider" ) type ( TokenType string - TokenTimestamp time.Time RefreshTokenID string - ExpireSecs float64 ) -func (f ExpireSecs) MarshalJSON() ([]byte, error) { - if float64(f) == float64(int(f)) { - return []byte(strconv.FormatFloat(float64(f), 'f', 1, 32)), nil - } - return []byte(strconv.FormatFloat(float64(f), 'f', -1, 32)), nil -} - -const PytTimeFormat = "2006-01-02T15:04:05.999999-07:00" - -func (t *TokenTimestamp) MarshalJSON() ([]byte, error) { - rv := fmt.Sprintf("%q", time.Time(*t).Format(PytTimeFormat)) - return []byte(rv), nil -} - -func (t *TokenTimestamp) UnmarshalJSON(b []byte) error { - s := strings.Trim(string(b), `"`) - tm, err := time.Parse(PytTimeFormat, s) - *t = TokenTimestamp(tm) - - return err -} - type RefreshToken struct { - ID RefreshTokenID `json:"id"` - UserID UserID `json:"user_id"` - ClientID *ClientID `json:"client_id"` - ClientName *string `json:"client_name"` - ClientIcon *string `json:"client_icon"` - TokenType TokenType `json:"token_type"` - CreatedAt *TokenTimestamp `json:"created_at"` - AccessTokenExpiration ExpireSecs `json:"access_token_expiration"` - Token string `json:"token"` - JWTKey string `json:"jwt_key"` - LastUsedAt *TokenTimestamp `json:"last_used_at"` - LastUsedIP *string `json:"last_used_ip"` - CredentialID *CredID `json:"credential_id"` - Version *string `json:"version"` + ID RefreshTokenID `json:"id"` + UserID UserID `json:"user_id"` + ClientID *ClientID `json:"client_id"` + ClientName *string `json:"client_name"` + ClientIcon *string `json:"client_icon"` + TokenType TokenType `json:"token_type"` + CreatedAt *common.PyTimestamp `json:"created_at"` + AccessTokenExpiration common.KeepZero `json:"access_token_expiration"` + Token string `json:"token"` + JWTKey string `json:"jwt_key"` + LastUsedAt *common.PyTimestamp `json:"last_used_at"` + LastUsedIP *string `json:"last_used_ip"` + CredentialID *CredID `json:"credential_id"` + Version *string `json:"version"` } type AccessSessionStore struct {