Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ConfigApiPort configuration setting for the port to run the API on. ConfigApiPort = os.Getenv("ApiPort") // ConfigDatabaseConnString configuration setting for the postgres database connection string. ConfigDatabaseConnString = os.Getenv("ApiDatabaseConnectionString") // ConfigSecretKey configuration setting for the secret key to encrypt data with. ConfigSecretKey = os.Getenv("ApiSecretKey") )
Functions ¶
This section is empty.
Types ¶
type ApiResponse ¶
ApiResponse A standard Api response model.
type JwtService ¶
type JwtService struct {
UsersRepository UsersRepository
}
JwtService provides Json Web Token authentication middleware and hashing for use in API microservices.
func NewJwtService ¶
func NewJwtService(r UsersRepository) JwtService
NewJwtService Creates a new JwtService instance, with a UsersRepository.
func (*JwtService) ComparePasswords ¶
func (s *JwtService) ComparePasswords(hash string, password string) error
ComparePasswords compares a hashed and non-hashed password to check if they match. Returns an error if they do not.
func (*JwtService) CreateJwtMiddleware ¶
func (s *JwtService) CreateJwtMiddleware(secretKey string) *jwt.GinJWTMiddleware
CreateJwtMiddleware provides a GIN compatible JWT middleware for authenticating with the API microservices.
func (*JwtService) HashPassword ¶
func (s *JwtService) HashPassword(password string) (string, error)
HashPassword hashes passwords for saving into storage securely. Returns an error if generating a hash fails for any reason.
type LoginRequest ¶
LoginRequest model to represent payload used by callers of the api to request a new JWT token. Payload needs to be in the form of {"username": "...", "password": "..."}
type User ¶
type User struct {
gorm.Model
// Username is the identifier for the user.
Username string `gorm:"primary_key;not null" json:"username"`
// Password is the secure pass for securing the user.
Password string `gorm:"not null" json:"password"`
}
User represents an API user and its database structure.
type UsersRepository ¶
type UsersRepository struct {
// contains filtered or unexported fields
}
UsersRepository provides methods to access data from within a database for storage of api users.
func NewUsersRepository ¶
func NewUsersRepository(db *gorm.DB) UsersRepository
NewUsersRepository creates a UsersRepository instance.
func (*UsersRepository) Delete ¶
func (r *UsersRepository) Delete(user User)
Delete removes a user from the database.
func (*UsersRepository) GetById ¶
func (r *UsersRepository) GetById(userId uuid.UUID) (bool, User)
GetById gets a user from the database using their unique storage identifier. Returns (true, user) if the user was found, or (false, nil) if no user could be found.
func (*UsersRepository) GetByUsername ¶
func (r *UsersRepository) GetByUsername(username string) (bool, User)
GetByUsername gets a user from the database using their username. Returns (true, user) if the user was found, or (false, nil) if no user could be found.
func (*UsersRepository) Save ¶
func (r *UsersRepository) Save(user User) User
Save saves a new user to the database. Returns the user added.