Browse Source

Fix docker image

pull/3/head
James Mills 1 month ago
parent
commit
e38fd47af6
Signed by: prologic GPG Key ID: AC4C014F1440EBD6
  1. 5
      Dockerfile
  2. 70
      internal/auth/manager.go
  3. 4
      internal/handlers.go
  4. 13
      internal/utils.go

5
Dockerfile

@ -17,9 +17,6 @@ RUN make deps
COPY go.mod .
COPY go.sum .
# Pre-fill go mod cache
RUN go mod download
# Copy static assets
COPY ./internal/static/css/* ./internal/static/css/
COPY ./internal/static/img/* ./internal/static/img/
@ -34,9 +31,7 @@ COPY ./internal/templates/* ./internal/templates/
# Copy sources
COPY *.go ./
COPY ./internal/*.go ./internal/
COPY ./internal/auth/*.go ./internal/auth/
COPY ./internal/session/*.go ./internal/session/
COPY ./internal/passwords/*.go ./internal/passwords/
COPY ./cmd/yarns/*.go ./cmd/yarns/
# Version/Commit (there there is no .git in Docker build context)

70
internal/auth/manager.go

@ -1,70 +0,0 @@
package auth
import (
"net/http"
"github.com/julienschmidt/httprouter"
"git.mills.io/yarnsocial/yarns/internal/session"
)
// Options ...
type Options struct {
login string
register string
}
// NewOptions ...
func NewOptions(login, register string) *Options {
return &Options{login, register}
}
// Manager ...
type Manager struct {
options *Options
}
// NewManager ...
func NewManager(options *Options) *Manager {
return &Manager{options}
}
// MustAuth ...
func (m *Manager) MustAuth(next httprouter.Handle) httprouter.Handle {
return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
if sess := r.Context().Value(session.SessionKey); sess != nil {
if _, ok := sess.(*session.Session).Get("username"); ok {
next(w, r, p)
return
}
}
http.Redirect(w, r, m.options.login, http.StatusFound)
}
}
// ShouldAuth ...
func (m *Manager) ShouldAuth(next httprouter.Handle) httprouter.Handle {
return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
if sess := r.Context().Value(session.SessionKey); sess != nil {
if _, ok := sess.(*session.Session).Get("username"); ok {
next(w, r, p)
return
}
}
http.Redirect(w, r, m.options.login, http.StatusFound)
}
}
func (m *Manager) HasAuth(next httprouter.Handle) httprouter.Handle {
return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
if sess := r.Context().Value(session.SessionKey); sess != nil {
if _, ok := sess.(*session.Session).Get("username"); ok {
http.Redirect(w, r, "/", http.StatusFound)
return
}
}
next(w, r, p)
}
}

4
internal/handlers.go

@ -19,10 +19,6 @@ import (
"github.com/vcraescu/go-paginator"
)
const (
MaxFailedLogins = 3 // By default 3 failed login attempts per 5 minutes
)
//go:embed pages/*.md
var pages embed.FS

13
internal/utils.go

@ -333,19 +333,6 @@ func NormalizeURL(url string) string {
return norm
}
// RedirectRefererURL constructs a Redirect URL from the given Request URL
// and possibly Referer, if the Referer's Base URL matches the Pod's Base URL
// will return the Referer URL otherwise the defaultURL. This is primarily used
// to redirect a user from a successful /login back to the page they were on.
func RedirectRefererURL(r *http.Request, conf *Config, defaultURL string) string {
referer := NormalizeURL(r.Header.Get("Referer"))
if referer != "" && strings.HasPrefix(referer, conf.BaseURL) {
return referer
}
return defaultURL
}
func HostnameFromURL(uri string) string {
u, err := url.Parse(uri)
if err != nil {

Loading…
Cancel
Save