Implement initial setup for the API handler

This commit is contained in:
Parminder Singh 2018-03-02 20:44:33 +05:30
parent 72a1bdffd6
commit 6fe226e0cc
3 changed files with 126 additions and 0 deletions

View file

@ -0,0 +1,106 @@
// Copyright 2017 Vector Creations Ltd
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package routing
import (
"html/template"
"net/http"
)
// RecaptchaTemplate is template for recaptcha auth
const RecaptchaTemplate = `
<html>
<head>
<title>Authentication</title>
<meta name='viewport' content='width=device-width, initial-scale=1,
user-scalable=no, minimum-scale=1.0, maximum-scale=1.0'>
<script src="https://www.google.com/recaptcha/api.js"
async defer></script>
<script src="//code.jquery.com/jquery-1.11.2.min.js"></script>
<link rel="stylesheet" href="/_matrix/static/client/register/style.css">
<script>
function captchaDone() {
$('#registrationForm').submit();
}
</script>
</head>
<body>
<form id="registrationForm" method="post" action="{{.MyUrl}}">
<div>
<p>
Hello! We need to prevent computer programs and other automated
things from creating accounts on this server.
</p>
<p>
Please verify that you're not a robot.
</p>
<input type="hidden" name="session" value="{{.Session}}" />
<div class="g-recaptcha"
data-sitekey="{{.SiteKey}}"
data-callback="captchaDone">
</div>
<noscript>
<input type="submit" value="All Done" />
</noscript>
</div>
</div>
</form>
</body>
</html>
`
// SuccessTemplate is template for success page after auth flow ends
const SuccessTemplate = `
<html>
<head>
<title>Success!</title>
<meta name='viewport' content='width=device-width, initial-scale=1,
user-scalable=no, minimum-scale=1.0, maximum-scale=1.0'>
<link rel="stylesheet" href="/_matrix/static/client/register/style.css">
<script>
if (window.onAuthDone) {
window.onAuthDone();
} else if (window.opener && window.opener.postMessage) {
window.opener.postMessage("authDone", "*");
}
</script>
</head>
<body>
<div>
<p>Thank you</p>
<p>You may now close this window and return to the application</p>
</div>
</body>
</html>
`
// AuthFallback implements GET on /auth/{authType}/fallback/web?session={sessionID}
func AuthFallback(w http.ResponseWriter, req *http.Request, authType string, sessionID string) {
if req.Method == "GET" {
t := template.Must(template.New("response").Parse(RECAPTCHA_TEMPLATE))
data := map[string]string{
"MyUrl": "TODO",
"Session": "TODO",
"SiteKey": "TODO",
}
if err := t.Execute(w, data); err != nil {
panic(err)
}
// TODO: Handle rest of flow
}
// TODO: Handle invalid request
}

View file

@ -174,6 +174,13 @@ func Setup(
}),
).Methods("GET", "POST", "OPTIONS")
r0mux.Handle("/auth/{authType}/fallback/web?session={sessionID}",
common.MakeHtmlAPI("authfallback", func(w http.ResponseWriter, req *http.Request) util.JSONResponse {
vars := mux.Vars(req)
return AuthFallback(w, req, vars["authType"], vars["sessionID"])
}),
).Methods("GET", "OPTIONS")
r0mux.Handle("/pushrules/",
common.MakeExternalAPI("push_rules", func(req *http.Request) util.JSONResponse {
// TODO: Implement push rules API

View file

@ -39,6 +39,19 @@ func MakeExternalAPI(metricsName string, f func(*http.Request) util.JSONResponse
return prometheus.InstrumentHandler(metricsName, http.HandlerFunc(withSpan))
}
// MakeHTMLAPI adds Span metrics to the HTML Handler function
// This is used to serve HTML template
func MakeHtmlAPI(metricsName string, f func(w http.ResponseWriter, *http.Request)) http.Handler {
withSpan := func(w http.ResponseWriter, req *http.Request) {
span := opentracing.StartSpan(metricsName)
defer span.Finish()
req = req.WithContext(opentracing.ContextWithSpan(req.Context(), span))
f(w, req)
}
return prometheus.InstrumentHandler(metricsName, http.HandlerFunc(withSpan))
}
// MakeInternalAPI turns a util.JSONRequestHandler function into an http.Handler.
// This is used for APIs that are internal to dendrite.
// If we are passed a tracing context in the request headers then we use that