Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NOISSUE - Move API to the common package #2608

Merged
merged 8 commits into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion internal/api/doc.go → api/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
// SPDX-License-Identifier: Apache-2.0

// Package api contains commonly used constants and functions
// for the HTTP endpoints.
// for the HTTP and gRPC API.
package api
15 changes: 5 additions & 10 deletions internal/api/auth.go → api/http/authn.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@
// Copyright (c) Abstract Machines
// SPDX-License-Identifier: Apache-2.0

package api
package http

import (
"context"
"net/http"

"github.com/absmach/supermq/auth"
"github.com/absmach/supermq/pkg/apiutil"
apiutil "github.com/absmach/supermq/api/http/util"
smqauthn "github.com/absmach/supermq/pkg/authn"
"github.com/go-chi/chi/v5"
)

type sessionKeyType string

const (
SessionKey = sessionKeyType("session")
)
const SessionKey = sessionKeyType("session")

func AuthenticateMiddleware(authn smqauthn.Authentication, domainCheck bool) func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
Expand All @@ -27,10 +24,8 @@ func AuthenticateMiddleware(authn smqauthn.Authentication, domainCheck bool) fun
EncodeError(r.Context(), apiutil.ErrBearerToken, w)
return
}
var resp smqauthn.Session
var err error

resp, err = authn.Authenticate(r.Context(), token)
resp, err := authn.Authenticate(r.Context(), token)
if err != nil {
EncodeError(r.Context(), err, w)
return
Expand All @@ -43,7 +38,7 @@ func AuthenticateMiddleware(authn smqauthn.Authentication, domainCheck bool) fun
return
}
resp.DomainID = domain
resp.DomainUserID = auth.EncodeDomainUserID(domain, resp.UserID)
resp.DomainUserID = domain + "_" + resp.UserID
}

ctx := context.WithValue(r.Context(), SessionKey, resp)
Expand Down
4 changes: 2 additions & 2 deletions internal/api/common.go → api/http/common.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
// Copyright (c) Abstract Machines
// SPDX-License-Identifier: Apache-2.0

package api
package http

import (
"context"
"encoding/json"
"net/http"

"github.com/absmach/supermq"
apiutil "github.com/absmach/supermq/api/http/util"
"github.com/absmach/supermq/bootstrap"
"github.com/absmach/supermq/certs"
"github.com/absmach/supermq/clients"
"github.com/absmach/supermq/groups"
"github.com/absmach/supermq/pkg/apiutil"
"github.com/absmach/supermq/pkg/errors"
svcerr "github.com/absmach/supermq/pkg/errors/service"
"github.com/absmach/supermq/users"
Expand Down
6 changes: 3 additions & 3 deletions internal/api/common_test.go → api/http/common_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) Abstract Machines
// SPDX-License-Identifier: Apache-2.0

package api_test
package http_test

import (
"context"
Expand All @@ -11,9 +11,9 @@ import (
"time"

"github.com/absmach/supermq"
"github.com/absmach/supermq/internal/api"
api "github.com/absmach/supermq/api/http"
apiutil "github.com/absmach/supermq/api/http/util"
"github.com/absmach/supermq/internal/testsutil"
"github.com/absmach/supermq/pkg/apiutil"
"github.com/absmach/supermq/pkg/errors"
svcerr "github.com/absmach/supermq/pkg/errors/service"
"github.com/stretchr/testify/assert"
Expand Down
6 changes: 6 additions & 0 deletions api/http/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Copyright (c) Abstract Machines
// SPDX-License-Identifier: Apache-2.0

// Package api contains commonly used constants and functions
// for the HTTP API.
package http
2 changes: 1 addition & 1 deletion pkg/apiutil/errors.go → api/http/util/errors.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) Abstract Machines
// SPDX-License-Identifier: Apache-2.0

package apiutil
package util

import "github.com/absmach/supermq/pkg/errors"

Expand Down
2 changes: 1 addition & 1 deletion pkg/apiutil/responses.go → api/http/util/responses.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) Abstract Machines
// SPDX-License-Identifier: Apache-2.0

package apiutil
package util

// ErrorRes represents the HTTP error response body.
type ErrorRes struct {
Expand Down
2 changes: 1 addition & 1 deletion pkg/apiutil/token.go → api/http/util/token.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) Abstract Machines
// SPDX-License-Identifier: Apache-2.0

package apiutil
package util

import (
"net/http"
Expand Down
4 changes: 2 additions & 2 deletions pkg/apiutil/token_test.go → api/http/util/token_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
// Copyright (c) Abstract Machines
// SPDX-License-Identifier: Apache-2.0

package apiutil_test
package util_test

import (
"net/http"
"testing"

"github.com/absmach/supermq/pkg/apiutil"
apiutil "github.com/absmach/supermq/api/http/util"
"github.com/stretchr/testify/assert"
)

Expand Down
2 changes: 1 addition & 1 deletion pkg/apiutil/transport.go → api/http/util/transport.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) Abstract Machines
// SPDX-License-Identifier: Apache-2.0

package apiutil
package util

import (
"context"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) Abstract Machines
// SPDX-License-Identifier: Apache-2.0

package apiutil_test
package util_test

import (
"context"
Expand All @@ -11,8 +11,8 @@ import (
"net/url"
"testing"

apiutil "github.com/absmach/supermq/api/http/util"
smqlog "github.com/absmach/supermq/logger"
"github.com/absmach/supermq/pkg/apiutil"
"github.com/absmach/supermq/pkg/errors"
svcerr "github.com/absmach/supermq/pkg/errors/service"
"github.com/stretchr/testify/assert"
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
12 changes: 6 additions & 6 deletions api/openapi/users.yml → apidocs/openapi/users.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1130,18 +1130,18 @@ components:
IssueToken:
type: object
properties:
identity:
username:
type: string
example: [email protected]
description: User identity - email address.
secret:
example: myUsername
description: Users' username.
password:
type: string
example: password
minimum: 8
description: User secret password.
required:
- identity
- secret
- username
- password

Error:
type: object
Expand Down
2 changes: 1 addition & 1 deletion auth/api/grpc/auth/endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ import (
"testing"
"time"

apiutil "github.com/absmach/supermq/api/http/util"
"github.com/absmach/supermq/auth"
grpcapi "github.com/absmach/supermq/auth/api/grpc/auth"
grpcAuthV1 "github.com/absmach/supermq/internal/grpc/auth/v1"
"github.com/absmach/supermq/internal/testsutil"
"github.com/absmach/supermq/pkg/apiutil"
"github.com/absmach/supermq/pkg/errors"
svcerr "github.com/absmach/supermq/pkg/errors/service"
"github.com/stretchr/testify/assert"
Expand Down
2 changes: 1 addition & 1 deletion auth/api/grpc/auth/requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
package auth

import (
apiutil "github.com/absmach/supermq/api/http/util"
"github.com/absmach/supermq/auth"
"github.com/absmach/supermq/pkg/apiutil"
)

type authenticateReq struct {
Expand Down
2 changes: 1 addition & 1 deletion auth/api/grpc/token/endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ import (
"testing"
"time"

apiutil "github.com/absmach/supermq/api/http/util"
"github.com/absmach/supermq/auth"
grpcapi "github.com/absmach/supermq/auth/api/grpc/token"
grpcTokenV1 "github.com/absmach/supermq/internal/grpc/token/v1"
"github.com/absmach/supermq/internal/testsutil"
"github.com/absmach/supermq/pkg/apiutil"
"github.com/absmach/supermq/pkg/errors"
svcerr "github.com/absmach/supermq/pkg/errors/service"
"github.com/stretchr/testify/assert"
Expand Down
2 changes: 1 addition & 1 deletion auth/api/grpc/token/requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
package token

import (
apiutil "github.com/absmach/supermq/api/http/util"
"github.com/absmach/supermq/auth"
"github.com/absmach/supermq/pkg/apiutil"
)

type issueReq struct {
Expand Down
2 changes: 1 addition & 1 deletion auth/api/grpc/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ package grpc
import (
"fmt"

apiutil "github.com/absmach/supermq/api/http/util"
"github.com/absmach/supermq/auth"
"github.com/absmach/supermq/pkg/apiutil"
"github.com/absmach/supermq/pkg/errors"
svcerr "github.com/absmach/supermq/pkg/errors/service"
"google.golang.org/grpc/codes"
Expand Down
2 changes: 1 addition & 1 deletion auth/api/http/keys/endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ import (
"testing"
"time"

apiutil "github.com/absmach/supermq/api/http/util"
"github.com/absmach/supermq/auth"
httpapi "github.com/absmach/supermq/auth/api/http"
"github.com/absmach/supermq/auth/jwt"
"github.com/absmach/supermq/auth/mocks"
smqlog "github.com/absmach/supermq/logger"
"github.com/absmach/supermq/pkg/apiutil"
svcerr "github.com/absmach/supermq/pkg/errors/service"
policymocks "github.com/absmach/supermq/pkg/policies/mocks"
"github.com/absmach/supermq/pkg/uuid"
Expand Down
2 changes: 1 addition & 1 deletion auth/api/http/keys/requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ package keys
import (
"time"

apiutil "github.com/absmach/supermq/api/http/util"
"github.com/absmach/supermq/auth"
"github.com/absmach/supermq/pkg/apiutil"
)

type issueKeyReq struct {
Expand Down
2 changes: 1 addition & 1 deletion auth/api/http/keys/requests_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ package keys
import (
"testing"

apiutil "github.com/absmach/supermq/api/http/util"
"github.com/absmach/supermq/auth"
"github.com/absmach/supermq/pkg/apiutil"
"github.com/stretchr/testify/assert"
)

Expand Down
4 changes: 2 additions & 2 deletions auth/api/http/keys/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import (
"net/http"
"strings"

api "github.com/absmach/supermq/api/http"
apiutil "github.com/absmach/supermq/api/http/util"
"github.com/absmach/supermq/auth"
"github.com/absmach/supermq/internal/api"
"github.com/absmach/supermq/pkg/apiutil"
"github.com/absmach/supermq/pkg/errors"
"github.com/go-chi/chi/v5"
kithttp "github.com/go-kit/kit/transport/http"
Expand Down
2 changes: 1 addition & 1 deletion auth/api/http/pats/requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (
"strings"
"time"

apiutil "github.com/absmach/supermq/api/http/util"
"github.com/absmach/supermq/auth"
"github.com/absmach/supermq/pkg/apiutil"
)

type createPatReq struct {
Expand Down
4 changes: 2 additions & 2 deletions auth/api/http/pats/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import (
"net/http"
"strings"

api "github.com/absmach/supermq/api/http"
apiutil "github.com/absmach/supermq/api/http/util"
"github.com/absmach/supermq/auth"
"github.com/absmach/supermq/internal/api"
"github.com/absmach/supermq/pkg/apiutil"
"github.com/absmach/supermq/pkg/errors"
"github.com/go-chi/chi/v5"
kithttp "github.com/go-kit/kit/transport/http"
Expand Down
4 changes: 2 additions & 2 deletions bootstrap/api/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ package api
import (
"context"

api "github.com/absmach/supermq/api/http"
apiutil "github.com/absmach/supermq/api/http/util"
"github.com/absmach/supermq/bootstrap"
"github.com/absmach/supermq/internal/api"
"github.com/absmach/supermq/pkg/apiutil"
"github.com/absmach/supermq/pkg/authn"
"github.com/absmach/supermq/pkg/errors"
svcerr "github.com/absmach/supermq/pkg/errors/service"
Expand Down
2 changes: 1 addition & 1 deletion bootstrap/api/endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ import (
"strings"
"testing"

apiutil "github.com/absmach/supermq/api/http/util"
"github.com/absmach/supermq/bootstrap"
bsapi "github.com/absmach/supermq/bootstrap/api"
"github.com/absmach/supermq/bootstrap/mocks"
"github.com/absmach/supermq/internal/testsutil"
smqlog "github.com/absmach/supermq/logger"
"github.com/absmach/supermq/pkg/apiutil"
smqauthn "github.com/absmach/supermq/pkg/authn"
authnmocks "github.com/absmach/supermq/pkg/authn/mocks"
"github.com/absmach/supermq/pkg/errors"
Expand Down
2 changes: 1 addition & 1 deletion bootstrap/api/requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
package api

import (
apiutil "github.com/absmach/supermq/api/http/util"
"github.com/absmach/supermq/bootstrap"
"github.com/absmach/supermq/pkg/apiutil"
)

const maxLimitSize = 100
Expand Down
2 changes: 1 addition & 1 deletion bootstrap/api/requests_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import (
"fmt"
"testing"

apiutil "github.com/absmach/supermq/api/http/util"
"github.com/absmach/supermq/bootstrap"
"github.com/absmach/supermq/internal/testsutil"
"github.com/absmach/supermq/pkg/apiutil"
"github.com/stretchr/testify/assert"
)

Expand Down
4 changes: 2 additions & 2 deletions bootstrap/api/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ import (
"strings"

"github.com/absmach/supermq"
api "github.com/absmach/supermq/api/http"
apiutil "github.com/absmach/supermq/api/http/util"
"github.com/absmach/supermq/bootstrap"
"github.com/absmach/supermq/internal/api"
"github.com/absmach/supermq/pkg/apiutil"
smqauthn "github.com/absmach/supermq/pkg/authn"
"github.com/absmach/supermq/pkg/errors"
"github.com/go-chi/chi/v5"
Expand Down
2 changes: 1 addition & 1 deletion certs/api/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ package api
import (
"context"

apiutil "github.com/absmach/supermq/api/http/util"
"github.com/absmach/supermq/certs"
"github.com/absmach/supermq/pkg/apiutil"
"github.com/absmach/supermq/pkg/errors"
"github.com/go-kit/kit/endpoint"
)
Expand Down
Loading
Loading