Implement postgres version of scopeStore.
Update the authd server to use postgres as its scopeStore, instead of memstore.
panic when starting the authd server if the CreateScopes call fails. This
should, ideally, ignore ErrScopeAlreadyExists errors, but does not as of this
commit.
Update the simple.gotmpl template to properly display scopes, after switching to
the Scope type instead of simply passing around the string the client supplied
broke the template and I never bothered fixing it.
Update the updateScopes method on the scopeStore (and the corresponding
UpdateScopes method on the Context type) to be updateScope/UpdateScope.
Operating on several scopes at a time like that is simply too challenging in
SQL and I can't justify the complexity with a use case.
Add a helper method to ScopeChange called Empty(), which returns true if the
ScopeChange is full of nil values.
Remove the ID from the ScopeChange type, because we're no longer accepting
multiple ScopeChange types in UpdateScope, so we can supply that information
outside the ScopeChange, which matches the rest of our update* methods.
Correct our tests in scope_test.go to correctly use the updateScope method
instead of the old updateScopes method. This generally just resulted in calling
updateScope multiple times, as opposed to just once.
Add a scope table initialization to the sql/postgres_init.sql script.
6 "code.secondbit.org/uuid.hg"
8 "github.com/secondbit/pan"
11 func (p Profile) GetSQLTableName() string {
15 func (l Login) GetSQLTableName() string {
19 func (p *postgres) getProfileByIDSQL(id uuid.ID) *pan.Query {
21 fields, _ := pan.GetFields(profile)
22 query := pan.New(pan.POSTGRES, "SELECT "+pan.QueryList(fields)+" FROM "+pan.GetTableName(profile))
24 query.Include(pan.GetUnquotedColumn(profile, "ID")+" = ? AND "+pan.GetUnquotedColumn(profile, "Deleted")+" = ?", id, false)
25 return query.FlushExpressions(" ")
28 func (p *postgres) getProfileByID(id uuid.ID) (Profile, error) {
29 query := p.getProfileByIDSQL(id)
30 rows, err := p.db.Query(query.String(), query.Args...)
37 err := pan.Unmarshal(rows, &profile)
43 if err := rows.Err(); err != nil {
47 return profile, ErrProfileNotFound
52 func (p *postgres) getProfileByLoginSQL(value string) *pan.Query {
55 fields, _ := pan.GetUnquotedAbsoluteFields(profile)
56 query := pan.New(pan.POSTGRES, "SELECT "+pan.QueryList(fields)+" FROM "+pan.GetTableName(profile))
57 query.Include("INNER JOIN " + pan.GetTableName(login))
58 query.Include("ON " + pan.GetUnquotedAbsoluteColumn(profile, "ID") + " = " + pan.GetUnquotedAbsoluteColumn(login, "ProfileID"))
60 query.Include(pan.GetUnquotedAbsoluteColumn(login, "Value")+" = ? AND "+pan.GetUnquotedAbsoluteColumn(profile, "Deleted")+" = ?", value, false)
61 return query.FlushExpressions(" ")
64 func (p *postgres) getProfileByLogin(value string) (Profile, error) {
65 query := p.getProfileByLoginSQL(value)
66 rows, err := p.db.Query(query.String(), query.Args...)
73 err := pan.Unmarshal(rows, &profile)
79 if err := rows.Err(); err != nil {
83 return profile, ErrProfileNotFound
88 func (p *postgres) saveProfileSQL(profile Profile) *pan.Query {
89 fields, values := pan.GetFields(profile)
90 query := pan.New(pan.POSTGRES, "INSERT INTO "+pan.GetTableName(profile))
91 query.Include("(" + pan.QueryList(fields) + ")")
92 query.Include("VALUES")
93 query.Include("("+pan.VariableList(len(values))+")", values...)
94 return query.FlushExpressions(" ")
97 func (p *postgres) saveProfile(profile Profile) error {
98 query := p.saveProfileSQL(profile)
99 _, err := p.db.Exec(query.String(), query.Args...)
100 if e, ok := err.(*pq.Error); ok && e.Constraint == "profiles_pkey" {
101 err = ErrProfileAlreadyExists
106 func (p *postgres) updateProfileSQL(id uuid.ID, change ProfileChange) *pan.Query {
108 query := pan.New(pan.POSTGRES, "UPDATE "+pan.GetTableName(profile)+" SET ")
109 query.IncludeIfNotNil(pan.GetUnquotedColumn(profile, "Name")+" = ?", change.Name)
110 query.IncludeIfNotNil(pan.GetUnquotedColumn(profile, "Passphrase")+" = ?", change.Passphrase)
111 query.IncludeIfNotNil(pan.GetUnquotedColumn(profile, "Iterations")+" = ?", change.Iterations)
112 query.IncludeIfNotNil(pan.GetUnquotedColumn(profile, "Salt")+" = ?", change.Salt)
113 query.IncludeIfNotNil(pan.GetUnquotedColumn(profile, "PassphraseScheme")+" = ?", change.PassphraseScheme)
114 query.IncludeIfNotNil(pan.GetUnquotedColumn(profile, "Compromised")+" = ?", change.Compromised)
115 query.IncludeIfNotNil(pan.GetUnquotedColumn(profile, "LockedUntil")+" = ?", change.LockedUntil)
116 query.IncludeIfNotNil(pan.GetUnquotedColumn(profile, "PassphraseReset")+" = ?", change.PassphraseReset)
117 query.IncludeIfNotNil(pan.GetUnquotedColumn(profile, "PassphraseResetCreated")+" = ?", change.PassphraseResetCreated)
118 query.IncludeIfNotNil(pan.GetUnquotedColumn(profile, "LastSeen")+" = ?", change.LastSeen)
119 query.IncludeIfNotNil(pan.GetUnquotedColumn(profile, "Deleted")+" = ?", change.Deleted)
120 query.FlushExpressions(", ")
122 query.Include(pan.GetUnquotedColumn(profile, "ID")+" = ?", id)
123 return query.FlushExpressions(" ")
126 func (p *postgres) updateProfile(id uuid.ID, change ProfileChange) error {
130 query := p.updateProfileSQL(id, change)
131 _, err := p.db.Exec(query.String(), query.Args...)
135 func (p *postgres) updateProfilesSQL(ids []uuid.ID, change BulkProfileChange) *pan.Query {
140 query := pan.New(pan.POSTGRES, "UPDATE "+pan.GetTableName(profile)+" SET ")
141 query.IncludeIfNotNil(pan.GetUnquotedColumn(profile, "Compromised")+" = ?", change.Compromised)
142 query.FlushExpressions(", ")
144 intids := make([]interface{}, len(ids))
145 for pos, id := range ids {
148 query.Include(pan.GetUnquotedColumn(profile, "ID")+" IN ("+pan.VariableList(len(ids))+")", intids...)
149 return query.FlushExpressions(" ")
152 func (p *postgres) updateProfiles(ids []uuid.ID, change BulkProfileChange) error {
153 query := p.updateProfilesSQL(ids, change)
154 _, err := p.db.Exec(query.String(), query.Args...)
158 func (p *postgres) addLoginSQL(login Login) *pan.Query {
159 fields, values := pan.GetFields(login)
160 query := pan.New(pan.POSTGRES, "INSERT INTO "+pan.GetTableName(login))
161 query.Include("(" + pan.QueryList(fields) + ")")
162 query.Include("VALUES")
163 query.Include("("+pan.VariableList(len(values))+")", values...)
164 return query.FlushExpressions(" ")
167 func (p *postgres) addLogin(login Login) error {
168 query := p.addLoginSQL(login)
169 _, err := p.db.Exec(query.String(), query.Args...)
170 if e, ok := err.(*pq.Error); ok && e.Constraint == "logins_pkey" {
171 return ErrLoginAlreadyExists
176 func (p *postgres) removeLoginSQL(value string, profile uuid.ID) *pan.Query {
178 query := pan.New(pan.POSTGRES, "DELETE FROM "+pan.GetTableName(login))
180 query.Include(pan.GetUnquotedColumn(login, "Value")+" = ? AND "+pan.GetUnquotedColumn(login, "ProfileID")+" = ?", value, profile)
181 return query.FlushExpressions(" ")
184 func (p *postgres) removeLogin(value string, profile uuid.ID) error {
185 query := p.removeLoginSQL(value, profile)
186 res, err := p.db.Exec(query.String(), query.Args...)
190 rows, err := res.RowsAffected()
195 return ErrLoginNotFound
200 func (p *postgres) recordLoginUseSQL(value string, when time.Time) *pan.Query {
202 query := pan.New(pan.POSTGRES, "UPDATE "+pan.GetTableName(login)+" SET ")
203 query.Include(pan.GetUnquotedColumn(login, "LastUsed")+" = ?", when)
205 query.Include(pan.GetUnquotedColumn(login, "Value")+" = ?", value)
206 return query.FlushExpressions(" ")
209 func (p *postgres) recordLoginUse(value string, when time.Time) error {
210 query := p.recordLoginUseSQL(value, when)
211 _, err := p.db.Exec(query.String(), query.Args...)
215 func (p *postgres) listLoginsSQL(profile uuid.ID, num, offset int) *pan.Query {
217 fields, _ := pan.GetFields(login)
218 query := pan.New(pan.POSTGRES, "SELECT "+pan.QueryList(fields)+" FROM "+pan.GetTableName(login))
220 query.Include(pan.GetUnquotedColumn(login, "ProfileID")+" = ?", profile)
221 query.IncludeLimit(int64(num))
222 query.IncludeOffset(int64(offset))
223 return query.FlushExpressions(" ")
226 func (p *postgres) listLogins(profile uuid.ID, num, offset int) ([]Login, error) {
227 query := p.listLoginsSQL(profile, num, offset)
228 rows, err := p.db.Query(query.String(), query.Args...)
230 return []Login{}, err
235 err = pan.Unmarshal(rows, &login)
239 logins = append(logins, login)
241 if err := rows.Err(); err != nil {