mirror of
https://github.com/mainflux/mainflux.git
synced 2025-04-24 13:48:49 +08:00

* Fix linting errors Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> * feat(linters): add ineffassign linter This commit adds the `ineffassign` linter to the project's `.golangci.yml` configuration file. The `ineffassign` linter helps identify and flag assignments to variables that are never used, helping to improve code quality and maintainability. Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> * Add extra linters Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> * feat(golangci): Add header check - Added goheader check to ensure all files have license headers - Added build tags for "nats" in the .golangci.yml file to include the necessary dependencies for the "nats" package during the build process. - Also, increased the maximum number of issues per linter and the maximum number of same issues reported by the linter to improve the code quality analysis. Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> * feat(.golangci.yml): Add new linters Add the following new linters to the .golangci.yml configuration file: - asasalint - asciicheck - bidichk - contextcheck - decorder - dogsled - errchkjson - errname - execinquery - exportloopref - ginkgolinter - gocheckcompilerdirectives These linters will help improve code quality and catch potential issues during the code review process. Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> --------- Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com>
111 lines
2.6 KiB
Go
111 lines
2.6 KiB
Go
// Copyright (c) Mainflux
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package postgres
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"time"
|
|
|
|
"github.com/mainflux/mainflux/auth"
|
|
"github.com/mainflux/mainflux/internal/postgres"
|
|
"github.com/mainflux/mainflux/pkg/errors"
|
|
)
|
|
|
|
var (
|
|
errSave = errors.New("failed to save key in database")
|
|
errRetrieve = errors.New("failed to retrieve key from database")
|
|
errDelete = errors.New("failed to delete key from database")
|
|
)
|
|
var _ auth.KeyRepository = (*repo)(nil)
|
|
|
|
type repo struct {
|
|
db postgres.Database
|
|
}
|
|
|
|
// New instantiates a PostgreSQL implementation of key repository.
|
|
func New(db postgres.Database) auth.KeyRepository {
|
|
return &repo{
|
|
db: db,
|
|
}
|
|
}
|
|
|
|
func (kr *repo) Save(ctx context.Context, key auth.Key) (string, error) {
|
|
q := `INSERT INTO keys (id, type, issuer_id, subject, issued_at, expires_at)
|
|
VALUES (:id, :type, :issuer_id, :subject, :issued_at, :expires_at)`
|
|
|
|
dbKey := toDBKey(key)
|
|
if _, err := kr.db.NamedExecContext(ctx, q, dbKey); err != nil {
|
|
return "", postgres.HandleError(err, errSave)
|
|
}
|
|
|
|
return dbKey.ID, nil
|
|
}
|
|
|
|
func (kr *repo) Retrieve(ctx context.Context, issuerID, id string) (auth.Key, error) {
|
|
q := `SELECT id, type, issuer_id, subject, issued_at, expires_at FROM keys WHERE issuer_id = $1 AND id = $2`
|
|
key := dbKey{}
|
|
if err := kr.db.QueryRowxContext(ctx, q, issuerID, id).StructScan(&key); err != nil {
|
|
if err == sql.ErrNoRows {
|
|
return auth.Key{}, errors.ErrNotFound
|
|
}
|
|
|
|
return auth.Key{}, postgres.HandleError(err, errRetrieve)
|
|
}
|
|
|
|
return toKey(key), nil
|
|
}
|
|
|
|
func (kr *repo) Remove(ctx context.Context, issuerID, id string) error {
|
|
q := `DELETE FROM keys WHERE issuer_id = :issuer_id AND id = :id`
|
|
key := dbKey{
|
|
ID: id,
|
|
Issuer: issuerID,
|
|
}
|
|
if _, err := kr.db.NamedExecContext(ctx, q, key); err != nil {
|
|
return errors.Wrap(errDelete, err)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
type dbKey struct {
|
|
ID string `db:"id"`
|
|
Type uint32 `db:"type"`
|
|
Issuer string `db:"issuer_id"`
|
|
Subject string `db:"subject"`
|
|
IssuedAt time.Time `db:"issued_at"`
|
|
ExpiresAt sql.NullTime `db:"expires_at,omitempty"`
|
|
}
|
|
|
|
func toDBKey(key auth.Key) dbKey {
|
|
ret := dbKey{
|
|
ID: key.ID,
|
|
Type: uint32(key.Type),
|
|
Issuer: key.Issuer,
|
|
Subject: key.Subject,
|
|
IssuedAt: key.IssuedAt,
|
|
}
|
|
if !key.ExpiresAt.IsZero() {
|
|
ret.ExpiresAt = sql.NullTime{Time: key.ExpiresAt, Valid: true}
|
|
}
|
|
|
|
return ret
|
|
}
|
|
|
|
func toKey(key dbKey) auth.Key {
|
|
ret := auth.Key{
|
|
ID: key.ID,
|
|
Type: auth.KeyType(key.Type),
|
|
Issuer: key.Issuer,
|
|
Subject: key.Subject,
|
|
IssuedAt: key.IssuedAt,
|
|
}
|
|
if key.ExpiresAt.Valid {
|
|
ret.ExpiresAt = key.ExpiresAt.Time
|
|
}
|
|
|
|
return ret
|
|
}
|