Implement password login stub response

This commit is contained in:
Kegan Dougal 2017-04-20 12:09:46 +01:00
parent baf97280f6
commit e75b332333
2 changed files with 43 additions and 4 deletions

View file

@ -1,6 +1,9 @@
package readers package readers
import ( 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/dendrite/clientapi/jsonerror"
"github.com/matrix-org/util" "github.com/matrix-org/util"
"net/http" "net/http"
@ -15,6 +18,17 @@ type flow struct {
Stages []string `json:"stages"` 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 { func passwordLogin() loginFlows {
f := loginFlows{} f := loginFlows{}
s := flow{"m.login.password", []string{"m.login.password"}} s := flow{"m.login.password", []string{"m.login.password"}}
@ -22,16 +36,37 @@ func passwordLogin() loginFlows {
return f 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" { if req.Method == "GET" {
return util.JSONResponse{ return util.JSONResponse{
Code: 200, Code: 200,
JSON: passwordLogin(), JSON: passwordLogin(),
} }
} else if req.Method == "POST" { } else if req.Method == "POST" {
var r passwordRequest
resErr := httputil.UnmarshalJSONRequest(req, &r)
if resErr != nil {
return *resErr
}
if r.User == "" {
return util.JSONResponse{ return util.JSONResponse{
Code: 403, Code: 400,
JSON: jsonerror.Forbidden("Not implemented"), JSON: jsonerror.BadJSON("'user' must be supplied."),
}
}
return util.JSONResponse{
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{ return util.JSONResponse{
@ -39,3 +74,7 @@ func Login(req *http.Request) util.JSONResponse {
JSON: jsonerror.NotFound("Bad method"), JSON: jsonerror.NotFound("Bad method"),
} }
} }
func makeUserID(localpart, domain string) string {
return fmt.Sprintf("@%s:%s", localpart, domain)
}

View file

@ -45,7 +45,7 @@ func Setup(servMux *http.ServeMux, httpClient *http.Client, cfg config.ClientAPI
) )
r0mux.Handle("/login", r0mux.Handle("/login",
make("login", util.NewJSONRequestHandler(func(req *http.Request) util.JSONResponse { make("login", util.NewJSONRequestHandler(func(req *http.Request) util.JSONResponse {
return readers.Login(req) return readers.Login(req, cfg)
})), })),
) )