Enable terminating sessions through the API.
Add a terminateSession method to the sessionStore that sets the Active property
of the Session to false.
Create a Context.TerminateSession wrapper for the terminateSession method on the
sessionStore.
Add a Sessions property to our response type so we can return a []Session in API
responses.
Use the URL-safe encoding when base64 encoding our session ID and CSRFToken, so
the ID can be passed in the URL and so our encodings are consistent.
Add a TerminateSessionHandler function that will extract a Session ID from the
request URL, authenticate the user, check that the authenticated user owns the
session in question, and terminate the session.
Add implementations for our new terminateSession method for the memstore and
postgres types.
Test both the memstore and postgres implementation of our terminateSession
helper in session_test.go.
6 "code.secondbit.org/uuid.hg"
9 "github.com/secondbit/pan"
12 func (s Session) GetSQLTableName() string {
16 func (p *postgres) createSessionSQL(session Session) *pan.Query {
17 fields, values := pan.GetFields(session)
18 query := pan.New(pan.POSTGRES, "INSERT INTO "+pan.GetTableName(session))
19 query.Include("(" + pan.QueryList(fields) + ")")
20 query.Include("VALUES")
21 query.Include("("+pan.VariableList(len(values))+")", values...)
22 return query.FlushExpressions(" ")
25 func (p *postgres) createSession(session Session) error {
26 query := p.createSessionSQL(session)
27 _, err := p.db.Exec(query.String(), query.Args...)
28 if e, ok := err.(*pq.Error); ok && e.Constraint == "sessions_pkey" {
29 err = ErrSessionAlreadyExists
34 func (p *postgres) getSessionSQL(id string) *pan.Query {
36 fields, _ := pan.GetFields(session)
37 query := pan.New(pan.POSTGRES, "SELECT "+pan.QueryList(fields)+" FROM "+pan.GetTableName(session))
39 query.Include(pan.GetUnquotedColumn(session, "ID")+" = ?", id)
40 return query.FlushExpressions(" ")
43 func (p *postgres) getSession(id string) (Session, error) {
44 query := p.getSessionSQL(id)
45 rows, err := p.db.Query(query.String(), query.Args...)
52 err := pan.Unmarshal(rows, &session)
58 if err = rows.Err(); err != nil {
62 return session, ErrSessionNotFound
67 func (p *postgres) terminateSessionSQL(id string) *pan.Query {
69 query := pan.New(pan.POSTGRES, "UPDATE "+pan.GetTableName(session)+" SET")
70 query.Include(pan.GetUnquotedColumn(session, "Active")+" = ?", false)
72 query.Include(pan.GetUnquotedColumn(session, "ID")+" = ?", id)
73 return query.FlushExpressions(" ")
76 func (p *postgres) terminateSession(id string) error {
77 query := p.terminateSessionSQL(id)
78 res, err := p.db.Exec(query.String(), query.Args...)
82 rows, err := res.RowsAffected()
87 return ErrSessionNotFound
92 func (p *postgres) removeSessionSQL(id string) *pan.Query {
94 query := pan.New(pan.POSTGRES, "DELETE FROM "+pan.GetTableName(session))
96 query.Include(pan.GetUnquotedColumn(session, "ID")+" = ?", id)
97 return query.FlushExpressions(" ")
100 func (p *postgres) removeSession(id string) error {
101 query := p.removeSessionSQL(id)
102 res, err := p.db.Exec(query.String(), query.Args...)
106 rows, err := res.RowsAffected()
111 return ErrSessionNotFound
116 func (p *postgres) listSessionsSQL(profile uuid.ID, before time.Time, num int64) *pan.Query {
118 fields, _ := pan.GetFields(session)
119 query := pan.New(pan.POSTGRES, "SELECT "+pan.QueryList(fields)+" FROM "+pan.GetTableName(session))
121 query.Include(pan.GetUnquotedColumn(session, "ProfileID")+" = ?", profile)
122 if !before.IsZero() {
123 query.Include(pan.GetUnquotedColumn(session, "Created")+" < ?", before)
125 query.FlushExpressions(" AND ")
127 query.IncludeLimit(num)
129 return query.FlushExpressions(" ")
132 func (p *postgres) listSessions(profile uuid.ID, before time.Time, num int64) ([]Session, error) {
133 query := p.listSessionsSQL(profile, before, num)
134 rows, err := p.db.Query(query.String(), query.Args...)
136 return []Session{}, err
138 var sessions []Session
141 err := pan.Unmarshal(rows, &session)
145 sessions = append(sessions, session)
147 if err = rows.Err(); err != nil {