Start to support deleting profiles through the API.
Create a removeLoginsByProfile method on the profileStore, to allow an easy way
to bulk-delete logins associated with a Profile after the Profile has been
deleted.
Create postgres and memstore implementations of the removeLoginsByProfile
method.
Create a cleanUpAfterProfileDeletion helper method that will clean up the child
objects of a Profile (its Sessions, Tokens, Clients, etc.). The intended usage
is to call this in a goroutine after a Profile has been deleted, to try and get
things back in order.
Detect when the UpdateProfileHandler API is used to set the Deleted flag of a
Profile to true, and clean up after the Profile when that's the case.
Add a DeleteProfileHandler API endpoint that is a shortcut to setting the
Deleted flag of a Profile to true and cleaning up after the Profile.
The problem with our approach thus far is that some of it is reversible and some
is not. If a Profile is maliciously/accidentally deleted, it's simple enough to
use the API as a superuser to restore the Profile. But doing that will not (and
cannot) restore the Logins associated with that Profile, for example. While it
would be nice to add a Deleted flag to our Logins that we could simply toggle,
that would wreak havoc with our database constraints and ensuring uniqueness of
Login values. I still don't have a solution for this, outside the superuser
manually restoring a Login for the Profile, after which the user can
authenticate themselves and add more Logins as desired. But there has to be a
better way.
I suppose since the passphrase is being stored with the Profile and not the
Login, we could offer an endpoint that would automate this, but... well, that
would be tricky. It would require the user remembering their Profile ID, and
let's be honest, nobody's going to remember a UUID.
Maybe such an endpoint would help from a customer service standpoint: we
identify their Profile manually, then send them to /profiles/ID/restorelogin or
something, and that lets them add a Login back to the Profile.
I'll figure it out later. For now, we know we at least have enough information
to identify a user is who they say they are and resolve the situation manually.
5 "github.com/secondbit/pan"
8 type authCodeScope struct {
13 func (acs authCodeScope) GetSQLTableName() string {
14 return "authorization_codes_scopes"
17 func (ac AuthorizationCode) GetSQLTableName() string {
18 return "authorization_codes"
21 func (p *postgres) getAuthorizationCodeSQL(code string) *pan.Query {
22 var ac AuthorizationCode
23 fields, _ := pan.GetFields(ac)
24 query := pan.New(pan.POSTGRES, "SELECT "+pan.QueryList(fields)+" FROM "+pan.GetTableName(ac))
26 query.Include(pan.GetUnquotedColumn(ac, "Code")+" = ?", code)
27 return query.FlushExpressions(" ")
30 func (p *postgres) getAuthorizationCodeScopesSQL(codes []string) *pan.Query {
32 fields, _ := pan.GetFields(acs)
33 codesI := make([]interface{}, len(codes))
34 for pos, code := range codes {
37 query := pan.New(pan.POSTGRES, "SELECT "+pan.QueryList(fields)+" FROM "+pan.GetTableName(acs))
39 query.Include(pan.GetUnquotedColumn(acs, "Code")+" IN ("+pan.VariableList(len(codesI))+")", codesI...)
40 return query.FlushExpressions(" ")
43 func (p *postgres) getAuthorizationCode(code string) (AuthorizationCode, error) {
44 query := p.getAuthorizationCodeSQL(code)
45 rows, err := p.db.Query(query.String(), query.Args...)
47 return AuthorizationCode{}, err
49 var ac AuthorizationCode
52 err := pan.Unmarshal(rows, &ac)
58 if err = rows.Err(); err != nil {
62 return ac, ErrAuthorizationCodeNotFound
64 query = p.getAuthorizationCodeScopesSQL([]string{code})
65 rows, err = p.db.Query(query.String(), query.Args...)
71 err = pan.Unmarshal(rows, &acs)
75 ac.Scopes = append(ac.Scopes, acs.Scope)
77 if err = rows.Err(); err != nil {
83 func (p *postgres) saveAuthorizationCodeSQL(authCode AuthorizationCode) *pan.Query {
84 fields, values := pan.GetFields(authCode)
85 query := pan.New(pan.POSTGRES, "INSERT INTO "+pan.GetTableName(authCode))
86 query.Include("(" + pan.QueryList(fields) + ")")
87 query.Include("VALUES")
88 query.Include("("+pan.VariableList(len(values))+")", values...)
89 return query.FlushExpressions(" ")
92 func (p *postgres) saveAuthorizationCodeScopesSQL(authCodeScopes []authCodeScope) *pan.Query {
93 fields, _ := pan.GetFields(authCodeScopes[0])
94 query := pan.New(pan.POSTGRES, "INSERT INTO "+pan.GetTableName(authCodeScopes[0]))
95 query.Include("(" + pan.QueryList(fields) + ")")
96 query.Include("VALUES")
97 query.FlushExpressions(" ")
98 for _, acs := range authCodeScopes {
99 _, values := pan.GetFields(acs)
100 query.Include("("+pan.VariableList(len(values))+")", values...)
102 return query.FlushExpressions(", ")
105 func (p *postgres) saveAuthorizationCode(authCode AuthorizationCode) error {
106 query := p.saveAuthorizationCodeSQL(authCode)
107 _, err := p.db.Exec(query.String(), query.Args...)
108 if e, ok := err.(*pq.Error); ok && e.Constraint == "authorization_codes_pkey" {
109 err = ErrAuthorizationCodeAlreadyExists
111 if err != nil || len(authCode.Scopes) < 1 {
114 var acs []authCodeScope
115 for _, scope := range authCode.Scopes {
116 acs = append(acs, authCodeScope{Code: authCode.Code, Scope: scope})
118 query = p.saveAuthorizationCodeScopesSQL(acs)
119 _, err = p.db.Exec(query.String(), query.Args...)
123 func (p *postgres) deleteAuthorizationCodeSQL(code string) *pan.Query {
124 var authCode AuthorizationCode
125 query := pan.New(pan.POSTGRES, "DELETE FROM "+pan.GetTableName(authCode))
127 query.Include(pan.GetUnquotedColumn(authCode, "Code")+" = ?", code)
128 return query.FlushExpressions(" ")
131 func (p *postgres) deleteAuthorizationCodeScopesSQL(code string) *pan.Query {
132 var acs authCodeScope
133 query := pan.New(pan.POSTGRES, "DELETE FROM "+pan.GetTableName(acs))
135 query.Include(pan.GetUnquotedColumn(acs, "Code")+" = ?", code)
136 return query.FlushExpressions(" ")
139 func (p *postgres) deleteAuthorizationCode(code string) error {
140 query := p.deleteAuthorizationCodeSQL(code)
141 res, err := p.db.Exec(query.String(), query.Args...)
145 rows, err := res.RowsAffected()
150 return ErrAuthorizationCodeNotFound
152 query = p.deleteAuthorizationCodeScopesSQL(code)
153 _, err = p.db.Exec(query.String(), query.Args...)
157 func (p *postgres) useAuthorizationCodeSQL(code string) *pan.Query {
158 var authCode AuthorizationCode
159 query := pan.New(pan.POSTGRES, "UPDATE "+pan.GetTableName(authCode)+" SET ")
160 query.Include(pan.GetUnquotedColumn(authCode, "Used")+" = ?", true)
162 query.Include(pan.GetUnquotedColumn(authCode, "Code")+" = ?", code)
163 return query.FlushExpressions(" ")
166 func (p *postgres) useAuthorizationCode(code string) error {
167 query := p.useAuthorizationCodeSQL(code)
168 res, err := p.db.Exec(query.String(), query.Args...)
172 rows, err := res.RowsAffected()
177 return ErrAuthorizationCodeNotFound