Skip to content

Commit a01c0d0

Browse files
bietkulsiddharthlatest
authored andcommitted
fix: add authentication to public key routes (#52)
* fix: add authentication to public key routes * fix: minor fixes
1 parent 810bdeb commit a01c0d0

7 files changed

Lines changed: 66 additions & 2 deletions

File tree

model/category/category.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ const (
3232
Rules
3333
Templates
3434
Suggestions
35+
Auth
3536
)
3637

3738
// String is an implementation of Stringer interface that returns the string representation of category.Categories.
@@ -50,6 +51,7 @@ func (c Category) String() string {
5051
"rules",
5152
"templates",
5253
"suggestions",
54+
"auth",
5355
}[c]
5456
}
5557

@@ -87,6 +89,8 @@ func (c *Category) UnmarshalJSON(bytes []byte) error {
8789
*c = Templates
8890
case Suggestions.String():
8991
*c = Suggestions
92+
case Auth.String():
93+
*c = Auth
9094
default:
9195
return fmt.Errorf("invalid category encountered: %v", category)
9296
}
@@ -123,6 +127,8 @@ func (c Category) MarshalJSON() ([]byte, error) {
123127
category = Templates.String()
124128
case Suggestions:
125129
category = Suggestions.String()
130+
case Auth:
131+
category = Auth.String()
126132
default:
127133
return nil, fmt.Errorf("invalid category encountered: %v" + c.String())
128134
}

model/permission/defaults.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ var (
2626
category.Rules,
2727
category.Templates,
2828
category.Suggestions,
29+
category.Auth,
2930
}
3031

3132
defaultOps = []op.Operation{
@@ -53,6 +54,7 @@ var (
5354
TemplatesLimit: 10,
5455
SuggestionsLimit: 10,
5556
StreamsLimit: 10,
57+
AuthLimit: 10,
5658
}
5759

5860
defaultAdminLimits = Limits{
@@ -70,5 +72,6 @@ var (
7072
TemplatesLimit: 30,
7173
SuggestionsLimit: 30,
7274
StreamsLimit: 30,
75+
AuthLimit: 30,
7376
}
7477
)

model/permission/permission.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ type Limits struct {
6666
TemplatesLimit int64 `json:"templates_limit"`
6767
SuggestionsLimit int64 `json:"suggestions_limit"`
6868
StreamsLimit int64 `json:"streams_limit"`
69+
AuthLimit int64 `json:"auth_limit"`
6970
}
7071

7172
// Options is a function type used to define a permission's properties.
@@ -458,6 +459,8 @@ func (p *Permission) GetLimitFor(c category.Category) (int64, error) {
458459
return p.Limits.TemplatesLimit, nil
459460
case category.Suggestions:
460461
return p.Limits.SuggestionsLimit, nil
462+
case category.Auth:
463+
return p.Limits.AuthLimit, nil
461464
case category.Streams:
462465
return p.Limits.StreamsLimit, nil
463466
default:
@@ -569,6 +572,9 @@ func (p *Permission) GetPatch(rolePatched bool) (map[string]interface{}, error)
569572
if p.Limits.StreamsLimit != 0 {
570573
limits["streams_limit"] = p.Limits.StreamsLimit
571574
}
575+
if p.Limits.AuthLimit != 0 {
576+
limits["streams_limit"] = p.Limits.AuthLimit
577+
}
572578

573579
patch["limits"] = limits
574580
}

model/user/defaults.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ var (
2626
category.Rules,
2727
category.Templates,
2828
category.Suggestions,
29+
category.Auth,
2930
}
3031

3132
defaultOps = []op.Operation{

plugins/auth/middleware.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,14 @@ import (
55
"fmt"
66
"log"
77
"net/http"
8+
"os"
89

910
"github.com/appbaseio/arc/middleware"
11+
"github.com/appbaseio/arc/middleware/classify"
12+
"github.com/appbaseio/arc/middleware/validate"
1013
"github.com/appbaseio/arc/model/category"
1114
"github.com/appbaseio/arc/model/credential"
15+
"github.com/appbaseio/arc/model/index"
1216
"github.com/appbaseio/arc/model/op"
1317
"github.com/appbaseio/arc/model/permission"
1418
"github.com/appbaseio/arc/model/user"
@@ -19,6 +23,48 @@ import (
1923
"golang.org/x/crypto/bcrypt"
2024
)
2125

26+
type chain struct {
27+
middleware.Fifo
28+
}
29+
30+
func (c *chain) Wrap(h http.HandlerFunc) http.HandlerFunc {
31+
return c.Adapt(h, list()...)
32+
}
33+
34+
func list() []middleware.Middleware {
35+
return []middleware.Middleware{
36+
classifyCategory,
37+
classifyIndices,
38+
classify.Op(),
39+
BasicAuth(),
40+
validate.Operation(),
41+
validate.Category(),
42+
}
43+
}
44+
45+
func classifyIndices(h http.HandlerFunc) http.HandlerFunc {
46+
return func(w http.ResponseWriter, req *http.Request) {
47+
publicKeyIndex := os.Getenv(envPublicKeyEsIndex)
48+
if publicKeyIndex == "" {
49+
publicKeyIndex = defaultPublicKeyEsIndex
50+
}
51+
ctx := index.NewContext(req.Context(), []string{publicKeyIndex})
52+
req = req.WithContext(ctx)
53+
h(w, req)
54+
}
55+
}
56+
57+
func classifyCategory(h http.HandlerFunc) http.HandlerFunc {
58+
return func(w http.ResponseWriter, req *http.Request) {
59+
permissionCategory := category.Auth
60+
61+
ctx := category.NewContext(req.Context(), &permissionCategory)
62+
req = req.WithContext(ctx)
63+
64+
h(w, req)
65+
}
66+
}
67+
2268
// BasicAuth middleware authenticates each requests against the basic auth credentials.
2369
func BasicAuth() middleware.Middleware {
2470
return Instance().basicAuth

plugins/auth/routes.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,20 @@ import (
77
)
88

99
func (a *Auth) routes() []plugins.Route {
10+
middleware := (&chain{}).Wrap
1011
routes := []plugins.Route{
1112
{
1213
Name: "Get public key",
1314
Methods: []string{http.MethodGet},
1415
Path: "/_public_key",
15-
HandlerFunc: a.getPublicKey(),
16+
HandlerFunc: middleware(a.getPublicKey()),
1617
Description: "GET the public key",
1718
},
1819
{
1920
Name: "Put public key",
2021
Methods: []string{http.MethodPut},
2122
Path: "/_public_key",
22-
HandlerFunc: a.setPublicKey(),
23+
HandlerFunc: middleware(a.setPublicKey()),
2324
Description: "Create or Update the public key",
2425
},
2526
}

plugins/permissions/e2e_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ var defaultAdminLimits = permission.Limits{
4949
TemplatesLimit: 30,
5050
SuggestionsLimit: 30,
5151
StreamsLimit: 30,
52+
AuthLimit: 30,
5253
}
5354

5455
var createPermissionResponse = map[string]interface{}{

0 commit comments

Comments
 (0)