remove useless isSecure dyn attr for cookies
This commit is contained in:
parent
c734729554
commit
f611e0c871
@ -6,10 +6,8 @@ API_ADMIN_PASSWORD=
|
|||||||
# in seconds (default to 30s)
|
# in seconds (default to 30s)
|
||||||
API_SESSION_EXPIRATION_DURATION=
|
API_SESSION_EXPIRATION_DURATION=
|
||||||
|
|
||||||
# default to 8585
|
# default to 8080
|
||||||
API_PORT=
|
API_PORT=
|
||||||
# default to "false"
|
|
||||||
API_SECURE=
|
|
||||||
|
|
||||||
# default to "./store"
|
# default to "./store"
|
||||||
API_STORE_DIR=
|
API_STORE_DIR=
|
||||||
@ -73,7 +73,7 @@ func postLogin(w http.ResponseWriter, r *http.Request, a services.IAuthenticate)
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
cookie := session.GenerateCookie(a.IsSecure())
|
cookie := session.GenerateCookie()
|
||||||
http.SetCookie(w, cookie)
|
http.SetCookie(w, cookie)
|
||||||
|
|
||||||
tmpl, err := templates.ExecuteLoginSuccessTmpl(w)
|
tmpl, err := templates.ExecuteLoginSuccessTmpl(w)
|
||||||
|
|||||||
4
main.go
4
main.go
@ -24,8 +24,6 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
isSecure = os.Getenv("API_SECURE") == "true"
|
|
||||||
|
|
||||||
port = sync.OnceValue[int](func() int {
|
port = sync.OnceValue[int](func() int {
|
||||||
port, err := strconv.Atoi(os.Getenv("API_PORT"))
|
port, err := strconv.Atoi(os.Getenv("API_PORT"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -51,7 +49,7 @@ func main() {
|
|||||||
ctx, fnCancel := signal.NotifyContext(context.Background(), os.Kill, os.Interrupt)
|
ctx, fnCancel := signal.NotifyContext(context.Background(), os.Kill, os.Interrupt)
|
||||||
defer fnCancel()
|
defer fnCancel()
|
||||||
|
|
||||||
auth := services.NewAuthentication(ctx, isSecure)
|
auth := services.NewAuthentication(ctx)
|
||||||
bs := services.NewStore(storeDir())
|
bs := services.NewStore(storeDir())
|
||||||
|
|
||||||
srv := server.NewServer(
|
srv := server.NewServer(
|
||||||
|
|||||||
@ -67,7 +67,7 @@ type Session struct {
|
|||||||
expirationTime time.Time
|
expirationTime time.Time
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Session) GenerateCookie(isSecure bool) *http.Cookie {
|
func (s *Session) GenerateCookie() *http.Cookie {
|
||||||
s.l.RLock()
|
s.l.RLock()
|
||||||
defer s.l.RUnlock()
|
defer s.l.RUnlock()
|
||||||
|
|
||||||
@ -75,7 +75,7 @@ func (s *Session) GenerateCookie(isSecure bool) *http.Cookie {
|
|||||||
Name: "session_id",
|
Name: "session_id",
|
||||||
Value: s.sessionID,
|
Value: s.sessionID,
|
||||||
HttpOnly: true,
|
HttpOnly: true,
|
||||||
Secure: isSecure,
|
Secure: true,
|
||||||
Expires: s.expirationTime,
|
Expires: s.expirationTime,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -83,7 +83,6 @@ func (s *Session) GenerateCookie(isSecure bool) *http.Cookie {
|
|||||||
type IAuthenticate interface {
|
type IAuthenticate interface {
|
||||||
IsLogged(r *http.Request) bool
|
IsLogged(r *http.Request) bool
|
||||||
Authenticate(username, password string) (*Session, error)
|
Authenticate(username, password string) (*Session, error)
|
||||||
IsSecure() bool
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var _ IAuthenticate = (*Authentication)(nil)
|
var _ IAuthenticate = (*Authentication)(nil)
|
||||||
@ -95,17 +94,15 @@ type Authentication struct {
|
|||||||
fnCancel context.CancelFunc
|
fnCancel context.CancelFunc
|
||||||
|
|
||||||
sessions map[string]*Session
|
sessions map[string]*Session
|
||||||
isSecure bool
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewAuthentication(ctx context.Context, isSecure bool) *Authentication {
|
func NewAuthentication(ctx context.Context) *Authentication {
|
||||||
ctxChild, fnCancel := context.WithCancel(ctx)
|
ctxChild, fnCancel := context.WithCancel(ctx)
|
||||||
|
|
||||||
s := &Authentication{
|
s := &Authentication{
|
||||||
ctx: ctxChild,
|
ctx: ctxChild,
|
||||||
fnCancel: fnCancel,
|
fnCancel: fnCancel,
|
||||||
sessions: map[string]*Session{},
|
sessions: map[string]*Session{},
|
||||||
isSecure: isSecure,
|
|
||||||
}
|
}
|
||||||
s.purgeWorker()
|
s.purgeWorker()
|
||||||
|
|
||||||
@ -146,10 +143,6 @@ func (a *Authentication) purgeWorker() {
|
|||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *Authentication) IsSecure() bool {
|
|
||||||
return a.isSecure
|
|
||||||
}
|
|
||||||
|
|
||||||
func (a *Authentication) Stop() {
|
func (a *Authentication) Stop() {
|
||||||
a.fnCancel()
|
a.fnCancel()
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user