mirror of
https://github.com/matrix-org/dendrite.git
synced 2025-12-11 08:53:11 -06:00
First-cut impl for registration
This commit is contained in:
parent
aa179d451c
commit
532127d3d2
|
|
@ -82,6 +82,7 @@ func (s *accountsStatements) insertAccount(localpart, hash string) (acc *types.A
|
||||||
acc = &types.Account{
|
acc = &types.Account{
|
||||||
Localpart: localpart,
|
Localpart: localpart,
|
||||||
UserID: makeUserID(localpart, s.serverName),
|
UserID: makeUserID(localpart, s.serverName),
|
||||||
|
ServerName: s.serverName,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
|
|
@ -97,6 +98,7 @@ func (s *accountsStatements) selectAccountByLocalpart(localpart string) (*types.
|
||||||
err := s.selectAccountByLocalpartStmt.QueryRow(localpart).Scan(&acc.Localpart)
|
err := s.selectAccountByLocalpartStmt.QueryRow(localpart).Scan(&acc.Localpart)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
acc.UserID = makeUserID(localpart, s.serverName)
|
acc.UserID = makeUserID(localpart, s.serverName)
|
||||||
|
acc.ServerName = s.serverName
|
||||||
}
|
}
|
||||||
return &acc, err
|
return &acc, err
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -14,10 +14,15 @@
|
||||||
|
|
||||||
package types
|
package types
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/matrix-org/gomatrixserverlib"
|
||||||
|
)
|
||||||
|
|
||||||
// Account represents a Matrix account on this home server.
|
// Account represents a Matrix account on this home server.
|
||||||
type Account struct {
|
type Account struct {
|
||||||
UserID string
|
UserID string
|
||||||
Localpart string
|
Localpart string
|
||||||
|
ServerName gomatrixserverlib.ServerName
|
||||||
// TODO: Other flags like IsAdmin, IsGuest
|
// TODO: Other flags like IsAdmin, IsGuest
|
||||||
// TODO: Device IDs
|
// TODO: Device IDs
|
||||||
// TODO: Associations (e.g. with application services)
|
// TODO: Associations (e.g. with application services)
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,11 @@
|
||||||
|
package types
|
||||||
|
|
||||||
|
// LoginType are specified by http://matrix.org/docs/spec/client_server/r0.2.0.html#login-types
|
||||||
|
type LoginType string
|
||||||
|
|
||||||
|
// The relevant login types implemented in Dendrite
|
||||||
|
const (
|
||||||
|
LoginTypeDummy = "m.login.dummy"
|
||||||
|
LoginTypeRecaptcha = "m.login.recaptcha"
|
||||||
|
LoginTypePassword = "m.login.password"
|
||||||
|
)
|
||||||
|
|
@ -79,6 +79,12 @@ func UnknownToken(msg string) *MatrixError {
|
||||||
return &MatrixError{"M_UNKNOWN_TOKEN", msg}
|
return &MatrixError{"M_UNKNOWN_TOKEN", msg}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// WeakPassword is an error which is returned when the client tries to register
|
||||||
|
// using a weak password. http://matrix.org/docs/spec/client_server/r0.2.0.html#password-based
|
||||||
|
func WeakPassword(msg string) *MatrixError {
|
||||||
|
return &MatrixError{"M_WEAK_PASSWORD", msg}
|
||||||
|
}
|
||||||
|
|
||||||
// LimitExceededError is a rate-limiting error.
|
// LimitExceededError is a rate-limiting error.
|
||||||
type LimitExceededError struct {
|
type LimitExceededError struct {
|
||||||
MatrixError
|
MatrixError
|
||||||
|
|
|
||||||
|
|
@ -19,6 +19,7 @@ import (
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/gorilla/mux"
|
"github.com/gorilla/mux"
|
||||||
|
"github.com/matrix-org/dendrite/clientapi/auth/storage"
|
||||||
"github.com/matrix-org/dendrite/clientapi/config"
|
"github.com/matrix-org/dendrite/clientapi/config"
|
||||||
"github.com/matrix-org/dendrite/clientapi/producers"
|
"github.com/matrix-org/dendrite/clientapi/producers"
|
||||||
"github.com/matrix-org/dendrite/clientapi/readers"
|
"github.com/matrix-org/dendrite/clientapi/readers"
|
||||||
|
|
@ -32,7 +33,7 @@ const pathPrefixR0 = "/_matrix/client/r0"
|
||||||
|
|
||||||
// Setup registers HTTP handlers with the given ServeMux. It also supplies the given http.Client
|
// Setup registers HTTP handlers with the given ServeMux. It also supplies the given http.Client
|
||||||
// to clients which need to make outbound HTTP requests.
|
// to clients which need to make outbound HTTP requests.
|
||||||
func Setup(servMux *http.ServeMux, httpClient *http.Client, cfg config.ClientAPI, producer *producers.RoomserverProducer, queryAPI api.RoomserverQueryAPI) {
|
func Setup(servMux *http.ServeMux, httpClient *http.Client, cfg config.ClientAPI, producer *producers.RoomserverProducer, queryAPI api.RoomserverQueryAPI, accountDB *storage.AccountDatabase) {
|
||||||
apiMux := mux.NewRouter()
|
apiMux := mux.NewRouter()
|
||||||
r0mux := apiMux.PathPrefix(pathPrefixR0).Subrouter()
|
r0mux := apiMux.PathPrefix(pathPrefixR0).Subrouter()
|
||||||
r0mux.Handle("/createRoom",
|
r0mux.Handle("/createRoom",
|
||||||
|
|
@ -61,6 +62,10 @@ func Setup(servMux *http.ServeMux, httpClient *http.Client, cfg config.ClientAPI
|
||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
r0mux.Handle("/register", makeAPI("register", func(req *http.Request) util.JSONResponse {
|
||||||
|
return writers.Register(req, accountDB)
|
||||||
|
}))
|
||||||
|
|
||||||
// Stub endpoints required by Riot
|
// Stub endpoints required by Riot
|
||||||
|
|
||||||
r0mux.Handle("/login",
|
r0mux.Handle("/login",
|
||||||
|
|
|
||||||
151
src/github.com/matrix-org/dendrite/clientapi/writers/register.go
Normal file
151
src/github.com/matrix-org/dendrite/clientapi/writers/register.go
Normal file
|
|
@ -0,0 +1,151 @@
|
||||||
|
package writers
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
log "github.com/Sirupsen/logrus"
|
||||||
|
"github.com/matrix-org/dendrite/clientapi/auth/storage"
|
||||||
|
"github.com/matrix-org/dendrite/clientapi/auth/types"
|
||||||
|
"github.com/matrix-org/dendrite/clientapi/httputil"
|
||||||
|
"github.com/matrix-org/dendrite/clientapi/jsonerror"
|
||||||
|
"github.com/matrix-org/gomatrixserverlib"
|
||||||
|
"github.com/matrix-org/util"
|
||||||
|
)
|
||||||
|
|
||||||
|
// registerRequest represents the submitted registration request.
|
||||||
|
// It can be broken down into 2 sections: the auth dictionary and registration parameters.
|
||||||
|
// Registration parameters vary depending on the request, and will need to remembered across
|
||||||
|
// sessions. If no parameters are supplied, the server should use the parameters previously
|
||||||
|
// remembered. If ANY parameters are supplied, the server should REPLACE all knowledge of
|
||||||
|
// previous paramters with the ones supplied. This mean you cannot "build up" request params.
|
||||||
|
type registerRequest struct {
|
||||||
|
// registration parameters.
|
||||||
|
Password string `json:"password"`
|
||||||
|
Username string `json:"username"`
|
||||||
|
// user-interactive auth params
|
||||||
|
Auth authDict `json:"auth"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type authDict struct {
|
||||||
|
Type types.LoginType `json:"type"`
|
||||||
|
Session string `json:"session"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// http://matrix.org/speculator/spec/HEAD/client_server/unstable.html#user-interactive-authentication-api
|
||||||
|
type userInteractiveResponse struct {
|
||||||
|
Flows []flow `json:"flows"`
|
||||||
|
Completed []types.LoginType `json:"completed"`
|
||||||
|
Params map[string]interface{} `json:"params"`
|
||||||
|
Session string `json:"session"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type flow struct {
|
||||||
|
Stages []types.LoginType `json:"stages"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func newUserInteractiveResponse(sessionID string, fs []flow) userInteractiveResponse {
|
||||||
|
return userInteractiveResponse{
|
||||||
|
fs, []types.LoginType{}, make(map[string]interface{}), sessionID,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// http://matrix.org/speculator/spec/HEAD/client_server/unstable.html#post-matrix-client-unstable-register
|
||||||
|
type registerResponse struct {
|
||||||
|
UserID string `json:"user_id"`
|
||||||
|
AccessToken string `json:"access_token"`
|
||||||
|
HomeServer gomatrixserverlib.ServerName `json:"home_server"`
|
||||||
|
DeviceID string `json:"device_id"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// Validate returns an error response if the request fails to validate.
|
||||||
|
func (r *registerRequest) Validate() *util.JSONResponse {
|
||||||
|
// https://github.com/matrix-org/synapse/blob/v0.20.0/synapse/rest/client/v2_alpha/register.py#L161
|
||||||
|
if len(r.Password) > 512 {
|
||||||
|
return &util.JSONResponse{
|
||||||
|
Code: 400,
|
||||||
|
JSON: jsonerror.BadJSON("'password' >512 characters"),
|
||||||
|
}
|
||||||
|
} else if len(r.Username) > 512 {
|
||||||
|
return &util.JSONResponse{
|
||||||
|
Code: 400,
|
||||||
|
JSON: jsonerror.BadJSON("'username' >512 characters"),
|
||||||
|
}
|
||||||
|
} else if len(r.Password) > 0 && len(r.Password) < 8 {
|
||||||
|
return &util.JSONResponse{
|
||||||
|
Code: 400,
|
||||||
|
JSON: jsonerror.WeakPassword("password too weak: min 8 chars"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Register processes a /register request. http://matrix.org/speculator/spec/HEAD/client_server/unstable.html#post-matrix-client-unstable-register
|
||||||
|
func Register(req *http.Request, accountDB *storage.AccountDatabase) util.JSONResponse {
|
||||||
|
var r registerRequest
|
||||||
|
resErr := httputil.UnmarshalJSONRequest(req, &r)
|
||||||
|
if resErr != nil {
|
||||||
|
return *resErr
|
||||||
|
}
|
||||||
|
if resErr = r.Validate(); resErr != nil {
|
||||||
|
return *resErr
|
||||||
|
}
|
||||||
|
|
||||||
|
logger := util.GetLogger(req.Context())
|
||||||
|
logger.WithFields(log.Fields{
|
||||||
|
"username": r.Username,
|
||||||
|
"auth.type": r.Auth.Type,
|
||||||
|
"session_id": r.Auth.Session,
|
||||||
|
}).Info("Processing registration request")
|
||||||
|
|
||||||
|
// TODO: Shared secret registration (create new user scripts)
|
||||||
|
// TODO: AS API registration
|
||||||
|
// TODO: Enable registration config flag
|
||||||
|
// TODO: Guest account upgrading
|
||||||
|
|
||||||
|
// All registration requests must specify what auth they are using to perform this request
|
||||||
|
if r.Auth.Type == "" {
|
||||||
|
return util.JSONResponse{
|
||||||
|
Code: 401,
|
||||||
|
// Hard-coded 'dummy' auth for now with a bogus session ID.
|
||||||
|
JSON: newUserInteractiveResponse("totallyuniquesessionid", []flow{
|
||||||
|
flow{[]types.LoginType{types.LoginTypeDummy}},
|
||||||
|
}),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: Handle loading of previous session parameters from database.
|
||||||
|
// TODO: Handle mapping registrationRequest parameters into session parameters
|
||||||
|
|
||||||
|
// TODO: email / msisdn / recaptcha auth types.
|
||||||
|
switch r.Auth.Type {
|
||||||
|
case types.LoginTypeDummy:
|
||||||
|
// there is nothing to do
|
||||||
|
return completeRegistration(accountDB, r.Username, r.Password)
|
||||||
|
default:
|
||||||
|
return util.JSONResponse{
|
||||||
|
Code: 501,
|
||||||
|
JSON: jsonerror.Unknown("unknown/unimplemented auth type"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func completeRegistration(accountDB *storage.AccountDatabase, username, password string) util.JSONResponse {
|
||||||
|
acc, err := accountDB.CreateAccount(username, password)
|
||||||
|
if err != nil {
|
||||||
|
return util.JSONResponse{
|
||||||
|
Code: 500,
|
||||||
|
JSON: jsonerror.Unknown("failed to create account: " + err.Error()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// TODO: Make and store a proper access_token
|
||||||
|
// TODO: Store the client's device information?
|
||||||
|
return util.JSONResponse{
|
||||||
|
Code: 200,
|
||||||
|
JSON: registerResponse{
|
||||||
|
UserID: acc.UserID,
|
||||||
|
AccessToken: acc.UserID, // FIXME
|
||||||
|
HomeServer: acc.ServerName,
|
||||||
|
DeviceID: "dendrite",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -79,6 +79,6 @@ func main() {
|
||||||
|
|
||||||
queryAPI := api.NewRoomserverQueryAPIHTTP(cfg.RoomserverURL, nil)
|
queryAPI := api.NewRoomserverQueryAPIHTTP(cfg.RoomserverURL, nil)
|
||||||
|
|
||||||
routing.Setup(http.DefaultServeMux, http.DefaultClient, cfg, roomserverProducer, queryAPI)
|
routing.Setup(http.DefaultServeMux, http.DefaultClient, cfg, roomserverProducer, queryAPI, nil)
|
||||||
log.Fatal(http.ListenAndServe(bindAddr, nil))
|
log.Fatal(http.ListenAndServe(bindAddr, nil))
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue