mercury/internal/database/token.go
2023-09-04 03:33:13 +02:00

45 lines
830 B
Go

package database
import (
"time"
"github.com/golang-jwt/jwt/v4"
"github.com/oklog/ulid/v2"
)
const InternalApplicationName = "mercury_internal"
const TokenCookieName = "mercury_token"
type Application struct {
ID ulid.ULID
Name string
}
type Token struct {
ID ulid.ULID
AppID ulid.ULID
UserID ulid.ULID
Scopes []string
Expires time.Time
}
func (t Token) ToClaims() TokenClaims {
createdAt := ulid.Time(t.ID.Time())
return TokenClaims{
TokenID: t.ID,
UserID: t.UserID,
RegisteredClaims: jwt.RegisteredClaims{
Issuer: "mercury",
ExpiresAt: jwt.NewNumericDate(t.Expires),
IssuedAt: jwt.NewNumericDate(createdAt),
NotBefore: jwt.NewNumericDate(createdAt),
},
}
}
type TokenClaims struct {
TokenID ulid.ULID `json:"jti"`
UserID ulid.ULID `json:"sub"`
jwt.RegisteredClaims
}