Skip to content

Commit f013315

Browse files
committed
Added some more scoping functions
1 parent 99ba24c commit f013315

File tree

5 files changed

+107
-1
lines changed

5 files changed

+107
-1
lines changed

pkg/handler/auth/middleware.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,20 +50,22 @@ func (middleware *auth) Wrap(ctx context.Context, next http.HandlerFunc) http.Ha
5050
authorized := true
5151
if token.IsZero() {
5252
authorized = false
53+
httpresponse.Error(w, http.StatusUnauthorized, "invalid or missing token")
5354
} else if !token.IsValid() {
5455
authorized = false
56+
httpresponse.Error(w, http.StatusUnauthorized, "invalid or missing token")
5557
} else if token.IsScope(ScopeRoot) {
5658
// Allow - token is a super-user token
5759
} else if allowedScopes := router.Scope(r.Context()); len(allowedScopes) == 0 {
5860
// Allow - no scopes have been defined on this endpoint
5961
} else if !token.IsScope(allowedScopes...) {
6062
// Deny - token does not have the required scopes
6163
authorized = false
64+
httpresponse.Error(w, http.StatusUnauthorized, "required scope: ", strings.Join(allowedScopes, ","))
6265
}
6366

6467
// Return unauthorized if token is not found or not valid
6568
if !authorized {
66-
httpresponse.Error(w, http.StatusUnauthorized)
6769
return
6870
}
6971

pkg/handler/router/endpoints.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package router
2+
3+
import (
4+
"context"
5+
"net/http"
6+
"regexp"
7+
8+
// Packages
9+
server "github.com/mutablelogic/go-server"
10+
httpresponse "github.com/mutablelogic/go-server/pkg/httpresponse"
11+
)
12+
13+
///////////////////////////////////////////////////////////////////////////////
14+
// GLOBALS
15+
16+
const (
17+
jsonIndent = 2
18+
)
19+
20+
var (
21+
reRoot = regexp.MustCompile(`^/?$`)
22+
)
23+
24+
///////////////////////////////////////////////////////////////////////////////
25+
// PUBLIC METHODS - ENDPOINTS
26+
27+
// Add endpoints to the router
28+
func (service *router) AddEndpoints(ctx context.Context, r server.Router) {
29+
// Path: /
30+
// Methods: GET
31+
// Scopes: read
32+
// Description: Get router services
33+
r.AddHandlerFuncRe(ctx, reRoot, service.GetScopes, http.MethodGet).(Route).
34+
SetScope(service.ScopeRead()...)
35+
}
36+
37+
///////////////////////////////////////////////////////////////////////////////
38+
// PUBLIC METHODS
39+
40+
// Get registered scopes
41+
func (service *router) GetScopes(w http.ResponseWriter, r *http.Request) {
42+
httpresponse.JSON(w, service.Scopes(), http.StatusOK, jsonIndent)
43+
}

pkg/handler/router/interface.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ type Router interface {
1515
// http status code, which will be 200 on success, 404 or 405 and
1616
// path parameters extracted from the path.
1717
Match(host, method, path string) (*matchedRoute, int)
18+
19+
// Return all known scopes
20+
Scopes() []string
1821
}
1922

2023
type Route interface {

pkg/handler/router/router.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,31 @@ func (router *router) Match(host, method, path string) (*matchedRoute, int) {
184184
return nil, http.StatusMethodNotAllowed
185185
}
186186

187+
func (router *router) Scopes() []string {
188+
scopes := make(map[string]bool)
189+
for _, r := range router.host {
190+
for _, h := range r.prefix {
191+
for _, r := range h.handlers {
192+
for _, s := range r.scopes {
193+
scopes[s] = true
194+
}
195+
}
196+
}
197+
}
198+
199+
// Gather all scopes
200+
result := make([]string, 0, len(scopes))
201+
for scope := range scopes {
202+
result = append(result, scope)
203+
}
204+
205+
// Sort alphabetically
206+
sort.Strings(result)
207+
208+
// Return the result
209+
return result
210+
}
211+
187212
///////////////////////////////////////////////////////////////////////////////
188213
// PRIVATE METHODS
189214

pkg/handler/router/scope.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package router
2+
3+
import (
4+
// Packages
5+
"github.com/mutablelogic/go-server/pkg/version"
6+
)
7+
8+
////////////////////////////////////////////////////////////////////////////////
9+
// GLOBALS
10+
11+
var (
12+
// Prefix
13+
scopePrefix = version.GitSource + "/scope/"
14+
)
15+
16+
////////////////////////////////////////////////////////////////////////////////
17+
// PUBLIC METHODS
18+
19+
func (router *router) ScopeRead() []string {
20+
// Return read (list, get) scopes
21+
return []string{
22+
scopePrefix + router.Label() + "/read",
23+
scopePrefix + defaultName + "/read",
24+
}
25+
}
26+
27+
func (router *router) ScopeWrite() []string {
28+
// Return write (create, delete, update) scopes
29+
return []string{
30+
scopePrefix + router.Label() + "/write",
31+
scopePrefix + defaultName + "/write",
32+
}
33+
}

0 commit comments

Comments
 (0)