Make client use our auth(n/z) scheme.
Our auth(n/z) scheme can be loosely defined as "encrypted tokens that nginx
transforms into headers" and "scopes for bypassing ACL". Our Go client, which is
what we'll be using to have services communicate with each other, follows this
paradigm now by auto-injecting the headers we'll need to identify ourselves.
This will work behind our firewall, but will be useless for the rest of the
world, which will need to go through the nginx bastion that can strip the
headers and replace them with the headers appropriate to the token attached to
the request.
This did involve setting a static client ID as the client for our
email_verification listener. Ideally, this would cause Client registration
(using that ID) when the listener starts up, ignoring ErrClientAlreadyExists. I
don't want to have to write the code that will allow us to bypass the Client ACL
properly right now, though, so we're just going to have to remember to manually
create that Client. Or not. I don't think it will do any harm (outside the OAuth
flow) to be using a Client ID that doesn't actually point to a Client. I just
think it'd be good for record-keeping purposes.
6 "code.secondbit.org/uuid.hg"
9 "github.com/secondbit/pan"
12 func (s Session) GetSQLTableName() string {
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(" ")
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
34 func (p *postgres) getSessionSQL(id string) *pan.Query {
36 fields, _ := pan.GetFields(session)
37 query := pan.New(pan.POSTGRES, "SELECT "+pan.QueryList(fields)+" FROM "+pan.GetTableName(session))
39 query.Include(pan.GetUnquotedColumn(session, "ID")+" = ?", id)
40 return query.FlushExpressions(" ")
43 func (p *postgres) getSession(id string) (Session, error) {
44 query := p.getSessionSQL(id)
45 rows, err := p.db.Query(query.String(), query.Args...)
52 err := pan.Unmarshal(rows, &session)
58 if err = rows.Err(); err != nil {
62 return session, ErrSessionNotFound
67 func (p *postgres) terminateSessionSQL(id string) *pan.Query {
69 query := pan.New(pan.POSTGRES, "UPDATE "+pan.GetTableName(session)+" SET")
70 query.Include(pan.GetUnquotedColumn(session, "Active")+" = ?", false)
72 query.Include(pan.GetUnquotedColumn(session, "ID")+" = ?", id)
73 return query.FlushExpressions(" ")
76 func (p *postgres) terminateSession(id string) error {
77 query := p.terminateSessionSQL(id)
78 res, err := p.db.Exec(query.String(), query.Args...)
82 rows, err := res.RowsAffected()
87 return ErrSessionNotFound
92 func (p *postgres) terminateSessionsByProfileSQL(profile uuid.ID) *pan.Query {
94 query := pan.New(pan.POSTGRES, "UPDATE "+pan.GetTableName(session)+" SET")
95 query.Include(pan.GetUnquotedColumn(session, "Active")+" = ?", false)
97 query.Include(pan.GetUnquotedColumn(session, "ProfileID")+" = ?", profile)
98 return query.FlushExpressions(" ")
101 func (p *postgres) terminateSessionsByProfile(profile uuid.ID) error {
102 query := p.terminateSessionsByProfileSQL(profile)
103 res, err := p.db.Exec(query.String(), query.Args...)
107 rows, err := res.RowsAffected()
112 return ErrProfileNotFound
117 func (p *postgres) removeSessionSQL(id string) *pan.Query {
119 query := pan.New(pan.POSTGRES, "DELETE FROM "+pan.GetTableName(session))
121 query.Include(pan.GetUnquotedColumn(session, "ID")+" = ?", id)
122 return query.FlushExpressions(" ")
125 func (p *postgres) removeSession(id string) error {
126 query := p.removeSessionSQL(id)
127 res, err := p.db.Exec(query.String(), query.Args...)
131 rows, err := res.RowsAffected()
136 return ErrSessionNotFound
141 func (p *postgres) listSessionsSQL(profile uuid.ID, before time.Time, num int64) *pan.Query {
143 fields, _ := pan.GetFields(session)
144 query := pan.New(pan.POSTGRES, "SELECT "+pan.QueryList(fields)+" FROM "+pan.GetTableName(session))
146 query.Include(pan.GetUnquotedColumn(session, "ProfileID")+" = ?", profile)
147 if !before.IsZero() {
148 query.Include(pan.GetUnquotedColumn(session, "Created")+" < ?", before)
150 query.FlushExpressions(" AND ")
152 query.IncludeLimit(num)
154 return query.FlushExpressions(" ")
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...)
161 return []Session{}, err
163 var sessions []Session
166 err := pan.Unmarshal(rows, &session)
170 sessions = append(sessions, session)
172 if err = rows.Err(); err != nil {