Skip to content

Commit e5dbbb9

Browse files
authored
Merge pull request #65 from mutablelogic/v4
Updated endpoints
2 parents cd13ddc + aaf3169 commit e5dbbb9

File tree

2 files changed

+14
-18
lines changed

2 files changed

+14
-18
lines changed

pkg/handler/auth/endpoints.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,8 @@ func (service *auth) CreateToken(w http.ResponseWriter, r *http.Request) {
100100

101101
// Check for a valid name
102102
req.Name = strings.TrimSpace(req.Name)
103-
if req.Name == "" {
104-
httpresponse.Error(w, http.StatusBadRequest, "missing 'name'")
103+
if !types.IsIdentifier(req.Name) {
104+
httpresponse.Error(w, http.StatusBadRequest, "invalid 'name'")
105105
return
106106
} else if token := service.jar.GetWithName(req.Name); !token.IsZero() {
107107
httpresponse.Error(w, http.StatusConflict, "duplicate 'name'")
@@ -111,6 +111,7 @@ func (service *auth) CreateToken(w http.ResponseWriter, r *http.Request) {
111111
duration = duration.Truncate(time.Minute)
112112
if duration < time.Minute {
113113
httpresponse.Error(w, http.StatusBadRequest, "invalid 'duration'")
114+
return
114115
} else {
115116
req.Duration.Duration = duration
116117
}
@@ -123,7 +124,7 @@ func (service *auth) CreateToken(w http.ResponseWriter, r *http.Request) {
123124
return
124125
}
125126

126-
// Add the token
127+
// Add the token to the jar
127128
if err := service.jar.Create(token); err != nil {
128129
httpresponse.Error(w, http.StatusInternalServerError, err.Error())
129130
return

pkg/handler/tokenjar/task.go

+10-15
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ package tokenjar
22

33
import (
44
"context"
5+
"path/filepath"
56
"time"
67

7-
"github.com/mutablelogic/go-server/pkg/provider"
8+
// Packages
9+
provider "github.com/mutablelogic/go-server/pkg/provider"
810
)
911

1012
////////////////////////////////////////////////////////////////////////////////
@@ -27,22 +29,15 @@ func (jar *tokenjar) Run(ctx context.Context) error {
2729
for {
2830
select {
2931
case <-ticker.C:
30-
if err := jar.write(); err != nil {
31-
logger.Print(ctx, err)
32+
if jar.Modified() {
33+
if err := jar.Write(); err != nil {
34+
logger.Print(ctx, err)
35+
} else {
36+
logger.Print(ctx, "Sync %q", filepath.Base(jar.filename))
37+
}
3238
}
3339
case <-ctx.Done():
34-
return jar.write()
40+
return jar.Write()
3541
}
3642
}
3743
}
38-
39-
////////////////////////////////////////////////////////////////////////////////
40-
// PRIVATE METHODS
41-
42-
func (jar *tokenjar) write() error {
43-
if !jar.Modified() {
44-
return nil
45-
} else {
46-
return jar.Write()
47-
}
48-
}

0 commit comments

Comments
 (0)