mirror of
				https://github.com/aclindsa/moneygo.git
				synced 2025-11-03 18:13:27 -05:00 
			
		
		
		
	Split Sessions into models
This commit is contained in:
		@@ -1,38 +1,15 @@
 | 
			
		||||
package handlers
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"crypto/rand"
 | 
			
		||||
	"encoding/base64"
 | 
			
		||||
	"encoding/json"
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"github.com/aclindsa/moneygo/internal/models"
 | 
			
		||||
	"io"
 | 
			
		||||
	"log"
 | 
			
		||||
	"net/http"
 | 
			
		||||
	"strings"
 | 
			
		||||
	"time"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
type Session struct {
 | 
			
		||||
	SessionId     int64
 | 
			
		||||
	SessionSecret string `json:"-"`
 | 
			
		||||
	UserId        int64
 | 
			
		||||
	Created       time.Time
 | 
			
		||||
	Expires       time.Time
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (s *Session) Write(w http.ResponseWriter) error {
 | 
			
		||||
	enc := json.NewEncoder(w)
 | 
			
		||||
	return enc.Encode(s)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (s *Session) Read(json_str string) error {
 | 
			
		||||
	dec := json.NewDecoder(strings.NewReader(json_str))
 | 
			
		||||
	return dec.Decode(s)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func GetSession(tx *Tx, r *http.Request) (*Session, error) {
 | 
			
		||||
	var s Session
 | 
			
		||||
func GetSession(tx *Tx, r *http.Request) (*models.Session, error) {
 | 
			
		||||
	var s models.Session
 | 
			
		||||
 | 
			
		||||
	cookie, err := r.Cookie("moneygo-session")
 | 
			
		||||
	if err != nil {
 | 
			
		||||
@@ -63,16 +40,8 @@ func DeleteSessionIfExists(tx *Tx, r *http.Request) error {
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func NewSessionCookie() (string, error) {
 | 
			
		||||
	bits := make([]byte, 128)
 | 
			
		||||
	if _, err := io.ReadFull(rand.Reader, bits); err != nil {
 | 
			
		||||
		return "", err
 | 
			
		||||
	}
 | 
			
		||||
	return base64.StdEncoding.EncodeToString(bits), nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type NewSessionWriter struct {
 | 
			
		||||
	session *Session
 | 
			
		||||
	session *models.Session
 | 
			
		||||
	cookie  *http.Cookie
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -82,14 +51,12 @@ func (n *NewSessionWriter) Write(w http.ResponseWriter) error {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func NewSession(tx *Tx, r *http.Request, userid int64) (*NewSessionWriter, error) {
 | 
			
		||||
	s := Session{}
 | 
			
		||||
 | 
			
		||||
	session_secret, err := NewSessionCookie()
 | 
			
		||||
	s, err := models.NewSession(userid)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	existing, err := tx.SelectInt("SELECT count(*) from sessions where SessionSecret=?", session_secret)
 | 
			
		||||
	existing, err := tx.SelectInt("SELECT count(*) from sessions where SessionSecret=?", s.SessionSecret)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
@@ -97,26 +64,12 @@ func NewSession(tx *Tx, r *http.Request, userid int64) (*NewSessionWriter, error
 | 
			
		||||
		return nil, fmt.Errorf("%d session(s) exist with the generated session_secret", existing)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	cookie := http.Cookie{
 | 
			
		||||
		Name:     "moneygo-session",
 | 
			
		||||
		Value:    session_secret,
 | 
			
		||||
		Path:     "/",
 | 
			
		||||
		Domain:   r.URL.Host,
 | 
			
		||||
		Expires:  time.Now().AddDate(0, 1, 0), // a month from now
 | 
			
		||||
		Secure:   true,
 | 
			
		||||
		HttpOnly: true,
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	s.SessionSecret = session_secret
 | 
			
		||||
	s.UserId = userid
 | 
			
		||||
	s.Created = time.Now()
 | 
			
		||||
	s.Expires = cookie.Expires
 | 
			
		||||
 | 
			
		||||
	err = tx.Insert(&s)
 | 
			
		||||
	err = tx.Insert(s)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
	return &NewSessionWriter{&s, &cookie}, nil
 | 
			
		||||
 | 
			
		||||
	return &NewSessionWriter{s, s.Cookie(r.URL.Host)}, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func SessionHandler(r *http.Request, context *Context) ResponseWriterWriter {
 | 
			
		||||
 
 | 
			
		||||
@@ -3,6 +3,7 @@ package handlers_test
 | 
			
		||||
import (
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"github.com/aclindsa/moneygo/internal/handlers"
 | 
			
		||||
	"github.com/aclindsa/moneygo/internal/models"
 | 
			
		||||
	"net/http"
 | 
			
		||||
	"net/http/cookiejar"
 | 
			
		||||
	"net/url"
 | 
			
		||||
@@ -26,8 +27,8 @@ func newSession(user *User) (*http.Client, error) {
 | 
			
		||||
	return &client, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func getSession(client *http.Client) (*handlers.Session, error) {
 | 
			
		||||
	var s handlers.Session
 | 
			
		||||
func getSession(client *http.Client) (*models.Session, error) {
 | 
			
		||||
	var s models.Session
 | 
			
		||||
	err := read(client, &s, "/v1/sessions/")
 | 
			
		||||
	return &s, err
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user