shared

package module
v0.1.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 15, 2020 License: GPL-3.0 Imports: 8 Imported by: 0

README

kaijday-api-shared

A collection of shared utilities for kaijday-api microservices.

  • Version [0.1.1]

Documentation

Index

Constants

This section is empty.

Variables

View Source
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 ApiError

type ApiError struct {
	Code  int    `json:"code"`
	Error string `json:"error"`
}

ApiError A standard Api response model for errors.

type ApiResponse

type ApiResponse struct {
	Code   int    `json:"code"`
	Result string `json:"result"`
}

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

type LoginRequest struct {
	Username string `json:"username"`
	Password string `json:"password"`
}

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.

func (User) TableName

func (User) TableName() string

TableName supplies the default schema and table name for the user in the database.

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL