Skip to content

Commit

Permalink
Merge pull request #65 from mutablelogic/v4
Browse files Browse the repository at this point in the history
Updated endpoints
  • Loading branch information
djthorpe authored Jun 4, 2024
2 parents cd13ddc + aaf3169 commit e5dbbb9
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 18 deletions.
7 changes: 4 additions & 3 deletions pkg/handler/auth/endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ func (service *auth) CreateToken(w http.ResponseWriter, r *http.Request) {

// Check for a valid name
req.Name = strings.TrimSpace(req.Name)
if req.Name == "" {
httpresponse.Error(w, http.StatusBadRequest, "missing 'name'")
if !types.IsIdentifier(req.Name) {
httpresponse.Error(w, http.StatusBadRequest, "invalid 'name'")
return
} else if token := service.jar.GetWithName(req.Name); !token.IsZero() {
httpresponse.Error(w, http.StatusConflict, "duplicate 'name'")
Expand All @@ -111,6 +111,7 @@ func (service *auth) CreateToken(w http.ResponseWriter, r *http.Request) {
duration = duration.Truncate(time.Minute)
if duration < time.Minute {
httpresponse.Error(w, http.StatusBadRequest, "invalid 'duration'")
return
} else {
req.Duration.Duration = duration
}
Expand All @@ -123,7 +124,7 @@ func (service *auth) CreateToken(w http.ResponseWriter, r *http.Request) {
return
}

// Add the token
// Add the token to the jar
if err := service.jar.Create(token); err != nil {
httpresponse.Error(w, http.StatusInternalServerError, err.Error())
return
Expand Down
25 changes: 10 additions & 15 deletions pkg/handler/tokenjar/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package tokenjar

import (
"context"
"path/filepath"
"time"

"github.com/mutablelogic/go-server/pkg/provider"
// Packages
provider "github.com/mutablelogic/go-server/pkg/provider"
)

////////////////////////////////////////////////////////////////////////////////
Expand All @@ -27,22 +29,15 @@ func (jar *tokenjar) Run(ctx context.Context) error {
for {
select {
case <-ticker.C:
if err := jar.write(); err != nil {
logger.Print(ctx, err)
if jar.Modified() {
if err := jar.Write(); err != nil {
logger.Print(ctx, err)
} else {
logger.Print(ctx, "Sync %q", filepath.Base(jar.filename))
}
}
case <-ctx.Done():
return jar.write()
return jar.Write()
}
}
}

////////////////////////////////////////////////////////////////////////////////
// PRIVATE METHODS

func (jar *tokenjar) write() error {
if !jar.Modified() {
return nil
} else {
return jar.Write()
}
}

0 comments on commit e5dbbb9

Please sign in to comment.