Skip to content

Commit

Permalink
website: use new secret version of the daypass
Browse files Browse the repository at this point in the history
  • Loading branch information
Wessie committed Apr 2, 2024
1 parent 86e21e3 commit fc466af
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 20 deletions.
8 changes: 4 additions & 4 deletions util/secret/secret_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@ import (
"strconv"
"testing"

"github.com/R-a-dio/valkyrie/util/daypass"
"github.com/R-a-dio/valkyrie/util/secret"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestSecretKeyGeneration(t *testing.T) {
s1, err := daypass.NewSecret(16)
s1, err := secret.NewSecret(16)
require.NoError(t, err)
require.True(t, s1.Equal(s1.Get(nil), nil), "s1 should equal itself")

s2, err := daypass.NewSecret(16)
s2, err := secret.NewSecret(16)
require.NoError(t, err)
require.True(t, s2.Equal(s2.Get(nil), nil), "s2 should equal itself")

Expand All @@ -27,7 +27,7 @@ func TestSecretKeyGeneration(t *testing.T) {
func TestSecretSaltComparison(t *testing.T) {
for i := 1; i < sha256.Size*2; i++ {
t.Run(strconv.Itoa(i), func(t *testing.T) {
s, err := daypass.NewSecret(i)
s, err := secret.NewSecret(i)
require.NoError(t, err)

salt := []byte("testing world")
Expand Down
8 changes: 4 additions & 4 deletions website/admin/home.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@ package admin
import (
"net/http"

"github.com/R-a-dio/valkyrie/util/daypass"
"github.com/R-a-dio/valkyrie/util/secret"
"github.com/R-a-dio/valkyrie/website/middleware"
)

type HomeInput struct {
middleware.Input
Daypass daypass.DaypassInfo
Daypass string
}

func NewHomeInput(r *http.Request, dp *daypass.Daypass) HomeInput {
func NewHomeInput(r *http.Request, dp secret.Secret) HomeInput {
return HomeInput{
Input: middleware.InputFromRequest(r),
Daypass: dp.Info(),
Daypass: dp.Get(nil),
}
}

Expand Down
6 changes: 3 additions & 3 deletions website/admin/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
radio "github.com/R-a-dio/valkyrie"
"github.com/R-a-dio/valkyrie/config"
"github.com/R-a-dio/valkyrie/templates"
"github.com/R-a-dio/valkyrie/util/daypass"
"github.com/R-a-dio/valkyrie/util/secret"
vmiddleware "github.com/R-a-dio/valkyrie/website/middleware"
"github.com/R-a-dio/valkyrie/website/shared"
"github.com/spf13/afero"
Expand All @@ -21,7 +21,7 @@ import (
func NewState(
_ context.Context,
cfg config.Config,
dp *daypass.Daypass,
dp secret.Secret,
newsCache *shared.NewsCache,
storage radio.StorageService,
search radio.SearchService,
Expand All @@ -48,7 +48,7 @@ func NewState(
type State struct {
config.Config

Daypass *daypass.Daypass
Daypass secret.Secret
News *shared.NewsCache
Storage radio.StorageService
Search radio.SearchService
Expand Down
7 changes: 5 additions & 2 deletions website/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
"github.com/R-a-dio/valkyrie/storage"
"github.com/R-a-dio/valkyrie/templates"
"github.com/R-a-dio/valkyrie/util"
"github.com/R-a-dio/valkyrie/util/daypass"
"github.com/R-a-dio/valkyrie/util/secret"
"github.com/R-a-dio/valkyrie/website/admin"
phpapi "github.com/R-a-dio/valkyrie/website/api/php"
v1 "github.com/R-a-dio/valkyrie/website/api/v1"
Expand Down Expand Up @@ -67,7 +67,10 @@ func Execute(ctx context.Context, cfg config.Config) error {
}
executor := siteTemplates.Executor()
// daypass generation
dpass := daypass.New(ctx)
dpass, err := secret.NewSecret(secret.DaypassLength)
if err != nil {
return errors.E(op, err)
}
// search service
searchService, err := search.Open(ctx, cfg)
if err != nil {
Expand Down
6 changes: 3 additions & 3 deletions website/public/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
radio "github.com/R-a-dio/valkyrie"
"github.com/R-a-dio/valkyrie/config"
"github.com/R-a-dio/valkyrie/templates"
"github.com/R-a-dio/valkyrie/util/daypass"
"github.com/R-a-dio/valkyrie/util/secret"
"github.com/R-a-dio/valkyrie/website/shared"
"github.com/rs/zerolog/hlog"

Expand All @@ -17,7 +17,7 @@ import (
func NewState(
ctx context.Context,
cfg config.Config,
dp *daypass.Daypass,
dp secret.Secret,
newsCache *shared.NewsCache,
exec templates.Executor,
manager radio.ManagerService,
Expand All @@ -40,7 +40,7 @@ func NewState(
type State struct {
config.Config

Daypass *daypass.Daypass
Daypass secret.Secret
News *shared.NewsCache
Templates templates.Executor
Manager radio.ManagerService
Expand Down
8 changes: 4 additions & 4 deletions website/public/submit.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
"github.com/R-a-dio/valkyrie/errors"
"github.com/R-a-dio/valkyrie/streamer/audio"
"github.com/R-a-dio/valkyrie/util"
"github.com/R-a-dio/valkyrie/util/daypass"
"github.com/R-a-dio/valkyrie/util/secret"
"github.com/R-a-dio/valkyrie/website/middleware"
"github.com/rs/zerolog/hlog"
)
Expand Down Expand Up @@ -89,7 +89,7 @@ func (s State) canSubmitSong(r *http.Request) (time.Duration, error) {
}

daypass := r.Header.Get(daypassHeader)
if s.Daypass.Is(daypass) { // daypass was used so can submit song
if s.Daypass.Equal(daypass, nil) { // daypass was used so can submit song
return 0, nil
}

Expand Down Expand Up @@ -396,7 +396,7 @@ func NewSubmissionForm(tempdir string, mr *multipart.Reader) (*SubmissionForm, e
// Validate checks if required fields are filled in the SubmissionForm and
// if a daypass was supplied if it was a valid one. Populates sf.Errors with
// any errors that occur and what input field caused it.
func (sf *SubmissionForm) Validate(ts radio.TrackStorage, dp *daypass.Daypass) bool {
func (sf *SubmissionForm) Validate(ts radio.TrackStorage, dp secret.Secret) bool {
sf.Errors = make(map[string]string)
if sf.File == "" {
sf.Errors["track"] = "no temporary file"
Expand All @@ -408,7 +408,7 @@ func (sf *SubmissionForm) Validate(ts radio.TrackStorage, dp *daypass.Daypass) b
sf.Errors["comment"] = "no comment supplied"
}
if sf.Daypass != "" {
sf.IsDaypass = dp.Is(sf.Daypass)
sf.IsDaypass = dp.Equal(sf.Daypass, nil)
if !sf.IsDaypass {
sf.Errors["daypass"] = "daypass invalid"
}
Expand Down

0 comments on commit fc466af

Please sign in to comment.