2022-08-30 02:30:36 +00:00
|
|
|
package repo
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"time"
|
|
|
|
|
2022-09-27 23:52:13 +00:00
|
|
|
"github.com/google/uuid"
|
2022-10-30 04:05:38 +00:00
|
|
|
"github.com/hay-kot/homebox/backend/internal/data/ent"
|
|
|
|
"github.com/hay-kot/homebox/backend/internal/data/ent/authtokens"
|
2022-08-30 02:30:36 +00:00
|
|
|
)
|
|
|
|
|
2022-09-05 08:26:21 +00:00
|
|
|
type TokenRepository struct {
|
2022-08-30 02:30:36 +00:00
|
|
|
db *ent.Client
|
|
|
|
}
|
|
|
|
|
2022-09-27 23:52:13 +00:00
|
|
|
type (
|
|
|
|
UserAuthTokenCreate struct {
|
|
|
|
TokenHash []byte `json:"token"`
|
|
|
|
UserID uuid.UUID `json:"userId"`
|
|
|
|
ExpiresAt time.Time `json:"expiresAt"`
|
|
|
|
}
|
|
|
|
|
|
|
|
UserAuthToken struct {
|
|
|
|
UserAuthTokenCreate
|
|
|
|
CreatedAt time.Time `json:"createdAt"`
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
func (u UserAuthToken) IsExpired() bool {
|
|
|
|
return u.ExpiresAt.Before(time.Now())
|
|
|
|
}
|
|
|
|
|
2022-08-30 02:30:36 +00:00
|
|
|
// GetUserFromToken get's a user from a token
|
2022-09-27 23:52:13 +00:00
|
|
|
func (r *TokenRepository) GetUserFromToken(ctx context.Context, token []byte) (UserOut, error) {
|
2022-08-31 00:40:39 +00:00
|
|
|
user, err := r.db.AuthTokens.Query().
|
2022-08-30 02:30:36 +00:00
|
|
|
Where(authtokens.Token(token)).
|
|
|
|
Where(authtokens.ExpiresAtGTE(time.Now())).
|
|
|
|
WithUser().
|
2022-08-31 00:40:39 +00:00
|
|
|
QueryUser().
|
|
|
|
WithGroup().
|
2022-08-30 02:30:36 +00:00
|
|
|
Only(ctx)
|
|
|
|
|
|
|
|
if err != nil {
|
2022-09-27 23:52:13 +00:00
|
|
|
return UserOut{}, err
|
2022-08-30 02:30:36 +00:00
|
|
|
}
|
|
|
|
|
2022-09-27 23:52:13 +00:00
|
|
|
return mapUserOut(user), nil
|
2022-08-30 02:30:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Creates a token for a user
|
2022-09-27 23:52:13 +00:00
|
|
|
func (r *TokenRepository) CreateToken(ctx context.Context, createToken UserAuthTokenCreate) (UserAuthToken, error) {
|
2022-08-30 02:30:36 +00:00
|
|
|
|
|
|
|
dbToken, err := r.db.AuthTokens.Create().
|
|
|
|
SetToken(createToken.TokenHash).
|
|
|
|
SetUserID(createToken.UserID).
|
|
|
|
SetExpiresAt(createToken.ExpiresAt).
|
|
|
|
Save(ctx)
|
|
|
|
|
|
|
|
if err != nil {
|
2022-09-27 23:52:13 +00:00
|
|
|
return UserAuthToken{}, err
|
2022-08-30 02:30:36 +00:00
|
|
|
}
|
|
|
|
|
2022-09-27 23:52:13 +00:00
|
|
|
return UserAuthToken{
|
|
|
|
UserAuthTokenCreate: UserAuthTokenCreate{
|
|
|
|
TokenHash: dbToken.Token,
|
|
|
|
UserID: createToken.UserID,
|
|
|
|
ExpiresAt: dbToken.ExpiresAt,
|
|
|
|
},
|
|
|
|
CreatedAt: dbToken.CreatedAt,
|
|
|
|
}, nil
|
2022-08-30 02:30:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteToken remove a single token from the database - equivalent to revoke or logout
|
2022-09-05 08:26:21 +00:00
|
|
|
func (r *TokenRepository) DeleteToken(ctx context.Context, token []byte) error {
|
2022-08-30 02:30:36 +00:00
|
|
|
_, err := r.db.AuthTokens.Delete().Where(authtokens.Token(token)).Exec(ctx)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// PurgeExpiredTokens removes all expired tokens from the database
|
2022-09-05 08:26:21 +00:00
|
|
|
func (r *TokenRepository) PurgeExpiredTokens(ctx context.Context) (int, error) {
|
2022-08-30 02:30:36 +00:00
|
|
|
tokensDeleted, err := r.db.AuthTokens.Delete().Where(authtokens.ExpiresAtLTE(time.Now())).Exec(ctx)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return tokensDeleted, nil
|
|
|
|
}
|
|
|
|
|
2022-09-05 08:26:21 +00:00
|
|
|
func (r *TokenRepository) DeleteAll(ctx context.Context) (int, error) {
|
2022-08-30 02:30:36 +00:00
|
|
|
amount, err := r.db.AuthTokens.Delete().Exec(ctx)
|
|
|
|
return amount, err
|
|
|
|
}
|