auth

Paddy 2015-04-25 Parent:6f473576c6ae

164:cf1aef6eb81f Go to Latest

auth/session_postgres.go

Clean up after Client deletion, finish cleaning up after Profile deletion. 6f473576c6ae started cleaning up after Profiles when they're deleted, but didn't clean up the Clients created by that Profile. This fixes that, and also fixes a BUG note about cleaning up after a Client when it's deleted. Extend the authorizationCodeStore to have a deleteAuthorizationCodesByClientID method that will delete the AuthorizationCodes that have been granted by the Client specified by the passed ID. We also implemented this in memstore and postgres, so tests continue to pass. Extend the clientStore to have a deleteClientsByOwner method that will delete the Clients that were created by the Profile specified by the passed ID. We also implemented this in memstore and postgres, so tests continue to pass. Extend the clientStore to have a removeEndpointsByClientID method that will delete the Endpoints that belong(ed) to a the Client specified by the passed ID. We also implemented this in memstore and postgres, so tests continue to pass. Extend the tokenStore to have a revokeTokensByClientID method that will revoke all the Tokens that were granted to the Client specified by the passed ID. We also implemented this in memstore and postgres, so tests continue to pass. When listing Clients by their owner, allow setting the num argument (which controls how many to return) to 0 or lower, and using that to signal "return all Clients belonging to this owner", instead of paging. This is useful when deleting the Clients belonging to a Profile as part of the cleanup after deleting the Profile. Create a cleanUpAfterClientDeletion helper function that will delete the Endpoints and AuthorizationCodes belonging to a Client, and revoke the Tokens belonging to a Client, as part of cleaning up after a Client has been deleted. Add a check in the handler for listing Clients owned by a Profile to disallow the num argument to be lower than 1, because the API should be forced to page. Call our cleanUpAfterClientDeletion once the Client has been deleted in the appropriate handler. Fill out our Context with new methods to wrap all the new methods we're adding to our *Stores. In cleanUpAfterProfileDeletion, obtain a list of clients belonging to the owner, use our new DeleteClientsByOwner method to remove all of them, and then use the list to run our new cleanUpAfterClientDeletion function to clear away the final remnants of a Profile when it's deleted.

History
1 package auth
3 import (
4 "time"
6 "code.secondbit.org/uuid.hg"
8 "github.com/lib/pq"
9 "github.com/secondbit/pan"
10 )
12 func (s Session) GetSQLTableName() string {
13 return "sessions"
14 }
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(" ")
23 }
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
30 }
31 return err
32 }
34 func (p *postgres) getSessionSQL(id string) *pan.Query {
35 var session Session
36 fields, _ := pan.GetFields(session)
37 query := pan.New(pan.POSTGRES, "SELECT "+pan.QueryList(fields)+" FROM "+pan.GetTableName(session))
38 query.IncludeWhere()
39 query.Include(pan.GetUnquotedColumn(session, "ID")+" = ?", id)
40 return query.FlushExpressions(" ")
41 }
43 func (p *postgres) getSession(id string) (Session, error) {
44 query := p.getSessionSQL(id)
45 rows, err := p.db.Query(query.String(), query.Args...)
46 if err != nil {
47 return Session{}, err
48 }
49 var session Session
50 var found bool
51 for rows.Next() {
52 err := pan.Unmarshal(rows, &session)
53 if err != nil {
54 return session, err
55 }
56 found = true
57 }
58 if err = rows.Err(); err != nil {
59 return session, err
60 }
61 if !found {
62 return session, ErrSessionNotFound
63 }
64 return session, nil
65 }
67 func (p *postgres) terminateSessionSQL(id string) *pan.Query {
68 var session Session
69 query := pan.New(pan.POSTGRES, "UPDATE "+pan.GetTableName(session)+" SET")
70 query.Include(pan.GetUnquotedColumn(session, "Active")+" = ?", false)
71 query.IncludeWhere()
72 query.Include(pan.GetUnquotedColumn(session, "ID")+" = ?", id)
73 return query.FlushExpressions(" ")
74 }
76 func (p *postgres) terminateSession(id string) error {
77 query := p.terminateSessionSQL(id)
78 res, err := p.db.Exec(query.String(), query.Args...)
79 if err != nil {
80 return err
81 }
82 rows, err := res.RowsAffected()
83 if err != nil {
84 return err
85 }
86 if rows < 1 {
87 return ErrSessionNotFound
88 }
89 return nil
90 }
92 func (p *postgres) terminateSessionsByProfileSQL(profile uuid.ID) *pan.Query {
93 var session Session
94 query := pan.New(pan.POSTGRES, "UPDATE "+pan.GetTableName(session)+" SET")
95 query.Include(pan.GetUnquotedColumn(session, "Active")+" = ?", false)
96 query.IncludeWhere()
97 query.Include(pan.GetUnquotedColumn(session, "ProfileID")+" = ?", profile)
98 return query.FlushExpressions(" ")
99 }
101 func (p *postgres) terminateSessionsByProfile(profile uuid.ID) error {
102 query := p.terminateSessionsByProfileSQL(profile)
103 res, err := p.db.Exec(query.String(), query.Args...)
104 if err != nil {
105 return err
106 }
107 rows, err := res.RowsAffected()
108 if err != nil {
109 return err
110 }
111 if rows < 1 {
112 return ErrProfileNotFound
113 }
114 return nil
115 }
117 func (p *postgres) removeSessionSQL(id string) *pan.Query {
118 var session Session
119 query := pan.New(pan.POSTGRES, "DELETE FROM "+pan.GetTableName(session))
120 query.IncludeWhere()
121 query.Include(pan.GetUnquotedColumn(session, "ID")+" = ?", id)
122 return query.FlushExpressions(" ")
123 }
125 func (p *postgres) removeSession(id string) error {
126 query := p.removeSessionSQL(id)
127 res, err := p.db.Exec(query.String(), query.Args...)
128 if err != nil {
129 return err
130 }
131 rows, err := res.RowsAffected()
132 if err != nil {
133 return err
134 }
135 if rows < 1 {
136 return ErrSessionNotFound
137 }
138 return nil
139 }
141 func (p *postgres) listSessionsSQL(profile uuid.ID, before time.Time, num int64) *pan.Query {
142 var session Session
143 fields, _ := pan.GetFields(session)
144 query := pan.New(pan.POSTGRES, "SELECT "+pan.QueryList(fields)+" FROM "+pan.GetTableName(session))
145 query.IncludeWhere()
146 query.Include(pan.GetUnquotedColumn(session, "ProfileID")+" = ?", profile)
147 if !before.IsZero() {
148 query.Include(pan.GetUnquotedColumn(session, "Created")+" < ?", before)
149 }
150 query.FlushExpressions(" AND ")
151 if num > 0 {
152 query.IncludeLimit(num)
153 }
154 return query.FlushExpressions(" ")
155 }
157 func (p *postgres) listSessions(profile uuid.ID, before time.Time, num int64) ([]Session, error) {
158 query := p.listSessionsSQL(profile, before, num)
159 rows, err := p.db.Query(query.String(), query.Args...)
160 if err != nil {
161 return []Session{}, err
162 }
163 var sessions []Session
164 for rows.Next() {
165 var session Session
166 err := pan.Unmarshal(rows, &session)
167 if err != nil {
168 return sessions, err
169 }
170 sessions = append(sessions, session)
171 }
172 if err = rows.Err(); err != nil {
173 return sessions, err
174 }
175 return sessions, nil
176 }