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.
4 "code.secondbit.org/uuid.hg"
7 "github.com/secondbit/pan"
10 type tokenScope struct {
15 func (t tokenScope) GetSQLTableName() string {
16 return "scopes_tokens"
19 func (t Token) GetSQLTableName() string {
23 func (p *postgres) getTokenSQL(token string, refresh bool) *pan.Query {
25 fields, _ := pan.GetFields(t)
26 query := pan.New(pan.POSTGRES, "SELECT "+pan.QueryList(fields)+" FROM "+pan.GetTableName(t))
29 query.Include(pan.GetUnquotedColumn(t, "AccessToken")+" = ?", token)
31 query.Include(pan.GetUnquotedColumn(t, "RefreshToken")+" = ?", token)
33 return query.FlushExpressions(" ")
36 func (p *postgres) getToken(token string, refresh bool) (Token, error) {
37 query := p.getTokenSQL(token, refresh)
38 rows, err := p.db.Query(query.String(), query.Args...)
45 err := pan.Unmarshal(rows, &t)
51 if err = rows.Err(); err != nil {
55 return t, ErrTokenNotFound
57 query = p.getTokenScopesSQL([]string{t.AccessToken})
58 rows, err = p.db.Query(query.String(), query.Args...)
64 err = pan.Unmarshal(rows, &ts)
68 t.Scopes = append(t.Scopes, ts.Scope)
70 if err = rows.Err(); err != nil {
76 func (p *postgres) saveTokenSQL(token Token) *pan.Query {
77 fields, values := pan.GetFields(token)
78 query := pan.New(pan.POSTGRES, "INSERT INTO "+pan.GetTableName(token))
79 query.Include("(" + pan.QueryList(fields) + ")")
80 query.Include("VALUES")
81 query.Include("("+pan.VariableList(len(values))+")", values...)
82 return query.FlushExpressions(" ")
85 func (p *postgres) saveTokenScopesSQL(ts []tokenScope) *pan.Query {
86 fields, _ := pan.GetFields(ts[0])
87 query := pan.New(pan.POSTGRES, "INSERT INTO "+pan.GetTableName(ts[0]))
88 query.Include("(" + pan.QueryList(fields) + ")")
89 query.Include("VALUES")
90 query.FlushExpressions(" ")
91 for _, t := range ts {
92 _, values := pan.GetFields(t)
93 query.Include("("+pan.VariableList(len(values))+")", values...)
95 return query.FlushExpressions(", ")
98 func (p *postgres) saveToken(token Token) error {
99 query := p.saveTokenSQL(token)
100 _, err := p.db.Exec(query.String(), query.Args...)
101 if e, ok := err.(*pq.Error); ok && e.Constraint == "tokens_pkey" {
102 err = ErrTokenAlreadyExists
104 if err != nil || len(token.Scopes) < 1 {
108 for _, scope := range token.Scopes {
109 ts = append(ts, tokenScope{Token: token.AccessToken, Scope: scope})
111 query = p.saveTokenScopesSQL(ts)
112 _, err = p.db.Exec(query.String(), query.Args...)
116 func (p *postgres) revokeTokenSQL(token string, refresh bool) *pan.Query {
118 query := pan.New(pan.POSTGRES, "UPDATE "+pan.GetTableName(t)+" SET ")
119 query.Include(pan.GetUnquotedColumn(t, "Revoked")+" = ?", true)
122 query.Include(pan.GetUnquotedColumn(t, "AccessToken")+" = ?", token)
124 query.Include(pan.GetUnquotedColumn(t, "RefreshToken")+" = ?", token)
126 return query.FlushExpressions(" ")
129 func (p *postgres) revokeToken(token string, refresh bool) error {
130 query := p.revokeTokenSQL(token, refresh)
131 res, err := p.db.Exec(query.String(), query.Args...)
135 rows, err := res.RowsAffected()
140 return ErrTokenNotFound
145 func (p *postgres) getTokensByProfileIDSQL(profileID uuid.ID, num, offset int) *pan.Query {
147 fields, _ := pan.GetFields(token)
148 query := pan.New(pan.POSTGRES, "SELECT "+pan.QueryList(fields)+" FROM "+pan.GetTableName(token))
150 query.Include(pan.GetUnquotedColumn(token, "ProfileID")+" = ?", profileID)
151 query.IncludeLimit(int64(num))
152 query.IncludeOffset(int64(offset))
153 return query.FlushExpressions(" ")
156 func (p *postgres) getTokenScopesSQL(tokens []string) *pan.Query {
158 fields, _ := pan.GetFields(t)
159 tokensI := make([]interface{}, len(tokens))
160 for pos, token := range tokens {
163 query := pan.New(pan.POSTGRES, "SELECT "+pan.QueryList(fields)+" FROM "+pan.GetTableName(t))
165 query.Include(pan.GetUnquotedColumn(t, "Token")+" IN ("+pan.VariableList(len(tokensI))+")", tokensI...)
166 return query.FlushExpressions(" ")
169 func (p *postgres) getTokensByProfileID(profileID uuid.ID, num, offset int) ([]Token, error) {
170 query := p.getTokensByProfileIDSQL(profileID, num, offset)
171 rows, err := p.db.Query(query.String(), query.Args...)
173 return []Token{}, err
176 var tokenIDs []string
179 err = pan.Unmarshal(rows, &token)
183 tokens = append(tokens, token)
184 tokenIDs = append(tokenIDs, token.AccessToken)
186 if err = rows.Err(); err != nil {
189 if len(tokenIDs) < 1 {
192 scopes := map[string][]string{}
193 query = p.getTokenScopesSQL(tokenIDs)
194 rows, err = p.db.Query(query.String(), query.Args...)
200 err = pan.Unmarshal(rows, &t)
204 scopes[t.Token] = append(scopes[t.Token], t.Scope)
206 if err = rows.Err(); err != nil {
209 for pos, token := range tokens {
210 token.Scopes = scopes[token.AccessToken]