Use postgres arrays for scope associations.
Use the new pqarrays library I wrote to store Scope associations for Tokens and
AuthorizationCodes, instead of using our hacky and abstraction-breaking
many-to-many code.
We also created the authStore.deleteAuthorizationCodesByProfileID method, to
clear out the AuthorizationCodes that belong to a Profile (used when the Profile
is deleted). So we added the implementation for memstore and for our postgres
store.
Call Context.DeleteAuthorizationCodesByProfileID when deleting a Profile to
clean up after it.
Rename sortedScopes to Scopes, which we use pqarrays.StringArray's methods on to
fulfill the sql.Scanner and driver.Valuer interfaces. This lets us store Scopes
in postgres arrays.
Create a stringsToScopes helper function that creates Scope objects, with their
IDs filled by the strings specified.
Update our GrantType.Validate function signature to return Scopes instead of
[]string.
Create a Scopes.Strings() helper method that returns a []string of the IDs of
the Scopes.
Update our SQL init file to use the new postgres array definition, instead of
the many-to-many definition.
4 "code.secondbit.org/uuid.hg"
6 "github.com/secondbit/pan"
9 func (ac AuthorizationCode) GetSQLTableName() string {
10 return "authorization_codes"
13 func (p *postgres) getAuthorizationCodeSQL(code string) *pan.Query {
14 var ac AuthorizationCode
15 fields, _ := pan.GetFields(ac)
16 query := pan.New(pan.POSTGRES, "SELECT "+pan.QueryList(fields)+" FROM "+pan.GetTableName(ac))
18 query.Include(pan.GetUnquotedColumn(ac, "Code")+" = ?", code)
19 return query.FlushExpressions(" ")
22 func (p *postgres) getAuthorizationCode(code string) (AuthorizationCode, error) {
23 query := p.getAuthorizationCodeSQL(code)
24 rows, err := p.db.Query(query.String(), query.Args...)
26 return AuthorizationCode{}, err
28 var ac AuthorizationCode
31 err := pan.Unmarshal(rows, &ac)
37 if err = rows.Err(); err != nil {
41 return ac, ErrAuthorizationCodeNotFound
46 func (p *postgres) saveAuthorizationCodeSQL(authCode AuthorizationCode) *pan.Query {
47 fields, values := pan.GetFields(authCode)
48 query := pan.New(pan.POSTGRES, "INSERT INTO "+pan.GetTableName(authCode))
49 query.Include("(" + pan.QueryList(fields) + ")")
50 query.Include("VALUES")
51 query.Include("("+pan.VariableList(len(values))+")", values...)
52 return query.FlushExpressions(" ")
55 func (p *postgres) saveAuthorizationCode(authCode AuthorizationCode) error {
56 query := p.saveAuthorizationCodeSQL(authCode)
57 _, err := p.db.Exec(query.String(), query.Args...)
58 if e, ok := err.(*pq.Error); ok && e.Constraint == "authorization_codes_pkey" {
59 err = ErrAuthorizationCodeAlreadyExists
64 func (p *postgres) deleteAuthorizationCodeSQL(code string) *pan.Query {
65 var authCode AuthorizationCode
66 query := pan.New(pan.POSTGRES, "DELETE FROM "+pan.GetTableName(authCode))
68 query.Include(pan.GetUnquotedColumn(authCode, "Code")+" = ?", code)
69 return query.FlushExpressions(" ")
72 func (p *postgres) deleteAuthorizationCode(code string) error {
73 query := p.deleteAuthorizationCodeSQL(code)
74 res, err := p.db.Exec(query.String(), query.Args...)
78 rows, err := res.RowsAffected()
83 return ErrAuthorizationCodeNotFound
88 func (p *postgres) deleteAuthorizationCodesByProfileIDSQL(profileID uuid.ID) *pan.Query {
89 var authCode AuthorizationCode
90 query := pan.New(pan.POSTGRES, "DELETE FROM "+pan.GetTableName(authCode))
92 query.Include(pan.GetUnquotedColumn(authCode, "ProfileID")+" = ?", profileID)
93 return query.FlushExpressions(" ")
96 func (p *postgres) deleteAuthorizationCodesByProfileID(profileID uuid.ID) error {
97 query := p.deleteAuthorizationCodesByProfileIDSQL(profileID)
98 res, err := p.db.Exec(query.String(), query.Args...)
102 rows, err := res.RowsAffected()
107 return ErrProfileNotFound
112 func (p *postgres) useAuthorizationCodeSQL(code string) *pan.Query {
113 var authCode AuthorizationCode
114 query := pan.New(pan.POSTGRES, "UPDATE "+pan.GetTableName(authCode)+" SET ")
115 query.Include(pan.GetUnquotedColumn(authCode, "Used")+" = ?", true)
117 query.Include(pan.GetUnquotedColumn(authCode, "Code")+" = ?", code)
118 return query.FlushExpressions(" ")
121 func (p *postgres) useAuthorizationCode(code string) error {
122 query := p.useAuthorizationCodeSQL(code)
123 res, err := p.db.Exec(query.String(), query.Args...)
127 rows, err := res.RowsAffected()
132 return ErrAuthorizationCodeNotFound