Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Subject field #674

Merged
merged 3 commits into from
Dec 4, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 49 additions & 14 deletions oauth2/fosite_store_sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,31 @@ type FositeSQLStore struct {
L logrus.FieldLogger
}

func sqlTemplate(table string) string {
return fmt.Sprintf(`CREATE TABLE IF NOT EXISTS hydra_oauth2_%s (
func sqlSchemaUp(table string, id string) string {
schemas := map[string]string{
"1": fmt.Sprintf(`CREATE TABLE IF NOT EXISTS hydra_oauth2_%s (
signature varchar(255) NOT NULL PRIMARY KEY,
request_id varchar(255) NOT NULL,
requested_at timestamp NOT NULL DEFAULT now(),
client_id text NOT NULL,
scope text NOT NULL,
client_id text NOT NULL,
scope text NOT NULL,
granted_scope text NOT NULL,
form_data text NOT NULL,
form_data text NOT NULL,
session_data text NOT NULL
)`, table)
)`, table),
"2": fmt.Sprintf("ALTER TABLE hydra_oauth2_%s ADD subject varchar(255) NOT NULL DEFAULT ''", table),
}

return schemas[id]
}

func sqlSchemaDown(table string, id string) string {
schemas := map[string]string{
"1": fmt.Sprintf(`DROP TABLE %s)`, table),
"2": fmt.Sprintf("ALTER TABLE hydra_oauth2_%s DROP COLUMN subject", table),
}

return schemas[id]
}

const (
Expand All @@ -62,16 +76,31 @@ var migrations = &migrate.MemoryMigrationSource{
{
Id: "1",
Up: []string{
sqlTemplate(sqlTableAccess),
sqlTemplate(sqlTableRefresh),
sqlTemplate(sqlTableCode),
sqlTemplate(sqlTableOpenID),
sqlSchemaUp(sqlTableAccess, "1"),
sqlSchemaUp(sqlTableRefresh, "1"),
sqlSchemaUp(sqlTableCode, "1"),
sqlSchemaUp(sqlTableOpenID, "1"),
},
Down: []string{
fmt.Sprintf("DROP TABLE %s", sqlTableAccess),
fmt.Sprintf("DROP TABLE %s", sqlTableRefresh),
fmt.Sprintf("DROP TABLE %s", sqlTableCode),
fmt.Sprintf("DROP TABLE %s", sqlTableOpenID),
sqlSchemaDown(sqlTableAccess, "1"),
sqlSchemaDown(sqlTableRefresh, "1"),
sqlSchemaDown(sqlTableCode, "1"),
sqlSchemaDown(sqlTableOpenID, "1"),
},
},
{
Id: "2",
Up: []string{
sqlSchemaUp(sqlTableAccess, "2"),
sqlSchemaUp(sqlTableRefresh, "2"),
sqlSchemaUp(sqlTableCode, "2"),
sqlSchemaUp(sqlTableOpenID, "2"),
},
Down: []string{
sqlSchemaDown(sqlTableAccess, "2"),
sqlSchemaDown(sqlTableRefresh, "2"),
sqlSchemaDown(sqlTableCode, "2"),
sqlSchemaDown(sqlTableOpenID, "2"),
},
},
},
Expand All @@ -86,6 +115,7 @@ var sqlParams = []string{
"granted_scope",
"form_data",
"session_data",
"subject",
}

type sqlData struct {
Expand All @@ -96,12 +126,16 @@ type sqlData struct {
Scopes string `db:"scope"`
GrantedScopes string `db:"granted_scope"`
Form string `db:"form_data"`
Subject string `db:"subject"`
Session []byte `db:"session_data"`
}

func sqlSchemaFromRequest(signature string, r fosite.Requester, logger logrus.FieldLogger) (*sqlData, error) {
subject := ""
if r.GetSession() == nil {
logger.Debugf("Got an empty session in sqlSchemaFromRequest")
} else {
subject = r.GetSession().GetSubject()
}

session, err := json.Marshal(r.GetSession())
Expand All @@ -118,6 +152,7 @@ func sqlSchemaFromRequest(signature string, r fosite.Requester, logger logrus.Fi
GrantedScopes: strings.Join([]string(r.GetGrantedScopes()), "|"),
Form: r.GetRequestForm().Encode(),
Session: session,
Subject: subject,
}, nil
}

Expand Down
4 changes: 2 additions & 2 deletions oauth2/fosite_store_test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,10 @@ func TestHelperRevokeRefreshToken(m pkg.FositeStorer) func(t *testing.T) {
_, err := m.GetRefreshTokenSession(ctx, "1111", &fosite.DefaultSession{})
assert.NotNil(t, err)

err = m.CreateRefreshTokenSession(ctx, "1111", &fosite.Request{ID: id, Client: &client.Client{ID: "foobar"}, RequestedAt: time.Now().Round(time.Second)})
err = m.CreateRefreshTokenSession(ctx, "1111", &fosite.Request{ID: id, Client: &client.Client{ID: "foobar"}, RequestedAt: time.Now().Round(time.Second), Session: &fosite.DefaultSession{}})
require.NoError(t, err)

err = m.CreateRefreshTokenSession(ctx, "1122", &fosite.Request{ID: id, Client: &client.Client{ID: "foobar"}, RequestedAt: time.Now().Round(time.Second)})
err = m.CreateRefreshTokenSession(ctx, "1122", &fosite.Request{ID: id, Client: &client.Client{ID: "foobar"}, RequestedAt: time.Now().Round(time.Second), Session: &fosite.DefaultSession{}})
require.NoError(t, err)

_, err = m.GetRefreshTokenSession(ctx, "1111", &fosite.DefaultSession{})
Expand Down