authapi

package module
v0.0.0-...-cfbb22e Latest Latest
Warning

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

Go to latest
Published: Mar 9, 2021 License: MIT Imports: 8 Imported by: 0

README

Authentication REST API starter/skeleton/template with Auth0 authentication

Go

Featuring Many:Many relationship between organization and users for multi-tenant applications.

TODO's:

  • Unit testing
  • Middleware to use DB connection pool instead of new connection or use Redis to cache profiles and rolse
  • Enhance docker integration
  • Solidify error handling, logging, and responses

DB Pre-requisite:

CREATE EXTENSION IF NOT EXISTS "uuid-ossp";

Documentation

Index

Constants

View Source
const (
	ECONFLICT = "conflict"  // action cannot be performed
	EINTERNAL = "internal"  // internal error
	EINVALID  = "invalid"   // validation failed
	ENOTFOUND = "not_found" // entity does not exist
)

Application error type.

Variables

This section is empty.

Functions

func ErrorMessage

func ErrorMessage(err error) string

ErrorMessage returns the human-readable message of the error, if available. Otherwise returns a generic error message.

func ErrorType

func ErrorType(err error) string

Types

type AccessRole

type AccessRole int

AccessRole represents access role type

const (
	// SuperAdminRole has all permissions
	OwnerRole AccessRole = 500

	// AdminRole has admin specific permissions
	SuperUserRole AccessRole = 400

	// CompanyAdminRole can edit company specific things
	AdminRole AccessRole = 300

	// LocationAdminRole can edit location specific things
	SupervisorRole AccessRole = 200

	// UserRole is a standard user
	UserRole AccessRole = 100
)

type AuthToken

type AuthToken struct {
	Token        string `json:"token"`
	RefreshToken string `json:"refresh_token"`
}

AuthToken holds authentication token details with refresh token

type AuthUser

type AuthUser struct {
	ID        int
	CompanyID int
	Username  string
	Email     string
	Role      AccessRole
}

AuthUser represents data stored in JWT token for user

type Base

type Base struct {
	ID        int         `json:"-"  db:"id"`
	CreatedAt pq.NullTime `json:"-"  db:"created_at"`
	UpdatedAt pq.NullTime `json:"-"  db:"updated_at"`
	DeletedAt pq.NullTime `json:"-"  db:"deleted_at"`
}

Base contains common fields for all tables

func (*Base) BeforeInsert

func (b *Base) BeforeInsert(ctx context.Context) (context.Context, error)

BeforeInsert hooks into insert operations, setting createdAt and updatedAt to current time

func (*Base) BeforeUpdate

func (b *Base) BeforeUpdate(ctx context.Context) (context.Context, error)

BeforeUpdate hooks into update operations, setting updatedAt to current time

type Error

type Error struct {
	// Machine-readable error code.
	Code    string `json:"-"`
	CodeInt int    `json:"code"`
	// Human-readable message.
	Message string `json:"msg"`
	// Logical operation and nested error.
	Op  string `json:"-"`
	Err error  `json:"-"`
}

Taken from: https://middlemost.com/failure-is-your-domain/

Error defines a standard application error.

func (*Error) Error

func (e *Error) Error() string

Error returns the string representation of the error message.

type ErrorResp

type ErrorResp struct {
	Error []Error `json:"errors"`
}

type Invitation

type Invitation struct {
	Base
	TokenHash      string        `json:"-" db:"token_hash"'`         //represents the hash of the token
	TokenStr       string        `json:"-" pg:"-" sql:"-"`           // represents the plaintext token string
	ExpiresAt      *time.Time    `json:"expires_at" db:"expires_at"` //token expiration
	InvitorID      int           `json:"-" db:"invitor_id"`          //ID of the person sending the invite
	Invitor        *User         `json:"-"`                          //person sending the invitation
	OrganizationID int           `json:"-" db:"organization_id"`
	Organization   *Organization `json:"organization"`
	Email          string        `json:"email" db:"email"` //email of the user being invited
	Used           bool          `json:"used" db:"used"`
	UUID           uuid.UUID     `json:"-" db:"uuid"`
}

type ListQuery

type ListQuery struct {
	Query string
	ID    int
}

ListQuery holds company/location data used for list db queries

type Location

type Location struct {
	Base
	Name    string `json:"name"`
	Active  bool   `json:"active"`
	Address string `json:"address"`

	CompanyID int `json:"company_id"`
}

Location represents company location model

type Logger

type Logger interface {
	// source, msg, error, params
	Log(echo.Context, string, string, error, map[string]interface{})
}

Logger represents logging interface

type Organization

type Organization struct {
	Base
	Name    string    `json:"name"  db:"name"`
	Active  bool      `json:"active"  db:"active"`
	Profile []Profile `json:"-" pg:",many2many:profiles"`
	UUID    uuid.UUID `json:"organizationID"  db:"uuid"`
}

type Pagination

type Pagination struct {
	Limit  int `json:"limit,omitempty"`
	Offset int `json:"offset,omitempty"`
}

Pagination data

type PaginationReq

type PaginationReq struct {
	Limit int `query:"limit"`
	Page  int `query:"page" validate:"min=0"`
}

PaginationReq holds pagination http fields and tags

func (PaginationReq) Transform

func (p PaginationReq) Transform() Pagination

Transform checks and converts http pagination into database pagination model

type Ping

type Ping struct {
	Resp string `json:""`
}

type Pong

type Pong struct {
	Resp string `json:""`
}

type Profile

type Profile struct {
	Base
	UUID           uuid.UUID     `json:"profileID" db:"uuid"`
	UserID         int           `json:"-" db:"user_id"`
	User           *User         `json:"-"`
	OrganizationID int           `json:"-" db:"organization_id"`
	Organization   *Organization `json:"organization"`
	RoleID         int           `json:"-" db:"role_id"`
	Role           *Role         `json:"role"`
	Active         bool          `json:"-" db:"active"`
}

Company represents Profile model

type RBACService

type RBACService interface {
	User(echo.Context) AuthUser
	EnforceRole(echo.Context, AccessRole) error
	EnforceUser(echo.Context, int) error
	EnforceCompany(echo.Context, int) error
	EnforceLocation(echo.Context, int) error
	AccountCreate(echo.Context, AccessRole, int, int) error
	IsLowerRole(echo.Context, AccessRole) error
}

RBACService represents role-based access control service interface

type RefreshToken

type RefreshToken struct {
	Token string `json:"token"`
}

RefreshToken holds authentication token details

type Role

type Role struct {
	ID          AccessRole `json:"-" db:"id"`
	AccessLevel AccessRole `json:"-" db:"access_level"`
	Name        string     `json:"name"  db:"name"`
	Active      bool       `json:"-"  db:"active"`
}

Role model

type User

type User struct {
	Base
	FirstName      string         `json:"firstName,omitempty" db:"first_name"`
	LastName       string         `json:"lastName,omitempty" db:"last_name"`
	Password       string         `json:"-" pg:"-" sql:"-"`
	Email          string         `json:"email,omitempty" db:"email"`
	Username       sql.NullString `json:"-"`
	Mobile         sql.NullString `json:"-" db:"mobile"`
	Phone          sql.NullString `json:"-" db:"phone"`
	Address        sql.NullString `json:"-" db:"address"`
	ExternalID     string         `json:"-" db:"external_id"`
	UUID           uuid.UUID      `json:"id,omitempty" db:"uuid"`
	OrganizationID int            `json:"-" db:"organization_id"`
	Profile        []Profile      `json:"profiles,omitempty"`
	TimeZone       *string        `json:"timeZone" db:"timezone"`
}

Jump to

Keyboard shortcuts

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