2019-12-16 16:22:09 +01:00
|
|
|
// Copyright (c) Mainflux
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
2020-12-29 23:02:35 +01:00
|
|
|
package auth
|
2019-12-16 16:22:09 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// ErrInvalidKeyIssuedAt indicates that the Key is being used before it's issued.
|
|
|
|
ErrInvalidKeyIssuedAt = errors.New("invalid issue time")
|
|
|
|
|
|
|
|
// ErrKeyExpired indicates that the Key is expired.
|
|
|
|
ErrKeyExpired = errors.New("use of expired key")
|
2020-11-13 21:46:04 +01:00
|
|
|
|
|
|
|
// ErrAPIKeyExpired indicates that the Key is expired
|
|
|
|
// and that the key type is API key.
|
|
|
|
ErrAPIKeyExpired = errors.New("use of expired API key")
|
2019-12-16 16:22:09 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
MF-1718 - Use static code analysis in CI (#1729)
* things, twins, and logger lint fixed
Signed-off-by: aryan <aryangodara03@gmail.com>
* all services updated, auth jwt not working, ineffectual assignment issue
Signed-off-by: aryan <aryangodara03@gmail.com>
* handle error from grpc server in endpointtest
Signed-off-by: aryan <aryangodara03@gmail.com>
* temp commit, auth/jwt needs to be resolved
Signed-off-by: aryan <aryangodara03@gmail.com>
* revert back to jwt v4 temporarily
Signed-off-by: aryan <aryangodara03@gmail.com>
* updated jwt tokenizer
Signed-off-by: aryan <aryangodara03@gmail.com>
* resolve EOF error for httptest requests
Signed-off-by: aryan <aryangodara03@gmail.com>
* fix auth jwt, update to registeredclaims
Signed-off-by: aryan <aryangodara03@gmail.com>
* fix ineffective assignment, auth/api/grpc endpoint failing
Signed-off-by: aryan <aryangodara03@gmail.com>
* temp commit, remove later
Signed-off-by: aryan <aryangodara03@gmail.com>
* fix grpc server setup
Signed-off-by: aryan <aryangodara03@gmail.com>
* resolve golangci tests, remove debug statements
Signed-off-by: aryan <aryangodara03@gmail.com>
* update golangci version and modify linters used
Signed-off-by: aryan <aryangodara03@gmail.com>
* fix failing tests
Signed-off-by: aryan <aryangodara03@gmail.com>
* fix grpc server for setup tests
Signed-off-by: aryan <aryangodara03@gmail.com>
* fix logging and errors inlined
Signed-off-by: aryan <aryangodara03@gmail.com>
* fix remarks, update grpc setup_test
Signed-off-by: aryan <aryangodara03@gmail.com>
* fix setup_test
Signed-off-by: aryan <aryangodara03@gmail.com>
* update setup_test grpc
Signed-off-by: aryan <aryangodara03@gmail.com>
* fix data race
Signed-off-by: aryan <aryangodara03@gmail.com>
* update setup_test grpc
Signed-off-by: aryan <aryangodara03@gmail.com>
* fix grpc setup down to single simple function
Signed-off-by: aryan <aryangodara03@gmail.com>
* fix linting issues
Signed-off-by: aryan <aryangodara03@gmail.com>
* resolve pr comments
Signed-off-by: aryan <aryangodara03@gmail.com>
* fix tests, handle returned errors, go mod tidy vendor
Signed-off-by: aryan <aryangodara03@gmail.com>
* fix errors from new linters
Signed-off-by: aryan <aryangodara03@gmail.com>
---------
Signed-off-by: aryan <aryangodara03@gmail.com>
2023-04-22 08:14:35 -07:00
|
|
|
// LoginKey is temporary User key received on successful login.
|
2021-12-24 14:53:06 +01:00
|
|
|
LoginKey uint32 = iota
|
2019-12-16 16:22:09 +01:00
|
|
|
// RecoveryKey represents a key for resseting password.
|
|
|
|
RecoveryKey
|
|
|
|
// APIKey enables the one to act on behalf of the user.
|
|
|
|
APIKey
|
|
|
|
)
|
|
|
|
|
|
|
|
// Key represents API key.
|
|
|
|
type Key struct {
|
|
|
|
ID string
|
|
|
|
Type uint32
|
2020-10-27 19:42:53 +01:00
|
|
|
IssuerID string
|
|
|
|
Subject string
|
2019-12-16 16:22:09 +01:00
|
|
|
IssuedAt time.Time
|
|
|
|
ExpiresAt time.Time
|
|
|
|
}
|
|
|
|
|
2023-05-25 06:13:29 +08:00
|
|
|
// KeyPage contains a page of keys.
|
|
|
|
type KeyPage struct {
|
|
|
|
PageMetadata
|
|
|
|
Keys []Key
|
|
|
|
}
|
|
|
|
|
2020-10-27 19:42:53 +01:00
|
|
|
// Identity contains ID and Email.
|
|
|
|
type Identity struct {
|
|
|
|
ID string
|
|
|
|
Email string
|
|
|
|
}
|
|
|
|
|
2019-12-16 16:22:09 +01:00
|
|
|
// Expired verifies if the key is expired.
|
|
|
|
func (k Key) Expired() bool {
|
2020-04-07 14:32:02 +02:00
|
|
|
if k.Type == APIKey && k.ExpiresAt.IsZero() {
|
|
|
|
return false
|
|
|
|
}
|
2019-12-16 16:22:09 +01:00
|
|
|
return k.ExpiresAt.UTC().Before(time.Now().UTC())
|
|
|
|
}
|
|
|
|
|
|
|
|
// KeyRepository specifies Key persistence API.
|
|
|
|
type KeyRepository interface {
|
|
|
|
// Save persists the Key. A non-nil error is returned to indicate
|
|
|
|
// operation failure
|
|
|
|
Save(context.Context, Key) (string, error)
|
|
|
|
|
2023-05-25 06:13:29 +08:00
|
|
|
// RetrieveByID retrieves Key by its unique identifier.
|
|
|
|
RetrieveByID(context.Context, string, string) (Key, error)
|
|
|
|
|
|
|
|
// RetrieveAll retrieves all keys for given user ID.
|
|
|
|
RetrieveAll(context.Context, string, PageMetadata) (KeyPage, error)
|
2019-12-16 16:22:09 +01:00
|
|
|
|
|
|
|
// Remove removes Key with provided ID.
|
|
|
|
Remove(context.Context, string, string) error
|
|
|
|
}
|