aboutsummaryrefslogtreecommitdiff
path: root/userapi
diff options
context:
space:
mode:
authorBruce MacDonald <brucewmacdonald@gmail.com>2021-04-07 05:26:20 -0700
committerGitHub <noreply@github.com>2021-04-07 13:26:20 +0100
commitd27607af78a53bda636f14f603b02b2952d6e1d8 (patch)
treec5c5488c7395a45af24ef598308ef7f6545515ca /userapi
parentf8d3a762c49a1dafe4e484a2440ade2bb6ba32ac (diff)
Implement OpenID module (#599) (#1812)
* Implement OpenID module (#599) - Unrelated: change Riot references to Element in client API routing Signed-off-by: Bruce MacDonald <contact@bruce-macdonald.com> * OpenID module tweaks (#599) - specify expiry is ms rather than vague ts - add OpenID token lifetime to configuration - use Go naming conventions for the path params - store plaintext token rather than hash - remove openid table sqllite mutex * Add default OpenID token lifetime (#599) * Update dendrite-config.yaml Co-authored-by: Kegsay <kegsay@gmail.com> Co-authored-by: Kegsay <kegan@matrix.org>
Diffstat (limited to 'userapi')
-rw-r--r--userapi/api/api.go41
-rw-r--r--userapi/internal/api.go28
-rw-r--r--userapi/inthttp/client.go18
-rw-r--r--userapi/inthttp/server.go26
-rw-r--r--userapi/storage/accounts/interface.go2
-rw-r--r--userapi/storage/accounts/postgres/openid_table.go84
-rw-r--r--userapi/storage/accounts/postgres/storage.go49
-rw-r--r--userapi/storage/accounts/sqlite3/openid_table.go86
-rw-r--r--userapi/storage/accounts/sqlite3/storage.go49
-rw-r--r--userapi/storage/accounts/storage.go6
-rw-r--r--userapi/storage/accounts/storage_wasm.go3
-rw-r--r--userapi/userapi_test.go2
12 files changed, 367 insertions, 27 deletions
diff --git a/userapi/api/api.go b/userapi/api/api.go
index 45e4e834..40735012 100644
--- a/userapi/api/api.go
+++ b/userapi/api/api.go
@@ -32,12 +32,14 @@ type UserInternalAPI interface {
PerformLastSeenUpdate(ctx context.Context, req *PerformLastSeenUpdateRequest, res *PerformLastSeenUpdateResponse) error
PerformDeviceUpdate(ctx context.Context, req *PerformDeviceUpdateRequest, res *PerformDeviceUpdateResponse) error
PerformAccountDeactivation(ctx context.Context, req *PerformAccountDeactivationRequest, res *PerformAccountDeactivationResponse) error
+ PerformOpenIDTokenCreation(ctx context.Context, req *PerformOpenIDTokenCreationRequest, res *PerformOpenIDTokenCreationResponse) error
QueryProfile(ctx context.Context, req *QueryProfileRequest, res *QueryProfileResponse) error
QueryAccessToken(ctx context.Context, req *QueryAccessTokenRequest, res *QueryAccessTokenResponse) error
QueryDevices(ctx context.Context, req *QueryDevicesRequest, res *QueryDevicesResponse) error
QueryAccountData(ctx context.Context, req *QueryAccountDataRequest, res *QueryAccountDataResponse) error
QueryDeviceInfos(ctx context.Context, req *QueryDeviceInfosRequest, res *QueryDeviceInfosResponse) error
QuerySearchProfiles(ctx context.Context, req *QuerySearchProfilesRequest, res *QuerySearchProfilesResponse) error
+ QueryOpenIDToken(ctx context.Context, req *QueryOpenIDTokenRequest, res *QueryOpenIDTokenResponse) error
}
// InputAccountDataRequest is the request for InputAccountData
@@ -226,6 +228,27 @@ type PerformAccountDeactivationResponse struct {
AccountDeactivated bool
}
+// PerformOpenIDTokenCreationRequest is the request for PerformOpenIDTokenCreation
+type PerformOpenIDTokenCreationRequest struct {
+ UserID string
+}
+
+// PerformOpenIDTokenCreationResponse is the response for PerformOpenIDTokenCreation
+type PerformOpenIDTokenCreationResponse struct {
+ Token OpenIDToken
+}
+
+// QueryOpenIDTokenRequest is the request for QueryOpenIDToken
+type QueryOpenIDTokenRequest struct {
+ Token string
+}
+
+// QueryOpenIDTokenResponse is the response for QueryOpenIDToken
+type QueryOpenIDTokenResponse struct {
+ Sub string // The Matrix User ID that generated the token
+ ExpiresAtMS int64
+}
+
// Device represents a client's device (mobile, web, etc)
type Device struct {
ID string
@@ -256,6 +279,24 @@ type Account struct {
// TODO: Associations (e.g. with application services)
}
+// OpenIDToken represents an OpenID token
+type OpenIDToken struct {
+ Token string
+ UserID string
+ ExpiresAtMS int64
+}
+
+// OpenIDTokenInfo represents the attributes associated with an issued OpenID token
+type OpenIDTokenAttributes struct {
+ UserID string
+ ExpiresAtMS int64
+}
+
+// UserInfo is for returning information about the user an OpenID token was issued for
+type UserInfo struct {
+ Sub string // The Matrix user's ID who generated the token
+}
+
// ErrorForbidden is an error indicating that the supplied access token is forbidden
type ErrorForbidden struct {
Message string
diff --git a/userapi/internal/api.go b/userapi/internal/api.go
index 0d01afa1..21933c1c 100644
--- a/userapi/internal/api.go
+++ b/userapi/internal/api.go
@@ -414,3 +414,31 @@ func (a *UserInternalAPI) PerformAccountDeactivation(ctx context.Context, req *a
res.AccountDeactivated = err == nil
return err
}
+
+// PerformOpenIDTokenCreation creates a new token that a relying party uses to authenticate a user
+func (a *UserInternalAPI) PerformOpenIDTokenCreation(ctx context.Context, req *api.PerformOpenIDTokenCreationRequest, res *api.PerformOpenIDTokenCreationResponse) error {
+ token := util.RandomString(24)
+
+ exp, err := a.AccountDB.CreateOpenIDToken(ctx, token, req.UserID)
+
+ res.Token = api.OpenIDToken{
+ Token: token,
+ UserID: req.UserID,
+ ExpiresAtMS: exp,
+ }
+
+ return err
+}
+
+// QueryOpenIDToken validates that the OpenID token was issued for the user, the replying party uses this for validation
+func (a *UserInternalAPI) QueryOpenIDToken(ctx context.Context, req *api.QueryOpenIDTokenRequest, res *api.QueryOpenIDTokenResponse) error {
+ openIDTokenAttrs, err := a.AccountDB.GetOpenIDTokenAttributes(ctx, req.Token)
+ if err != nil {
+ return err
+ }
+
+ res.Sub = openIDTokenAttrs.UserID
+ res.ExpiresAtMS = openIDTokenAttrs.ExpiresAtMS
+
+ return nil
+}
diff --git a/userapi/inthttp/client.go b/userapi/inthttp/client.go
index 680e4cb5..1cb5ef0a 100644
--- a/userapi/inthttp/client.go
+++ b/userapi/inthttp/client.go
@@ -35,6 +35,7 @@ const (
PerformLastSeenUpdatePath = "/userapi/performLastSeenUpdate"
PerformDeviceUpdatePath = "/userapi/performDeviceUpdate"
PerformAccountDeactivationPath = "/userapi/performAccountDeactivation"
+ PerformOpenIDTokenCreationPath = "/userapi/performOpenIDTokenCreation"
QueryProfilePath = "/userapi/queryProfile"
QueryAccessTokenPath = "/userapi/queryAccessToken"
@@ -42,6 +43,7 @@ const (
QueryAccountDataPath = "/userapi/queryAccountData"
QueryDeviceInfosPath = "/userapi/queryDeviceInfos"
QuerySearchProfilesPath = "/userapi/querySearchProfiles"
+ QueryOpenIDTokenPath = "/userapi/queryOpenIDToken"
)
// NewUserAPIClient creates a UserInternalAPI implemented by talking to a HTTP POST API.
@@ -148,6 +150,14 @@ func (h *httpUserInternalAPI) PerformAccountDeactivation(ctx context.Context, re
return httputil.PostJSON(ctx, span, h.httpClient, apiURL, req, res)
}
+func (h *httpUserInternalAPI) PerformOpenIDTokenCreation(ctx context.Context, request *api.PerformOpenIDTokenCreationRequest, response *api.PerformOpenIDTokenCreationResponse) error {
+ span, ctx := opentracing.StartSpanFromContext(ctx, "PerformOpenIDTokenCreation")
+ defer span.Finish()
+
+ apiURL := h.apiURL + PerformOpenIDTokenCreationPath
+ return httputil.PostJSON(ctx, span, h.httpClient, apiURL, request, response)
+}
+
func (h *httpUserInternalAPI) QueryProfile(
ctx context.Context,
request *api.QueryProfileRequest,
@@ -207,3 +217,11 @@ func (h *httpUserInternalAPI) QuerySearchProfiles(ctx context.Context, req *api.
apiURL := h.apiURL + QuerySearchProfilesPath
return httputil.PostJSON(ctx, span, h.httpClient, apiURL, req, res)
}
+
+func (h *httpUserInternalAPI) QueryOpenIDToken(ctx context.Context, req *api.QueryOpenIDTokenRequest, res *api.QueryOpenIDTokenResponse) error {
+ span, ctx := opentracing.StartSpanFromContext(ctx, "QueryOpenIDToken")
+ defer span.Finish()
+
+ apiURL := h.apiURL + QueryOpenIDTokenPath
+ return httputil.PostJSON(ctx, span, h.httpClient, apiURL, req, res)
+}
diff --git a/userapi/inthttp/server.go b/userapi/inthttp/server.go
index e495e353..1c1cfdcd 100644
--- a/userapi/inthttp/server.go
+++ b/userapi/inthttp/server.go
@@ -117,6 +117,19 @@ func AddRoutes(internalAPIMux *mux.Router, s api.UserInternalAPI) {
return util.JSONResponse{Code: http.StatusOK, JSON: &response}
}),
)
+ internalAPIMux.Handle(PerformOpenIDTokenCreationPath,
+ httputil.MakeInternalAPI("performOpenIDTokenCreation", func(req *http.Request) util.JSONResponse {
+ request := api.PerformOpenIDTokenCreationRequest{}
+ response := api.PerformOpenIDTokenCreationResponse{}
+ if err := json.NewDecoder(req.Body).Decode(&request); err != nil {
+ return util.MessageResponse(http.StatusBadRequest, err.Error())
+ }
+ if err := s.PerformOpenIDTokenCreation(req.Context(), &request, &response); err != nil {
+ return util.ErrorResponse(err)
+ }
+ return util.JSONResponse{Code: http.StatusOK, JSON: &response}
+ }),
+ )
internalAPIMux.Handle(QueryProfilePath,
httputil.MakeInternalAPI("queryProfile", func(req *http.Request) util.JSONResponse {
request := api.QueryProfileRequest{}
@@ -195,6 +208,19 @@ func AddRoutes(internalAPIMux *mux.Router, s api.UserInternalAPI) {
return util.JSONResponse{Code: http.StatusOK, JSON: &response}
}),
)
+ internalAPIMux.Handle(QueryOpenIDTokenPath,
+ httputil.MakeInternalAPI("queryOpenIDToken", func(req *http.Request) util.JSONResponse {
+ request := api.QueryOpenIDTokenRequest{}
+ response := api.QueryOpenIDTokenResponse{}
+ if err := json.NewDecoder(req.Body).Decode(&request); err != nil {
+ return util.MessageResponse(http.StatusBadRequest, err.Error())
+ }
+ if err := s.QueryOpenIDToken(req.Context(), &request, &response); err != nil {
+ return util.ErrorResponse(err)
+ }
+ return util.JSONResponse{Code: http.StatusOK, JSON: &response}
+ }),
+ )
internalAPIMux.Handle(InputAccountDataPath,
httputil.MakeInternalAPI("inputAccountDataPath", func(req *http.Request) util.JSONResponse {
request := api.InputAccountDataRequest{}
diff --git a/userapi/storage/accounts/interface.go b/userapi/storage/accounts/interface.go
index c86b2c39..5aa61b90 100644
--- a/userapi/storage/accounts/interface.go
+++ b/userapi/storage/accounts/interface.go
@@ -52,6 +52,8 @@ type Database interface {
GetAccountByLocalpart(ctx context.Context, localpart string) (*api.Account, error)
SearchProfiles(ctx context.Context, searchString string, limit int) ([]authtypes.Profile, error)
DeactivateAccount(ctx context.Context, localpart string) (err error)
+ CreateOpenIDToken(ctx context.Context, token, localpart string) (exp int64, err error)
+ GetOpenIDTokenAttributes(ctx context.Context, token string) (*api.OpenIDTokenAttributes, error)
}
// Err3PIDInUse is the error returned when trying to save an association involving
diff --git a/userapi/storage/accounts/postgres/openid_table.go b/userapi/storage/accounts/postgres/openid_table.go
new file mode 100644
index 00000000..86c19705
--- /dev/null
+++ b/userapi/storage/accounts/postgres/openid_table.go
@@ -0,0 +1,84 @@
+package postgres
+
+import (
+ "context"
+ "database/sql"
+
+ "github.com/matrix-org/dendrite/internal/sqlutil"
+ "github.com/matrix-org/dendrite/userapi/api"
+ "github.com/matrix-org/gomatrixserverlib"
+ log "github.com/sirupsen/logrus"
+)
+
+const openIDTokenSchema = `
+-- Stores data about openid tokens issued for accounts.
+CREATE TABLE IF NOT EXISTS open_id_tokens (
+ -- The value of the token issued to a user
+ token TEXT NOT NULL PRIMARY KEY,
+ -- The Matrix user ID for this account
+ localpart TEXT NOT NULL,
+ -- When the token expires, as a unix timestamp (ms resolution).
+ token_expires_at_ms BIGINT NOT NULL
+);
+`
+
+const insertTokenSQL = "" +
+ "INSERT INTO open_id_tokens(token, localpart, token_expires_at_ms) VALUES ($1, $2, $3)"
+
+const selectTokenSQL = "" +
+ "SELECT localpart, token_expires_at_ms FROM open_id_tokens WHERE token = $1"
+
+type tokenStatements struct {
+ insertTokenStmt *sql.Stmt
+ selectTokenStmt *sql.Stmt
+ serverName gomatrixserverlib.ServerName
+}
+
+func (s *tokenStatements) prepare(db *sql.DB, server gomatrixserverlib.ServerName) (err error) {
+ _, err = db.Exec(openIDTokenSchema)
+ if err != nil {
+ return
+ }
+ if s.insertTokenStmt, err = db.Prepare(insertTokenSQL); err != nil {
+ return
+ }
+ if s.selectTokenStmt, err = db.Prepare(selectTokenSQL); err != nil {
+ return
+ }
+ s.serverName = server
+ return
+}
+
+// insertToken inserts a new OpenID Connect token to the DB.
+// Returns new token, otherwise returns error if the token already exists.
+func (s *tokenStatements) insertToken(
+ ctx context.Context,
+ txn *sql.Tx,
+ token, localpart string,
+ expiresAtMS int64,
+) (err error) {
+ stmt := sqlutil.TxStmt(txn, s.insertTokenStmt)
+ _, err = stmt.ExecContext(ctx, token, localpart, expiresAtMS)
+ return
+}
+
+// selectOpenIDTokenAtrributes gets the attributes associated with an OpenID token from the DB
+// Returns the existing token's attributes, or err if no token is found
+func (s *tokenStatements) selectOpenIDTokenAtrributes(
+ ctx context.Context,
+ token string,
+) (*api.OpenIDTokenAttributes, error) {
+ var openIDTokenAttrs api.OpenIDTokenAttributes
+ err := s.selectTokenStmt.QueryRowContext(ctx, token).Scan(
+ &openIDTokenAttrs.UserID,
+ &openIDTokenAttrs.ExpiresAtMS,
+ )
+ if err != nil {
+ if err != sql.ErrNoRows {
+ log.WithError(err).Error("Unable to retrieve token from the db")
+ }
+ return nil, err
+ }
+
+ return &openIDTokenAttrs, nil
+}
diff --git a/userapi/storage/accounts/postgres/storage.go b/userapi/storage/accounts/postgres/storage.go
index 3933fe5b..c5e74ed1 100644
--- a/userapi/storage/accounts/postgres/storage.go
+++ b/userapi/storage/accounts/postgres/storage.go
@@ -20,6 +20,7 @@ import (
"encoding/json"
"errors"
"strconv"
+ "time"
"github.com/matrix-org/dendrite/clientapi/auth/authtypes"
"github.com/matrix-org/dendrite/internal/sqlutil"
@@ -39,25 +40,28 @@ type Database struct {
db *sql.DB
writer sqlutil.Writer
sqlutil.PartitionOffsetStatements
- accounts accountsStatements
- profiles profilesStatements
- accountDatas accountDataStatements
- threepids threepidStatements
- serverName gomatrixserverlib.ServerName
- bcryptCost int
+ accounts accountsStatements
+ profiles profilesStatements
+ accountDatas accountDataStatements
+ threepids threepidStatements
+ openIDTokens tokenStatements
+ serverName gomatrixserverlib.ServerName
+ bcryptCost int
+ openIDTokenLifetimeMS int64
}
// NewDatabase creates a new accounts and profiles database
-func NewDatabase(dbProperties *config.DatabaseOptions, serverName gomatrixserverlib.ServerName, bcryptCost int) (*Database, error) {
+func NewDatabase(dbProperties *config.DatabaseOptions, serverName gomatrixserverlib.ServerName, bcryptCost int, openIDTokenLifetimeMS int64) (*Database, error) {
db, err := sqlutil.Open(dbProperties)
if err != nil {
return nil, err
}
d := &Database{
- serverName: serverName,
- db: db,
- writer: sqlutil.NewDummyWriter(),
- bcryptCost: bcryptCost,
+ serverName: serverName,
+ db: db,
+ writer: sqlutil.NewDummyWriter(),
+ bcryptCost: bcryptCost,
+ openIDTokenLifetimeMS: openIDTokenLifetimeMS,
}
// Create tables before executing migrations so we don't fail if the table is missing,
@@ -86,6 +90,9 @@ func NewDatabase(dbProperties *config.DatabaseOptions, serverName gomatrixserver
if err = d.threepids.prepare(db); err != nil {
return nil, err
}
+ if err = d.openIDTokens.prepare(db, serverName); err != nil {
+ return nil, err
+ }
return d, nil
}
@@ -341,3 +348,23 @@ func (d *Database) SearchProfiles(ctx context.Context, searchString string, limi
func (d *Database) DeactivateAccount(ctx context.Context, localpart string) (err error) {
return d.accounts.deactivateAccount(ctx, localpart)
}
+
+// CreateOpenIDToken persists a new token that was issued through OpenID Connect
+func (d *Database) CreateOpenIDToken(
+ ctx context.Context,
+ token, localpart string,
+) (int64, error) {
+ expiresAtMS := time.Now().UnixNano()/int64(time.Millisecond) + d.openIDTokenLifetimeMS
+ err := sqlutil.WithTransaction(d.db, func(txn *sql.Tx) error {
+ return d.openIDTokens.insertToken(ctx, txn, token, localpart, expiresAtMS)
+ })
+ return expiresAtMS, err
+}
+
+// GetOpenIDTokenAttributes gets the attributes of issued an OIDC auth token
+func (d *Database) GetOpenIDTokenAttributes(
+ ctx context.Context,
+ token string,
+) (*api.OpenIDTokenAttributes, error) {
+ return d.openIDTokens.selectOpenIDTokenAtrributes(ctx, token)
+}
diff --git a/userapi/storage/accounts/sqlite3/openid_table.go b/userapi/storage/accounts/sqlite3/openid_table.go
new file mode 100644
index 00000000..80b9dd4c
--- /dev/null
+++ b/userapi/storage/accounts/sqlite3/openid_table.go
@@ -0,0 +1,86 @@
+package sqlite3
+
+import (
+ "context"
+ "database/sql"
+
+ "github.com/matrix-org/dendrite/internal/sqlutil"
+ "github.com/matrix-org/dendrite/userapi/api"
+ "github.com/matrix-org/gomatrixserverlib"
+ log "github.com/sirupsen/logrus"
+)
+
+const openIDTokenSchema = `
+-- Stores data about accounts.
+CREATE TABLE IF NOT EXISTS open_id_tokens (
+ -- The value of the token issued to a user
+ token TEXT NOT NULL PRIMARY KEY,
+ -- The Matrix user ID for this account
+ localpart TEXT NOT NULL,
+ -- When the token expires, as a unix timestamp (ms resolution).
+ token_expires_at_ms BIGINT NOT NULL
+);
+`
+
+const insertTokenSQL = "" +
+ "INSERT INTO open_id_tokens(token, localpart, token_expires_at_ms) VALUES ($1, $2, $3)"
+
+const selectTokenSQL = "" +
+ "SELECT localpart, token_expires_at_ms FROM open_id_tokens WHERE token = $1"
+
+type tokenStatements struct {
+ db *sql.DB
+ insertTokenStmt *sql.Stmt
+ selectTokenStmt *sql.Stmt
+ serverName gomatrixserverlib.ServerName
+}
+
+func (s *tokenStatements) prepare(db *sql.DB, server gomatrixserverlib.ServerName) (err error) {
+ s.db = db
+ _, err = db.Exec(openIDTokenSchema)
+ if err != nil {
+ return err
+ }
+ if s.insertTokenStmt, err = db.Prepare(insertTokenSQL); err != nil {
+ return
+ }
+ if s.selectTokenStmt, err = db.Prepare(selectTokenSQL); err != nil {
+ return
+ }
+ s.serverName = server
+ return
+}
+
+// insertToken inserts a new OpenID Connect token to the DB.
+// Returns new token, otherwise returns error if the token already exists.
+func (s *tokenStatements) insertToken(
+ ctx context.Context,
+ txn *sql.Tx,
+ token, localpart string,
+ expiresAtMS int64,
+) (err error) {
+ stmt := sqlutil.TxStmt(txn, s.insertTokenStmt)
+ _, err = stmt.ExecContext(ctx, token, localpart, expiresAtMS)
+ return
+}
+
+// selectOpenIDTokenAtrributes gets the attributes associated with an OpenID token from the DB
+// Returns the existing token's attributes, or err if no token is found
+func (s *tokenStatements) selectOpenIDTokenAtrributes(
+ ctx context.Context,
+ token string,
+) (*api.OpenIDTokenAttributes, error) {
+ var openIDTokenAttrs api.OpenIDTokenAttributes
+ err := s.selectTokenStmt.QueryRowContext(ctx, token).Scan(
+ &openIDTokenAttrs.UserID,
+ &openIDTokenAttrs.ExpiresAtMS,
+ )
+ if err != nil {
+ if err != sql.ErrNoRows {
+ log.WithError(err).Error("Unable to retrieve token from the db")
+ }
+ return nil, err
+ }
+
+ return &openIDTokenAttrs, nil
+}
diff --git a/userapi/storage/accounts/sqlite3/storage.go b/userapi/storage/accounts/sqlite3/storage.go
index 07cc68b3..c0f7118c 100644
--- a/userapi/storage/accounts/sqlite3/storage.go
+++ b/userapi/storage/accounts/sqlite3/storage.go
@@ -21,6 +21,7 @@ import (
"errors"
"strconv"
"sync"
+ "time"
"github.com/matrix-org/dendrite/clientapi/auth/authtypes"
"github.com/matrix-org/dendrite/internal/sqlutil"
@@ -37,12 +38,14 @@ type Database struct {
writer sqlutil.Writer
sqlutil.PartitionOffsetStatements
- accounts accountsStatements
- profiles profilesStatements
- accountDatas accountDataStatements
- threepids threepidStatements
- serverName gomatrixserverlib.ServerName
- bcryptCost int
+ accounts accountsStatements
+ profiles profilesStatements
+ accountDatas accountDataStatements
+ threepids threepidStatements
+ openIDTokens tokenStatements
+ serverName gomatrixserverlib.ServerName
+ bcryptCost int
+ openIDTokenLifetimeMS int64
accountsMu sync.Mutex
profilesMu sync.Mutex
@@ -51,16 +54,17 @@ type Database struct {
}
// NewDatabase creates a new accounts and profiles database
-func NewDatabase(dbProperties *config.DatabaseOptions, serverName gomatrixserverlib.ServerName, bcryptCost int) (*Database, error) {
+func NewDatabase(dbProperties *config.DatabaseOptions, serverName gomatrixserverlib.ServerName, bcryptCost int, openIDTokenLifetimeMS int64) (*Database, error) {
db, err := sqlutil.Open(dbProperties)
if err != nil {
return nil, err
}
d := &Database{
- serverName: serverName,
- db: db,
- writer: sqlutil.NewExclusiveWriter(),
- bcryptCost: bcryptCost,
+ serverName: serverName,
+ db: db,
+ writer: sqlutil.NewExclusiveWriter(),
+ bcryptCost: bcryptCost,
+ openIDTokenLifetimeMS: openIDTokenLifetimeMS,
}
// Create tables before executing migrations so we don't fail if the table is missing,
@@ -90,6 +94,9 @@ func NewDatabase(dbProperties *config.DatabaseOptions, serverName gomatrixserver
if err = d.threepids.prepare(db); err != nil {
return nil, err
}
+ if err = d.openIDTokens.prepare(db, serverName); err != nil {
+ return nil, err
+ }
return d, nil
}
@@ -379,3 +386,23 @@ func (d *Database) SearchProfiles(ctx context.Context, searchString string, limi
func (d *Database) DeactivateAccount(ctx context.Context, localpart string) (err error) {
return d.accounts.deactivateAccount(ctx, localpart)
}
+
+// CreateOpenIDToken persists a new token that was issued for OpenID Connect
+func (d *Database) CreateOpenIDToken(
+ ctx context.Context,
+ token, localpart string,
+) (int64, error) {
+ expiresAtMS := time.Now().UnixNano()/int64(time.Millisecond) + d.openIDTokenLifetimeMS
+ err := d.writer.Do(d.db, nil, func(txn *sql.Tx) error {
+ return d.openIDTokens.insertToken(ctx, txn, token, localpart, expiresAtMS)
+ })
+ return expiresAtMS, err
+}
+
+// GetOpenIDTokenAttributes gets the attributes of issued an OIDC auth token
+func (d *Database) GetOpenIDTokenAttributes(
+ ctx context.Context,
+ token string,
+) (*api.OpenIDTokenAttributes, error) {
+ return d.openIDTokens.selectOpenIDTokenAtrributes(ctx, token)
+}
diff --git a/userapi/storage/accounts/storage.go b/userapi/storage/accounts/storage.go
index 28c437da..3489c9d0 100644
--- a/userapi/storage/accounts/storage.go
+++ b/userapi/storage/accounts/storage.go
@@ -27,12 +27,12 @@ import (
// NewDatabase opens a new Postgres or Sqlite database (based on dataSourceName scheme)
// and sets postgres connection parameters
-func NewDatabase(dbProperties *config.DatabaseOptions, serverName gomatrixserverlib.ServerName, bcryptCost int) (Database, error) {
+func NewDatabase(dbProperties *config.DatabaseOptions, serverName gomatrixserverlib.ServerName, bcryptCost int, openIDTokenLifetimeMS int64) (Database, error) {
switch {
case dbProperties.ConnectionString.IsSQLite():
- return sqlite3.NewDatabase(dbProperties, serverName, bcryptCost)
+ return sqlite3.NewDatabase(dbProperties, serverName, bcryptCost, openIDTokenLifetimeMS)
case dbProperties.ConnectionString.IsPostgres():
- return postgres.NewDatabase(dbProperties, serverName, bcryptCost)
+ return postgres.NewDatabase(dbProperties, serverName, bcryptCost, openIDTokenLifetimeMS)
default:
return nil, fmt.Errorf("unexpected database type")
}
diff --git a/userapi/storage/accounts/storage_wasm.go b/userapi/storage/accounts/storage_wasm.go
index 8038326f..11a88a20 100644
--- a/userapi/storage/accounts/storage_wasm.go
+++ b/userapi/storage/accounts/storage_wasm.go
@@ -26,10 +26,11 @@ func NewDatabase(
dbProperties *config.DatabaseOptions,
serverName gomatrixserverlib.ServerName,
bcryptCost int,
+ openIDTokenLifetimeMS int64,
) (Database, error) {
switch {
case dbProperties.ConnectionString.IsSQLite():
- return sqlite3.NewDatabase(dbProperties, serverName, bcryptCost)
+ return sqlite3.NewDatabase(dbProperties, serverName, bcryptCost, openIDTokenLifetimeMS)
case dbProperties.ConnectionString.IsPostgres():
return nil, fmt.Errorf("can't use Postgres implementation")
default:
diff --git a/userapi/userapi_test.go b/userapi/userapi_test.go
index 9a45a2dc..0141258e 100644
--- a/userapi/userapi_test.go
+++ b/userapi/userapi_test.go
@@ -26,7 +26,7 @@ const (
func MustMakeInternalAPI(t *testing.T) (api.UserInternalAPI, accounts.Database) {
accountDB, err := accounts.NewDatabase(&config.DatabaseOptions{
ConnectionString: "file::memory:",
- }, serverName, bcrypt.MinCost)
+ }, serverName, bcrypt.MinCost, config.DefaultOpenIDTokenLifetimeMS)
if err != nil {
t.Fatalf("failed to create account DB: %s", err)
}