Compare commits
2 commits
4de1344512
...
25ed921736
Author | SHA1 | Date | |
---|---|---|---|
25ed921736 | |||
9db15a648b |
2 changed files with 55 additions and 41 deletions
40
internal/common/types.go
Normal file
40
internal/common/types.go
Normal file
|
@ -0,0 +1,40 @@
|
||||||
|
package common
|
||||||
|
|
||||||
|
// Convenience types
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
type (
|
||||||
|
// PyTimeStamp is a python-style long nano timestamp
|
||||||
|
PyTimestamp time.Time
|
||||||
|
|
||||||
|
// KeepZero is a special float that keeps the trailing zero on marshal
|
||||||
|
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
|
||||||
|
}
|
|
@ -2,47 +2,21 @@ package auth
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
|
||||||
"strings"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/labstack/echo/v4"
|
"github.com/labstack/echo/v4"
|
||||||
|
|
||||||
|
"dynatron.me/x/blasphem/internal/common"
|
||||||
"dynatron.me/x/blasphem/internal/generate"
|
"dynatron.me/x/blasphem/internal/generate"
|
||||||
"dynatron.me/x/blasphem/pkg/auth/provider"
|
"dynatron.me/x/blasphem/pkg/auth/provider"
|
||||||
)
|
)
|
||||||
|
|
||||||
type (
|
type (
|
||||||
TokenType string
|
TokenType string
|
||||||
TokenTimestamp time.Time
|
|
||||||
RefreshTokenID string
|
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 {
|
type RefreshToken struct {
|
||||||
ID RefreshTokenID `json:"id"`
|
ID RefreshTokenID `json:"id"`
|
||||||
UserID UserID `json:"user_id"`
|
UserID UserID `json:"user_id"`
|
||||||
|
@ -50,11 +24,11 @@ type RefreshToken struct {
|
||||||
ClientName *string `json:"client_name"`
|
ClientName *string `json:"client_name"`
|
||||||
ClientIcon *string `json:"client_icon"`
|
ClientIcon *string `json:"client_icon"`
|
||||||
TokenType TokenType `json:"token_type"`
|
TokenType TokenType `json:"token_type"`
|
||||||
CreatedAt *TokenTimestamp `json:"created_at"`
|
CreatedAt *common.PyTimestamp `json:"created_at"`
|
||||||
AccessTokenExpiration ExpireSecs `json:"access_token_expiration"`
|
AccessTokenExpiration common.KeepZero `json:"access_token_expiration"`
|
||||||
Token string `json:"token"`
|
Token string `json:"token"`
|
||||||
JWTKey string `json:"jwt_key"`
|
JWTKey string `json:"jwt_key"`
|
||||||
LastUsedAt *TokenTimestamp `json:"last_used_at"`
|
LastUsedAt *common.PyTimestamp `json:"last_used_at"`
|
||||||
LastUsedIP *string `json:"last_used_ip"`
|
LastUsedIP *string `json:"last_used_ip"`
|
||||||
CredentialID *CredID `json:"credential_id"`
|
CredentialID *CredID `json:"credential_id"`
|
||||||
Version *string `json:"version"`
|
Version *string `json:"version"`
|
||||||
|
|
Loading…
Reference in a new issue