package services import ( "database/sql" "errors" "time" "git.jamestombleson.com/jtom38/go-cook/internal/domain" "git.jamestombleson.com/jtom38/go-cook/internal/repositories" "github.com/google/uuid" ) type RefreshToken interface { Create(username string, expiresAt time.Time) (string, error) GetByName(name string) (domain.RefreshTokenEntity, error) Delete(id int64) (int64, error) IsRequestValid(username, refreshToken string, jwtExpiresAt time.Time) error } type RefreshTokenService struct { table repositories.RefreshTokenTable } func NewRefreshTokenService(conn *sql.DB) RefreshTokenService { return RefreshTokenService{ table: repositories.NewRefreshTokenRepository(conn), } } func (rt RefreshTokenService) Create(username string, expiresAt time.Time) (string, error) { token, err := uuid.NewV7() if err != nil { return "", err } rows, err := rt.table.Create(username, token.String(), expiresAt) if err != nil { return "", err } if rows != 1 { return "", errors.New("expected one row but got none") } return token.String(), nil } // Find the saved refresh token for a user and return it if it exists func (rt RefreshTokenService) GetByName(name string) (domain.RefreshTokenEntity, error) { return rt.table.GetByUsername(name) } // This will request that a object is removed from the database func (rt RefreshTokenService) Delete(id int64) (int64, error) { return rt.table.DeleteById(id) } func (rt RefreshTokenService) IsRequestValid(username, refreshToken string, jwtExpiresAt time.Time) error { token, err := rt.GetByName(username) if err != nil { return err } if (token.Token != refreshToken) { return errors.New("the refresh token given does not match") } if (token.ExpiresAt != jwtExpiresAt) { return errors.New("the time when the jwt token expires does not match what was given") } return nil }