Add password protected links
This commit is contained in:
parent
2baa74d520
commit
a01f540a29
6 changed files with 67 additions and 28 deletions
|
|
@ -9,18 +9,21 @@ type CreationModel struct {
|
||||||
Id string `json:"id"`
|
Id string `json:"id"`
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
RedirectURL string `json:"redirectUrl"`
|
RedirectURL string `json:"redirectUrl"`
|
||||||
|
Password string `json:"password"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type ResourceModel struct {
|
type ResourceModel struct {
|
||||||
Id string `json:"id"`
|
Id string `json:"id"`
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
RedirectURL string `json:"redirectUrl"`
|
RedirectURL string `json:"redirectUrl"`
|
||||||
|
Password string `json:"password"`
|
||||||
CreationTime int64 `json:"creationTime"`
|
CreationTime int64 `json:"creationTime"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type UpdateModel struct {
|
type UpdateModel struct {
|
||||||
Name string `json:"name,omitempty"`
|
Name string `json:"name,omitempty"`
|
||||||
RedirectURL string `json:"redirectUrl,omitempty"`
|
RedirectURL string `json:"redirectUrl,omitempty"`
|
||||||
|
Password string `json:"password"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *CreationModel) MapModelToEntity() (*Link, error) {
|
func (m *CreationModel) MapModelToEntity() (*Link, error) {
|
||||||
|
|
@ -33,6 +36,7 @@ func (m *CreationModel) MapModelToEntity() (*Link, error) {
|
||||||
Id: m.Id,
|
Id: m.Id,
|
||||||
Name: m.Name,
|
Name: m.Name,
|
||||||
RedirectURL: *u,
|
RedirectURL: *u,
|
||||||
|
Password: m.Password,
|
||||||
CreationTime: time.Now().UTC(),
|
CreationTime: time.Now().UTC(),
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
@ -42,6 +46,7 @@ func MapEntityToModel(entity *Link) ResourceModel {
|
||||||
Id: entity.Id,
|
Id: entity.Id,
|
||||||
Name: entity.Name,
|
Name: entity.Name,
|
||||||
RedirectURL: entity.RedirectURL.String(),
|
RedirectURL: entity.RedirectURL.String(),
|
||||||
|
Password: entity.Password,
|
||||||
CreationTime: entity.CreationTime.Unix(),
|
CreationTime: entity.CreationTime.Unix(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -9,6 +9,7 @@ type Link struct {
|
||||||
Id string
|
Id string
|
||||||
Name string
|
Name string
|
||||||
RedirectURL url.URL
|
RedirectURL url.URL
|
||||||
|
Password string
|
||||||
CreationTime time.Time
|
CreationTime time.Time
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -11,13 +11,14 @@ import (
|
||||||
|
|
||||||
func redirectHandler(ctx echo.Context, serv Service) error {
|
func redirectHandler(ctx echo.Context, serv Service) error {
|
||||||
linkId := ctx.Param("id")
|
linkId := ctx.Param("id")
|
||||||
|
linkPassword := ctx.QueryParam("password")
|
||||||
|
|
||||||
link, err := serv.GetById(linkId)
|
redirectUrl, err := serv.AccessLink(linkId, linkPassword)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return ctx.Redirect(http.StatusSeeOther, link.RedirectURL.String())
|
return ctx.Redirect(http.StatusSeeOther, redirectUrl.String())
|
||||||
}
|
}
|
||||||
|
|
||||||
func creationHandler(ctx echo.Context, serv Service) error {
|
func creationHandler(ctx echo.Context, serv Service) error {
|
||||||
|
|
|
||||||
|
|
@ -34,12 +34,12 @@ func (r *PgRepository) Save(link *Link) error {
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
sql := `
|
sql := `
|
||||||
INSERT INTO links (id, name, redirect_url, creation_time)
|
INSERT INTO links (id, name, redirect_url, password, creation_time)
|
||||||
VALUES ($1, $2, $3, $4::timestamp)
|
VALUES ($1, $2, $3, $4, $5::timestamp)
|
||||||
`
|
`
|
||||||
|
|
||||||
database.LogPoolState(r.pool, "Saving link")
|
database.LogPoolState(r.pool, "Saving link")
|
||||||
_, err := r.pool.Exec(ctx, sql, link.Id, link.Name, link.RedirectURL.String(), link.CreationTime.Format("2006-01-02 15:04:05"))
|
_, err := r.pool.Exec(ctx, sql, link.Id, link.Name, link.RedirectURL.String(), link.Password, link.CreationTime.Format("2006-01-02 15:04:05"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
@ -52,7 +52,7 @@ func (r *PgRepository) FindById(id string) (*Link, error) {
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
sql := `
|
sql := `
|
||||||
SELECT id, name, redirect_url, creation_time
|
SELECT id, name, redirect_url, password, creation_time
|
||||||
FROM links
|
FROM links
|
||||||
WHERE id = $1
|
WHERE id = $1
|
||||||
`
|
`
|
||||||
|
|
@ -82,7 +82,7 @@ func (r *PgRepository) GetAll(limit int, offset int) (Links, error) {
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
sql := `
|
sql := `
|
||||||
SELECT id, name, redirect_url, creation_time
|
SELECT id, name, redirect_url, password, creation_time
|
||||||
FROM links
|
FROM links
|
||||||
LIMIT $1
|
LIMIT $1
|
||||||
OFFSET $2
|
OFFSET $2
|
||||||
|
|
@ -122,12 +122,12 @@ func (r *PgRepository) Update(link *Link) error {
|
||||||
|
|
||||||
sql := `
|
sql := `
|
||||||
UPDATE links
|
UPDATE links
|
||||||
SET name = $1, redirect_url = $2
|
SET name = $1, redirect_url = $2, password = $3
|
||||||
WHERE id = $3
|
WHERE id = $4
|
||||||
`
|
`
|
||||||
|
|
||||||
database.LogPoolState(r.pool, "Updating link")
|
database.LogPoolState(r.pool, "Updating link")
|
||||||
_, err := r.pool.Exec(ctx, sql, link.Name, link.RedirectURL.String(), link.Id)
|
_, err := r.pool.Exec(ctx, sql, link.Name, link.RedirectURL.String(), link.Password, link.Id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
@ -159,11 +159,11 @@ func mapRowToEntity(r interface{}) (*Link, error) {
|
||||||
|
|
||||||
switch v := r.(type) {
|
switch v := r.(type) {
|
||||||
case pgx.Row:
|
case pgx.Row:
|
||||||
if err := v.Scan(&entity.Id, &entity.Name, &urlStr, &t); err != nil {
|
if err := v.Scan(&entity.Id, &entity.Name, &urlStr, &entity.Password, &t); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
case pgx.Rows:
|
case pgx.Rows:
|
||||||
if err := v.Scan(&entity.Id, &entity.Name, &urlStr, &t); err != nil {
|
if err := v.Scan(&entity.Id, &entity.Name, &urlStr, &entity.Password, &t); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
|
|
|
||||||
|
|
@ -3,11 +3,15 @@ package link
|
||||||
import (
|
import (
|
||||||
apperrors "cgnolink/errors"
|
apperrors "cgnolink/errors"
|
||||||
"github.com/patrickmn/go-cache"
|
"github.com/patrickmn/go-cache"
|
||||||
|
"net/url"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var linkNotFoundError = apperrors.NotFoundError{Message: "Link with given ID was not found."}
|
||||||
|
|
||||||
type Service interface {
|
type Service interface {
|
||||||
Create(link *Link) error
|
Create(link *Link) error
|
||||||
GetById(id string) (*Link, error)
|
GetById(id string) (*Link, error)
|
||||||
|
AccessLink(id string, password string) (*url.URL, error)
|
||||||
GetAll(limit int, offset int) (Links, error)
|
GetAll(limit int, offset int) (Links, error)
|
||||||
Update(data *Link) error
|
Update(data *Link) error
|
||||||
DeleteById(id string) error
|
DeleteById(id string) error
|
||||||
|
|
@ -18,6 +22,21 @@ type PgService struct {
|
||||||
cache *cache.Cache
|
cache *cache.Cache
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (service *PgService) AccessLink(id string, password string) (*url.URL, error) {
|
||||||
|
link, err := service.GetById(id)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if link.Password != "" {
|
||||||
|
if password == "" || link.Password != password {
|
||||||
|
return nil, linkNotFoundError
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return &link.RedirectURL, nil
|
||||||
|
}
|
||||||
|
|
||||||
func NewService(rep Repository) Service {
|
func NewService(rep Repository) Service {
|
||||||
return &PgService{
|
return &PgService{
|
||||||
rep: rep,
|
rep: rep,
|
||||||
|
|
@ -25,8 +44,8 @@ func NewService(rep Repository) Service {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *PgService) Create(link *Link) error {
|
func (service *PgService) Create(link *Link) error {
|
||||||
existingLink, err := s.rep.FindById(link.Id)
|
existingLink, err := service.rep.FindById(link.Id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return apperrors.UnknownError{Err: err}
|
return apperrors.UnknownError{Err: err}
|
||||||
}
|
}
|
||||||
|
|
@ -35,36 +54,36 @@ func (s *PgService) Create(link *Link) error {
|
||||||
return apperrors.AlreadyExistsError{Message: "Link with given ID already exists."}
|
return apperrors.AlreadyExistsError{Message: "Link with given ID already exists."}
|
||||||
}
|
}
|
||||||
|
|
||||||
if err = s.rep.Save(link); err != nil {
|
if err = service.rep.Save(link); err != nil {
|
||||||
return apperrors.UnknownError{Err: err}
|
return apperrors.UnknownError{Err: err}
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *PgService) GetById(id string) (*Link, error) {
|
func (service *PgService) GetById(id string) (*Link, error) {
|
||||||
if v, found := s.cache.Get(id); found {
|
if v, found := service.cache.Get(id); found {
|
||||||
if link, ok := v.(*Link); ok {
|
if link, ok := v.(*Link); ok {
|
||||||
return link, nil
|
return link, nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
link, err := s.rep.FindById(id)
|
link, err := service.rep.FindById(id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, apperrors.UnknownError{Err: err}
|
return nil, apperrors.UnknownError{Err: err}
|
||||||
}
|
}
|
||||||
|
|
||||||
if link == nil {
|
if link == nil {
|
||||||
return nil, apperrors.NotFoundError{Message: "Link with given ID was not found."}
|
return nil, linkNotFoundError
|
||||||
}
|
}
|
||||||
|
|
||||||
s.cache.Set(id, link, cache.DefaultExpiration)
|
service.cache.Set(id, link, cache.DefaultExpiration)
|
||||||
|
|
||||||
return link, nil
|
return link, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *PgService) GetAll(limit int, offset int) (Links, error) {
|
func (service *PgService) GetAll(limit int, offset int) (Links, error) {
|
||||||
links, err := s.rep.GetAll(limit, offset)
|
links, err := service.rep.GetAll(limit, offset)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, apperrors.UnknownError{Err: err}
|
return nil, apperrors.UnknownError{Err: err}
|
||||||
}
|
}
|
||||||
|
|
@ -72,20 +91,20 @@ func (s *PgService) GetAll(limit int, offset int) (Links, error) {
|
||||||
return links, nil
|
return links, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *PgService) Update(data *Link) error {
|
func (service *PgService) Update(data *Link) error {
|
||||||
if err := s.rep.Update(data); err != nil {
|
if err := service.rep.Update(data); err != nil {
|
||||||
return apperrors.UnknownError{Err: err}
|
return apperrors.UnknownError{Err: err}
|
||||||
}
|
}
|
||||||
|
service.cache.Delete(data.Id)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *PgService) DeleteById(id string) error {
|
func (service *PgService) DeleteById(id string) error {
|
||||||
s.cache.Delete(id)
|
if err := service.rep.DeleteById(id); err != nil {
|
||||||
|
|
||||||
if err := s.rep.DeleteById(id); err != nil {
|
|
||||||
return apperrors.UnknownError{Err: err}
|
return apperrors.UnknownError{Err: err}
|
||||||
}
|
}
|
||||||
|
service.cache.Delete(id)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
||||||
13
migrations/002_add_password.sql
Normal file
13
migrations/002_add_password.sql
Normal file
|
|
@ -0,0 +1,13 @@
|
||||||
|
ALTER TABLE links
|
||||||
|
ADD COLUMN password varchar;
|
||||||
|
|
||||||
|
UPDATE links
|
||||||
|
SET password = '';
|
||||||
|
|
||||||
|
ALTER TABLE links
|
||||||
|
ALTER COLUMN password SET not null;
|
||||||
|
|
||||||
|
---- create above / drop below ----
|
||||||
|
|
||||||
|
ALTER TABLE links
|
||||||
|
DROP COLUMN password;
|
||||||
Loading…
Add table
Add a link
Reference in a new issue