Skip to content

Commit

Permalink
Merge pull request #64 from mutablelogic/v4
Browse files Browse the repository at this point in the history
Fixes to endpoints
  • Loading branch information
djthorpe authored Jun 4, 2024
2 parents 638720d + 015abbb commit cd13ddc
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
29 changes: 19 additions & 10 deletions pkg/handler/auth/endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,19 @@ import (
router "github.com/mutablelogic/go-server/pkg/handler/router"
httprequest "github.com/mutablelogic/go-server/pkg/httprequest"
httpresponse "github.com/mutablelogic/go-server/pkg/httpresponse"
types "github.com/mutablelogic/go-server/pkg/types"
)

///////////////////////////////////////////////////////////////////////////////
// GLOBALS

const (
jsonIndent = 2

// Token should be at least eight bytes (16 chars)
reTokenString = `[a-zA-Z0-9]{16}[a-zA-Z0-9]*`
)

var (
reRoot = regexp.MustCompile(`^/?$`)
reToken = regexp.MustCompile(`^/(` + reTokenString + `)/?$`)
reToken = regexp.MustCompile(`^/(` + types.ReIdentifier + `)/?$`)
)

///////////////////////////////////////////////////////////////////////////////
Expand All @@ -46,13 +44,13 @@ func (service *auth) AddEndpoints(ctx context.Context, router server.Router) {
// Description: Create a new token
router.AddHandlerFuncRe(ctx, reRoot, service.CreateToken, http.MethodPost)

// Path: /<token>
// Path: /<token-name>
// Methods: GET
// Scopes: read // TODO: Add scopes
// Description: Get a token
router.AddHandlerFuncRe(ctx, reToken, service.GetToken, http.MethodGet)

// Path: /<token>
// Path: /<token-name>
// Methods: DELETE, PATCH
// Scopes: write // TODO: Add scopes
// Description: Delete or update a token
Expand All @@ -67,6 +65,7 @@ func (service *auth) ListTokens(w http.ResponseWriter, r *http.Request) {
tokens := service.jar.Tokens()
result := make([]*Token, 0, len(tokens))
for _, token := range tokens {
// Remove the token value
token.Value = ""
result = append(result, &token)
}
Expand All @@ -76,7 +75,7 @@ func (service *auth) ListTokens(w http.ResponseWriter, r *http.Request) {
// Get a token
func (service *auth) GetToken(w http.ResponseWriter, r *http.Request) {
urlParameters := router.Params(r.Context())
token := service.jar.GetWithValue(strings.ToLower(urlParameters[0]))
token := service.jar.GetWithName(strings.ToLower(urlParameters[0]))
if token.IsZero() {
httpresponse.Error(w, http.StatusNotFound)
return
Expand All @@ -103,8 +102,18 @@ func (service *auth) CreateToken(w http.ResponseWriter, r *http.Request) {
req.Name = strings.TrimSpace(req.Name)
if req.Name == "" {
httpresponse.Error(w, http.StatusBadRequest, "missing 'name'")
} else if token := service.jar.GetWithName(req.Name); token.IsValid() {
return
} else if token := service.jar.GetWithName(req.Name); !token.IsZero() {
httpresponse.Error(w, http.StatusConflict, "duplicate 'name'")
return
} else if duration := req.Duration.Duration; duration > 0 {
// Truncate duration to minute, check
duration = duration.Truncate(time.Minute)
if duration < time.Minute {
httpresponse.Error(w, http.StatusBadRequest, "invalid 'duration'")
} else {
req.Duration.Duration = duration
}
}

// Create the token
Expand All @@ -128,10 +137,10 @@ func (service *auth) CreateToken(w http.ResponseWriter, r *http.Request) {
httpresponse.JSON(w, token, http.StatusCreated, jsonIndent)
}

// Update an existing token
// Update (patch, delete) an existing token
func (service *auth) UpdateToken(w http.ResponseWriter, r *http.Request) {
urlParameters := router.Params(r.Context())
token := service.jar.GetWithValue(strings.ToLower(urlParameters[0]))
token := service.jar.GetWithName(strings.ToLower(urlParameters[0]))
if token.IsZero() {
httpresponse.Error(w, http.StatusNotFound)
return
Expand Down
2 changes: 1 addition & 1 deletion pkg/handler/auth/scope.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ import "github.com/mutablelogic/go-server/pkg/version"

var (
// Root scope allows ANY operation
ScopeRoot = version.GitSource + "scope/root"
ScopeRoot = version.GitSource + "/scope/root"
)

0 comments on commit cd13ddc

Please sign in to comment.