auth
152:de5e09680f6b Browse Files
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.
authd/server.go authd/templates/simple.gotmpl context.go scope.go scope_postgres.go scope_test.go sql/postgres_init.sql
1.1 --- a/authd/server.go Sun Mar 22 16:26:37 2015 -0400 1.2 +++ b/authd/server.go Tue Mar 24 20:39:03 2015 -0400 1.3 @@ -25,7 +25,7 @@ 1.4 ProfileStore: &p, 1.5 TokenStore: store, 1.6 SessionStore: store, 1.7 - ScopeStore: store, 1.8 + ScopeStore: &p, 1.9 Template: template.Must(template.New("base").ParseGlob("./templates/*.gotmpl")), 1.10 LoginURI: "/login", 1.11 } 1.12 @@ -40,6 +40,9 @@ 1.13 err = context.CreateScopes([]auth.Scope{ 1.14 {ID: "testscope", Name: "Test Scope"}, 1.15 }) 1.16 + if err != nil { 1.17 + panic(err) 1.18 + } 1.19 1.20 router := mux.NewRouter() 1.21 auth.RegisterOAuth2(router, context)
2.1 --- a/authd/templates/simple.gotmpl Sun Mar 22 16:26:37 2015 -0400 2.2 +++ b/authd/templates/simple.gotmpl Tue Mar 24 20:39:03 2015 -0400 2.3 @@ -25,7 +25,11 @@ 2.4 <p>{{ .error }}</p>{{ end }}{{ if .internal_error }} 2.5 <h1>Error</h1> 2.6 <p>{{ .internal_error }}</p>{{ end }}{{ if not .error }}{{ if not .internal_error }}<h1>Grant access</h1> 2.7 - <p>{{ .client.Name }} is requesting access to your account. if you grant it, you'll be redirected to {{ .redirectURL }}. Their access will be limited to {{ .scope }}. You are granting access for {{ .profile.Name }}.</p>{{ end }}{{ end }} 2.8 + <p>{{ .client.Name }} is requesting access to your account. if you grant it, you'll be redirected to {{ .redirectURL }}.{{ if .scopes }} Their access will be limited to:</p> 2.9 + <ul>{{ range .scopes }} 2.10 + <li>{{ .Name }}{{ if .Description }}: {{ .Description }}{{ end }}</li>{{ end }} 2.11 + </ul>{{ end }} 2.12 + <p>You are granting access for {{ .profile.Name }}.</p>{{ end }}{{ end }} 2.13 <form method="POST"> 2.14 <input type="submit" name="grant" value="approved"> 2.15 <input type="hidden" name="csrftoken" value="{{ .csrftoken }}">
3.1 --- a/context.go Sun Mar 22 16:26:37 2015 -0400 3.2 +++ b/context.go Tue Mar 24 20:39:03 2015 -0400 3.3 @@ -370,11 +370,11 @@ 3.4 return c.scopes.getScopes(ids) 3.5 } 3.6 3.7 -func (c Context) UpdateScopes(changes []ScopeChange) ([]Scope, error) { 3.8 +func (c Context) UpdateScope(id string, change ScopeChange) error { 3.9 if c.scopes == nil { 3.10 - return []Scope{}, ErrNoScopeStore 3.11 + return ErrNoScopeStore 3.12 } 3.13 - return c.scopes.updateScopes(changes) 3.14 + return c.scopes.updateScope(id, change) 3.15 } 3.16 3.17 func (c Context) RemoveScopes(ids []string) error {
4.1 --- a/scope.go Sun Mar 22 16:26:37 2015 -0400 4.2 +++ b/scope.go Tue Mar 24 20:39:03 2015 -0400 4.3 @@ -60,15 +60,18 @@ 4.4 4.5 // ScopeChange represents a change to a Scope. 4.6 type ScopeChange struct { 4.7 - ID string 4.8 Name *string 4.9 Description *string 4.10 } 4.11 4.12 +func (s ScopeChange) Empty() bool { 4.13 + return s.Name == nil && s.Description == nil 4.14 +} 4.15 + 4.16 type scopeStore interface { 4.17 createScopes(scopes []Scope) error 4.18 getScopes(ids []string) ([]Scope, error) 4.19 - updateScopes(changes []ScopeChange) ([]Scope, error) 4.20 + updateScope(id string, change ScopeChange) error 4.21 removeScopes(ids []string) error 4.22 listScopes() ([]Scope, error) 4.23 } 4.24 @@ -106,27 +109,17 @@ 4.25 return scopes, nil 4.26 } 4.27 4.28 -func (m *memstore) updateScopes(changes []ScopeChange) ([]Scope, error) { 4.29 +func (m *memstore) updateScope(id string, change ScopeChange) error { 4.30 m.scopeLock.Lock() 4.31 defer m.scopeLock.Unlock() 4.32 4.33 - scopes := []Scope{} 4.34 - 4.35 - for pos, change := range changes { 4.36 - if _, ok := m.scopes[change.ID]; !ok { 4.37 - return []Scope{}, ErrScopeNotFound{Pos: pos, ID: change.ID} 4.38 - } 4.39 + scope, ok := m.scopes[id] 4.40 + if !ok { 4.41 + return ErrScopeNotFound{Pos: 0, ID: id} 4.42 } 4.43 - for _, change := range changes { 4.44 - scope := m.scopes[change.ID] 4.45 - scope.ApplyChange(change) 4.46 - m.scopes[change.ID] = scope 4.47 - scopes = append(scopes, scope) 4.48 - } 4.49 - sorted := sortedScopes(scopes) 4.50 - sort.Sort(sorted) 4.51 - scopes = sorted 4.52 - return scopes, nil 4.53 + scope.ApplyChange(change) 4.54 + m.scopes[id] = scope 4.55 + return nil 4.56 } 4.57 4.58 func (m *memstore) removeScopes(ids []string) error {
5.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 5.2 +++ b/scope_postgres.go Tue Mar 24 20:39:03 2015 -0400 5.3 @@ -0,0 +1,135 @@ 5.4 +package auth 5.5 + 5.6 +import ( 5.7 + "github.com/secondbit/pan" 5.8 +) 5.9 + 5.10 +func (s Scope) GetSQLTableName() string { 5.11 + return "scopes" 5.12 +} 5.13 + 5.14 +func (p *postgres) createScopesSQL(scopes []Scope) *pan.Query { 5.15 + fields, _ := pan.GetFields(scopes[0]) 5.16 + query := pan.New(pan.POSTGRES, "INSERT INTO "+pan.GetTableName(scopes[0])) 5.17 + query.Include("(" + pan.QueryList(fields) + ")") 5.18 + query.Include("VALUES") 5.19 + for _, scope := range scopes { 5.20 + _, values := pan.GetFields(scope) 5.21 + query.Include("("+pan.VariableList(len(values))+")", values...) 5.22 + } 5.23 + return query.FlushExpressions(" ") 5.24 +} 5.25 + 5.26 +func (p *postgres) createScopes(scopes []Scope) error { 5.27 + if len(scopes) < 1 { 5.28 + return nil 5.29 + } 5.30 + query := p.createScopesSQL(scopes) 5.31 + _, err := p.db.Exec(query.String(), query.Args...) 5.32 + return err 5.33 +} 5.34 + 5.35 +func (p *postgres) getScopesSQL(ids []string) *pan.Query { 5.36 + var scope Scope 5.37 + intids := make([]interface{}, len(ids)) 5.38 + for pos, id := range ids { 5.39 + intids[pos] = id 5.40 + } 5.41 + fields, _ := pan.GetFields(scope) 5.42 + query := pan.New(pan.POSTGRES, "SELECT "+pan.QueryList(fields)+" FROM "+pan.GetTableName(scope)) 5.43 + query.IncludeWhere() 5.44 + query.Include(pan.GetUnquotedColumn(scope, "ID") + " IN") 5.45 + query.Include("("+pan.VariableList(len(ids))+")", intids...) 5.46 + return query.FlushExpressions(" ") 5.47 +} 5.48 + 5.49 +func (p *postgres) getScopes(ids []string) ([]Scope, error) { 5.50 + query := p.getScopesSQL(ids) 5.51 + rows, err := p.db.Query(query.String(), query.Args...) 5.52 + if err != nil { 5.53 + return []Scope{}, err 5.54 + } 5.55 + var scopes []Scope 5.56 + for rows.Next() { 5.57 + var scope Scope 5.58 + err := pan.Unmarshal(rows, &scope) 5.59 + if err != nil { 5.60 + return scopes, err 5.61 + } 5.62 + scopes = append(scopes, scope) 5.63 + } 5.64 + if err = rows.Err(); err != nil { 5.65 + return scopes, err 5.66 + } 5.67 + return scopes, nil 5.68 +} 5.69 + 5.70 +func (p *postgres) updateScopeSQL(id string, change ScopeChange) *pan.Query { 5.71 + var scope Scope 5.72 + query := pan.New(pan.POSTGRES, "UPDATE "+pan.GetTableName(scope)+" SET ") 5.73 + query.IncludeIfNotNil(pan.GetUnquotedColumn(scope, "Name")+" = ?", change.Name) 5.74 + query.IncludeIfNotNil(pan.GetUnquotedColumn(scope, "Description")+" = ?", change.Description) 5.75 + query.FlushExpressions(", ") 5.76 + query.IncludeWhere() 5.77 + query.Include(pan.GetUnquotedColumn(scope, "ID")+" = ?", id) 5.78 + return query.FlushExpressions(" ") 5.79 +} 5.80 + 5.81 +func (p *postgres) updateScope(id string, change ScopeChange) error { 5.82 + if change.Empty() { 5.83 + return nil 5.84 + } 5.85 + query := p.updateScopeSQL(id, change) 5.86 + _, err := p.db.Exec(query.String(), query.Args...) 5.87 + return err 5.88 +} 5.89 + 5.90 +func (p *postgres) removeScopesSQL(ids []string) *pan.Query { 5.91 + var scope Scope 5.92 + intids := make([]interface{}, len(ids)) 5.93 + for pos, id := range ids { 5.94 + intids[pos] = id 5.95 + } 5.96 + query := pan.New(pan.POSTGRES, "DELETE FROM "+pan.GetTableName(scope)) 5.97 + query.IncludeWhere() 5.98 + query.Include(pan.GetUnquotedColumn(scope, "ID") + " IN") 5.99 + query.Include("("+pan.VariableList(len(ids))+")", intids...) 5.100 + return query.FlushExpressions(" ") 5.101 +} 5.102 + 5.103 +func (p *postgres) removeScopes(ids []string) error { 5.104 + query := p.removeScopesSQL(ids) 5.105 + _, err := p.db.Exec(query.String(), query.Args...) 5.106 + if err != nil { 5.107 + return err 5.108 + } 5.109 + return nil 5.110 +} 5.111 + 5.112 +func (p *postgres) listScopesSQL() *pan.Query { 5.113 + var scope Scope 5.114 + fields, _ := pan.GetFields(scope) 5.115 + query := pan.New(pan.POSTGRES, "SELECT "+pan.QueryList(fields)+" FROM "+pan.GetTableName(scope)) 5.116 + return query.FlushExpressions(" ") 5.117 +} 5.118 + 5.119 +func (p *postgres) listScopes() ([]Scope, error) { 5.120 + query := p.listScopesSQL() 5.121 + rows, err := p.db.Query(query.String(), query.Args...) 5.122 + if err != nil { 5.123 + return []Scope{}, err 5.124 + } 5.125 + var scopes []Scope 5.126 + for rows.Next() { 5.127 + var scope Scope 5.128 + err = pan.Unmarshal(rows, &scope) 5.129 + if err != nil { 5.130 + return scopes, err 5.131 + } 5.132 + scopes = append(scopes, scope) 5.133 + } 5.134 + if err = rows.Err(); err != nil { 5.135 + return scopes, err 5.136 + } 5.137 + return scopes, nil 5.138 +}
6.1 --- a/scope_test.go Sun Mar 22 16:26:37 2015 -0400 6.2 +++ b/scope_test.go Tue Mar 24 20:39:03 2015 -0400 6.3 @@ -36,15 +36,12 @@ 6.4 updatedName := "Updated Scope" 6.5 updatedDescription := "An updated scope." 6.6 update := ScopeChange{ 6.7 - ID: scope.ID, 6.8 Name: &updatedName, 6.9 } 6.10 update2 := ScopeChange{ 6.11 - ID: scope2.ID, 6.12 Description: &updatedDescription, 6.13 } 6.14 update3 := ScopeChange{ 6.15 - ID: scope3.ID, 6.16 Name: &updatedName, 6.17 Description: &updatedDescription, 6.18 } 6.19 @@ -110,23 +107,21 @@ 6.20 t.Errorf("Expected %s to be %+v for scope %s, got %+v from %T", field, val1, s.ID, val2, store) 6.21 } 6.22 } 6.23 - updated, err := context.UpdateScopes([]ScopeChange{update, update2, update3}) 6.24 + err = context.UpdateScope(scope.ID, update) 6.25 if err != nil { 6.26 - t.Errorf("Unexpected error updating scopes in %T: %+v", store, err) 6.27 - } 6.28 - if len(updated) != 3 { 6.29 - t.Logf("%+v", updated) 6.30 - t.Errorf("Expected %d results, got %d from %T", 3, len(updated), store) 6.31 + t.Errorf("Unexpected error updating scope in %T: %+v", store, err) 6.32 } 6.33 scope.ApplyChange(update) 6.34 + err = context.UpdateScope(scope2.ID, update2) 6.35 + if err != nil { 6.36 + t.Errorf("Unexpected error updating scope in %T: %+v", store, err) 6.37 + } 6.38 scope2.ApplyChange(update2) 6.39 + err = context.UpdateScope(scope3.ID, update3) 6.40 + if err != nil { 6.41 + t.Errorf("Unexpected error updating scope in %T: %+v", store, err) 6.42 + } 6.43 scope3.ApplyChange(update3) 6.44 - for pos, s := range []Scope{scope, scope2, scope3} { 6.45 - success, field, val1, val2 = compareScopes(s, updated[pos]) 6.46 - if !success { 6.47 - t.Errorf("Expected %s to be %+v for scope %s, got %+v from %T", field, val1, s.ID, val2, store) 6.48 - } 6.49 - } 6.50 retrieved, err = context.ListScopes() 6.51 if err != nil { 6.52 t.Errorf("Unexpected error retrieving scopes from %T: %+v", store, err) 6.53 @@ -167,7 +162,7 @@ 6.54 t.Errorf("Expected error to be for scope ID %s, but got %s from %T", scope.ID, e.ID, store) 6.55 } 6.56 } 6.57 - updated, err = context.UpdateScopes([]ScopeChange{update}) 6.58 + err = context.UpdateScope(scope.ID, update) 6.59 if err == nil { 6.60 t.Errorf("No error returned updating non-existent scopes from %T", store) 6.61 }
7.1 --- a/sql/postgres_init.sql Sun Mar 22 16:26:37 2015 -0400 7.2 +++ b/sql/postgres_init.sql Tue Mar 24 20:39:03 2015 -0400 7.3 @@ -40,3 +40,9 @@ 7.4 normalized_uri VARCHAR(512) NOT NULL, 7.5 added TIMESTAMPTZ NOT NULL 7.6 ); 7.7 + 7.8 +CREATE TABLE IF NOT EXISTS scopes ( 7.9 + id VARCHAR(64) PRIMARY KEY, 7.10 + name VARCHAR(64) NOT NULL, 7.11 + description TEXT NOT NULL 7.12 +);