Skip to content

Commit 26974fa

Browse files
committed
ci: fix linter warnings, update tests
Signed-off-by: Felix Gateru <[email protected]>
1 parent 7e86ffe commit 26974fa

File tree

8 files changed

+130
-127
lines changed

8 files changed

+130
-127
lines changed

http/adapter.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,12 +125,11 @@ func (svc *adapterService) authorize(ctx context.Context, clientKey, domainID, c
125125
return authnRes.GetId(), nil
126126
}
127127

128-
129128
// extractClientSecret returns value of the client secret. If there is no client key - an empty value is returned.
130129
func extractClientSecret(token string) string {
131130
if !strings.HasPrefix(token, apiutil.ClientPrefix) {
132131
return ""
133132
}
134133

135134
return strings.TrimPrefix(token, apiutil.ClientPrefix)
136-
}
135+
}

http/adapter_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
grpcClientsV1 "github.com/absmach/supermq/api/grpc/clients/v1"
1515
chmocks "github.com/absmach/supermq/channels/mocks"
1616
climocks "github.com/absmach/supermq/clients/mocks"
17+
smqhttp "github.com/absmach/supermq/http"
1718
"github.com/absmach/supermq/pkg/connections"
1819
"github.com/absmach/supermq/pkg/errors"
1920
svcerr "github.com/absmach/supermq/pkg/errors/service"
@@ -22,7 +23,6 @@ import (
2223
"github.com/absmach/supermq/pkg/policies"
2324
"github.com/stretchr/testify/assert"
2425
"github.com/stretchr/testify/mock"
25-
smqhttp "github.com/absmach/supermq/http"
2626
)
2727

2828
const (

http/api/endpoint.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,10 @@ func messageHandler(ctx context.Context, svc smqhttp.Service, logger *slog.Logge
4141
return
4242
}
4343

44-
api.EncodeResponse(ctx, w, publishMessageRes{})
45-
44+
err = api.EncodeResponse(ctx, w, publishMessageRes{})
45+
if err != nil {
46+
encodeError(ctx, w, err)
47+
}
4648
}
4749
}
4850

http/api/endpoint_test.go

Lines changed: 103 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -110,14 +110,14 @@ func TestPublish(t *testing.T) {
110110
clients := new(climocks.ClientsServiceClient)
111111
authn := new(authnMocks.Authentication)
112112
channels := new(chmocks.ChannelsServiceClient)
113-
// ctSenmlJSON := "application/senml+json"
114-
// ctSenmlCBOR := "application/senml+cbor"
115-
// ctJSON := "application/json"
113+
ctSenmlJSON := "application/senml+json"
114+
ctSenmlCBOR := "application/senml+cbor"
115+
ctJSON := "application/json"
116116
clientKey := "client_key"
117-
// invalidKey := invalidValue
117+
invalidKey := invalidValue
118118
msg := `[{"n":"current","t":-1,"v":1.6}]`
119-
// msgJSON := `{"field1":"val1","field2":"val2"}`
120-
// msgCBOR := `81A3616E6763757272656E746174206176FB3FF999999999999A`
119+
msgJSON := `{"field1":"val1","field2":"val2"}`
120+
msgCBOR := `81A3616E6763757272656E746174206176FB3FF999999999999A`
121121
svc, pub := newService(authn, clients, channels)
122122
target := newTargetHTTPServer()
123123
defer target.Close()
@@ -141,81 +141,81 @@ func TestPublish(t *testing.T) {
141141
authzErr error
142142
err error
143143
}{
144-
// {
145-
// desc: "publish message successfully",
146-
// domainID: domainID,
147-
// chanID: chanID,
148-
// msg: msg,
149-
// contentType: ctSenmlJSON,
150-
// key: clientKey,
151-
// status: http.StatusAccepted,
152-
// authnRes: &grpcClientsV1.AuthnRes{Id: clientID, Authenticated: true},
153-
// authzRes: &grpcChannelsV1.AuthzRes{Authorized: true},
154-
// },
155-
// {
156-
// desc: "publish message with application/senml+cbor content-type",
157-
// domainID: domainID,
158-
// chanID: chanID,
159-
// msg: msgCBOR,
160-
// contentType: ctSenmlCBOR,
161-
// key: clientKey,
162-
// status: http.StatusAccepted,
163-
// authnRes: &grpcClientsV1.AuthnRes{Id: clientID, Authenticated: true},
164-
// authzRes: &grpcChannelsV1.AuthzRes{Authorized: true},
165-
// },
166-
// {
167-
// desc: "publish message with application/json content-type",
168-
// domainID: domainID,
169-
// chanID: chanID,
170-
// msg: msgJSON,
171-
// contentType: ctJSON,
172-
// key: clientKey,
173-
// status: http.StatusAccepted,
174-
// authnRes: &grpcClientsV1.AuthnRes{Id: clientID, Authenticated: true},
175-
// authzRes: &grpcChannelsV1.AuthzRes{Authorized: true},
176-
// },
177-
// {
178-
// desc: "publish message with empty key",
179-
// domainID: domainID,
180-
// chanID: chanID,
181-
// msg: msg,
182-
// contentType: ctSenmlJSON,
183-
// key: "",
184-
// status: http.StatusBadRequest,
185-
// },
186-
// {
187-
// desc: "publish message with basic auth",
188-
// domainID: domainID,
189-
// chanID: chanID,
190-
// msg: msg,
191-
// contentType: ctSenmlJSON,
192-
// key: clientKey,
193-
// basicAuth: true,
194-
// status: http.StatusAccepted,
195-
// authnRes: &grpcClientsV1.AuthnRes{Id: clientID, Authenticated: true},
196-
// authzRes: &grpcChannelsV1.AuthzRes{Authorized: true},
197-
// },
198-
// {
199-
// desc: "publish message with invalid key",
200-
// domainID: domainID,
201-
// chanID: chanID,
202-
// msg: msg,
203-
// contentType: ctSenmlJSON,
204-
// key: invalidKey,
205-
// status: http.StatusUnauthorized,
206-
// authnRes: &grpcClientsV1.AuthnRes{Authenticated: false},
207-
// },
208-
// {
209-
// desc: "publish message with invalid basic auth",
210-
// domainID: domainID,
211-
// chanID: chanID,
212-
// msg: msg,
213-
// contentType: ctSenmlJSON,
214-
// key: invalidKey,
215-
// basicAuth: true,
216-
// status: http.StatusUnauthorized,
217-
// authnRes: &grpcClientsV1.AuthnRes{Authenticated: false},
218-
// },
144+
{
145+
desc: "publish message successfully",
146+
domainID: domainID,
147+
chanID: chanID,
148+
msg: msg,
149+
contentType: ctSenmlJSON,
150+
key: clientKey,
151+
status: http.StatusAccepted,
152+
authnRes: &grpcClientsV1.AuthnRes{Id: clientID, Authenticated: true},
153+
authzRes: &grpcChannelsV1.AuthzRes{Authorized: true},
154+
},
155+
{
156+
desc: "publish message with application/senml+cbor content-type",
157+
domainID: domainID,
158+
chanID: chanID,
159+
msg: msgCBOR,
160+
contentType: ctSenmlCBOR,
161+
key: clientKey,
162+
status: http.StatusAccepted,
163+
authnRes: &grpcClientsV1.AuthnRes{Id: clientID, Authenticated: true},
164+
authzRes: &grpcChannelsV1.AuthzRes{Authorized: true},
165+
},
166+
{
167+
desc: "publish message with application/json content-type",
168+
domainID: domainID,
169+
chanID: chanID,
170+
msg: msgJSON,
171+
contentType: ctJSON,
172+
key: clientKey,
173+
status: http.StatusAccepted,
174+
authnRes: &grpcClientsV1.AuthnRes{Id: clientID, Authenticated: true},
175+
authzRes: &grpcChannelsV1.AuthzRes{Authorized: true},
176+
},
177+
{
178+
desc: "publish message with empty key",
179+
domainID: domainID,
180+
chanID: chanID,
181+
msg: msg,
182+
contentType: ctSenmlJSON,
183+
key: "",
184+
status: http.StatusBadRequest,
185+
},
186+
{
187+
desc: "publish message with basic auth",
188+
domainID: domainID,
189+
chanID: chanID,
190+
msg: msg,
191+
contentType: ctSenmlJSON,
192+
key: clientKey,
193+
basicAuth: true,
194+
status: http.StatusAccepted,
195+
authnRes: &grpcClientsV1.AuthnRes{Id: clientID, Authenticated: true},
196+
authzRes: &grpcChannelsV1.AuthzRes{Authorized: true},
197+
},
198+
{
199+
desc: "publish message with invalid key",
200+
domainID: domainID,
201+
chanID: chanID,
202+
msg: msg,
203+
contentType: ctSenmlJSON,
204+
key: invalidKey,
205+
status: http.StatusUnauthorized,
206+
authnRes: &grpcClientsV1.AuthnRes{Authenticated: false},
207+
},
208+
{
209+
desc: "publish message with invalid basic auth",
210+
domainID: domainID,
211+
chanID: chanID,
212+
msg: msg,
213+
contentType: ctSenmlJSON,
214+
key: invalidKey,
215+
basicAuth: true,
216+
status: http.StatusUnauthorized,
217+
authnRes: &grpcClientsV1.AuthnRes{Authenticated: false},
218+
},
219219
{
220220
desc: "publish message without content type",
221221
domainID: domainID,
@@ -227,28 +227,28 @@ func TestPublish(t *testing.T) {
227227
authnRes: &grpcClientsV1.AuthnRes{Id: clientID, Authenticated: true},
228228
authzRes: &grpcChannelsV1.AuthzRes{Authorized: true},
229229
},
230-
// {
231-
// desc: "publish message to empty channel",
232-
// domainID: domainID,
233-
// chanID: "",
234-
// msg: msg,
235-
// contentType: ctSenmlJSON,
236-
// key: clientKey,
237-
// status: http.StatusBadRequest,
238-
// authnRes: &grpcClientsV1.AuthnRes{Id: clientID, Authenticated: true},
239-
// authzRes: &grpcChannelsV1.AuthzRes{Authorized: false},
240-
// },
241-
// {
242-
// desc: "publish message with invalid domain ID",
243-
// domainID: invalidValue,
244-
// chanID: chanID,
245-
// msg: msg,
246-
// contentType: ctSenmlJSON,
247-
// key: clientKey,
248-
// status: http.StatusUnauthorized,
249-
// authnRes: &grpcClientsV1.AuthnRes{Id: clientID, Authenticated: true},
250-
// authzRes: &grpcChannelsV1.AuthzRes{Authorized: false},
251-
// },
230+
{
231+
desc: "publish message to empty channel",
232+
domainID: domainID,
233+
chanID: "",
234+
msg: msg,
235+
contentType: ctSenmlJSON,
236+
key: clientKey,
237+
status: http.StatusBadRequest,
238+
authnRes: &grpcClientsV1.AuthnRes{Id: clientID, Authenticated: true},
239+
authzRes: &grpcChannelsV1.AuthzRes{Authorized: false},
240+
},
241+
{
242+
desc: "publish message with invalid domain ID",
243+
domainID: invalidValue,
244+
chanID: chanID,
245+
msg: msg,
246+
contentType: ctSenmlJSON,
247+
key: clientKey,
248+
status: http.StatusUnauthorized,
249+
authnRes: &grpcClientsV1.AuthnRes{Id: clientID, Authenticated: true},
250+
authzRes: &grpcChannelsV1.AuthzRes{Authorized: false},
251+
},
252252
}
253253

254254
for _, tc := range cases {

http/api/transport.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ var (
4747

4848
// MakeHandler returns a HTTP handler for API endpoints.
4949
func MakeHandler(ctx context.Context, svc smqhttp.Service, logger *slog.Logger, instanceID string) http.Handler {
50-
5150
r := chi.NewRouter()
5251

5352
r.HandleFunc("/m/{domainID}/c/{chanID}", messageHandler(ctx, svc, logger))

http/handler.go

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ func (h *handler) AuthPublish(ctx context.Context, topic *string, payload *[]byt
110110
if err != nil {
111111
return err
112112
}
113+
fmt.Println("Got here")
113114

114115
clientID, clientType, err := h.authAccess(ctx, string(s.Password), domainID, chanID, connections.Publish)
115116
if err != nil {
@@ -210,10 +211,25 @@ func (h *handler) Disconnect(ctx context.Context) error {
210211
}
211212

212213
func (h *handler) authAccess(ctx context.Context, token, domainID, chanID string, msgType connections.ConnType) (string, string, error) {
213-
var clientID, clientType string
214+
var clientID, clientType, secret string
214215
switch {
215-
case strings.HasPrefix(string(token), "Client"):
216-
secret := strings.TrimPrefix(string(token), apiutil.ClientPrefix)
216+
case strings.HasPrefix(string(token), apiutil.BearerPrefix):
217+
token := strings.TrimPrefix(string(token), apiutil.BearerPrefix)
218+
authnSession, err := h.authn.Authenticate(ctx, token)
219+
if err != nil {
220+
h.logger.Info(fmt.Sprintf(logInfoFailedAuthNToken, err))
221+
return "", "", mgate.NewHTTPProxyError(http.StatusUnauthorized, svcerr.ErrAuthentication)
222+
}
223+
clientType = policies.UserType
224+
clientID = authnSession.DomainUserID
225+
default:
226+
if token == "" {
227+
return "", "", mgate.NewHTTPProxyError(http.StatusUnauthorized, svcerr.ErrAuthentication)
228+
}
229+
secret = token
230+
if strings.HasPrefix(string(token), "Client") {
231+
secret = strings.TrimPrefix(string(token), apiutil.ClientPrefix)
232+
}
217233
authnRes, err := h.clients.Authenticate(ctx, &grpcClientsV1.AuthnReq{ClientSecret: secret})
218234
if err != nil {
219235
h.logger.Info(fmt.Sprintf(logInfoFailedAuthNClient, secret, err))
@@ -225,17 +241,6 @@ func (h *handler) authAccess(ctx context.Context, token, domainID, chanID string
225241
}
226242
clientType = policies.ClientType
227243
clientID = authnRes.GetId()
228-
case strings.HasPrefix(string(token), apiutil.BearerPrefix):
229-
token := strings.TrimPrefix(string(token), apiutil.BearerPrefix)
230-
authnSession, err := h.authn.Authenticate(ctx, token)
231-
if err != nil {
232-
h.logger.Info(fmt.Sprintf(logInfoFailedAuthNToken, err))
233-
return "", "", mgate.NewHTTPProxyError(http.StatusUnauthorized, svcerr.ErrAuthentication)
234-
}
235-
clientType = policies.UserType
236-
clientID = authnSession.DomainUserID
237-
default:
238-
return "", "", mgate.NewHTTPProxyError(http.StatusUnauthorized, svcerr.ErrAuthentication)
239244
}
240245

241246
ar := &grpcChannelsV1.AuthzReq{

http/middleware/doc.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@
66
//
77
// For more details about tracing instrumentation for SuperMQ messaging refer
88
// to the documentation at https://docs.supermq.abstractmachines.fr/tracing/.
9-
package middleware
9+
package middleware

pkg/sdk/message_test.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,10 +114,8 @@ func TestSendMessage(t *testing.T) {
114114
domainID: domainID,
115115
msg: msg,
116116
secret: "",
117-
authRes: &grpcClientsV1.AuthnRes{Authenticated: false, Id: ""},
118-
authErr: svcerr.ErrAuthentication,
119117
svcErr: nil,
120-
err: errors.NewSDKErrorWithStatus(svcerr.ErrAuthentication, http.StatusUnauthorized),
118+
err: errors.NewSDKErrorWithStatus(errors.Wrap(apiutil.ErrValidation, apiutil.ErrBearerKey), http.StatusBadRequest),
121119
},
122120
{
123121
desc: "publish message with invalid client key",

0 commit comments

Comments
 (0)