diff --git a/src/github.com/matrix-org/dendrite/clientapi/readers/login.go b/src/github.com/matrix-org/dendrite/clientapi/readers/login.go index 1f2733fa4..faad732ad 100644 --- a/src/github.com/matrix-org/dendrite/clientapi/readers/login.go +++ b/src/github.com/matrix-org/dendrite/clientapi/readers/login.go @@ -1,6 +1,9 @@ package readers import ( + "fmt" + "github.com/matrix-org/dendrite/clientapi/config" + "github.com/matrix-org/dendrite/clientapi/httputil" "github.com/matrix-org/dendrite/clientapi/jsonerror" "github.com/matrix-org/util" "net/http" @@ -15,6 +18,17 @@ type flow struct { Stages []string `json:"stages"` } +type passwordRequest struct { + User string `json:"user"` + Password string `json:"password"` +} + +type loginResponse struct { + UserID string `json:"user_id"` + AccessToken string `json:"access_token"` + HomeServer string `json:"home_server"` +} + func passwordLogin() loginFlows { f := loginFlows{} s := flow{"m.login.password", []string{"m.login.password"}} @@ -22,16 +36,37 @@ func passwordLogin() loginFlows { return f } -func Login(req *http.Request) util.JSONResponse { +// Login implements GET and POST /login +func Login(req *http.Request, cfg config.ClientAPI) util.JSONResponse { if req.Method == "GET" { return util.JSONResponse{ Code: 200, JSON: passwordLogin(), } } else if req.Method == "POST" { + var r passwordRequest + resErr := httputil.UnmarshalJSONRequest(req, &r) + if resErr != nil { + return *resErr + } + if r.User == "" { + return util.JSONResponse{ + Code: 400, + JSON: jsonerror.BadJSON("'user' must be supplied."), + } + } return util.JSONResponse{ - Code: 403, - JSON: jsonerror.Forbidden("Not implemented"), + Code: 200, + JSON: loginResponse{ + UserID: makeUserID(r.User, cfg.ServerName), + AccessToken: makeUserID(r.User, cfg.ServerName), // FIXME: token is the user ID for now + HomeServer: cfg.ServerName, + }, + } + } else if req.Method == "OPTIONS" { + return util.JSONResponse{ + Code: 200, + JSON: nil, } } return util.JSONResponse{ @@ -39,3 +74,7 @@ func Login(req *http.Request) util.JSONResponse { JSON: jsonerror.NotFound("Bad method"), } } + +func makeUserID(localpart, domain string) string { + return fmt.Sprintf("@%s:%s", localpart, domain) +} diff --git a/src/github.com/matrix-org/dendrite/clientapi/routing/routing.go b/src/github.com/matrix-org/dendrite/clientapi/routing/routing.go index 352b3af0f..18220dbd0 100644 --- a/src/github.com/matrix-org/dendrite/clientapi/routing/routing.go +++ b/src/github.com/matrix-org/dendrite/clientapi/routing/routing.go @@ -45,7 +45,7 @@ func Setup(servMux *http.ServeMux, httpClient *http.Client, cfg config.ClientAPI ) r0mux.Handle("/login", make("login", util.NewJSONRequestHandler(func(req *http.Request) util.JSONResponse { - return readers.Login(req) + return readers.Login(req, cfg) })), )