2021-04-07 14:26:20 +02:00
|
|
|
package postgres
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"database/sql"
|
2022-11-11 17:41:37 +01:00
|
|
|
"fmt"
|
2021-04-07 14:26:20 +02:00
|
|
|
|
|
|
|
"github.com/matrix-org/dendrite/internal/sqlutil"
|
|
|
|
"github.com/matrix-org/dendrite/userapi/api"
|
2022-02-18 14:51:59 +01:00
|
|
|
"github.com/matrix-org/dendrite/userapi/storage/tables"
|
2021-04-07 14:26:20 +02:00
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
|
|
|
const openIDTokenSchema = `
|
|
|
|
-- Stores data about openid tokens issued for accounts.
|
2022-10-18 16:59:08 +02:00
|
|
|
CREATE TABLE IF NOT EXISTS userapi_openid_tokens (
|
2021-04-07 14:26:20 +02:00
|
|
|
-- 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,
|
2022-11-11 17:41:37 +01:00
|
|
|
server_name TEXT NOT NULL,
|
2021-04-07 14:26:20 +02:00
|
|
|
-- When the token expires, as a unix timestamp (ms resolution).
|
|
|
|
token_expires_at_ms BIGINT NOT NULL
|
|
|
|
);
|
|
|
|
`
|
|
|
|
|
2022-02-18 14:51:59 +01:00
|
|
|
const insertOpenIDTokenSQL = "" +
|
2022-11-11 17:41:37 +01:00
|
|
|
"INSERT INTO userapi_openid_tokens(token, localpart, server_name, token_expires_at_ms) VALUES ($1, $2, $3, $4)"
|
2021-04-07 14:26:20 +02:00
|
|
|
|
2022-02-18 14:51:59 +01:00
|
|
|
const selectOpenIDTokenSQL = "" +
|
2022-11-11 17:41:37 +01:00
|
|
|
"SELECT localpart, server_name, token_expires_at_ms FROM userapi_openid_tokens WHERE token = $1"
|
2021-04-07 14:26:20 +02:00
|
|
|
|
2022-02-18 14:51:59 +01:00
|
|
|
type openIDTokenStatements struct {
|
2021-04-07 14:26:20 +02:00
|
|
|
insertTokenStmt *sql.Stmt
|
|
|
|
selectTokenStmt *sql.Stmt
|
|
|
|
serverName gomatrixserverlib.ServerName
|
|
|
|
}
|
|
|
|
|
2022-02-18 14:51:59 +01:00
|
|
|
func NewPostgresOpenIDTable(db *sql.DB, serverName gomatrixserverlib.ServerName) (tables.OpenIDTable, error) {
|
|
|
|
s := &openIDTokenStatements{
|
|
|
|
serverName: serverName,
|
|
|
|
}
|
|
|
|
_, err := db.Exec(openIDTokenSchema)
|
2021-04-07 14:26:20 +02:00
|
|
|
if err != nil {
|
2022-02-18 14:51:59 +01:00
|
|
|
return nil, err
|
2021-04-07 14:26:20 +02:00
|
|
|
}
|
2022-02-18 14:51:59 +01:00
|
|
|
return s, sqlutil.StatementList{
|
|
|
|
{&s.insertTokenStmt, insertOpenIDTokenSQL},
|
|
|
|
{&s.selectTokenStmt, selectOpenIDTokenSQL},
|
2021-07-28 19:30:04 +02:00
|
|
|
}.Prepare(db)
|
2021-04-07 14:26:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// insertToken inserts a new OpenID Connect token to the DB.
|
|
|
|
// Returns new token, otherwise returns error if the token already exists.
|
2022-02-18 14:51:59 +01:00
|
|
|
func (s *openIDTokenStatements) InsertOpenIDToken(
|
2021-04-07 14:26:20 +02:00
|
|
|
ctx context.Context,
|
|
|
|
txn *sql.Tx,
|
2022-11-11 17:41:37 +01:00
|
|
|
token, localpart string, serverName gomatrixserverlib.ServerName,
|
2021-04-07 14:26:20 +02:00
|
|
|
expiresAtMS int64,
|
|
|
|
) (err error) {
|
|
|
|
stmt := sqlutil.TxStmt(txn, s.insertTokenStmt)
|
2022-11-11 17:41:37 +01:00
|
|
|
_, err = stmt.ExecContext(ctx, token, localpart, serverName, expiresAtMS)
|
2021-04-07 14:26:20 +02:00
|
|
|
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
|
2022-02-18 14:51:59 +01:00
|
|
|
func (s *openIDTokenStatements) SelectOpenIDTokenAtrributes(
|
2021-04-07 14:26:20 +02:00
|
|
|
ctx context.Context,
|
|
|
|
token string,
|
|
|
|
) (*api.OpenIDTokenAttributes, error) {
|
|
|
|
var openIDTokenAttrs api.OpenIDTokenAttributes
|
2022-11-11 17:41:37 +01:00
|
|
|
var localpart string
|
|
|
|
var serverName gomatrixserverlib.ServerName
|
2021-04-07 14:26:20 +02:00
|
|
|
err := s.selectTokenStmt.QueryRowContext(ctx, token).Scan(
|
2022-11-11 17:41:37 +01:00
|
|
|
&localpart, &serverName,
|
2021-04-07 14:26:20 +02:00
|
|
|
&openIDTokenAttrs.ExpiresAtMS,
|
|
|
|
)
|
2022-11-11 17:41:37 +01:00
|
|
|
openIDTokenAttrs.UserID = fmt.Sprintf("@%s:%s", localpart, serverName)
|
2021-04-07 14:26:20 +02:00
|
|
|
if err != nil {
|
|
|
|
if err != sql.ErrNoRows {
|
|
|
|
log.WithError(err).Error("Unable to retrieve token from the db")
|
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &openIDTokenAttrs, nil
|
|
|
|
}
|